@@ -336,17 +336,37 @@ function pyjlwrap_repr(o::PyPtr)
336
336
return oret
337
337
end
338
338
339
- # use this to create a new jlwrap type, with init to set up custom members
340
- function pyjlwrap_type (name:: String , init:: Function )
339
+ jlWrapType = PyTypeObject ()
340
+
341
+ function pyjlwrap_init ()
341
342
if pyversion () < v " 2.6"
342
343
error (" Python version 2.6 or later required for T_PYSSIZET" )
343
344
end
344
- PyTypeObject (name, sizeof (Py_jlWrap),
345
+ global jlWrapType
346
+ if (jlWrapType:: PyTypeObject ). tp_name == C_NULL
347
+ jlWrapType:: PyTypeObject =
348
+ PyTypeObject (" PyCall.jlwrap" , sizeof (Py_jlWrap),
349
+ t:: PyTypeObject -> begin
350
+ t. tp_flags |= Py_TPFLAGS_BASETYPE
351
+ t. tp_members = convert (Ptr{Void}, pyjlwrap_members);
352
+ t. tp_dealloc = cfunction (pyjlwrap_dealloc,
353
+ Void, (PyPtr,))
354
+ t. tp_repr = cfunction (pyjlwrap_repr,
355
+ PyPtr, (PyPtr,))
356
+ end )
357
+ end
358
+
359
+ end
360
+
361
+ # use this to create a new jlwrap type, with init to set up custom members
362
+ function pyjlwrap_type (name:: String , init:: Function )
363
+ pyjlwrap_init ()
364
+ PyTypeObject (name,
365
+ sizeof (Py_jlWrap) + sizeof (PyPtr), # must be > base type
345
366
t:: PyTypeObject -> begin
346
- t. tp_members = convert (Ptr{Void}, pyjlwrap_members);
347
- t. tp_dealloc = cfunction (pyjlwrap_dealloc,
348
- Void, (PyPtr,))
349
- t. tp_repr = cfunction (pyjlwrap_repr, PyPtr, (PyPtr,))
367
+ t. tp_base = ccall (:jl_value_ptr , Ptr{Void},
368
+ (Ptr{PyTypeObject},),
369
+ & (jlWrapType:: PyTypeObject ))
350
370
init (t)
351
371
end )
352
372
end
@@ -365,5 +385,13 @@ function pyjlwrap_new(pyT::PyTypeObject, value::Any)
365
385
return o
366
386
end
367
387
388
+ is_pyjlwrap (o:: PyObject ) = (jlWrapType:: PyTypeObject ). tp_name != C_NULL && ccall (pyfunc (:PyObject_IsInstance ), Cint, (PyPtr,Ptr{PyTypeObject}), o, & (jlWrapType:: PyTypeObject )) == 1
389
+
390
+ # ###############################################################
391
+ # Fallback conversion: if we don't have a better conversion function,
392
+ # just wrap the Julia object in a Python object
393
+
394
+ PyObject (x:: Any ) = begin pyjlwrap_init (); pyjlwrap_new (jlWrapType, x); end
395
+
368
396
# ###############################################################
369
397
0 commit comments