Skip to content

Commit fbc2ea9

Browse files
committed
Cut down on ifdefs (PBG_SETATTR)
1 parent c4246a9 commit fbc2ea9

File tree

3 files changed

+6
-15
lines changed

3 files changed

+6
-15
lines changed

pybindgen/cppclass.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,12 +2004,8 @@ def generate(self, code_sink, module):
20042004
'PyModule_AddObject(m, (char *) \"%s\", (PyObject *) _TYPEREF %s);' % (
20052005
class_python_name, self.pytypestruct))
20062006
else:
2007-
module.after_init.write_code(
2008-
'#ifdef Py_LIMITED_API\n'
2009-
'PyObject_SetAttrString((PyObject*) %s, (char *) \"%s\", (PyObject *) %s);\n'
2010-
'#else\n'
2011-
'PyDict_SetItemString((PyObject*) %s.tp_dict, \"%s\", (PyObject *) &%s);\n'
2012-
'#endif\n' % (self.outer_class.pytypestruct, class_python_name, self.pytypestruct,
2007+
module.after_init.write_code('PBG_SETATTR(%s, \"%s\", _TYPEREF %s);\n'
2008+
% (
20132009
self.outer_class.pytypestruct, class_python_name, self.pytypestruct))
20142010

20152011
have_constructor = self._generate_constructor(code_sink)

pybindgen/enum.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,10 @@ def generate(self, unused_code_sink):
169169
module.after_init.write_code(
170170
' // %s\n'
171171
'tmp_value = PyLong_FromLong(%s);\n'
172-
'#ifdef Py_LIMITED_API\n'
173-
'PyObject_SetAttrString((PyObject*) %s, (char *) \"%s\", tmp_value);\n'
174-
'#else\n'
175-
'PyDict_SetItemString((PyObject*) %s.tp_dict, \"%s\", tmp_value);\n'
176-
'#endif\n'
172+
'PBG_SETATTR(%s, \"%s\", tmp_value);\n'
177173
'Py_DECREF(tmp_value);'
178174
% (
179-
value_str, value_str, self.outer_class.pytypestruct, value_name,
180-
self.outer_class.pytypestruct, value_name))
175+
value_str, value_str, self.outer_class.pytypestruct, value_name))
181176
module.after_init.unindent()
182177
module.after_init.write_code("}")
183178

pybindgen/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ def write_preamble(code_sink, min_python_version=None):
123123
#endif
124124
125125
#ifdef Py_LIMITED_API
126-
# define PBG_SETATTR(_type, _name, _value) PyObject_SetAttrString((PyObject*) _type, (char *) _name, _value);
126+
# define PBG_SETATTR(_type, _name, _value) PyObject_SetAttrString((PyObject*) _type, (char *) _name, (PyObject*) _value);
127127
#else
128-
# define PBG_SETATTR(_type, _name, _value) PyDict_SetItemString((PyObject*) _type.tp_dict, _name, _value);
128+
# define PBG_SETATTR(_type, _name, _value) PyDict_SetItemString((PyObject*) _type.tp_dict, _name, (PyObject*) _value);
129129
#endif
130130
131131
#if PY_VERSION_HEX >= 0x03000000

0 commit comments

Comments
 (0)