Commit 589800e3 authored by Xavier Thompson's avatar Xavier Thompson

Allow conversion from PyObject to CyObject

parent 12c3cd84
......@@ -3998,6 +3998,21 @@ class CypClassType(CppClassType):
return False
self.to_py_function = "__Pyx_PyObject_FromCyObject"
return True
def create_from_py_utility_code(self, env):
if not self.wrapper_type:
return False
wrapper_objstruct = self.wrapper_type.objstruct_cname
underlying_type_name = self.cname
self.from_py_function = "__Pyx_PyObject_AsCyObject<%s, %s>" % (wrapper_objstruct, underlying_type_name)
return True
def from_py_call_code(self, source_code, result_code, error_pos, code,
from_py_function=None, error_condition=None):
extra_args = [self.wrapper_type.typeptr_cname if self.wrapper_type else None]
return self._assign_from_py_code(
source_code, result_code, error_pos, code, from_py_function, error_condition, extra_args=extra_args)
def empty_declaration_code(self):
if self._empty_declaration is None:
......
......@@ -185,6 +185,31 @@
return ob;
}
/*
* Cast from CyObject to PyObject:
* - borrow an Python reference
* - return a new atomic reference
*
* template:
* - W: the type of the extension type wrapper
* - U: the type of the underlying cypclass
* - T: pointer to the PyTypeObject instance of the wrapper
*/
template <typename W, typename U>
static inline U* __Pyx_PyObject_AsCyObject(PyObject * ob, PyTypeObject * type) {
// the PyObject is not of the expected type
if (ob->ob_type != type)
return NULL;
W * wrapper = (W *)ob;
U * underlying = dynamic_cast<U *>(wrapper->nogil_cyobject);
// no underlying cyobject: shouldn't happen, playing it safe for now
if (underlying == NULL)
return NULL;
// return a new atomic reference
underlying->CyObject_INCREF();
return underlying;
}
/* Cast argument to CyObject* type. */
#define _CyObject_CAST(op) op
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment