diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 7cb1211..7a4136b 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -2004,12 +2004,8 @@ def generate(self, code_sink, module): 'PyModule_AddObject(m, (char *) \"%s\", (PyObject *) _TYPEREF %s);' % ( class_python_name, self.pytypestruct)) else: - module.after_init.write_code( - '#ifdef Py_LIMITED_API\n' - 'PyObject_SetAttrString((PyObject*) %s, (char *) \"%s\", (PyObject *) %s);\n' - '#else\n' - 'PyDict_SetItemString((PyObject*) %s.tp_dict, \"%s\", (PyObject *) &%s);\n' - '#endif\n' % (self.outer_class.pytypestruct, class_python_name, self.pytypestruct, + module.after_init.write_code('PBG_SETATTR(%s, \"%s\", _TYPEREF %s);\n' + % ( self.outer_class.pytypestruct, class_python_name, self.pytypestruct)) have_constructor = self._generate_constructor(code_sink) diff --git a/pybindgen/enum.py b/pybindgen/enum.py index 461ffa0..7779887 100644 --- a/pybindgen/enum.py +++ b/pybindgen/enum.py @@ -169,15 +169,10 @@ def generate(self, unused_code_sink): module.after_init.write_code( ' // %s\n' 'tmp_value = PyLong_FromLong(%s);\n' - '#ifdef Py_LIMITED_API\n' - 'PyObject_SetAttrString((PyObject*) %s, (char *) \"%s\", tmp_value);\n' - '#else\n' - 'PyDict_SetItemString((PyObject*) %s.tp_dict, \"%s\", tmp_value);\n' - '#endif\n' + 'PBG_SETATTR(%s, \"%s\", tmp_value);\n' 'Py_DECREF(tmp_value);' % ( - value_str, value_str, self.outer_class.pytypestruct, value_name, - self.outer_class.pytypestruct, value_name)) + value_str, value_str, self.outer_class.pytypestruct, value_name)) module.after_init.unindent() module.after_init.write_code("}") diff --git a/pybindgen/utils.py b/pybindgen/utils.py index 74c7cf9..2b30543 100644 --- a/pybindgen/utils.py +++ b/pybindgen/utils.py @@ -123,9 +123,9 @@ def write_preamble(code_sink, min_python_version=None): #endif #ifdef Py_LIMITED_API -# define PBG_SETATTR(_type, _name, _value) PyObject_SetAttrString((PyObject*) _type, (char *) _name, _value); +# define PBG_SETATTR(_type, _name, _value) PyObject_SetAttrString((PyObject*) _type, (char *) _name, (PyObject*) _value); #else -# define PBG_SETATTR(_type, _name, _value) PyDict_SetItemString((PyObject*) _type.tp_dict, _name, _value); +# define PBG_SETATTR(_type, _name, _value) PyDict_SetItemString((PyObject*) _type.tp_dict, _name, (PyObject*) _value); #endif #if PY_VERSION_HEX >= 0x03000000