From 2e7450020d0e3e68ea0f06f1bfe1c429579b8d2a Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 28 Jul 2008 12:37:12 +0100 Subject: [PATCH] Implement null_ok and default_value for CppClassPtrParameter. Closes #252447. --- pybindgen/cppclass_typehandlers.py | 55 ++++++++++++++++++++++++++---- pybindgen/gccxmlparser.py | 2 ++ tests/c-hello/hello.c | 10 ++++++ tests/c-hello/hello.h | 4 +++ tests/c-hello/hellotest.py | 12 +++++++ 5 files changed, 76 insertions(+), 7 deletions(-) diff --git a/pybindgen/cppclass_typehandlers.py b/pybindgen/cppclass_typehandlers.py index 1362e89..a4f6bee 100644 --- a/pybindgen/cppclass_typehandlers.py +++ b/pybindgen/cppclass_typehandlers.py @@ -393,7 +393,8 @@ class CppClassPtrParameter(CppClassParameterBase): DIRECTIONS = [Parameter.DIRECTION_IN] SUPPORTS_TRANSFORMATIONS = True - def __init__(self, ctype, name, transfer_ownership=None, custodian=None, is_const=False): + def __init__(self, ctype, name, transfer_ownership=None, custodian=None, is_const=False, + null_ok=False, default_value=None): """ @param ctype: C type, normally 'MyClass*' @param name: parameter name @@ -416,6 +417,12 @@ class CppClassPtrParameter(CppClassParameterBase): @param is_const: if true, the parameter has a const attached to the leftmost + @param null_ok: if true, None is accepted and mapped into a C NULL pointer + + @param default_value: default parameter value (as C expression + string); probably, the only default value that makes sense + here is probably 'NULL'. + @note: only arguments which are instances of C++ classes wrapped by PyBindGen can be used as custodians. """ @@ -434,6 +441,8 @@ class CppClassPtrParameter(CppClassParameterBase): "not be given when there is a custodian") self.transfer_ownership = False self.custodian = custodian + self.null_ok = null_ok + self.default_value = default_value def convert_python_to_c(self, wrapper): "parses python args to get C++ value" @@ -442,21 +451,53 @@ class CppClassPtrParameter(CppClassParameterBase): if self.take_value_from_python_self: self.py_name = 'self' + value_ptr = 'self->obj' else: self.py_name = wrapper.declarations.declare_variable( - self.cpp_class.pystruct+'*', self.name) - wrapper.parse_params.add_parameter( - 'O!', ['&'+self.cpp_class.pytypestruct, '&'+self.py_name], self.name) + self.cpp_class.pystruct+'*', self.name, + initializer=(self.default_value and 'NULL' or None)) - value = self.transformation.transform( - self, wrapper.declarations, wrapper.before_call, '%s->obj' % self.py_name) + value_ptr = wrapper.declarations.declare_variable("%s*" % self.cpp_class.full_name, + "%s_ptr" % self.name) + + if self.null_ok: + num = wrapper.parse_params.add_parameter('O', ['&'+self.py_name], self.name, optional=bool(self.default_value)) + + wrapper.before_call.write_error_check( + + "%s && ((PyObject *) %s != Py_None) && !PyObject_IsInstance((PyObject *) %s, (PyObject *) &%s)" + % (self.py_name, self.py_name, self.py_name, self.cpp_class.pytypestruct), + + 'PyErr_SetString(PyExc_TypeError, "Parameter %i must be %s");' % (num, self.cpp_class.name)) + + wrapper.before_call.write_code("if (%(PYNAME)s) {\n" + " if ((PyObject *) %(PYNAME)s == Py_None)\n" + " %(VALUE)s = NULL;\n" + " else\n" + " %(VALUE)s = %(PYNAME)s->obj;\n" + "} else {\n" + " %(VALUE)s = NULL;\n" + "}" % dict(PYNAME=self.py_name, VALUE=value_ptr)) + + else: + + wrapper.parse_params.add_parameter( + 'O!', ['&'+self.cpp_class.pytypestruct, '&'+self.py_name], self.name, optional=bool(self.default_value)) + wrapper.before_call.write_code("%s = (%s ? %s->obj : NULL);" % (value_ptr, self.py_name, self.py_name)) + + value = self.transformation.transform(self, wrapper.declarations, wrapper.before_call, value_ptr) wrapper.call_params.append(value) if self.transfer_ownership: if not isinstance(self.cpp_class.memory_policy, cppclass.ReferenceCountingPolicy): - wrapper.after_call.write_code('%s->obj = NULL;' % (self.py_name,)) + # if we transfer ownership, in the end we no longer own the object, so clear our pointer + wrapper.after_call.write_code('if (%s)\n %s->obj = NULL;' % (self.py_name, self.py_name,)) else: + wrapper.before_call.write_code("if (%s) {" % self.py_name) + wrapper.before_call.indent() self.cpp_class.memory_policy.write_incref(wrapper.before_call, "%s->obj" % self.py_name) + wrapper.before_call.unindent() + wrapper.before_call.write_code("}") def convert_c_to_python(self, wrapper): diff --git a/pybindgen/gccxmlparser.py b/pybindgen/gccxmlparser.py index 0de7428..d4f8495 100644 --- a/pybindgen/gccxmlparser.py +++ b/pybindgen/gccxmlparser.py @@ -307,6 +307,8 @@ class GccXmlTypeRegistry(object): kwargs['array_length'] = int(value) elif name == 'default_value': kwargs['default_value'] = value + elif name == 'null_ok': + kwargs['null_ok'] = annotations_scanner.parse_boolean(value) else: warnings.warn("invalid annotation name %r" % name, AnnotationsWarning) diff --git a/tests/c-hello/hello.c b/tests/c-hello/hello.c index 538fb33..eca8983 100644 --- a/tests/c-hello/hello.c +++ b/tests/c-hello/hello.c @@ -91,3 +91,13 @@ const HelloFoo* hello_foo_get_self (HelloFoo *foo) return foo; } +int hello_get_hash (const HelloFoo *foo) +{ + if (foo) + { + return (int) (long) foo; + } else { + return -1; + } +} + diff --git a/tests/c-hello/hello.h b/tests/c-hello/hello.h index 9c530f4..386a9ac 100644 --- a/tests/c-hello/hello.h +++ b/tests/c-hello/hello.h @@ -27,6 +27,10 @@ const char * hello_foo_get_data (HelloFoo *foo); const HelloFoo* hello_foo_get_self (HelloFoo *foo); +/* -#- @foo(null_ok=true, default_value=NULL) -#- */ +int hello_get_hash (const HelloFoo *foo); + + #ifdef __cplusplus } #endif diff --git a/tests/c-hello/hellotest.py b/tests/c-hello/hellotest.py index e071117..ae24eb7 100644 --- a/tests/c-hello/hellotest.py +++ b/tests/c-hello/hellotest.py @@ -37,6 +37,18 @@ class TestHello(unittest.TestCase): while gc.collect(): pass + def test_null_ok(self): + foo = hello.Foo(3) + + h1 = hello.get_hash(foo) + self.assertNotEqual(h1, -1) + + h2 = hello.get_hash(None) + self.assertEqual(h2, -1) + + h3 = hello.get_hash() + self.assertEqual(h3, -1) + if __name__ == '__main__': unittest.main()