Skip to content

Commit

Permalink
Remove Python2 logic. (NatronGitHub#985)
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Colwell <300262+acolwell@users.noreply.github.com>
  • Loading branch information
acolwell committed Jul 23, 2024
1 parent eb364b6 commit 7683518
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 162 deletions.
37 changes: 0 additions & 37 deletions Engine/AppManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,9 @@ AppManager::loadFromArgs(const CLArgs& cl)
{

#ifdef DEBUG
#if PY_MAJOR_VERSION >= 3
for (std::size_t i = 0; i < _imp->commandLineArgsWide.size(); ++i) {
std::cout << "argv[" << i << "] = " << StrUtils::utf16_to_utf8( std::wstring(_imp->commandLineArgsWide[i]) ) << std::endl;
}
#else
for (std::size_t i = 0; i < _imp->commandLineArgsUtf8.size(); ++i) {
std::cout << "argv[" << i << "] = " << _imp->commandLineArgsUtf8[i] << std::endl;
}
#endif
#endif

// This needs to be done BEFORE creating qApp because
Expand Down Expand Up @@ -1765,11 +1759,7 @@ addToPythonPathFunctor(const QDir& directory)
std::string addToPythonPath("sys.path.append(str('");

addToPythonPath += directory.absolutePath().toStdString();
#if PY_MAJOR_VERSION >= 3
addToPythonPath += "'))\n";
#else
addToPythonPath += "').decode('utf-8'))\n";
#endif

std::string err;
bool ok = NATRON_PYTHON_NAMESPACE::interpretPythonScript(addToPythonPath, &err, 0);
Expand Down Expand Up @@ -2957,11 +2947,7 @@ AppManager::initPython()
///Must be called prior to Py_Initialize (calls PyImport_AppendInittab())
initBuiltinPythonModules();

#if PY_MAJOR_VERSION >= 3
_imp->mainModule = NATRON_PYTHON_NAMESPACE::initializePython3(_imp->commandLineArgsWide);
#else
_imp->mainModule = NATRON_PYTHON_NAMESPACE::initializePython2(_imp->commandLineArgsUtf8);
#endif

std::string err;
std::string modulename = NATRON_ENGINE_PYTHON_MODULE_NAME;
Expand Down Expand Up @@ -3053,23 +3039,13 @@ AppManager::getMainModule()
///The symbol has been generated by Shiboken in Engine/NatronEngine/natronengine_module_wrapper.cpp
extern "C"
{
#if PY_MAJOR_VERSION >= 3
// Python 3
PyObject* PyInit_NatronEngine();
#else
void initNatronEngine();
#endif
}

void
AppManager::initBuiltinPythonModules()
{
#if PY_MAJOR_VERSION >= 3
// Python 3
int ret = PyImport_AppendInittab(NATRON_ENGINE_PYTHON_MODULE_NAME, &PyInit_NatronEngine);
#else
int ret = PyImport_AppendInittab(NATRON_ENGINE_PYTHON_MODULE_NAME, &initNatronEngine);
#endif
if (ret == -1) {
throw std::runtime_error("Failed to initialize built-in Python module.");
}
Expand Down Expand Up @@ -3121,12 +3097,7 @@ AppManager::launchPythonInterpreter()
assert(PyGILState_Check()); // Not available prior to Python 3.4
#endif

#if PY_MAJOR_VERSION >= 3
// Python 3
Py_Main(1, &_imp->commandLineArgsWide[0]);
#else
Py_Main(1, &_imp->commandLineArgsUtf8[0]);
#endif
}

int
Expand Down Expand Up @@ -3640,11 +3611,7 @@ NATRON_PYTHON_NAMESPACE::interpretPythonScript(const std::string& script,
Py_DECREF(pyStr);

// See if we can get a full traceback
#if PY_MAJOR_VERSION >= 3
PyObject* module_name = PyUnicode_FromString("traceback");
#else
PyObject* module_name = PyString_FromString("traceback");
#endif
PyObject* pyth_module = PyImport_Import(module_name);
Py_DECREF(module_name);

Expand All @@ -3659,11 +3626,7 @@ NATRON_PYTHON_NAMESPACE::interpretPythonScript(const std::string& script,
if (pyth_func && PyCallable_Check(pyth_func)) {
PyObject *pyth_val = PyObject_CallFunctionObjArgs(pyth_func, pyExcType, pyExcValue, pyExcTraceback, NULL);
if (pyth_val) {
#if PY_MAJOR_VERSION >= 3
PyObject *emptyString = PyUnicode_FromString("");
#else
PyObject *emptyString = PyString_FromString("");
#endif
PyObject *strList = PyObject_CallMethod(emptyString, (char*)"join", (char*)"(O)", pyth_val);
Py_DECREF(emptyString);
Py_DECREF(pyth_val);
Expand Down
25 changes: 0 additions & 25 deletions Engine/AppManagerPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,11 @@ GCC_DIAG_ON(unused-parameter)
BOOST_CLASS_EXPORT(NATRON_NAMESPACE::FrameParams)
BOOST_CLASS_EXPORT(NATRON_NAMESPACE::ImageParams)

#if PY_MAJOR_VERSION >= 3
// Python 3

//Borrowed from https://github.com/python/cpython/blob/634cb7aa2936a09e84c5787d311291f0e042dba3/Python/fileutils.c
//Somehow Python 3 dev forced every C application embedding python to have their own code to convert char** to wchar_t**
static wchar_t*
char2wchar(const char* arg)
{
#if PY_MAJOR_VERSION > 3 || PY_MAJOR_MINOR >= 7
// https://docs.python.org/3/c-api/sys.html#c.Py_DecodeLocale
return Py_DecodeLocale(arg, NULL);
#else // PY_VERSION < 3.7
wchar_t *res = NULL;

#ifdef HAVE_BROKEN_MBSTOWCS
Expand Down Expand Up @@ -203,7 +196,6 @@ char2wchar(const char* arg)
}
*out = 0;
#endif // ifdef HAVE_MBRTOWC
#endif // PY_VERSION < 3.7

return res;
oom:
Expand All @@ -213,9 +205,6 @@ char2wchar(const char* arg)
return NULL;
} // char2wchar

#endif // if PY_MAJOR_VERSION >= 3


NATRON_NAMESPACE_ENTER
AppManagerPrivate::AppManagerPrivate()
: globalTLS()
Expand Down Expand Up @@ -252,10 +241,8 @@ AppManagerPrivate::AppManagerPrivate()
, nThreadsMutex()
, runningThreadsCount()
, lastProjectLoadedCreatedDuringRC2Or3(false)
#if PY_MAJOR_VERSION >= 3
, commandLineArgsWideOriginal()
, commandLineArgsWide()
#endif
, commandLineArgsUtf8Original()
, commandLineArgsUtf8()
, nArgs(0)
Expand Down Expand Up @@ -294,12 +281,9 @@ AppManagerPrivate::~AppManagerPrivate()
for (std::size_t i = 0; i < commandLineArgsUtf8Original.size(); ++i) {
free(commandLineArgsUtf8Original[i]);
}
#if PY_MAJOR_VERSION >= 3
// Python 3
for (std::size_t i = 0; i < commandLineArgsWideOriginal.size(); ++i) {
free(commandLineArgsWideOriginal[i]);
}
#endif
}

#ifdef NATRON_USE_BREAKPAD
Expand Down Expand Up @@ -1177,26 +1161,17 @@ void
AppManagerPrivate::copyUtf8ArgsToMembers(const std::vector<std::string>& utf8Args)
{
// Copy command line args to local members that live throughout the lifetime of AppManager
#if PY_MAJOR_VERSION >= 3
// Python 3
commandLineArgsWideOriginal.resize(utf8Args.size());
#endif
commandLineArgsUtf8Original.resize(utf8Args.size());
nArgs = (int)utf8Args.size();
for (std::size_t i = 0; i < utf8Args.size(); ++i) {
commandLineArgsUtf8Original[i] = strdup(utf8Args[i].c_str());

// Python 3 needs wchar_t arguments
#if PY_MAJOR_VERSION >= 3
// Python 3
commandLineArgsWideOriginal[i] = char2wchar(utf8Args[i].c_str());
#endif
}
commandLineArgsUtf8 = commandLineArgsUtf8Original;
#if PY_MAJOR_VERSION >= 3
// Python 3
commandLineArgsWide = commandLineArgsWideOriginal;
#endif
}

void
Expand Down
2 changes: 1 addition & 1 deletion Engine/Knob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2710,7 +2710,7 @@ KnobHelper::validateExpression(const std::string& expression,
if ( PyFloat_Check(ret) ) {
index = std::floor( (double)PyFloat_AsDouble(ret) + 0.5 );
} else if ( PyLong_Check(ret) ) {
index = (int)PyInt_AsLong(ret);
index = (int)PyLong_AsLong(ret);
} else if (PyObject_IsTrue(ret) == 1) {
index = 1;
}
Expand Down
18 changes: 2 additions & 16 deletions Engine/KnobImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@
#include <QtCore/QCoreApplication>
#include <QtCore/QThread>

// clang-format off
CLANG_DIAG_OFF(mismatched-tags)
GCC_DIAG_OFF(unused-parameter)
#include <shiboken.h>
CLANG_DIAG_ON(mismatched-tags)
GCC_DIAG_ON(unused-parameter)
// clang-format on

#include "Global/PythonUtils.h"

#include "Engine/Curve.h"
Expand Down Expand Up @@ -346,9 +338,7 @@ template <>
int
KnobHelper::pyObjectToType(PyObject* o)
{
if (PyInt_Check(o)) {
return (int)PyInt_AsLong(o);
} else if (PyLong_Check(o)) {
if (PyLong_Check(o)) {
return (int)PyLong_AsLong(o);
} else if (PyFloat_Check(o)) {
return (int)PyFloat_AsDouble(o);
Expand All @@ -371,9 +361,7 @@ template <>
double
KnobHelper::pyObjectToType(PyObject* o)
{
if (PyInt_Check(o)) {
return (double)PyInt_AsLong(o);
} else if (PyLong_Check(o)) {
if (PyLong_Check(o)) {
return (double)PyLong_AsLong(o);
} else if (PyFloat_Check(o)) {
return PyFloat_AsDouble(o);
Expand Down Expand Up @@ -462,8 +450,6 @@ Knob<T>::evaluateExpression_pod(double time,

if ( PyFloat_Check(ret) ) {
*value = PyFloat_AsDouble(ret);
} else if ( PyInt_Check(ret) ) {
*value = (double)PyInt_AsLong(ret);
} else if ( PyLong_Check(ret) ) {
*value = (double)PyLong_AsLong(ret);
} else if (PyObject_IsTrue(ret) == 1) {
Expand Down
56 changes: 0 additions & 56 deletions Global/PythonUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,7 @@ void setupPythonEnv(const std::string& binPath)
# if defined(NATRON_CONFIG_SNAPSHOT) || defined(DEBUG)
printf( "Py_SetPythonHome(\"%s\")\n", pythonHome.c_str() );
# endif
# if PY_MAJOR_VERSION >= 3
// Python 3
Py_SetPythonHome( const_cast<wchar_t*>( pythonHomeW.c_str() ) );
# else
// Python 2
Py_SetPythonHome( const_cast<char*>( pythonHome.c_str() ) );
# endif
}


Expand Down Expand Up @@ -217,30 +211,15 @@ void setupPythonEnv(const std::string& binPath)
pythonPath = toPrependStr + pathSep + pythonPath;
}
// Py_SetPath() sets the whole path, but setting PYTHONPATH still keeps the system's python path
# if 0 // PY_MAJOR_VERSION >= 3 // commented for the reason above
std::wstring pythonPathString = StrUtils::utf8_to_utf16(pythonPath);
Py_SetPath( pythonPathString.c_str() ); // argument is copied internally, no need to use static storage
# else
# if 0//def __NATRON_WIN32__
// qputenv on mingw will just call putenv, but we want to keep the utf16 info, so we need to call _wputenv
_wputenv_s(L"PYTHONPATH", StrUtils::utf8_to_utf16(pythonPath).c_str());
# else
ProcInfo::putenv_wrapper( "PYTHONPATH", pythonPath.c_str() );
//Py_SetPath( pythonPathString.c_str() ); // does not exist in Python 2
# endif
# endif
# if defined(NATRON_CONFIG_SNAPSHOT) || defined(DEBUG)
printf( "PYTHONPATH set to %s\n", pythonPath.c_str() );
# endif
}

} // setupPythonEnv

#if PY_MAJOR_VERSION >= 3
PyObject* initializePython3(const std::vector<wchar_t*>& commandLineArgsWide)
#else
PyObject* initializePython2(const std::vector<char*>& commandLineArgsUtf8)
#endif
{
//See https://developer.blender.org/T31507
//Python will not load anything in site-packages if this is set
Expand All @@ -264,14 +243,7 @@ PyObject* initializePython2(const std::vector<char*>& commandLineArgsUtf8)
// Must be done before Py_Initialize (see doc of Py_Initialize)
//

#if PY_MAJOR_VERSION >= 3
// Python 3
Py_SetProgramName(commandLineArgsWide[0]);
#else
// Python 2
printf( "Py_SetProgramName(\"%s\")\n", commandLineArgsUtf8[0] );
Py_SetProgramName(commandLineArgsUtf8[0]);
#endif

/////////////////////////////////////////
// Py_Initialize
Expand All @@ -284,7 +256,6 @@ PyObject* initializePython2(const std::vector<char*>& commandLineArgsUtf8)
Py_Initialize();
// pythonHome must be const, so that the c_str() pointer is never invalidated

#if PY_MAJOR_VERSION >= 3
// Py_SetPath clears sys.prefix and sys.exec_prefix
// https://github.com/NatronGitHub/Natron/issues/696
PyObject *prefix = PyUnicode_FromWideChar(Py_GetPythonHome(), -1);
Expand All @@ -293,19 +264,12 @@ PyObject* initializePython2(const std::vector<char*>& commandLineArgsUtf8)
PyObject *exec_prefix = PyUnicode_FromWideChar(Py_GetPythonHome(), -1);
PySys_SetObject(const_cast<char*>("exec_prefix"), exec_prefix);
Py_XDECREF(exec_prefix);
#endif

/////////////////////////////////////////
// PySys_SetArgv
/////////////////////////////////////////
//
#if PY_MAJOR_VERSION >= 3
// Python 3
PySys_SetArgv( commandLineArgsWide.size(), const_cast<wchar_t**>(&commandLineArgsWide[0]) ); /// relative module import
#else
// Python 2
PySys_SetArgv( commandLineArgsUtf8.size(), const_cast<char**>(&commandLineArgsUtf8[0]) ); /// relative module import
#endif

PyObject* mainModule = PyImport_ImportModule("__main__"); //create main module , new ref

Expand Down Expand Up @@ -340,32 +304,13 @@ PyObject* initializePython2(const std::vector<char*>& commandLineArgsUtf8)
printf( "Py_OptimizeFlag is %d\n", Py_OptimizeFlag );
printf( "Py_NoSiteFlag is %d\n", Py_NoSiteFlag );
printf( "Py_BytesWarningFlag is %d\n", Py_BytesWarningFlag );
#if PY_MAJOR_VERSION < 3
printf( "Py_UseClassExceptionsFlag is %d\n", Py_UseClassExceptionsFlag );
#endif
printf( "Py_FrozenFlag is %d\n", Py_FrozenFlag );
#if PY_MAJOR_VERSION < 3
printf( "Py_TabcheckFlag is %d\n", Py_TabcheckFlag );
printf( "Py_UnicodeFlag is %d\n", Py_UnicodeFlag );
#else
printf( "Py_HashRandomizationFlag is %d\n", Py_HashRandomizationFlag );
printf( "Py_IsolatedFlag is %d\n", Py_IsolatedFlag );
printf( "Py_QuietFlag is %d\n", Py_QuietFlag );
#endif
printf( "Py_IgnoreEnvironmentFlag is %d\n", Py_IgnoreEnvironmentFlag );
#if PY_MAJOR_VERSION < 3
printf( "Py_DivisionWarningFlag is %d\n", Py_DivisionWarningFlag );
#endif
printf( "Py_DontWriteBytecodeFlag is %d\n", Py_DontWriteBytecodeFlag );
printf( "Py_NoUserSiteDirectory is %d\n", Py_NoUserSiteDirectory );
#if PY_MAJOR_VERSION < 3
printf( "Py_GetProgramName is %s\n", Py_GetProgramName() );
printf( "Py_GetPrefix is %s\n", Py_GetPrefix() );
printf( "Py_GetExecPrefix is %s\n", Py_GetPrefix() );
printf( "Py_GetProgramFullPath is %s\n", Py_GetProgramFullPath() );
printf( "Py_GetPath is %s\n", Py_GetPath() );
printf( "Py_GetPythonHome is %s\n", Py_GetPythonHome() );
#else // PY_MAJOR_VERSION >= 3
printf( "Py_GetProgramName is %ls\n", Py_GetProgramName() );
printf( "Py_GetPrefix is %ls\n", Py_GetPrefix() );
printf( "Py_GetExecPrefix is %ls\n", Py_GetPrefix() );
Expand Down Expand Up @@ -418,7 +363,6 @@ PyObject* initializePython2(const std::vector<char*>& commandLineArgsUtf8)
if (v) {
Py_DECREF(v);
}
#endif // PY_MAJOR_VERSION >= 3
}
#endif

Expand Down
4 changes: 0 additions & 4 deletions Global/PythonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ void setupPythonEnv(const std::string& binPath);
* @brief Must be called after setupPythonEnv(), calls Py_SetProgramName and Py_Initialize, PySys_SetArgv
* @returns A pointer to the main module
**/
#if PY_MAJOR_VERSION >= 3
PyObject* initializePython3(const std::vector<wchar_t*>& commandLineArgsWide);
#else
PyObject* initializePython2(const std::vector<char*>& commandLineArgsUtf8);
#endif

std::string PyStringToStdString(PyObject* obj);

Expand Down
Loading

0 comments on commit 7683518

Please sign in to comment.