python 3 support
This commit is contained in:
1 parent
d5e595d270
commit
444ad69dbc
11 files changed
+66
-23
No files matched your search
@@ -437,7 +437,7 @@ static void
|
||||
%s(%s *self)
|
||||
{
|
||||
%s
|
||||
self->ob_type->tp_free((PyObject*)self);
|
||||
Py_TYPE(self)->tp_free((PyObject*)self);
|
||||
}
|
||||
''' % (container_tp_dealloc_function_name, self.pystruct,
|
||||
self._get_container_delete_code()))
|
||||
@@ -453,7 +453,7 @@ static void
|
||||
{
|
||||
Py_CLEAR(self->container);
|
||||
%s
|
||||
self->ob_type->tp_free((PyObject*)self);
|
||||
Py_TYPE(self)->tp_free((PyObject*)self);
|
||||
}
|
||||
''' % (iter_tp_dealloc_function_name, self.iter_pystruct, self._get_iter_delete_code()))
|
||||
|
||||
|
||||
@@ -268,8 +268,7 @@ class PyMetaclass(object):
|
||||
"""
|
||||
code_sink.writeln('''
|
||||
PyTypeObject %(pytypestruct)s = {
|
||||
PyObject_HEAD_INIT(NULL)
|
||||
0, /* ob_size */
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
(char *) "%(name)s", /* tp_name */
|
||||
0, /* tp_basicsize */
|
||||
0, /* tp_itemsize */
|
||||
|
||||
@@ -1760,7 +1760,7 @@ typedef struct {
|
||||
else:
|
||||
parent_typestruct = self.parent.pytypestruct
|
||||
metaclass = PyMetaclass(self.metaclass_name,
|
||||
"%s.ob_type" % parent_typestruct,
|
||||
"Py_TYPE(&%s)" % parent_typestruct,
|
||||
self.static_attributes)
|
||||
metaclass.generate(code_sink, module)
|
||||
|
||||
@@ -1776,7 +1776,7 @@ typedef struct {
|
||||
% (self.pytypestruct, basenum, base.pytypestruct))
|
||||
|
||||
if metaclass is not None:
|
||||
module.after_init.write_code('%s.ob_type = &%s;' %
|
||||
module.after_init.write_code('Py_TYPE(&%s) = &%s;' %
|
||||
(self.pytypestruct, metaclass.pytypestruct))
|
||||
|
||||
module.after_init.write_error_check('PyType_Ready(&%s)'
|
||||
@@ -2355,7 +2355,7 @@ static void
|
||||
else:
|
||||
code_block.write_code(self._get_delete_code())
|
||||
|
||||
code_block.write_code('self->ob_type->tp_free((PyObject*)self);')
|
||||
code_block.write_code('Py_TYPE(self)->tp_free((PyObject*)self);')
|
||||
|
||||
code_block.write_cleanup()
|
||||
|
||||
|
||||
@@ -212,7 +212,7 @@ static void
|
||||
{
|
||||
Py_CLEAR(self->container);
|
||||
%s
|
||||
self->ob_type->tp_free((PyObject*)self);
|
||||
Py_TYPE(self)->tp_free((PyObject*)self);
|
||||
}
|
||||
''' % (iter_tp_dealloc_function_name, self.iter_pystruct, self._get_iter_delete_code()))
|
||||
|
||||
|
||||
@@ -300,7 +300,7 @@ class CppClassParameter(CppClassParameterBase):
|
||||
wrapper.before_call.write_code("} else {\n")
|
||||
wrapper.before_call.indent()
|
||||
possible_type_names = ", ".join([cls.name for cls in [self.cpp_class] + implicit_conversion_sources])
|
||||
wrapper.before_call.write_code("PyErr_Format(PyExc_TypeError, \"parameter must an instance of one of the types (%s), not %%s\", %s->ob_type->tp_name);" % (possible_type_names, self.py_name))
|
||||
wrapper.before_call.write_code("PyErr_Format(PyExc_TypeError, \"parameter must an instance of one of the types (%s), not %%s\", Py_TYPE(%s)->tp_name);" % (possible_type_names, self.py_name))
|
||||
wrapper.before_call.write_error_return()
|
||||
wrapper.before_call.unindent()
|
||||
wrapper.before_call.write_code("}")
|
||||
@@ -420,7 +420,7 @@ class CppClassRefParameter(CppClassParameterBase):
|
||||
wrapper.before_call.write_code("} else {\n")
|
||||
wrapper.before_call.indent()
|
||||
possible_type_names = ", ".join([cls.name for cls in [self.cpp_class] + implicit_conversion_sources])
|
||||
wrapper.before_call.write_code("PyErr_Format(PyExc_TypeError, \"parameter must an instance of one of the types (%s), not %%s\", %s->ob_type->tp_name);" % (possible_type_names, self.py_name))
|
||||
wrapper.before_call.write_code("PyErr_Format(PyExc_TypeError, \"parameter must an instance of one of the types (%s), not %%s\", Py_TYPE(%s)->tp_name);" % (possible_type_names, self.py_name))
|
||||
wrapper.before_call.write_error_return()
|
||||
wrapper.before_call.unindent()
|
||||
wrapper.before_call.write_code("}")
|
||||
|
||||
@@ -621,7 +621,7 @@ class CppConstructor(ForwardWrapperBase):
|
||||
else:
|
||||
## We should only create a helper class instance when
|
||||
## being called from a user python subclass.
|
||||
self.before_call.write_code("if (self->ob_type != &%s)" % class_.pytypestruct)
|
||||
self.before_call.write_code("if (Py_TYPE(self) != &%s)" % class_.pytypestruct)
|
||||
self.before_call.write_code("{")
|
||||
self.before_call.indent()
|
||||
|
||||
@@ -1011,7 +1011,7 @@ class CppVirtualMethodProxy(ReverseWrapperBase):
|
||||
self.before_call.add_cleanup_code('Py_XDECREF(%s);' % py_method)
|
||||
|
||||
self.before_call.write_code(
|
||||
r'if (%s == NULL || %s->ob_type == &PyCFunction_Type) {' % (py_method, py_method))
|
||||
r'if (%s == NULL || Py_TYPE(%s) == &PyCFunction_Type) {' % (py_method, py_method))
|
||||
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);'
|
||||
|
||||
+1
-1
@@ -160,7 +160,7 @@ class Enum(object):
|
||||
value_str = "%s::%s" % (self.outer_class.full_name, value)
|
||||
module.after_init.write_code(
|
||||
' // %s\n'
|
||||
'tmp_value = PyInt_FromLong(%s);\n'
|
||||
'tmp_value = PyLong_FromLong(%s);\n'
|
||||
'PyDict_SetItemString((PyObject*) %s.tp_dict, \"%s\", tmp_value);\n'
|
||||
'Py_DECREF(tmp_value);'
|
||||
% (
|
||||
|
||||
+32
-2
@@ -230,7 +230,7 @@ class ModuleBase(dict):
|
||||
|
||||
self.cpp_namespace = cpp_namespace
|
||||
if self.parent is None:
|
||||
error_return = 'return;'
|
||||
error_return = 'return MOD_ERROR;'
|
||||
self.after_forward_declarations = MemoryCodeSink()
|
||||
else:
|
||||
self.after_forward_declarations = None
|
||||
@@ -708,10 +708,16 @@ class ModuleBase(dict):
|
||||
mod_init_name = '.'.join(self.get_module_path())
|
||||
else:
|
||||
mod_init_name = module_file_base_name
|
||||
self.before_init.write_code('#if PY_VERSION_HEX >= 0x03000000')
|
||||
self.before_init.write_code(
|
||||
"m = PyModule_Create(&%s_moduledef);"
|
||||
% (self.name))
|
||||
self.before_init.write_code('#else')
|
||||
self.before_init.write_code(
|
||||
"m = Py_InitModule3((char *) \"%s\", %s_functions, %s);"
|
||||
% (mod_init_name, self.prefix,
|
||||
self.docstring and '"'+self.docstring+'"' or 'NULL'))
|
||||
self.before_init.write_code('#endif')
|
||||
self.before_init.write_error_check("m == NULL")
|
||||
|
||||
main_sink = out.get_main_code_sink()
|
||||
@@ -817,16 +823,38 @@ class ModuleBase(dict):
|
||||
self.body.flush_to(main_sink)
|
||||
|
||||
## now generate the module init function itself
|
||||
main_sink.writeln('#if PY_VERSION_HEX >= 0x03000000\n'
|
||||
'static struct PyModuleDef %s_moduledef = {\n'
|
||||
' PyModuleDef_HEAD_INIT,\n'
|
||||
' "%s",\n'
|
||||
' %s,\n'
|
||||
' -1,\n'
|
||||
' %s_functions,\n'
|
||||
'};\n'
|
||||
'#endif' % (self.name, mod_init_name,
|
||||
self.docstring and '"'+self.docstring+'"' or 'NULL',
|
||||
self.prefix))
|
||||
main_sink.writeln()
|
||||
if self.parent is None:
|
||||
main_sink.writeln('''
|
||||
#if PY_VERSION_HEX >= 0x03000000
|
||||
#define MOD_ERROR NULL
|
||||
#define MOD_INIT(name) PyInit_##name(void)
|
||||
#define MOD_RETURN(val) val
|
||||
#else
|
||||
#define MOD_ERROR
|
||||
#define MOD_INIT(name) init##name(void)
|
||||
#define MOD_RETURN(val)
|
||||
#endif
|
||||
PyMODINIT_FUNC
|
||||
#if defined(__GNUC__) && __GNUC__ >= 4
|
||||
__attribute__ ((visibility("default")))
|
||||
#endif''')
|
||||
else:
|
||||
main_sink.writeln("static PyObject *")
|
||||
if module_file_base_name is None:
|
||||
if self.parent is None:
|
||||
main_sink.writeln("MOD_INIT(%s)" % (self.name,))
|
||||
elif module_file_base_name is None:
|
||||
main_sink.writeln("%s(void)" % (self.init_function_name,))
|
||||
else:
|
||||
main_sink.writeln("init%s(void)" % (module_file_base_name,))
|
||||
@@ -838,6 +866,8 @@ __attribute__ ((visibility("default")))
|
||||
self.after_init.sink.flush_to(main_sink)
|
||||
if self.parent is not None:
|
||||
main_sink.writeln("return m;")
|
||||
else:
|
||||
main_sink.writeln("return MOD_RETURN(m);")
|
||||
main_sink.unindent()
|
||||
main_sink.writeln('}')
|
||||
|
||||
|
||||
@@ -5,8 +5,7 @@ The class PyTypeObject generates a PyTypeObject structure contents.
|
||||
class PyTypeObject(object):
|
||||
TEMPLATE = (
|
||||
'PyTypeObject %(typestruct)s = {\n'
|
||||
' PyObject_HEAD_INIT(NULL)\n'
|
||||
' 0, /* ob_size */\n'
|
||||
' PyVarObject_HEAD_INIT(NULL, 0)\n'
|
||||
' (char *) "%(tp_name)s", /* tp_name */\n'
|
||||
' %(tp_basicsize)s, /* tp_basicsize */\n'
|
||||
' 0, /* tp_itemsize */\n'
|
||||
@@ -216,9 +215,9 @@ static PySequenceMethods %(variable)s = {
|
||||
(binaryfunc) %(sq_concat)s,
|
||||
(ssizeargfunc) %(sq_repeat)s,
|
||||
(ssizeargfunc) %(sq_item)s,
|
||||
(ssizessizeargfunc) %(sq_slice)s,
|
||||
%(sq_slice)s,
|
||||
(ssizeobjargproc) %(sq_ass_item)s,
|
||||
(ssizessizeobjargproc) %(sq_ass_slice)s,
|
||||
%(sq_ass_slice)s,
|
||||
(objobjproc) %(sq_contains)s,
|
||||
/* Added in release 2.0 */
|
||||
(binaryfunc) %(sq_inplace_concat)s,
|
||||
@@ -239,7 +238,7 @@ static Py_ssize_t
|
||||
if (py_result == NULL) {
|
||||
return -1;
|
||||
}
|
||||
result = PyInt_AsSsize_t(py_result);
|
||||
result = PyLong_AsSsize_t(py_result);
|
||||
Py_DECREF(py_result);
|
||||
return result;
|
||||
}
|
||||
@@ -262,7 +261,7 @@ static Py_ssize_t
|
||||
if (py_result == NULL) {
|
||||
return -1;
|
||||
}
|
||||
result = PyInt_AsSsize_t(py_result);
|
||||
result = PyLong_AsSsize_t(py_result);
|
||||
Py_DECREF(py_result);
|
||||
return result;
|
||||
}
|
||||
@@ -327,7 +326,7 @@ static int
|
||||
slots.setdefault('sq_repeat', 'NULL')
|
||||
slots.setdefault('sq_item', 'NULL')
|
||||
slots.setdefault('sq_slice', 'NULL')
|
||||
slots.setdefault('sq_ass_item', 'NULL')
|
||||
slots.setdefault('sq_ass_item', 'NULL')
|
||||
slots.setdefault('sq_ass_slice', 'NULL')
|
||||
slots.setdefault('sq_contains', 'NULL')
|
||||
slots.setdefault('sq_inplace_concat', 'NULL')
|
||||
|
||||
@@ -91,7 +91,7 @@ class UnsignedIntPtrParam(PointerParameter):
|
||||
|
||||
wrapper.before_call.write_code("%(elem)s = PyList_GET_ITEM(%(py_list)s, %(idx)s);" % vars())
|
||||
wrapper.before_call.write_error_check(
|
||||
'!(PyInt_Check(%(elem)s) || PyLong_Check(%(elem)s))',
|
||||
'!PyNumber_Check(%(elem)s)',
|
||||
'PyErr_SetString(PyExc_TypeError, "Parameter `%s\' must be a list of %i ints / longs");'
|
||||
% (self.name, self.array_length))
|
||||
wrapper.before_call.write_code("%(name)s[%(idx)s] = PyLong_AsUnsignedInt(%(elem)s);" % vars())
|
||||
|
||||
@@ -89,6 +89,21 @@ typedef intobjargproc ssizeobjargproc;
|
||||
#endif
|
||||
''')
|
||||
|
||||
if min_python_version < (2, 6):
|
||||
code_sink.writeln(r'''
|
||||
#ifndef PyVarObject_HEAD_INIT
|
||||
#define PyVarObject_HEAD_INIT(type, size) \
|
||||
PyObject_HEAD_INIT(type) size,
|
||||
#endif
|
||||
''')
|
||||
|
||||
code_sink.writeln(r'''
|
||||
#if PY_VERSION_HEX >= 0x03000000
|
||||
typedef void* cmpfunc;
|
||||
#endif
|
||||
''')
|
||||
|
||||
|
||||
code_sink.writeln(r'''
|
||||
#if __GNUC__ > 2
|
||||
# define PYBINDGEN_UNUSED(param) param __attribute__((__unused__))
|
||||
|
||||
Reference in new issue
Block a user