From 76835188e63f2223341f8cc16efd64c201d468eb Mon Sep 17 00:00:00 2001 From: Aaron Colwell <300262+acolwell@users.noreply.github.com> Date: Tue, 23 Jul 2024 12:48:31 -0700 Subject: [PATCH] Remove Python2 logic. (#985) Signed-off-by: Aaron Colwell <300262+acolwell@users.noreply.github.com> --- Engine/AppManager.cpp | 37 ---------------------- Engine/AppManagerPrivate.cpp | 25 --------------- Engine/Knob.cpp | 2 +- Engine/KnobImpl.h | 18 ++--------- Global/PythonUtils.cpp | 56 --------------------------------- Global/PythonUtils.h | 4 --- Gui/GuiApplicationManager10.cpp | 10 ------ PythonBin/python_main.cpp | 13 -------- 8 files changed, 3 insertions(+), 162 deletions(-) diff --git a/Engine/AppManager.cpp b/Engine/AppManager.cpp index 134d7d485..586c5e303 100644 --- a/Engine/AppManager.cpp +++ b/Engine/AppManager.cpp @@ -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 @@ -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); @@ -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; @@ -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."); } @@ -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 @@ -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); @@ -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); diff --git a/Engine/AppManagerPrivate.cpp b/Engine/AppManagerPrivate.cpp index cf6468142..55922950c 100644 --- a/Engine/AppManagerPrivate.cpp +++ b/Engine/AppManagerPrivate.cpp @@ -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 @@ -203,7 +196,6 @@ char2wchar(const char* arg) } *out = 0; #endif // ifdef HAVE_MBRTOWC -#endif // PY_VERSION < 3.7 return res; oom: @@ -213,9 +205,6 @@ char2wchar(const char* arg) return NULL; } // char2wchar -#endif // if PY_MAJOR_VERSION >= 3 - - NATRON_NAMESPACE_ENTER AppManagerPrivate::AppManagerPrivate() : globalTLS() @@ -252,10 +241,8 @@ AppManagerPrivate::AppManagerPrivate() , nThreadsMutex() , runningThreadsCount() , lastProjectLoadedCreatedDuringRC2Or3(false) -#if PY_MAJOR_VERSION >= 3 , commandLineArgsWideOriginal() , commandLineArgsWide() -#endif , commandLineArgsUtf8Original() , commandLineArgsUtf8() , nArgs(0) @@ -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 @@ -1177,26 +1161,17 @@ void AppManagerPrivate::copyUtf8ArgsToMembers(const std::vector& 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 diff --git a/Engine/Knob.cpp b/Engine/Knob.cpp index acc443777..d3fd61651 100644 --- a/Engine/Knob.cpp +++ b/Engine/Knob.cpp @@ -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; } diff --git a/Engine/KnobImpl.h b/Engine/KnobImpl.h index 6db230e50..153a829d9 100644 --- a/Engine/KnobImpl.h +++ b/Engine/KnobImpl.h @@ -40,14 +40,6 @@ #include #include -// clang-format off -CLANG_DIAG_OFF(mismatched-tags) -GCC_DIAG_OFF(unused-parameter) -#include -CLANG_DIAG_ON(mismatched-tags) -GCC_DIAG_ON(unused-parameter) -// clang-format on - #include "Global/PythonUtils.h" #include "Engine/Curve.h" @@ -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); @@ -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); @@ -462,8 +450,6 @@ Knob::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) { diff --git a/Global/PythonUtils.cpp b/Global/PythonUtils.cpp index bda806fd9..67e237757 100755 --- a/Global/PythonUtils.cpp +++ b/Global/PythonUtils.cpp @@ -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( pythonHomeW.c_str() ) ); -# else - // Python 2 - Py_SetPythonHome( const_cast( pythonHome.c_str() ) ); -# endif } @@ -217,18 +211,7 @@ 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 @@ -236,11 +219,7 @@ void setupPythonEnv(const std::string& binPath) } // setupPythonEnv -#if PY_MAJOR_VERSION >= 3 PyObject* initializePython3(const std::vector& commandLineArgsWide) -#else -PyObject* initializePython2(const std::vector& commandLineArgsUtf8) -#endif { //See https://developer.blender.org/T31507 //Python will not load anything in site-packages if this is set @@ -264,14 +243,7 @@ PyObject* initializePython2(const std::vector& 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 @@ -284,7 +256,6 @@ PyObject* initializePython2(const std::vector& 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); @@ -293,19 +264,12 @@ PyObject* initializePython2(const std::vector& commandLineArgsUtf8) PyObject *exec_prefix = PyUnicode_FromWideChar(Py_GetPythonHome(), -1); PySys_SetObject(const_cast("exec_prefix"), exec_prefix); Py_XDECREF(exec_prefix); -#endif ///////////////////////////////////////// // PySys_SetArgv ///////////////////////////////////////// // -#if PY_MAJOR_VERSION >= 3 - // Python 3 PySys_SetArgv( commandLineArgsWide.size(), const_cast(&commandLineArgsWide[0]) ); /// relative module import -#else - // Python 2 - PySys_SetArgv( commandLineArgsUtf8.size(), const_cast(&commandLineArgsUtf8[0]) ); /// relative module import -#endif PyObject* mainModule = PyImport_ImportModule("__main__"); //create main module , new ref @@ -340,32 +304,13 @@ PyObject* initializePython2(const std::vector& 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() ); @@ -418,7 +363,6 @@ PyObject* initializePython2(const std::vector& commandLineArgsUtf8) if (v) { Py_DECREF(v); } -#endif // PY_MAJOR_VERSION >= 3 } #endif diff --git a/Global/PythonUtils.h b/Global/PythonUtils.h index 323e78e45..7c7e45d82 100755 --- a/Global/PythonUtils.h +++ b/Global/PythonUtils.h @@ -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& commandLineArgsWide); -#else -PyObject* initializePython2(const std::vector& commandLineArgsUtf8); -#endif std::string PyStringToStdString(PyObject* obj); diff --git a/Gui/GuiApplicationManager10.cpp b/Gui/GuiApplicationManager10.cpp index 9686425e0..99bda2614 100644 --- a/Gui/GuiApplicationManager10.cpp +++ b/Gui/GuiApplicationManager10.cpp @@ -1140,12 +1140,7 @@ GuiApplicationManager::clearNodeClipBoard() ///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_NatronGui(); -#else -void initNatronGui(); -#endif } @@ -1154,12 +1149,7 @@ GuiApplicationManager::initBuiltinPythonModules() { AppManager::initBuiltinPythonModules(); -#if PY_MAJOR_VERSION >= 3 - // Python 3 int ret = PyImport_AppendInittab(NATRON_GUI_PYTHON_MODULE_NAME, &PyInit_NatronGui); -#else - int ret = PyImport_AppendInittab(NATRON_GUI_PYTHON_MODULE_NAME, &initNatronGui); -#endif if (ret == -1) { throw std::runtime_error("Failed to initialize built-in Python module."); } diff --git a/PythonBin/python_main.cpp b/PythonBin/python_main.cpp index 75cc7b6ad..ac2dce4d7 100644 --- a/PythonBin/python_main.cpp +++ b/PythonBin/python_main.cpp @@ -30,8 +30,6 @@ #include -#if PY_MAJOR_VERSION >= 3 - // OSX 10.6 Snow Leopard is missing wcsdup #if (__APPLE__ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070) #include @@ -51,7 +49,6 @@ wcsdup(const wchar_t *s) return wmemcpy(copy, s, len); } #endif -#endif @@ -77,8 +74,6 @@ extern "C" { NATRON_NAMESPACE::NATRON_PYTHON_NAMESPACE::setupPythonEnv(binPath); -#if PY_MAJOR_VERSION >= 3 - // Python 3 std::vector wideArgs(argc); for (int i = 0; i < argc; ++i) { std::wstring utf16 = NATRON_NAMESPACE::StrUtils::utf8_to_utf16(commandLineArgsUtf8[i]); @@ -86,14 +81,6 @@ extern "C" { } int ret = Py_Main(commandLineArgsUtf8.size(), &wideArgs[0]); return ret; -#else - std::vector utf8Args(argc); - for (int i = 0; i < argc; ++i) { - utf8Args[i] = strdup(commandLineArgsUtf8[i].c_str()); - } - int ret = Py_Main(commandLineArgsUtf8.size(), &utf8Args[0]); - return ret; -#endif } //main #ifdef __NATRON_WIN32__ } // extern "C"