Skip to content

Commit

Permalink
Cut down on ifdefs (PBG_SETATTR)
Browse files Browse the repository at this point in the history
  • Loading branch information
stt committed May 26, 2018
1 parent c4246a9 commit fbc2ea9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
8 changes: 2 additions & 6 deletions pybindgen/cppclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions pybindgen/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("}")

Expand Down
4 changes: 2 additions & 2 deletions pybindgen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fbc2ea9

Please sign in to comment.