Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 2 additions & 50 deletions src/PythonQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -114,7 +113,6 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
_self->_p->_pyFutureClass = asyncio.getVariable("Future");
}
}
#endif

PythonQt::priv()->setupSharedLibrarySuffixes();

Expand Down Expand Up @@ -348,11 +346,7 @@ PythonQt::PythonQt(int flags, const QByteArray& pythonQtModuleName)
_p->_initFlags = flags;

if ((flags & PythonAlreadyInitialized) == 0) {
#ifdef PY3K
Py_SetProgramName(const_cast<wchar_t*>(L"PythonQt"));
#else
Py_SetProgramName(const_cast<char*>("PythonQt"));
#endif
if (flags & IgnoreSiteModule) {
// this prevents the automatic importing of Python site files
Py_NoSiteFlag = 1;
Expand Down Expand Up @@ -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;
Expand All @@ -466,9 +459,6 @@ PythonQtObjectPtr PythonQtPrivate::checkAndRunCoroutine(const PythonQtObjectPtr&
Py_XDECREF(methodName);
}
Py_XDECREF(args);
#else
Q_UNUSED(object)
#endif
return result;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -1846,7 +1817,6 @@ static PyMethodDef PythonQtMethods[] = {
{nullptr, nullptr, 0, nullptr}
};

#ifdef PY3K
static PyModuleDef PythonQtModuleDef = {
PyModuleDef_HEAD_INIT,
"",
Expand All @@ -1858,20 +1828,15 @@ static PyModuleDef PythonQtModuleDef = {
nullptr,
nullptr
};
#endif

void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQtModuleName)
{
QByteArray name = "PythonQt";
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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 )
Expand Down
7 changes: 0 additions & 7 deletions src/PythonQt.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,9 @@ typedef QObject* PythonQtQObjectCreatorFunctionCB();
template<class T> 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
/*!
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 4 additions & 20 deletions src/PythonQtBoolResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand All @@ -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 = {
Expand Down Expand Up @@ -155,4 +140,3 @@ PyTypeObject PythonQtBoolResult_Type = {
0, /* tp_dictoffset */
(initproc)&PythonQtBoolResult_init, /* tp_init */
};

19 changes: 0 additions & 19 deletions src/PythonQtClassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 */
Expand All @@ -653,4 +635,3 @@ PyTypeObject PythonQtClassWrapper_Type = {
};

//-------------------------------------------------------

Loading
Loading