Skip to content

Commit 610c577

Browse files
committed
STYLE: Anticipate PythonQt update migrating to Python 3 Unicode and Long APIs
Follow-up of 4513569 ("COMP: Remove Python 2.x support from PythonCore and PythonWidget libraries", 2023-12-09) changing the code to use Python 3's Unicode and Long APIs, in anticipation of an update to PythonQt that will remove the older APIs. Specifically, it replaces: * `PyString_Check with `PyUnicode_Check` * `PyString_AsString` with `PyUnicode_AsUTF8` * `PyString_FromString` with `PyUnicode_FromString` * `PyInt_AsLong` with `PyLong_AsLong` No functional change intended.
1 parent 833ae56 commit 610c577

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

Libs/Scripting/Python/Core/Testing/Cpp/ctkAbstractPythonManagerTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,9 @@ void ctkAbstractPythonManagerTester::testPythonModule()
423423
}
424424
else
425425
{
426-
returnedPyString = PyString_FromString("");
426+
returnedPyString = PyUnicode_FromString("");
427427
}
428-
QString returnedString = PyString_AsString(returnedPyString);
428+
QString returnedString = PyUnicode_AsUTF8(returnedPyString);
429429
QCOMPARE(returnedString, expectedReturnedString);
430430
}
431431

@@ -469,9 +469,9 @@ void ctkAbstractPythonManagerTester::testPythonObject()
469469
}
470470
else
471471
{
472-
returnedPyObjectString = PyString_FromString("");
472+
returnedPyObjectString = PyUnicode_FromString("");
473473
}
474-
QString returnedString = PyString_AsString(returnedPyObjectString);
474+
QString returnedString = PyUnicode_AsUTF8(returnedPyObjectString);
475475
QCOMPARE(returnedString, expectedReturnedString);
476476
}
477477

Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ QStringList ctkAbstractPythonManager::dir_object(PyObject* object,
370370
PyErr_Clear();
371371
continue;
372372
}
373-
QString key_str(PyString_AsString(key));
373+
QString key_str(PyUnicode_AsUTF8(key));
374374
// Append "()" if the associated object is a function
375375
if (appendParenthesis && PyCallable_Check(value))
376376
{
@@ -635,7 +635,7 @@ PyObject* ctkAbstractPythonManager::pythonObject(const QString& variableNameAndF
635635
{
636636
continue;
637637
}
638-
QString keyStr = PyString_AsString(key);
638+
QString keyStr = PyUnicode_AsUTF8(key);
639639
if (keyStr.operator ==(compareFunction.toLatin1()))
640640
{
641641
finalPythonObject = value;

Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ int ctkPythonConsoleCompleterPrivate::parameterCountBuiltInFunction(const QStrin
216216
if (PyObject_HasAttrString(pFunction, "__doc__"))
217217
{
218218
PyObject* pDoc = PyObject_GetAttrString(pFunction, "__doc__");
219-
if (PyString_Check(pDoc))
219+
if (PyUnicode_Check(pDoc))
220220
{
221-
QString docString = PyString_AsString(pDoc);
221+
QString docString = PyUnicode_AsUTF8(pDoc);
222222
QString argumentExtract = docString.mid(docString.indexOf("(")+1, docString.indexOf(")") - docString.indexOf("(")-1);
223223
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
224224
QStringList arguments = argumentExtract.split(",", Qt::SkipEmptyParts);
@@ -247,7 +247,7 @@ int ctkPythonConsoleCompleterPrivate::parameterCountUserDefinedFunction(const QS
247247
PyObject* ac = PyObject_GetAttrString(fc, "co_argcount");
248248
if (ac)
249249
{
250-
parameterCount = PyInt_AsLong(ac);
250+
parameterCount = PyLong_AsLong(ac);
251251
Py_DECREF(ac);
252252
}
253253
Py_DECREF(fc);
@@ -269,7 +269,7 @@ int ctkPythonConsoleCompleterPrivate::parameterCountUserDefinedClassFunction(con
269269
PyObject* ac = PyObject_GetAttrString(fc, "co_argcount");
270270
if (ac)
271271
{
272-
parameterCount = PyInt_AsLong(ac);
272+
parameterCount = PyLong_AsLong(ac);
273273
Py_DECREF(ac);
274274
}
275275
Py_DECREF(fc);
@@ -288,9 +288,9 @@ int ctkPythonConsoleCompleterPrivate::parameterCountFromDocumentation(const QStr
288288
if (PyObject_HasAttrString(pFunction, "__call__"))
289289
{
290290
PyObject* pDoc = PyObject_GetAttrString(pFunction, "__doc__");
291-
if (PyString_Check(pDoc))
291+
if (PyUnicode_Check(pDoc))
292292
{
293-
QString docString = PyString_AsString(pDoc);
293+
QString docString = PyUnicode_AsUTF8(pDoc);
294294
QString argumentExtract = docString.mid(docString.indexOf("(")+1, docString.indexOf(")") - docString.indexOf("(")-1);
295295
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
296296
QStringList arguments = argumentExtract.split(",", Qt::SkipEmptyParts);
@@ -677,28 +677,28 @@ void ctkPythonConsole::initialize(ctkAbstractPythonManager* newPythonManager)
677677
QString ctkPythonConsole::ps1() const
678678
{
679679
PyObject * ps1 = PySys_GetObject(const_cast<char*>("ps1"));
680-
const char * ps1_str = PyString_AsString(ps1);
680+
const char * ps1_str = PyUnicode_AsUTF8(ps1);
681681
return QLatin1String(ps1_str);
682682
}
683683

684684
//----------------------------------------------------------------------------
685685
void ctkPythonConsole::setPs1(const QString& newPs1)
686686
{
687-
PySys_SetObject(const_cast<char*>("ps1"), PyString_FromString(newPs1.toLatin1().data()));
687+
PySys_SetObject(const_cast<char*>("ps1"), PyUnicode_FromString(newPs1.toLatin1().data()));
688688
}
689689

690690
//----------------------------------------------------------------------------
691691
QString ctkPythonConsole::ps2() const
692692
{
693693
PyObject * ps2 = PySys_GetObject(const_cast<char*>("ps2"));
694-
const char * ps2_str = PyString_AsString(ps2);
694+
const char * ps2_str = PyUnicode_AsUTF8(ps2);
695695
return QLatin1String(ps2_str);
696696
}
697697

698698
//----------------------------------------------------------------------------
699699
void ctkPythonConsole::setPs2(const QString& newPs2)
700700
{
701-
PySys_SetObject(const_cast<char*>("ps2"), PyString_FromString(newPs2.toLatin1().data()));
701+
PySys_SetObject(const_cast<char*>("ps2"), PyUnicode_FromString(newPs2.toLatin1().data()));
702702
}
703703

704704
//----------------------------------------------------------------------------

0 commit comments

Comments
 (0)