def compileSourceToBytecode(source_code, filename): """Compile given source code into bytecode.""" if "no_docstrings" in getPythonFlags(): tree = ast.parse(source_code, filename) _removeDocFromBody(tree) for node in ast.walk(tree): # Check if it's a documented thing. if not isinstance(node, doc_having): continue _removeDocFromBody(node) bytecode = compile( tree, filename=filename, mode="exec", dont_inherit=True, ) else: bytecode = compile( source_code, filename=filename, mode="exec", dont_inherit=True, ) return bytecode
def buildAssertNode(provider, node, source_ref): # Build assert statements. These are re-formulated as described in the # developer manual too. They end up as conditional statement with raises of # AssertionError exceptions. # Underlying assumption: # # Assert x, y is the same as: # if not x: # raise AssertionError, y # Therefore assert statements are really just conditional statements with a # static raise contained. # exception_value = buildNode(provider, node.msg, source_ref, True) if "no_asserts" in getPythonFlags(): return None if exception_value is not None and python_version > 272: exception_value = ExpressionMakeTuple( elements = (exception_value,), source_ref = source_ref ) raise_statement = StatementRaiseException( exception_type = ExpressionBuiltinExceptionRef( exception_name = "AssertionError", source_ref = source_ref ), exception_value = exception_value, exception_trace = None, exception_cause = None, source_ref = source_ref ) return StatementConditional( condition = ExpressionOperationNOT( operand = buildNode(provider, node.test, source_ref), source_ref = source_ref ), yes_branch = StatementsSequence( statements = ( raise_statement, ), source_ref = source_ref ), no_branch = None, source_ref = source_ref )
def buildAssertNode(provider, node, source_ref): # Build assert statements. These are re-formulated as described in the # developer manual too. They end up as conditional statement with raises of # AssertionError exceptions. # Underlying assumption: # # Assert x, y is the same as: # if not x: # raise AssertionError, y # Therefore assert statements are really just conditional statements with a # static raise contained. # exception_value = buildNode(provider, node.msg, source_ref, True) if "no_asserts" in getPythonFlags(): return None if exception_value is not None and python_version > 272: exception_value = ExpressionMakeTuple( elements=(exception_value,), source_ref=source_ref ) raise_statement = StatementRaiseException( exception_type=ExpressionBuiltinExceptionRef( exception_name="AssertionError", source_ref=source_ref ), exception_value=exception_value, exception_trace=None, exception_cause=None, source_ref=source_ref, ) return makeStatementConditional( condition=ExpressionOperationNOT( operand=buildNode(provider, node.test, source_ref), source_ref=source_ref ), yes_branch=raise_statement, no_branch=None, source_ref=source_ref, )
def makeExpressionBuiltinRef(builtin_name, locals_scope, source_ref): assert builtin_name in builtin_names, builtin_name if builtin_name in quick_names: return makeConstantRefNode(constant=quick_names[builtin_name], source_ref=source_ref) elif builtin_name == "__debug__": return makeConstantRefNode(constant="no_asserts" not in getPythonFlags(), source_ref=source_ref) elif builtin_name in builtin_type_names: return makeExpressionBuiltinTypeRef(builtin_name=builtin_name, source_ref=source_ref) elif builtin_name in ("dir", "eval", "exec", "execfile", "locals", "vars"): return ExpressionBuiltinWithContextRef(builtin_name=builtin_name, locals_scope=locals_scope, source_ref=source_ref) else: return ExpressionBuiltinRef(builtin_name=builtin_name, source_ref=source_ref)
def runScons(main_module, quiet): # Scons gets transported many details, that we express as variables, and # have checks for them, leading to many branches and statements, # pylint: disable=too-many-branches,too-many-statements options = { "name" : os.path.basename( getTreeFilenameWithSuffix(main_module, "") ), "result_name" : getResultBasepath(main_module), "source_dir" : getSourceDirectoryPath(main_module), "debug_mode" : _asBoolStr(Options.isDebug()), "python_debug" : _asBoolStr(Options.isPythonDebug()), "unstripped_mode" : _asBoolStr(Options.isUnstripped()), "module_mode" : _asBoolStr(Options.shallMakeModule()), "full_compat" : _asBoolStr(Options.isFullCompat()), "experimental" : ','.join(Options.getExperimentalIndications()), "trace_mode" : _asBoolStr(Options.shallTraceExecution()), "python_version" : python_version_str, "target_arch" : Utils.getArchitecture(), "python_prefix" : sys.prefix, "nuitka_src" : SconsInterface.getSconsDataPath(), "nuitka_cache" : getCacheDir(), "module_count" : "%d" % ( 1 + \ len(ModuleRegistry.getDoneUserModules()) + \ len(ModuleRegistry.getUncompiledNonTechnicalModules()) ) } if not Options.shallMakeModule(): options["result_exe"] = getResultFullpath(main_module) # Ask Scons to cache on Windows, except where the directory is thrown # away. On non-Windows you can should use ccache instead. if not Options.isRemoveBuildDir() and Utils.getOS() == "Windows": options["cache_mode"] = "true" if Options.isLto(): options["lto_mode"] = "true" if Options.shallDisableConsoleWindow(): options["win_disable_console"] = "true" if Options.isStandaloneMode(): options["standalone_mode"] = "true" if not Options.isStandaloneMode() and \ not Options.shallMakeModule() and \ isUninstalledPython(): options["uninstalled_python"] = "true" if ModuleRegistry.getUncompiledTechnicalModules(): options["frozen_modules"] = str( len(ModuleRegistry.getUncompiledTechnicalModules()) ) if Options.isShowScons(): options["show_scons"] = "true" if Options.isMingw64(): options["mingw_mode"] = "true" if Options.getMsvcVersion(): msvc_version = Options.getMsvcVersion() msvc_version = msvc_version.replace("exp", "Exp") if '.' not in msvc_version: msvc_version += ".0" options["msvc_version"] = msvc_version if Options.isClang(): options["clang_mode"] = "true" if Options.getIconPath(): options["icon_path"] = Options.getIconPath() if Options.isProfile(): options["profile_mode"] = "true" if "no_warnings" in getPythonFlags(): options["no_python_warnings"] = "true" if python_version < 300 and sys.flags.py3k_warning: options["python_sysflag_py3k_warning"] = "true" if python_version < 300 and (sys.flags.division_warning or sys.flags.py3k_warning): options["python_sysflag_division_warning"] = "true" if sys.flags.bytes_warning: options["python_sysflag_bytes_warning"] = "true" if int(os.environ.get("NUITKA_SITE_FLAG", "no_site" in Options.getPythonFlags())): options["python_sysflag_no_site"] = "true" if "trace_imports" in Options.getPythonFlags(): options["python_sysflag_verbose"] = "true" if python_version < 300 and sys.flags.unicode: options["python_sysflag_unicode"] = "true" if python_version >= 370 and sys.flags.utf8_mode: options["python_sysflag_utf8"] = "true" abiflags = getPythonABI() if abiflags: options["abiflags"] = abiflags return SconsInterface.runScons(options, quiet), options
def runSconsBackend(quiet): # Scons gets transported many details, that we express as variables, and # have checks for them, leading to many branches and statements, # pylint: disable=too-many-branches,too-many-statements asBoolStr = SconsInterface.asBoolStr options = { "result_name": OutputDirectories.getResultBasepath(onefile=False), "source_dir": OutputDirectories.getSourceDirectoryPath(), "debug_mode": asBoolStr(Options.is_debug), "python_debug": asBoolStr(Options.isPythonDebug()), "unstripped_mode": asBoolStr(Options.isUnstripped()), "module_mode": asBoolStr(Options.shallMakeModule()), "full_compat": asBoolStr(Options.is_fullcompat), "experimental": ",".join(Options.getExperimentalIndications()), "trace_mode": asBoolStr(Options.shallTraceExecution()), "python_version": python_version_str, "target_arch": Utils.getArchitecture(), "python_prefix": getDirectoryRealPath(sys.prefix), "nuitka_src": SconsInterface.getSconsDataPath(), "module_count": "%d" % ( 1 + len(ModuleRegistry.getDoneModules()) + len(ModuleRegistry.getUncompiledNonTechnicalModules()) ), } if not Options.shallMakeModule(): options["result_exe"] = OutputDirectories.getResultFullpath(onefile=False) if Options.shallUseStaticLibPython(): options["static_libpython"] = asBoolStr(True) if Options.isStandaloneMode(): options["standalone_mode"] = asBoolStr(True) if Options.isOnefileMode(): options["onefile_mode"] = asBoolStr(True) if Options.isWindowsOnefileTempDirMode(): options["onefile_temp_mode"] = asBoolStr(True) if Options.getForcedStdoutPath(): options["forced_stdout_path"] = Options.getForcedStdoutPath() if Options.getForcedStderrPath(): options["forced_stderr_path"] = Options.getForcedStderrPath() if Options.shallTreatUninstalledPython(): options["uninstalled_python"] = asBoolStr(True) if ModuleRegistry.getUncompiledTechnicalModules(): options["frozen_modules"] = str( len(ModuleRegistry.getUncompiledTechnicalModules()) ) if Utils.getOS() == "Windows": options["noelf_mode"] = asBoolStr(True) if Options.isProfile(): options["profile_mode"] = asBoolStr(True) if "no_warnings" in getPythonFlags(): options["no_python_warnings"] = asBoolStr(True) if "no_asserts" in getPythonFlags(): options["python_sysflag_optimize"] = asBoolStr(True) if python_version < 0x300 and sys.flags.py3k_warning: options["python_sysflag_py3k_warning"] = asBoolStr(True) if python_version < 0x300 and ( sys.flags.division_warning or sys.flags.py3k_warning ): options["python_sysflag_division_warning"] = asBoolStr(True) if sys.flags.bytes_warning: options["python_sysflag_bytes_warning"] = asBoolStr(True) if int(os.environ.get("NUITKA_SITE_FLAG", Options.hasPythonFlagNoSite())): options["python_sysflag_no_site"] = asBoolStr(True) if "trace_imports" in Options.getPythonFlags(): options["python_sysflag_verbose"] = asBoolStr(True) if "no_randomization" in Options.getPythonFlags(): options["python_sysflag_no_randomization"] = asBoolStr(True) if python_version < 0x300 and sys.flags.unicode: options["python_sysflag_unicode"] = asBoolStr(True) if python_version >= 0x370 and sys.flags.utf8_mode: options["python_sysflag_utf8"] = asBoolStr(True) abiflags = getPythonABI() if abiflags: options["abiflags"] = abiflags if Options.shallMakeModule(): options["module_suffix"] = getSharedLibrarySuffix(preferred=True) SconsInterface.setCommonOptions(options) return ( SconsInterface.runScons( options=options, quiet=quiet, scons_filename="Backend.scons" ), options, )
def runScons(main_module, quiet): # Scons gets transported many details, that we express as variables, and # have checks for them, leading to many branches, pylint: disable=R0912 python_version_str = "%d.%d" % (sys.version_info[0], sys.version_info[1]) if hasattr(sys, "abiflags"): if Options.isPythonDebug() or hasattr(sys, "getobjects"): if sys.abiflags.startswith("d"): python_version_str += sys.abiflags else: python_version_str += "d" + sys.abiflags else: python_version_str += sys.abiflags def asBoolStr(value): return "true" if value else "false" options = { "name": Utils.basename(getTreeFilenameWithSuffix(main_module, "")), "result_name": getResultBasepath(main_module), "source_dir": getSourceDirectoryPath(main_module), "debug_mode": asBoolStr(Options.isDebug()), "python_debug": asBoolStr(Options.isPythonDebug()), "unstripped_mode": asBoolStr(Options.isUnstripped()), "module_mode": asBoolStr(Options.shallMakeModule()), "optimize_mode": asBoolStr(Options.isOptimize()), "full_compat": asBoolStr(Options.isFullCompat()), "experimental": asBoolStr(Options.isExperimental()), "trace_mode": asBoolStr(Options.shallTraceExecution()), "python_version": python_version_str, "target_arch": Utils.getArchitecture(), "python_prefix": sys.prefix, "nuitka_src": SconsInterface.getSconsDataPath(), "module_count": "%d" % (1 + len(ModuleRegistry.getDoneUserModules()) + len(ModuleRegistry.getUncompiledNonTechnicalModules())), } # Ask Scons to cache on Windows, except where the directory is thrown # away. On non-Windows you can should use ccache instead. if not Options.isRemoveBuildDir() and Utils.getOS() == "Windows": options["cache_mode"] = "true" if Options.isLto(): options["lto_mode"] = "true" if Options.shallDisableConsoleWindow(): options["win_disable_console"] = "true" if Options.isStandaloneMode(): options["standalone_mode"] = "true" if not Options.isStandaloneMode() and not Options.shallMakeModule() and isUninstalledPython(): options["uninstalled_python"] = "true" if ModuleRegistry.getUncompiledTechnicalModules(): options["frozen_modules"] = str(len(ModuleRegistry.getUncompiledTechnicalModules())) if Options.isShowScons(): options["show_scons"] = "true" if Options.isMingw(): options["mingw_mode"] = "true" if Options.getMsvcVersion(): msvc_version = Options.getMsvcVersion() msvc_version = msvc_version.replace("exp", "Exp") if "." not in msvc_version: msvc_version += ".0" options["msvc_version"] = msvc_version if Options.isClang(): options["clang_mode"] = "true" if Options.getIconPath(): options["icon_path"] = Options.getIconPath() if Options.isProfile(): options["profile_mode"] = "true" if "no_warnings" in getPythonFlags(): options["no_python_warnings"] = "true" if python_version < 300 and sys.flags.py3k_warning: options["python_sysflag_py3k_warning"] = "true" if python_version < 300 and (sys.flags.division_warning or sys.flags.py3k_warning): options["python_sysflag_division_warning"] = "true" if sys.flags.bytes_warning: options["python_sysflag_bytes_warning"] = "true" if int(os.environ.get("NUITKA_SITE_FLAG", "no_site" in Options.getPythonFlags())): options["python_sysflag_no_site"] = "true" if "trace_imports" in Options.getPythonFlags(): options["python_sysflag_verbose"] = "true" if python_version < 300 and sys.flags.unicode: options["python_sysflag_unicode"] = "true" return SconsInterface.runScons(options, quiet), options
def mayRaiseException(self, exception_type): return False def mayHaveSideEffects(self): return False def getStrValue(self): return makeConstantRefNode( constant=str(self.getCompileTimeConstant()), user_provided=True, source_ref=self.getSourceReference(), ) _debug_value = "no_asserts" not in getPythonFlags() def makeExpressionBuiltinRef(builtin_name, source_ref): assert builtin_name in builtin_names, builtin_name quick_names = { "None": None, "True": True, "False": False, "__debug__": _debug_value, "Ellipsis": Ellipsis, } if builtin_name in quick_names: return makeConstantRefNode(constant=quick_names[builtin_name],
def runScons(main_module, quiet): # Scons gets transported many details, that we express as variables, and # have checks for them, leading to many branches and statements, # pylint: disable=too-many-branches,too-many-statements options = { "name": os.path.basename(getTreeFilenameWithSuffix(main_module, "")), "result_name": getResultBasepath(main_module), "source_dir": getSourceDirectoryPath(main_module), "debug_mode": _asBoolStr(Options.isDebug()), "python_debug": _asBoolStr(Options.isPythonDebug()), "unstripped_mode": _asBoolStr(Options.isUnstripped()), "module_mode": _asBoolStr(Options.shallMakeModule()), "full_compat": _asBoolStr(Options.isFullCompat()), "experimental": ",".join(Options.getExperimentalIndications()), "trace_mode": _asBoolStr(Options.shallTraceExecution()), "python_version": python_version_str, "target_arch": Utils.getArchitecture(), "python_prefix": sys.prefix, "nuitka_src": SconsInterface.getSconsDataPath(), "nuitka_cache": getCacheDir(), "module_count": "%d" % ( 1 + len(ModuleRegistry.getDoneUserModules()) + len(ModuleRegistry.getUncompiledNonTechnicalModules()) ), } if not Options.shallMakeModule(): options["result_exe"] = getResultFullpath(main_module) # Ask Scons to cache on Windows, except where the directory is thrown # away. On non-Windows you can should use ccache instead. if not Options.isRemoveBuildDir() and Utils.getOS() == "Windows": options["cache_mode"] = "true" if Options.isLto(): options["lto_mode"] = "true" # For AnaConda default to trying static lib python library, which # normally is just not available or if it is even unusable. if "Anaconda" in sys.version: options["static_libpython"] = "true" if Options.shallDisableConsoleWindow(): options["win_disable_console"] = "true" if Options.isStandaloneMode(): options["standalone_mode"] = "true" if ( not Options.isStandaloneMode() and not Options.shallMakeModule() and isUninstalledPython() ): options["uninstalled_python"] = "true" if ModuleRegistry.getUncompiledTechnicalModules(): options["frozen_modules"] = str( len(ModuleRegistry.getUncompiledTechnicalModules()) ) if Options.isShowScons(): options["show_scons"] = "true" if Options.isMingw64(): options["mingw_mode"] = "true" if Options.getMsvcVersion(): msvc_version = Options.getMsvcVersion() msvc_version = msvc_version.replace("exp", "Exp") if "." not in msvc_version: msvc_version += ".0" options["msvc_version"] = msvc_version if Options.isClang(): options["clang_mode"] = "true" if Options.getIconPath(): options["icon_path"] = Options.getIconPath() if Options.isProfile(): options["profile_mode"] = "true" if "no_warnings" in getPythonFlags(): options["no_python_warnings"] = "true" if "no_asserts" in getPythonFlags(): options["python_sysflag_optimize"] = "true" if python_version < 300 and sys.flags.py3k_warning: options["python_sysflag_py3k_warning"] = "true" if python_version < 300 and (sys.flags.division_warning or sys.flags.py3k_warning): options["python_sysflag_division_warning"] = "true" if sys.flags.bytes_warning: options["python_sysflag_bytes_warning"] = "true" if int(os.environ.get("NUITKA_SITE_FLAG", "no_site" in Options.getPythonFlags())): options["python_sysflag_no_site"] = "true" if "trace_imports" in Options.getPythonFlags(): options["python_sysflag_verbose"] = "true" if python_version < 300 and sys.flags.unicode: options["python_sysflag_unicode"] = "true" if python_version >= 370 and sys.flags.utf8_mode: options["python_sysflag_utf8"] = "true" abiflags = getPythonABI() if abiflags: options["abiflags"] = abiflags return SconsInterface.runScons(options, quiet), options
def runSconsBackend(quiet): # Scons gets transported many details, that we express as variables, and # have checks for them, leading to many branches and statements, # pylint: disable=too-many-branches,too-many-statements asBoolStr = SconsInterface.asBoolStr options = { "result_name": OutputDirectories.getResultBasepath(onefile=False), "source_dir": OutputDirectories.getSourceDirectoryPath(), "debug_mode": asBoolStr(Options.is_debug), "python_debug": asBoolStr(Options.isPythonDebug()), "unstripped_mode": asBoolStr(Options.isUnstripped()), "module_mode": asBoolStr(Options.shallMakeModule()), "full_compat": asBoolStr(Options.is_fullcompat), "experimental": ",".join(Options.getExperimentalIndications()), "trace_mode": asBoolStr(Options.shallTraceExecution()), "python_version": python_version_str, "target_arch": Utils.getArchitecture(), "python_prefix": getDirectoryRealPath(sys.prefix), "nuitka_src": SconsInterface.getSconsDataPath(), "module_count": "%d" % (1 + len(ModuleRegistry.getDoneModules()) + len(ModuleRegistry.getUncompiledNonTechnicalModules())), } if not Options.shallMakeModule(): options["result_exe"] = OutputDirectories.getResultFullpath() # Ask Scons to cache on Windows, except where the directory is thrown # away. On non-Windows you can should use ccache instead. if not Options.isRemoveBuildDir() and Utils.getOS() == "Windows": options["cache_mode"] = "true" if Options.isLto(): options["lto_mode"] = asBoolStr(True) if Options.shallUseStaticLibPython(): options["static_libpython"] = asBoolStr(True) if Options.shallDisableConsoleWindow(): options["win_disable_console"] = asBoolStr(True) if Options.isStandaloneMode(): options["standalone_mode"] = asBoolStr(True) if Options.shallTreatUninstalledPython(): options["uninstalled_python"] = asBoolStr(True) if ModuleRegistry.getUncompiledTechnicalModules(): options["frozen_modules"] = str( len(ModuleRegistry.getUncompiledTechnicalModules())) if Options.isShowScons(): options["show_scons"] = asBoolStr(True) if Options.isMingw64(): options["mingw_mode"] = asBoolStr(True) if Options.getMsvcVersion(): msvc_version = Options.getMsvcVersion() msvc_version = msvc_version.replace("exp", "Exp") if "." not in msvc_version: msvc_version += ".0" options["msvc_version"] = msvc_version if Utils.getOS() == "Windows": options["noelf_mode"] = asBoolStr(True) if Options.isClang(): options["clang_mode"] = asBoolStr(True) if Options.isProfile(): options["profile_mode"] = asBoolStr(True) if "no_warnings" in getPythonFlags(): options["no_python_warnings"] = asBoolStr(True) if "no_asserts" in getPythonFlags(): options["python_sysflag_optimize"] = asBoolStr(True) if python_version < 0x300 and sys.flags.py3k_warning: options["python_sysflag_py3k_warning"] = asBoolStr(True) if python_version < 0x300 and (sys.flags.division_warning or sys.flags.py3k_warning): options["python_sysflag_division_warning"] = asBoolStr(True) if sys.flags.bytes_warning: options["python_sysflag_bytes_warning"] = asBoolStr(True) if int( os.environ.get("NUITKA_SITE_FLAG", "no_site" in Options.getPythonFlags())): options["python_sysflag_no_site"] = asBoolStr(True) if "trace_imports" in Options.getPythonFlags(): options["python_sysflag_verbose"] = asBoolStr(True) if "no_randomization" in Options.getPythonFlags(): options["python_sysflag_no_randomization"] = asBoolStr(True) if python_version < 0x300 and sys.flags.unicode: options["python_sysflag_unicode"] = asBoolStr(True) if python_version >= 0x370 and sys.flags.utf8_mode: options["python_sysflag_utf8"] = asBoolStr(True) abiflags = getPythonABI() if abiflags: options["abiflags"] = abiflags cpp_defines = Plugins.getPreprocessorSymbols() if cpp_defines: options["cpp_defines"] = ",".join( "%s%s%s" % (key, "=" if value else "", value or "") for key, value in cpp_defines.items()) link_libraries = Plugins.getExtraLinkLibraries() if link_libraries: options["link_libraries"] = ",".join(link_libraries) if Options.shallMakeModule(): options["module_suffix"] = getSharedLibrarySuffix(preferred=True) if Options.shallRunInDebugger(): options["full_names"] = asBoolStr(True) if Options.assumeYesForDownloads(): options["assume_yes_for_downloads"] = asBoolStr(True) return ( SconsInterface.runScons(options=options, quiet=quiet, scons_filename="Backend.scons"), options, )
def mayRaiseException(self, exception_type): return False def mayHaveSideEffects(self): return False def getStrValue(self): return makeConstantRefNode( constant=str(self.getCompileTimeConstant()), user_provided=True, source_ref=self.getSourceReference(), ) _debug_value = "no_asserts" not in getPythonFlags() def makeExpressionBuiltinRef(builtin_name, source_ref): assert builtin_name in builtin_names, builtin_name quick_names = { "None": None, "True": True, "False": False, "__debug__": _debug_value, "Ellipsis": Ellipsis, } if builtin_name in quick_names: return makeConstantRefNode(