diff --git a/src/PythonQt.cpp b/src/PythonQt.cpp index cfaad5cdd..57e1905ae 100644 --- a/src/PythonQt.cpp +++ b/src/PythonQt.cpp @@ -101,7 +101,6 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName) _self->_p->_pySourcelessFileLoader = importlib.getVariable("SourcelessFileLoader"); } -#ifdef PY3K // Import asyncio only when not explicitly disabled. // Importing asyncio on Py3.12+ pulls in ssl/_ssl; some environments/tests // want to avoid that during early embedded init. @@ -114,7 +113,6 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName) _self->_p->_pyFutureClass = asyncio.getVariable("Future"); } } -#endif PythonQt::priv()->setupSharedLibrarySuffixes(); @@ -348,11 +346,7 @@ PythonQt::PythonQt(int flags, const QByteArray& pythonQtModuleName) _p->_initFlags = flags; if ((flags & PythonAlreadyInitialized) == 0) { -#ifdef PY3K Py_SetProgramName(const_cast(L"PythonQt")); -#else - Py_SetProgramName(const_cast("PythonQt")); -#endif if (flags & IgnoreSiteModule) { // this prevents the automatic importing of Python site files Py_NoSiteFlag = 1; @@ -443,7 +437,6 @@ void PythonQtPrivate::setTaskDoneCallback(const PythonQtObjectPtr & callable) PythonQtObjectPtr PythonQtPrivate::checkAndRunCoroutine(const PythonQtObjectPtr& object) { PythonQtObjectPtr result; -#ifdef PY3K if (!PyCoro_CheckExact(object)) { return result; @@ -466,9 +459,6 @@ PythonQtObjectPtr PythonQtPrivate::checkAndRunCoroutine(const PythonQtObjectPtr& Py_XDECREF(methodName); } Py_XDECREF(args); -#else - Q_UNUSED(object) -#endif return result; } @@ -1000,11 +990,7 @@ QVariant PythonQt::evalCode(PyObject* object, PyObject* pycode) { } PyObject* r = nullptr; if (dict) { -#ifdef PY3K r = PyEval_EvalCode(pycode, globals, dict); -#else - r = PyEval_EvalCode((PyCodeObject*)pycode, globals, dict); -#endif } if (r) { result = PythonQtConv::PyObjToQVariant(r); @@ -1264,14 +1250,7 @@ QStringList PythonQt::introspectObject(PyObject* object, ObjectType type) keys = PyDict_Keys(object); isDict = true; } else { -#if defined(MEVISLAB) && !defined(PY3K) - int oldPy3kWarningFlag = Py_Py3kWarningFlag; - Py_Py3kWarningFlag = 0; // temporarily disable Python 3 warnings keys = PyObject_Dir(object); - Py_Py3kWarningFlag = oldPy3kWarningFlag; -#else - keys = PyObject_Dir(object); -#endif } if (keys) { int count = PyList_Size(keys); @@ -1307,9 +1286,6 @@ QStringList PythonQt::introspectObject(PyObject* object, ObjectType type) && value->ob_type != &PyModule_Type && value->ob_type != &PyType_Type && value->ob_type != &PythonQtSlotFunction_Type -#ifndef PY3K - && value->ob_type != &PyClass_Type -#endif ) { results << keystr; } @@ -1662,14 +1638,9 @@ int custom_system_exit_exception_handler() // return exitcode; PyErr_Fetch(&exception, &value, &tb); -#ifndef PY3K - if (Py_FlushLine()) { - PyErr_Clear(); - } -#else - // TODO: unclear what to do, since Py_FlushLine is gone... -#endif + fflush(stdout); + if (value == nullptr || value == Py_None) goto done; if (PyExceptionInstance_Check(value)) { @@ -1846,7 +1817,6 @@ static PyMethodDef PythonQtMethods[] = { {nullptr, nullptr, 0, nullptr} }; -#ifdef PY3K static PyModuleDef PythonQtModuleDef = { PyModuleDef_HEAD_INIT, "", @@ -1858,7 +1828,6 @@ static PyModuleDef PythonQtModuleDef = { nullptr, nullptr }; -#endif void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQtModuleName) { @@ -1866,12 +1835,8 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ if (!pythonQtModuleName.isEmpty()) { name = pythonQtModuleName; } -#ifdef PY3K PythonQtModuleDef.m_name = name.constData(); _p->_pythonQtModule = PyModule_Create(&PythonQtModuleDef); -#else - _p->_pythonQtModule = Py_InitModule(name.constData(), PythonQtMethods); -#endif _p->_pythonQtModuleName = name; Py_INCREF((PyObject*)&PythonQtBoolResult_Type); @@ -1911,13 +1876,11 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ } Py_XDECREF(old_module_names); -#ifdef PY3K PyObject* modulesAttr = PyObject_GetAttrString(sys.object(), "modules"); PyObject* pyUnicodeObject = PyUnicode_FromString(name.constData()); PyDict_SetItem(modulesAttr, pyUnicodeObject, _p->_pythonQtModule.object()); Py_XDECREF(modulesAttr); Py_XDECREF(pyUnicodeObject); -#endif } bool PythonQt::redirectStdOutCallbackEnabled() const @@ -2287,9 +2250,6 @@ bool PythonQtPrivate::isMethodDescriptor(PyObject* object) const !PyObject_HasAttrString(object, "__set__") && !PyMethod_Check(object) && !PyFunction_Check(object) -#ifndef PY3K - && !PyClass_Check(object) -#endif ) { return true; } @@ -2651,20 +2611,12 @@ void PythonQtPrivate::shellClassDeleted( void* shellClass ) PyObject* PythonQtPrivate::wrapMemoryAsBuffer( const void* data, Py_ssize_t size ) { -#ifdef PY3K return PyMemoryView_FromMemory((char*)data, size, PyBUF_READ); -#else - return PyBuffer_FromMemory((char*)data, size); -#endif } PyObject* PythonQtPrivate::wrapMemoryAsBuffer( void* data, Py_ssize_t size ) { -#ifdef PY3K return PyMemoryView_FromMemory((char*)data, size, PyBUF_WRITE); -#else - return PyBuffer_FromReadWriteMemory((char*)data, size); -#endif } PythonQtClassInfo* PythonQtPrivate::getClassInfo( const QMetaObject* meta ) diff --git a/src/PythonQt.h b/src/PythonQt.h index 155299e3d..432e6c980 100644 --- a/src/PythonQt.h +++ b/src/PythonQt.h @@ -147,15 +147,9 @@ typedef QObject* PythonQtQObjectCreatorFunctionCB(); template QObject* PythonQtCreateObject() { return new T(); } //! Helper define to convert from QString to Python C-API -#ifdef PY3K #define QStringToPythonConstCharPointer(arg) ((arg).toUtf8().constData()) #define QStringToPythonCharPointer(arg) ((arg).toUtf8().data()) #define QStringToPythonEncoding(arg) ((arg).toUtf8()) -#else -#define QStringToPythonConstCharPointer(arg) ((arg).toLatin1().constData()) -#define QStringToPythonCharPointer(arg) ((arg).toLatin1().data()) -#define QStringToPythonEncoding(arg) ((arg).toLatin1()) -#endif //! The main interface to the Python Qt binding, realized as a singleton /*! @@ -368,7 +362,6 @@ class PYTHONQT_EXPORT PythonQt : public QObject { //! Parses the given file and returns the python code object, this can then be used to call evalCode() //! It uses Python's importlib machinery to load the file's code and supports source and sourceless loading //! and generation of cache files. - //! This method is PY3K only! PythonQtObjectPtr parseFileWithPythonLoaders(const QString& filename); //! evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string) diff --git a/src/PythonQtBoolResult.cpp b/src/PythonQtBoolResult.cpp index 57c450576..08b441290 100644 --- a/src/PythonQtBoolResult.cpp +++ b/src/PythonQtBoolResult.cpp @@ -58,7 +58,7 @@ static PyObject *PythonQtBoolResult_repr(PythonQtBoolResultObject *obj) return PyString_FromString(wrapper->_value?"BoolResult(True)":"BoolResult(False)"); } -static int PythonQtBoolResult_nonzero(PyObject *obj) +static int PythonQtBoolResult_bool(PyObject *obj) { PythonQtBoolResultObject* wrapper = (PythonQtBoolResultObject*)obj; return wrapper->_value; @@ -69,38 +69,25 @@ static PyNumberMethods PythonQtBoolResult_as_number = { nullptr, /* nb_add */ nullptr, /* nb_subtract */ nullptr, /* nb_multiply */ -#ifndef PY3K - nullptr, /* nb_divide */ -#endif nullptr, /* nb_remainder */ nullptr, /* nb_divmod */ nullptr, /* nb_power */ nullptr, /* nb_negative */ nullptr, /* nb_positive */ nullptr, /* nb_absolute */ - PythonQtBoolResult_nonzero, /* nb_nonzero / nb_bool in Py3K */ + PythonQtBoolResult_bool, /* nb_bool */ nullptr, /* nb_invert */ nullptr, /* nb_lshift */ nullptr, /* nb_rshift */ nullptr, /* nb_and */ nullptr, /* nb_xor */ nullptr, /* nb_or */ -#ifndef PY3K - nullptr, /* nb_coerce */ -#endif nullptr, /* nb_int */ - nullptr, /* nb_long / nb_reserved in Py3K */ + nullptr, /* nb_reserved */ nullptr, /* nb_float */ -#ifndef PY3K - nullptr, /* nb_oct */ - nullptr, /* nb_hex */ -#endif nullptr, /* nb_inplace_add */ nullptr, /* nb_inplace_subtract */ nullptr, /* nb_inplace_multiply */ -#ifndef PY3K - nullptr, /* nb_inplace_divide */ -#endif nullptr, /* nb_inplace_remainder */ nullptr, /* nb_inplace_power */ nullptr, /* nb_inplace_lshift */ @@ -112,9 +99,7 @@ static PyNumberMethods PythonQtBoolResult_as_number = { nullptr, /* nb_true_divide */ nullptr, /* nb_inplace_floor_divide */ nullptr, /* nb_inplace_true_divide */ -#ifdef PY3K - nullptr, /* nb_index in Py3K */ -#endif + nullptr, /* nb_index */ }; PyTypeObject PythonQtBoolResult_Type = { @@ -155,4 +140,3 @@ PyTypeObject PythonQtBoolResult_Type = { 0, /* tp_dictoffset */ (initproc)&PythonQtBoolResult_init, /* tp_init */ }; - diff --git a/src/PythonQtClassWrapper.cpp b/src/PythonQtClassWrapper.cpp index b64fea24f..f66df0c96 100644 --- a/src/PythonQtClassWrapper.cpp +++ b/src/PythonQtClassWrapper.cpp @@ -260,9 +260,6 @@ static void initializeSlots(PythonQtClassWrapper* wrap) wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_mul; } if (typeSlots & PythonQt::Type_Divide) { -#ifndef PY3K - wrap->_base.as_number.nb_divide = (binaryfunc)PythonQtInstanceWrapper_div; -#endif wrap->_base.as_number.nb_true_divide = (binaryfunc)PythonQtInstanceWrapper_div; } if (typeSlots & PythonQt::Type_And) { @@ -294,9 +291,6 @@ static void initializeSlots(PythonQtClassWrapper* wrap) wrap->_base.as_number.nb_inplace_multiply = (binaryfunc)PythonQtInstanceWrapper_imul; } if (typeSlots & PythonQt::Type_InplaceDivide) { -#ifndef PY3K - wrap->_base.as_number.nb_inplace_divide = (binaryfunc)PythonQtInstanceWrapper_idiv; -#endif wrap->_base.as_number.nb_inplace_true_divide = (binaryfunc)PythonQtInstanceWrapper_idiv; } if (typeSlots & PythonQt::Type_InplaceAnd) { @@ -321,11 +315,7 @@ static void initializeSlots(PythonQtClassWrapper* wrap) wrap->_base.as_number.nb_invert = (unaryfunc)PythonQtInstanceWrapper_invert; } if (typeSlots & PythonQt::Type_NonZero) { -#ifdef PY3K wrap->_base.as_number.nb_bool = (inquiry)PythonQtInstanceWrapper_nonzero; -#else - wrap->_base.as_number.nb_nonzero = (inquiry)PythonQtInstanceWrapper_nonzero; -#endif } } } @@ -563,11 +553,7 @@ static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name) } // look for the internal methods (className(), help()) -#ifdef PY3K PyObject* internalMethod = PyObject_GenericGetAttr(obj, name); -#else - PyObject* internalMethod = Py_FindMethod( PythonQtClassWrapper_methods, obj, (char*)attributeName); -#endif if (internalMethod) { return internalMethod; } @@ -634,11 +620,7 @@ PyTypeObject PythonQtClassWrapper_Type = { 0, /* tp_weaklistoffset */ nullptr, /* tp_iter */ nullptr, /* tp_iternext */ - #ifdef PY3K PythonQtClassWrapper_methods, /* tp_methods */ - #else - nullptr, /* tp_methods */ - #endif nullptr, /* tp_members */ nullptr, /* tp_getset */ nullptr, /* tp_base */ @@ -653,4 +635,3 @@ PyTypeObject PythonQtClassWrapper_Type = { }; //------------------------------------------------------- - diff --git a/src/PythonQtConversion.cpp b/src/PythonQtConversion.cpp index 5e8cc5bb0..d92eeddaf 100644 --- a/src/PythonQtConversion.cpp +++ b/src/PythonQtConversion.cpp @@ -804,11 +804,7 @@ QString PythonQtConv::PyObjGetRepresentation(PyObject* val) QString r; PyObject* str = PyObject_Repr(val); if (str) { - #ifdef PY3K r = PyObjGetString(str); - #else - r = QString(PyString_AS_STRING(str)); - #endif Py_DECREF(str); } return r; @@ -820,31 +816,12 @@ QString PythonQtConv::PyObjGetString(PyObject* val, bool strict, bool& ok) { if (val == nullptr) { r = "None"; } else -#ifndef PY3K - // in Python 3, we don't want to convert to QString, since we don't know anything about the encoding - // in Python 2, we assume the default for str is latin-1 - if (val->ob_type == &PyBytes_Type) { - r = QString::fromLatin1(PyBytes_AS_STRING(val)); - } else -#endif if (PyUnicode_Check(val)) { -#ifdef PY3K r = QString::fromUtf8(PyUnicode_AsUTF8(val)); -#else - PyObject *ptmp = PyUnicode_AsUTF8String(val); - if(ptmp) { - r = QString::fromUtf8(PyString_AS_STRING(ptmp)); - Py_DECREF(ptmp); - } -#endif } else if (!strict) { PyObject* str = PyObject_Str(val); if (str) { -#ifdef PY3K r = QString::fromUtf8(PyUnicode_AsUTF8(str)); -#else - r = QString(PyString_AS_STRING(str)); -#endif Py_DECREF(str); } else { ok = false; @@ -942,11 +919,6 @@ int PythonQtConv::PyObjGetInt(PyObject* val, bool strict, bool &ok) { qint64 PythonQtConv::PyObjGetLongLong(PyObject* val, bool strict, bool &ok) { qint64 d = 0; ok = true; -#ifndef PY3K - if (val->ob_type == &PyInt_Type) { - d = PyInt_AS_LONG(val); - } else -#endif if (val->ob_type == &PyLong_Type) { d = PyLong_AsLongLong(val); } else if (!strict) { @@ -977,11 +949,6 @@ qint64 PythonQtConv::PyObjGetLongLong(PyObject* val, bool strict, bool &ok) { quint64 PythonQtConv::PyObjGetULongLong(PyObject* val, bool strict, bool &ok) { quint64 d = 0; ok = true; -#ifndef PY3K - if (Py_TYPE(val) == &PyInt_Type) { - d = PyInt_AS_LONG(val); - } else -#endif if (Py_TYPE(val) == &PyLong_Type) { d = PyLong_AsUnsignedLongLong(val); } else if (!strict) { @@ -1015,11 +982,6 @@ double PythonQtConv::PyObjGetDouble(PyObject* val, bool strict, bool &ok) { if (val->ob_type == &PyFloat_Type) { d = PyFloat_AS_DOUBLE(val); } else if (!strict) { -#ifndef PY3K - if (PyObject_TypeCheck(val, &PyInt_Type)) { - d = PyInt_AS_LONG(val); - } else -#endif if (PyLong_Check(val)) { d = static_cast(PyLong_AsLongLong(val)); } else if (val == Py_False) { @@ -1090,21 +1052,11 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type) if (val == nullptr) { type = QMetaType::UnknownType; // Equivalent to QVariant::Invalid or unregistered type } else if (PyBytes_Check(val)) { -#ifdef PY3K - // In Python 3, it is a ByteArray type = QMetaType::QByteArray; -#else - // In Python 2, we need to use String, since it might be a string - type = QMetaType::QString; -#endif } else if (PyUnicode_Check(val)) { type = QMetaType::QString; } else if (val == Py_False || val == Py_True) { type = QMetaType::Bool; -#ifndef PY3K - } else if (PyObject_TypeCheck(val, &PyInt_Type)) { - type = QMetaType::Int; -#endif } else if (PyLong_Check(val)) { // return int if the value fits into that range, // otherwise it would not be possible to get an int from Python 3 @@ -1236,11 +1188,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type) case QMetaType::QByteArray: { bool ok; -#ifdef PY3K v = QVariant(PyObjGetBytes(val, false, ok)); -#else - v = QVariant(PyObjGetString(val, false, ok)); -#endif } break; case QMetaType::QString: @@ -1634,10 +1582,6 @@ QByteArray PythonQtConv::getCPPTypeName(PyObject* type) result = "double"; } else if (typeObject == &PyBool_Type) { result = "bool"; -#ifndef PY3K - } else if (typeObject == &PyInt_Type) { - result = "qint32"; -#endif } else if (typeObject == &PyLong_Type) { result = "qint64"; } else if (isStringType(typeObject)) { @@ -1657,11 +1601,7 @@ QByteArray PythonQtConv::getCPPTypeName(PyObject* type) bool PythonQtConv::isStringType(PyTypeObject* type) { -#ifdef PY3K return type == &PyUnicode_Type; -#else - return type == &PyUnicode_Type || type == &PyString_Type; -#endif } void PythonQtConv::registerStringViewTypes() diff --git a/src/PythonQtImporter.cpp b/src/PythonQtImporter.cpp index aef4b1dfb..beb414c88 100644 --- a/src/PythonQtImporter.cpp +++ b/src/PythonQtImporter.cpp @@ -284,7 +284,6 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args) } // set __package__ only for Python 3, because in Python 2 it causes the exception "__package__ set to non-string" -#ifdef PY3K // The package attribute is needed to resolve the package name if it is referenced as '.'. For example, // when importing the encodings package, there is an import statement 'from . import aliases'. This import // would fail when reloading the encodings package with importlib. @@ -294,10 +293,8 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args) Py_DECREF(mod); return nullptr; } -#endif } -#ifdef PY3K PyObject* fullnameObj = PyUnicode_FromString(fullname); PyObject* fullPathObj = PythonQtConv::QStringToPyObject(fullPath); PyObject* fullCachePathObj = !fullCachePath.isEmpty() ? PythonQtConv::QStringToPyObject(fullCachePath) : nullptr; @@ -305,9 +302,7 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args) Py_XDECREF(fullnameObj); Py_XDECREF(fullPathObj); Py_XDECREF(fullCachePathObj); -#else - mod = PyImport_ExecCodeModuleEx(fullname, code, fullPath.toLatin1().data()); -#endif + if (PythonQt::importInterface()) { PythonQt::importInterface()->importedModule(fullname); } @@ -565,27 +560,11 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena "# can't create %s\n", QStringToPythonConstCharPointer(filename)); return; } -#if PY_VERSION_HEX < 0x02040000 - PyMarshal_WriteLongToFile(PyImport_GetMagicNumber(), fp); -#else PyMarshal_WriteLongToFile(PyImport_GetMagicNumber(), fp, Py_MARSHAL_VERSION); -#endif /* First write a 0 for mtime */ -#if PY_VERSION_HEX < 0x02040000 - PyMarshal_WriteLongToFile(0L, fp); -#else PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION); -#endif -#ifdef PY3K PyMarshal_WriteLongToFile(sourceSize, fp, Py_MARSHAL_VERSION); -#else - Q_UNUSED(sourceSize) -#endif -#if PY_VERSION_HEX < 0x02040000 - PyMarshal_WriteObjectToFile((PyObject *)co, fp); -#else PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION); -#endif if (ferror(fp)) { if (Py_VerboseFlag) PySys_WriteStderr("# can't write %s\n", QStringToPythonConstCharPointer(filename)); @@ -596,11 +575,7 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena } /* Now write the true mtime */ fseek(fp, 4L, 0); -#if PY_VERSION_HEX < 0x02040000 - PyMarshal_WriteLongToFile(mtime, fp); -#else PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION); -#endif fflush(fp); fclose(fp); if (Py_VerboseFlag) { @@ -645,15 +620,11 @@ PythonQtImport::unmarshalCode(const QString& path, const QByteArray& data, time_ } } -#ifdef PY3K // Python 3 also stores the size of the *.py file, but we ignore it for now int sourceSize = getLong((unsigned char *)buf + 8); Q_UNUSED(sourceSize) // read the module code = PyMarshal_ReadObjectFromString(buf + 12, size - 12); -#else - code = PyMarshal_ReadObjectFromString(buf + 8, size - 8); -#endif if (code == nullptr) return nullptr; if (!PyCode_Check(code)) { @@ -673,15 +644,10 @@ PyObject * PythonQtImport::compileSource(const QString& path, const QByteArray& data) { PyObject *code; -#ifdef PY3K PyObject* filename = PythonQtConv::QStringToPyObject(path); code = Py_CompileStringObject(data.data(), filename, Py_file_input, nullptr, -1); Py_DECREF(filename); -#else - code = Py_CompileString(data.data(), QStringToPythonConstCharPointer(path), - Py_file_input); -#endif return code; } @@ -808,14 +774,10 @@ PythonQtImport::getModuleCode(PythonQtImporter *self, const char* fullname, QStr } if (code != nullptr) { modpath = test; -#ifdef PY3K if (isbytecode) { cachemodpath = modpath; modpath = getSourceFilename(modpath); } -#else - Q_UNUSED(cachemodpath) -#endif } return code; } @@ -869,7 +831,6 @@ PyObject* PythonQtImport::getCodeFromPyc(const QString& file) PyDoc_STRVAR(mlabimport_doc, "Imports python files into PythonQt, completely replaces internal python import"); -#ifdef PY3K static struct PyModuleDef PythonQtImport_def = { PyModuleDef_HEAD_INIT, "PythonQtImport", /* m_name */ @@ -881,7 +842,6 @@ static struct PyModuleDef PythonQtImport_def = { nullptr, /* m_clear */ nullptr /* m_free */ }; -#endif void PythonQtImport::init() { @@ -911,12 +871,7 @@ void PythonQtImport::init() mlab_searchorder[4] = tmp; } -#ifdef PY3K mod = PyModule_Create(&PythonQtImport_def); -#else - mod = Py_InitModule4("PythonQtImport", NULL, mlabimport_doc, - NULL, PYTHON_API_VERSION); -#endif PythonQtImportError = PyErr_NewException(const_cast("PythonQtImport.PythonQtImportError"), PyExc_ImportError, nullptr); diff --git a/src/PythonQtInstanceWrapper.cpp b/src/PythonQtInstanceWrapper.cpp index 41a3e5560..7708be6b9 100644 --- a/src/PythonQtInstanceWrapper.cpp +++ b/src/PythonQtInstanceWrapper.cpp @@ -595,11 +595,7 @@ static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name) } // look for the internal methods (className(), help()) -#ifdef PY3K PyObject* internalMethod = PyObject_GenericGetAttr(obj, name); -#else - PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName); -#endif if (internalMethod) { return internalMethod; } @@ -791,7 +787,6 @@ static PyObject * PythonQtInstanceWrapper_str(PyObject * obj) // QByteArray should be directly returned as a str if (wrapper->classInfo()->metaTypeId() == QMetaType::QByteArray) { QByteArray* b = (QByteArray*) wrapper->_wrappedPtr; -#ifdef PY3K // Note: In Python 2, this was used to access the data() of a byte array. // Since in Python 3 str() will return a unicode, this is no longer possible. // The user needs to call .data() to get access to the data as bytes. @@ -803,13 +798,6 @@ static PyObject * PythonQtInstanceWrapper_str(PyObject * obj) } else { return PyUnicode_New(0, 0); } -#else - if (b->data()) { - return PyString_FromStringAndSize(b->data(), b->size()); - } else { - return PyString_FromString(""); - } -#endif } const char* typeName = obj->ob_type->tp_name; @@ -857,7 +845,7 @@ static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj) } } -static int PythonQtInstanceWrapper_builtin_nonzero(PyObject *obj) +static int PythonQtInstanceWrapper_builtin_bool(PyObject *obj) { PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj; return (wrapper->_wrappedPtr == nullptr && wrapper->_obj == nullptr)?0:1; @@ -881,38 +869,25 @@ static PyNumberMethods PythonQtInstanceWrapper_as_number = { nullptr, /* nb_add */ nullptr, /* nb_subtract */ nullptr, /* nb_multiply */ -#ifndef PY3K - nullptr, /* nb_divide */ -#endif nullptr, /* nb_remainder */ nullptr, /* nb_divmod */ nullptr, /* nb_power */ nullptr, /* nb_negative */ nullptr, /* nb_positive */ nullptr, /* nb_absolute */ - PythonQtInstanceWrapper_builtin_nonzero, /* nb_nonzero / nb_bool in Py3K */ + PythonQtInstanceWrapper_builtin_bool, /* nb_bool */ nullptr, /* nb_invert */ nullptr, /* nb_lshift */ nullptr, /* nb_rshift */ nullptr, /* nb_and */ nullptr, /* nb_xor */ nullptr, /* nb_or */ -#ifndef PY3K - nullptr, /* nb_coerce */ -#endif nullptr, /* nb_int */ - nullptr, /* nb_long / nb_reserved in Py3K */ + nullptr, /* nb_reserved */ nullptr, /* nb_float */ -#ifndef PY3K - nullptr, /* nb_oct */ - nullptr, /* nb_hex */ -#endif nullptr, /* nb_inplace_add */ nullptr, /* nb_inplace_subtract */ nullptr, /* nb_inplace_multiply */ -#ifndef PY3K - nullptr, /* nb_inplace_divide */ -#endif nullptr, /* nb_inplace_remainder */ nullptr, /* nb_inplace_power */ nullptr, /* nb_inplace_lshift */ @@ -924,9 +899,7 @@ static PyNumberMethods PythonQtInstanceWrapper_as_number = { nullptr, /* nb_true_divide */ nullptr, /* nb_inplace_floor_divide */ nullptr, /* nb_inplace_true_divide */ -#ifdef PY3K - nullptr, /* nb_index in Py3K */ -#endif + nullptr, /* nb_index */ }; PyTypeObject PythonQtInstanceWrapper_Type = { @@ -949,11 +922,7 @@ PyTypeObject PythonQtInstanceWrapper_Type = { PythonQtInstanceWrapper_getattro, /*tp_getattro*/ PythonQtInstanceWrapper_setattro, /*tp_setattro*/ nullptr, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE -#ifndef PY3K - | Py_TPFLAGS_CHECKTYPES -#endif - , /*tp_flags*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ "PythonQtInstanceWrapper object", /* tp_doc */ nullptr, /* tp_traverse */ nullptr, /* tp_clear */ @@ -961,11 +930,7 @@ PyTypeObject PythonQtInstanceWrapper_Type = { 0, /* tp_weaklistoffset */ nullptr, /* tp_iter */ nullptr, /* tp_iternext */ -#ifdef PY3K - PythonQtInstanceWrapper_methods, -#else - 0, /* tp_methods */ -#endif + PythonQtInstanceWrapper_methods, /* tp_methods */ nullptr, /* tp_members */ nullptr, /* tp_getset */ nullptr, /* tp_base */ diff --git a/src/PythonQtSignal.cpp b/src/PythonQtSignal.cpp index eafacca50..5dd51cc44 100644 --- a/src/PythonQtSignal.cpp +++ b/src/PythonQtSignal.cpp @@ -150,13 +150,6 @@ static PyObject * meth_get__self__(PythonQtSignalFunctionObject *m, void * /*closure*/) { PyObject *self; -#ifndef PY3K - if (PyEval_GetRestricted()) { - PyErr_SetString(PyExc_RuntimeError, - "method.__self__ not accessible in restricted mode"); - return NULL; - } -#endif self = m->m_self; if (self == nullptr) self = Py_None; @@ -409,11 +402,7 @@ PyTypeObject PythonQtSignalFunction_Type = { 0, /* tp_vectorcall_offset */ nullptr, /* tp_getattr */ nullptr, /* tp_setattr */ -#ifdef PY3K nullptr, -#else - (cmpfunc)meth_compare, /* tp_compare */ -#endif (reprfunc)meth_repr, /* tp_repr */ nullptr, /* tp_as_number */ nullptr, /* tp_as_sequence */ diff --git a/src/PythonQtSlot.cpp b/src/PythonQtSlot.cpp index 29c8ae4e8..8dfcbcfd7 100644 --- a/src/PythonQtSlot.cpp +++ b/src/PythonQtSlot.cpp @@ -609,13 +609,6 @@ static PyObject * meth_get__self__(PythonQtSlotFunctionObject *m, void * /*closure*/) { PyObject *self; -#ifndef PY3K - if (PyEval_GetRestricted()) { - PyErr_SetString(PyExc_RuntimeError, - "method.__self__ not accessible in restricted mode"); - return nullptr; - } -#endif self = m->m_self; if (self == nullptr) self = Py_None; @@ -828,11 +821,7 @@ PyTypeObject PythonQtSlotFunction_Type = { 0, /* tp_vectorcall_offset */ nullptr, /* tp_getattr */ nullptr, /* tp_setattr */ -#ifdef PY3K nullptr, -#else - (cmpfunc)meth_compare, /* tp_compare */ -#endif (reprfunc)meth_repr, /* tp_repr */ nullptr, /* tp_as_number */ nullptr, /* tp_as_sequence */ diff --git a/src/PythonQtStdOut.cpp b/src/PythonQtStdOut.cpp index 685a61a3f..4d332a0c8 100644 --- a/src/PythonQtStdOut.cpp +++ b/src/PythonQtStdOut.cpp @@ -61,27 +61,13 @@ static PyObject *PythonQtStdOutRedirect_write(PyObject *self, PyObject *args) if (PyTuple_GET_SIZE(args)>=1) { PyObject* obj = PyTuple_GET_ITEM(args,0); if (PyUnicode_Check(obj)) { -#ifdef PY3K output = QString::fromUtf8(PyUnicode_AsUTF8(obj)); -#else - PyObject *tmp = PyUnicode_AsUTF8String(obj); - if(tmp) { - output = QString::fromUtf8(PyString_AS_STRING(tmp)); - Py_DECREF(tmp); - } else { - return NULL; - } -#endif } else { char *string; if (!PyArg_ParseTuple(args, "s", &string)) { return nullptr; } -#ifdef PY3K output = QString::fromUtf8(string); -#else - output = QString::fromLatin1(string); -#endif } } diff --git a/src/PythonQtUtils.h b/src/PythonQtUtils.h index 4c908bd72..c342df2e6 100644 --- a/src/PythonQtUtils.h +++ b/src/PythonQtUtils.h @@ -85,12 +85,7 @@ namespace PythonQtUtils //! Returns of the python object is a class type inline bool isPythonClassType(PyObject* obj) { -#ifdef PY3K return PyType_Check(obj); -#else - // support old-style classes and new style classes - return (obj->ob_type == &PyClass_Type || obj->ob_type == &PyType_Type); -#endif } //! Returns the meta type ID from a type name