From 55732e19082a566f644e08bd28b9cff6161dcb7e Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 17 Sep 2018 10:46:49 +0200 Subject: Remove obfuscation of variables and modules in the initialization code Since the extensions are now loaded as modules/packages, separation of initialization code in Qt Creator and the code in the extensions is automatically handled. (Python modules/packages cannot access variables/modules/functions from code that imports them.) Change-Id: I7c1db034d76993d42c5d594fa5872aa1001d884e Reviewed-by: Friedemann Kleint --- docs/extensions.md | 8 -------- plugins/pythonextensions/pyutil.cpp | 38 +++++-------------------------------- plugins/pythonextensions/pyutil.h | 2 -- 3 files changed, 5 insertions(+), 43 deletions(-) diff --git a/docs/extensions.md b/docs/extensions.md index 378d347..7152ef6 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -108,14 +108,6 @@ When importing modules, the following important locations will be checked (in th if you want to use non-standard Python packages / modules - This last path is accessible with `PluginInstance.pythonPackagePath()` -## Reserved variable names -Names that look like -```Python -qt_creator_{SOME_NAME}_symbol_mchawrioklpilnjajqkfl -``` -are reserved for the ExtensionManager. Unless you know exactly what you are doing, you should never -touch any of these variables. - ## Installing dependencies If you want to install dependencies, you can include a standard Python `requirements.txt` diff --git a/plugins/pythonextensions/pyutil.cpp b/plugins/pythonextensions/pyutil.cpp index ad4b66e..b563c55 100644 --- a/plugins/pythonextensions/pyutil.cpp +++ b/plugins/pythonextensions/pyutil.cpp @@ -45,17 +45,6 @@ #include #include -// Python has no concept of private variables and there -// is no way to declare a namespace or scope that will be -// inaccessible from the user script. -// To avoid naming collisions with the setup and tear down -// scripts that attempt to separate different extensions on -// the level of user code (or at least make them appear separated), -// this macro mangles the names of variables used. -// Use as: -// "... some Python code ..." privatName("my_var_name") "... more code ..." -#define privateName(name) "qt_creator_" name "_symbol_mchawrioklpilnjajqkfl" - // Setup and utility functions for QtCreator bindings // from typesystem.xml @@ -138,8 +127,8 @@ State init() // The Python interpreter eats SIGINT, which is not what we want. // This stops it from happening. // See https://mail.python.org/pipermail/cplusplus-sig/2012-December/016858.html - if (!runSimpleString("import signal as " privateName("signal") "\n" - "" privateName("signal") ".signal(" privateName("signal") ".SIGINT, " privateName("signal") ".SIG_DFL)")) { + if (!runSimpleString("import signal\n" + "signal.signal(signal.SIGINT, signal.SIG_DFL)")) { qWarning("Failed to prevent SIGINT capture."); } @@ -232,31 +221,14 @@ bool runScript(const std::string &script) return (init() >= PythonInitialized && runSimpleString(script)); } -bool runScriptWithPath(const std::string &script, const std::string &path) -{ - // I couldn't find a direct c api, but this should cause no variable name - // collisions. It also cleans the imported modules after the script finishes - const std::string s = -"import sys as " privateName("sys") "\n" -"" privateName("path") " = list(" privateName("sys") ".path)\n" -"" privateName("sys") ".path.insert(0, \"" + path + "\")\n" -"" privateName("loaded_modules") " = list(" privateName("sys") ".modules.keys())\n" -"" + script + "\n" -"" privateName("sys") ".path = " privateName("path") "\n" -"for m in list(" privateName("sys") ".modules):\n" -" if m not in " privateName("loaded_modules") ":\n" -" del(" privateName("sys") ".modules[m])\n"; - return runScript(s); -} - bool addToSysPath(const std::string &path) { // Add a path to Pythons sys.path // Used for installing dependencies into custom // directory const std::string s = -"import sys as " privateName("sys") "\n" -"" privateName("sys") ".path.append(\"" + path + "\")"; +"import sys\n" +"sys.path.append(\"" + path + "\")"; return runScript(s); } @@ -267,7 +239,7 @@ bool pipInstallRequirements(const std::string &requirements, const std::string & "import pip._internal\n" "if pip._internal.main(['install', '-t', '" + target + "', '-r', '" + requirements + "']) == 0:\n" " open('" + requirements + "'.replace('requirements.txt', 'requirements.txt.installed'), 'a').close()\n"; - return runScriptWithPath(s, ""); + return runScript(s); } } // namespace PyUtil diff --git a/plugins/pythonextensions/pyutil.h b/plugins/pythonextensions/pyutil.h index 16d7405..62a1622 100644 --- a/plugins/pythonextensions/pyutil.h +++ b/plugins/pythonextensions/pyutil.h @@ -61,8 +61,6 @@ PYTHONEXTENSIONSSHARED_EXPORT bool bindSubPyObject(const QString &moduleName, co PYTHONEXTENSIONSSHARED_EXPORT bool runScript(const std::string &script); -PYTHONEXTENSIONSSHARED_EXPORT bool runScriptWithPath(const std::string &script, const std::string &path); - PYTHONEXTENSIONSSHARED_EXPORT bool addToSysPath(const std::string &path); PYTHONEXTENSIONSSHARED_EXPORT bool pipInstallRequirements(const std::string &requirements, const std::string &target); -- cgit v1.2.3