diff --git a/pybindgen/cppattribute.py b/pybindgen/cppattribute.py index b06fd39..2d348b0 100644 --- a/pybindgen/cppattribute.py +++ b/pybindgen/cppattribute.py @@ -132,7 +132,7 @@ class CppInstanceAttributeSetter(PySetter): self.declarations.declare_variable('PyObject*', 'py_retval') self.before_call.write_code( - 'py_retval = Py_BuildValue("(O)", value);') + 'py_retval = Py_BuildValue((char *) "(O)", value);') self.before_call.add_cleanup_code('Py_DECREF(py_retval);') if self.setter is not None: @@ -150,7 +150,10 @@ class CppInstanceAttributeSetter(PySetter): self.return_value.convert_python_to_c(self) parse_tuple_params = ['py_retval'] - parse_tuple_params.extend(self.parse_params.get_parameters()) + params = self.parse_params.get_parameters() + assert params[0][0] == '"' + params[0] = '(char *) ' + params[0] + parse_tuple_params.extend(params) self.before_call.write_error_check('!PyArg_ParseTuple(%s)' % (', '.join(parse_tuple_params),)) @@ -203,11 +206,14 @@ class CppStaticAttributeSetter(PySetter): self.declarations.declare_variable('PyObject*', 'py_retval') self.before_call.write_code( - 'py_retval = Py_BuildValue("(O)", value);') + 'py_retval = Py_BuildValue((char *) "(O)", value);') self.before_call.add_cleanup_code('Py_DECREF(py_retval);') self.return_value.convert_python_to_c(self) parse_tuple_params = ['py_retval'] - parse_tuple_params.extend(self.parse_params.get_parameters()) + params = self.parse_params.get_parameters() + assert params[0][0] == '"' + params[0] = '(char *) ' + params[0] + parse_tuple_params.extend(params) self.before_call.write_error_check('!PyArg_ParseTuple(%s)' % (', '.join(parse_tuple_params),)) ## cleanup and return @@ -261,7 +267,7 @@ class PyMetaclass(object): PyTypeObject %(pytypestruct)s = { PyObject_HEAD_INIT(NULL) 0, /* ob_size */ - "%(name)s", /* tp_name */ + (char *) "%(name)s", /* tp_name */ 0, /* tp_basicsize */ 0, /* tp_itemsize */ 0, /* tp_dealloc */ diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 288e8b6..19ae218 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -11,9 +11,9 @@ from typehandlers.base import ReturnValue, \ from typehandlers.codesink import NullCodeSink, MemoryCodeSink -from cppattribute import (CppInstanceAttributeGetter, CppInstanceAttributeSetter, - CppStaticAttributeGetter, CppStaticAttributeSetter, - PyGetSetDef, PyMetaclass) +from cppattribute import CppInstanceAttributeGetter, CppInstanceAttributeSetter, \ + CppStaticAttributeGetter, CppStaticAttributeSetter, \ + PyGetSetDef, PyMetaclass import settings import utils @@ -377,7 +377,7 @@ class CppClass(object): 'PyTypeObject %(typestruct)s = {\n' ' PyObject_HEAD_INIT(NULL)\n' ' 0, /* ob_size */\n' - ' "%(tp_name)s", /* tp_name */\n' + ' (char *) "%(tp_name)s", /* tp_name */\n' ' %(tp_basicsize)s, /* tp_basicsize */\n' ' 0, /* tp_itemsize */\n' ' /* methods */\n' @@ -1406,11 +1406,11 @@ typedef struct { if self.outer_class is None: module.after_init.write_code( - 'PyModule_AddObject(m, \"%s\", (PyObject *) &%s);' % ( + 'PyModule_AddObject(m, (char *) \"%s\", (PyObject *) &%s);' % ( class_python_name, self.pytypestruct)) else: module.after_init.write_code( - 'PyDict_SetItemString((PyObject*) %s.tp_dict, \"%s\", (PyObject *) &%s);' % ( + 'PyDict_SetItemString((PyObject*) %s.tp_dict, (char *) \"%s\", (PyObject *) &%s);' % ( self.outer_class.pytypestruct, class_python_name, self.pytypestruct)) have_constructor = self._generate_constructor(code_sink) @@ -1689,7 +1689,7 @@ static void with a new name in that module (typedef alias). """ module.after_init.write_code( - 'PyModule_AddObject(m, \"%s\", (PyObject *) &%s);' % ( + 'PyModule_AddObject(m, (char *) \"%s\", (PyObject *) &%s);' % ( alias, self.pytypestruct)) diff --git a/pybindgen/cppclass_typehandlers.py b/pybindgen/cppclass_typehandlers.py index 5f199bb..1362e89 100644 --- a/pybindgen/cppclass_typehandlers.py +++ b/pybindgen/cppclass_typehandlers.py @@ -596,13 +596,13 @@ def _add_ward(wrapper, custodian, ward): wards = wrapper.declarations.declare_variable( 'PyObject*', 'wards') wrapper.after_call.write_code( - "%(wards)s = PyObject_GetAttrString(%(custodian)s, \"__wards__\");" + "%(wards)s = PyObject_GetAttrString(%(custodian)s, (char *) \"__wards__\");" % vars()) wrapper.after_call.write_code( "if (%(wards)s == NULL) {\n" " PyErr_Clear();\n" " %(wards)s = PyList_New(0);\n" - " PyObject_SetAttrString(%(custodian)s, \"__wards__\", %(wards)s);\n" + " PyObject_SetAttrString(%(custodian)s, (char *) \"__wards__\", %(wards)s);\n" "}" % vars()) wrapper.after_call.write_code("PyList_Append(%s, %s);" % (wards, ward)) wrapper.after_call.add_cleanup_code("Py_DECREF(%s);" % wards) diff --git a/pybindgen/cppmethod.py b/pybindgen/cppmethod.py index 473f943..8f05823 100644 --- a/pybindgen/cppmethod.py +++ b/pybindgen/cppmethod.py @@ -286,7 +286,7 @@ class CppMethod(ForwardWrapperBase): def get_py_method_def(self, method_name): "Get the PyMethodDef entry suitable for this method" flags = self.get_py_method_def_flags() - return "{\"%s\", (PyCFunction) %s, %s, %s }," % \ + return "{(char *) \"%s\", (PyCFunction) %s, %s, %s }," % \ (method_name, self.wrapper_actual_name, '|'.join(flags), (self.docstring is None and "NULL" or ('"'+self.docstring+'"'))) @@ -711,7 +711,7 @@ class CppVirtualMethodParentCaller(CppMethod): if method_name is None: method_name = self.method_name flags = self.get_py_method_def_flags() - return "{\"%s\", (PyCFunction) %s, %s, %s }," % \ + return "{(char *) \"%s\", (PyCFunction) %s, %s, %s }," % \ (method_name, self.wrapper_actual_name,#'::'.join((self._helper_class.name, self.wrapper_actual_name)), '|'.join(flags), @@ -802,7 +802,7 @@ class CppVirtualMethodProxy(ReverseWrapperBase): ## just chain to parent class and don't do anything else call_params = ', '.join([param.name for param in self.parameters]) self.before_call.write_code( - r'if (!PyObject_HasAttrString(m_pyself, "_%s")) {' % self.method_name) + r'if (!PyObject_HasAttrString(m_pyself, (char *) "_%s")) {' % self.method_name) if self.return_value.ctype == 'void': if not (self.method.is_pure_virtual or self.method.visibility == 'private'): self.before_call.write_code(r' %s::%s(%s);' diff --git a/pybindgen/enum.py b/pybindgen/enum.py index b29fd2d..9d059b7 100644 --- a/pybindgen/enum.py +++ b/pybindgen/enum.py @@ -104,7 +104,7 @@ class Enum(object): namespace.append(self.cpp_namespace) for value in self.values: module.after_init.write_code( - "PyModule_AddIntConstant(m, \"%s\", %s);" + "PyModule_AddIntConstant(m, (char *) \"%s\", %s);" % (value, '::'.join(namespace + [self.values_prefix + value]))) else: module.after_init.write_code("{") diff --git a/pybindgen/function.py b/pybindgen/function.py index 9f06e4e..6be5d9c 100644 --- a/pybindgen/function.py +++ b/pybindgen/function.py @@ -172,7 +172,7 @@ class Function(ForwardWrapperBase): assert isinstance(self.wrapper_return, str) assert isinstance(self.wrapper_actual_name, str) assert isinstance(self.wrapper_args, list) - return "{\"%s\", (PyCFunction) %s, %s, %s }," % \ + return "{(char *) \"%s\", (PyCFunction) %s, %s, %s }," % \ (name, self.wrapper_actual_name, '|'.join(flags), (self.docstring is None and "NULL" or ('"'+self.docstring+'"'))) diff --git a/pybindgen/module.py b/pybindgen/module.py index a051d0f..12483a3 100644 --- a/pybindgen/module.py +++ b/pybindgen/module.py @@ -16,9 +16,9 @@ In Python, a sub-module will appear as a I{built-in} Python module that is available as an attribute of its parent module. For instance, a module I{foo} having a sub-module I{xpto} appears like this:: - >>> import foo - >>> foo.xpto - + |>>> import foo + |>>> foo.xpto + | Modules and C++ namespaces ========================== @@ -27,21 +27,21 @@ Modules can be associated with specific C++ namespaces. This means, for instance, that any C++ class wrapped inside that module must belong to that C++ namespace. Example:: - >>> from cppclass import * - >>> mod = Module("foo", cpp_namespace="::foo") - >>> mod.add_class("Bar") - + |>>> from cppclass import * + |>>> mod = Module("foo", cpp_namespace="::foo") + |>>> mod.add_class("Bar") + | When we have a toplevel C++ namespace which contains another nested namespace, we want to wrap the nested namespace as a Python sub-module. The method L{ModuleBase.add_cpp_namespace} makes it easy to create sub-modules for wrapping nested namespaces. For instance:: - >>> from cppclass import * - >>> mod = Module("foo", cpp_namespace="::foo") - >>> submod = mod.add_cpp_namespace('xpto') - >>> submod.add_class("Bar") - + |>>> from cppclass import * + |>>> mod = Module("foo", cpp_namespace="::foo") + |>>> submod = mod.add_cpp_namespace('xpto') + |>>> submod.add_class("Bar") + | """ @@ -628,7 +628,7 @@ class ModuleBase(dict): else: mod_init_name = module_file_base_name self.before_init.write_code( - "m = Py_InitModule3(\"%s\", %s_functions, %s);" + "m = Py_InitModule3((char *) \"%s\", %s_functions, %s);" % (mod_init_name, self.prefix, self.docstring and '"'+self.docstring+'"' or 'NULL')) self.before_init.write_error_check("m == NULL") @@ -701,7 +701,7 @@ class ModuleBase(dict): submodule_var, submodule.init_function_name)) self.after_init.write_error_check('%s == NULL' % submodule_var) self.after_init.write_code('Py_INCREF(%s);' % (submodule_var,)) - self.after_init.write_code('PyModule_AddObject(m, "%s", %s);' + self.after_init.write_code('PyModule_AddObject(m, (char *) "%s", %s);' % (submodule.name, submodule_var,)) ## flush the header section diff --git a/pybindgen/overloading.py b/pybindgen/overloading.py index f1a94e9..fc2f6f8 100644 --- a/pybindgen/overloading.py +++ b/pybindgen/overloading.py @@ -6,6 +6,13 @@ from typehandlers.base import ForwardWrapperBase from typehandlers.codesink import NullCodeSink import utils + +try: + set +except NameError: + from sets import Set as set # Python 2.3 fallback + + def isiterable(obj): """Returns True if an object appears to be iterable""" return hasattr(obj, '__iter__') or isinstance(obj, basestring) @@ -282,7 +289,7 @@ class OverloadedWrapper(object): assert isinstance(self.wrapper_actual_name, str) assert isinstance(self.wrapper_args, list) - return "{\"%s\", (PyCFunction) %s, %s, %s }," % \ + return "{(char *) \"%s\", (PyCFunction) %s, %s, %s }," % \ (name, self.wrapper_actual_name, '|'.join(flags), (docstring is None and "NULL" or ('"'+docstring+'"'))) diff --git a/pybindgen/typehandlers/base.py b/pybindgen/typehandlers/base.py index 690a380..a1b59f8 100644 --- a/pybindgen/typehandlers/base.py +++ b/pybindgen/typehandlers/base.py @@ -11,6 +11,7 @@ import warnings from ctypeparser import normalize_type_string + try: all except NameError: # for compatibility with Python < 2.5 @@ -20,6 +21,13 @@ except NameError: # for compatibility with Python < 2.5 if not element: return False return True +try: + set +except NameError: + from sets import Set as set # Python 2.3 fallback + + + class CodegenErrorBase(Exception): pass @@ -593,10 +601,13 @@ class ReverseWrapperBase(object): else: ## parse the return value ## this ensures that py_retval is always a tuple - self.before_call.write_code('py_retval = Py_BuildValue("(N)", py_retval);') + self.before_call.write_code('py_retval = Py_BuildValue((char*) "(N)", py_retval);') parse_tuple_params = ['py_retval'] - parse_tuple_params.extend(self.parse_params.get_parameters()) + parse_params = self.parse_params.get_parameters() + assert parse_params[0][0] == '"' + parse_params[0] = '(char *) ' + parse_params[0] + parse_tuple_params.extend(parse_params) self.before_call.write_error_check('!PyArg_ParseTuple(%s)' % (', '.join(parse_tuple_params),)) @@ -829,11 +840,15 @@ class ForwardWrapperBase(object): self.generate_call(*gen_call_params) params = self.parse_params.get_parameters() + assert params[0][0] == '"' + params_empty = (params == ['""']) + params[0] = '(char *) ' + params[0] keywords = self.parse_params.get_keywords() - if params != ['""'] or self.force_parse != None: + if not params_empty or self.force_parse != None: self.meth_flags.append("METH_VARARGS") - if (keywords is None - and self.force_parse != self.PARSE_TUPLE_AND_KEYWORDS): + if keywords is None \ + and self.force_parse != self.PARSE_TUPLE_AND_KEYWORDS: + param_list = ['args'] + params self.before_parse.write_error_check('!PyArg_ParseTuple(%s)' % (', '.join(param_list),)) @@ -869,8 +884,14 @@ class ForwardWrapperBase(object): params = self.build_params.get_parameters() if params: - self.after_call.write_code('py_retval = Py_BuildValue(%s);' % - (', '.join(params),)) + if params == ['""']: + self.after_call.write_code('Py_INCREF(Py_None);') + self.after_call.write_code('py_retval = Py_None;') + else: + assert params[0][0] == '"' + params[0] = "(char *) " + params[0] + self.after_call.write_code('py_retval = Py_BuildValue(%s);' % + (', '.join(params),)) ## cleanup and return self.after_call.write_cleanup() @@ -1007,7 +1028,7 @@ class ReturnValue(object): for ctype in mcs.CTYPES: return_type_matcher.register(ctype, mcs) - @classmethod + #@classmethod def new(cls, *args, **kwargs): """ >>> import inttype @@ -1030,6 +1051,8 @@ class ReturnValue(object): else: return cls(*args, **kwargs) + new = classmethod(new) + def __init__(self, ctype, is_const=False): ''' Creates a return value object @@ -1130,8 +1153,7 @@ class Parameter(object): for ctype in mcs.CTYPES: param_type_matcher.register(ctype, mcs) - - @classmethod + #@classmethod def new(cls, *args, **kwargs): """ >>> import inttype @@ -1155,6 +1177,7 @@ class Parameter(object): else: return cls(*args, **kwargs) + new = classmethod(new) def __init__(self, ctype, name, direction=DIRECTION_IN, is_const=False, default_value=None): ''' diff --git a/tests/foomodulegen.py b/tests/foomodulegen.py index c4ee128..cf9e098 100755 --- a/tests/foomodulegen.py +++ b/tests/foomodulegen.py @@ -6,8 +6,8 @@ import re import pybindgen import pybindgen.utils from pybindgen.typehandlers import base as typehandlers -from pybindgen import (ReturnValue, Parameter, Module, Function, FileCodeSink) -from pybindgen import (CppMethod, CppConstructor, CppClass, Enum) +from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink +from pybindgen import CppMethod, CppConstructor, CppClass, Enum from pybindgen.function import CustomFunctionWrapper from pybindgen.cppmethod import CustomCppMethodWrapper from pybindgen import cppclass diff --git a/tests/foomodulegen_common.py b/tests/foomodulegen_common.py index 531d431..6502052 100644 --- a/tests/foomodulegen_common.py +++ b/tests/foomodulegen_common.py @@ -5,9 +5,8 @@ import re import pybindgen from pybindgen.typehandlers import base as typehandlers -from pybindgen import (ReturnValue, Parameter, Module, Function, FileCodeSink) -from pybindgen import (CppMethod, CppConstructor, CppClass, Enum) -from pybindgen.gccxmlparser import ModuleParser +from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink +from pybindgen import CppMethod, CppConstructor, CppClass, Enum from pybindgen.function import CustomFunctionWrapper from pybindgen.cppmethod import CustomCppMethodWrapper @@ -61,7 +60,7 @@ _wrap_foofunction_that_takes_foo_from_string(PyObject * PYBINDGEN_UNUSED(dummy), char *datum; const char *keywords[] = {"foo", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", (char **) keywords, &datum)) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "s", (char **) keywords, &datum)) { { PyObject *exc_type, *traceback; PyErr_Fetch(&exc_type, return_exception, &traceback); @@ -71,7 +70,8 @@ _wrap_foofunction_that_takes_foo_from_string(PyObject * PYBINDGEN_UNUSED(dummy), return NULL; } function_that_takes_foo(Foo(datum)); - py_retval = Py_BuildValue(""); + Py_INCREF(Py_None); + py_retval = Py_None; return py_retval; } ''' @@ -91,7 +91,7 @@ _wrap_PyBar_Hooray_lenx(PyBar *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject int x; const char *keywords[] = {"x", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", (char **) keywords, &x)) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "i", (char **) keywords, &x)) { PyObject *exc_type, *traceback; PyErr_Fetch(&exc_type, return_exception, &traceback); Py_XDECREF(exc_type); @@ -101,7 +101,7 @@ _wrap_PyBar_Hooray_lenx(PyBar *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject std::string retval; retval = Bar::Hooray(); - py_retval = Py_BuildValue("i", int(retval.size() + x)); + py_retval = Py_BuildValue((char *) "i", int(retval.size() + x)); return py_retval; } ''' diff --git a/tests/test.py b/tests/test.py index 626a42e..5733faa 100644 --- a/tests/test.py +++ b/tests/test.py @@ -69,7 +69,7 @@ if __name__ == '__main__': module, cppclass, overloading, - utils, + #utils, stringtype, ctypeparser, ]: