Skip to content

Commit

Permalink
Base: [skip ci] remove Py2 code from PyObjectBase
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Apr 12, 2021
1 parent 733cbce commit a257498
Showing 1 changed file with 6 additions and 42 deletions.
48 changes: 6 additions & 42 deletions src/Base/PyObjectBase.cpp
Expand Up @@ -103,11 +103,7 @@ PyTypeObject PyObjectBase::Type = {
/* --- Functions to access object as input/output buffer ---------*/
0, /* tp_as_buffer */
/* --- Flags to define presence of optional/expanded features */
#if PY_MAJOR_VERSION >= 3
Py_TPFLAGS_BASETYPE|Py_TPFLAGS_DEFAULT, /*tp_flags */
#else
Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_CLASS, /*tp_flags */
#endif
"The most base class for Python binding", /*tp_doc */
0, /*tp_traverse */
0, /*tp_clear */
Expand All @@ -134,10 +130,8 @@ PyTypeObject PyObjectBase::Type = {
0, /*tp_subclasses */
0, /*tp_weaklist */
0, /*tp_del */
0 /*tp_version_tag */
#if PY_MAJOR_VERSION >= 3
,0 /*tp_finalize */
#endif
0, /*tp_version_tag */
0 /*tp_finalize */
#if PY_VERSION_HEX >= 0x03090000
,0 /*tp_vectorcall */
#elif PY_VERSION_HEX >= 0x03080000
Expand All @@ -161,11 +155,7 @@ PyMethodDef PyObjectBase::Methods[] = {
PyObject* PyObjectBase::__getattro(PyObject * obj, PyObject *attro)
{
const char *attr;
#if PY_MAJOR_VERSION >= 3
attr = PyUnicode_AsUTF8(attro);
#else
attr = PyString_AsString(attro);
#endif

// For the __class__ attribute get it directly as with
// ExtensionContainerPy::getCustomAttributes we may get
Expand Down Expand Up @@ -204,7 +194,9 @@ PyObject* PyObjectBase::__getattro(PyObject * obj, PyObject *attro)
pyObj->trackAttribute(attr, value);
}
}
else if (value && PyCFunction_Check(value)) {
else
#endif
if (value && PyCFunction_Check(value)) {
// ExtensionContainerPy::initialization() transfers the methods of an
// extension object by creating PyCFunction objects.
// At this point no 'self' object is passed but is handled and determined
Expand All @@ -220,18 +212,14 @@ PyObject* PyObjectBase::__getattro(PyObject * obj, PyObject *attro)
PyErr_Format(PyExc_AttributeError, "<no object bound to built-in method %s>", attr);
}
}
#endif

return value;
}

int PyObjectBase::__setattro(PyObject *obj, PyObject *attro, PyObject *value)
{
const char *attr;
#if PY_MAJOR_VERSION >= 3
attr = PyUnicode_AsUTF8(attro);
#else
attr = PyString_AsString(attro);
#endif

//FIXME: In general we don't allow to delete attributes (i.e. value=0). However, if we want to allow
//we must check then in _setattr() of all subclasses whether value is 0.
Expand Down Expand Up @@ -294,11 +282,7 @@ PyObject *PyObjectBase::_getattr(const char *attr)
else {
// As fallback solution use Python's default method to get generic attributes
PyObject *w, *res;
#if PY_MAJOR_VERSION >= 3
w = PyUnicode_InternFromString(attr);
#else
w = PyString_InternFromString(attr);
#endif
if (w != NULL) {
res = PyObject_GenericGetAttr(this, w);
Py_XDECREF(w);
Expand All @@ -318,11 +302,7 @@ int PyObjectBase::_setattr(const char *attr, PyObject *value)
return -1; // filter out softspace
PyObject *w;
// As fallback solution use Python's default method to get generic attributes
#if PY_MAJOR_VERSION >= 3
w = PyUnicode_InternFromString(attr); // new reference
#else
w = PyString_InternFromString(attr); // new reference
#endif
if (w != NULL) {
// call methods from tp_getset if defined
int res = PyObject_GenericSetAttr(this, w, value);
Expand Down Expand Up @@ -354,13 +334,8 @@ void PyObjectBase::resetAttribute()
if (attrDict) {
// This is the attribute name to the parent structure
// which we search for in the dict
#if PY_MAJOR_VERSION < 3
PyObject* key1 = PyString_FromString("__attribute_of_parent__");
PyObject* key2 = PyString_FromString("__instance_of_parent__");
#else
PyObject* key1 = PyBytes_FromString("__attribute_of_parent__");
PyObject* key2 = PyBytes_FromString("__instance_of_parent__");
#endif
PyObject* attr = PyDict_GetItem(attrDict, key1);
PyObject* inst = PyDict_GetItem(attrDict, key2);
if (attr) {
Expand All @@ -379,15 +354,9 @@ void PyObjectBase::setAttributeOf(const char* attr, PyObject* par)
if (!attrDict) {
attrDict = PyDict_New();
}
#if PY_MAJOR_VERSION < 3
PyObject* key1 = PyString_FromString("__attribute_of_parent__");
PyObject* key2 = PyString_FromString("__instance_of_parent__");
PyObject* attro = PyString_FromString(attr);
#else
PyObject* key1 = PyBytes_FromString("__attribute_of_parent__");
PyObject* key2 = PyBytes_FromString("__instance_of_parent__");
PyObject* attro = PyUnicode_FromString(attr);
#endif
PyDict_SetItem(attrDict, key1, attro);
PyDict_SetItem(attrDict, key2, par);
Py_DECREF(attro);
Expand All @@ -403,13 +372,8 @@ void PyObjectBase::startNotify()
if (attrDict) {
// This is the attribute name to the parent structure
// which we search for in the dict
#if PY_MAJOR_VERSION < 3
PyObject* key1 = PyString_FromString("__attribute_of_parent__");
PyObject* key2 = PyString_FromString("__instance_of_parent__");
#else
PyObject* key1 = PyBytes_FromString("__attribute_of_parent__");
PyObject* key2 = PyBytes_FromString("__instance_of_parent__");
#endif
PyObject* attr = PyDict_GetItem(attrDict, key1);
PyObject* parent = PyDict_GetItem(attrDict, key2);
if (attr && parent) {
Expand Down

0 comments on commit a257498

Please sign in to comment.