209 lines
No EOL
8.1 KiB
Python
209 lines
No EOL
8.1 KiB
Python
import os
|
|
import sys
|
|
from typing import Dict, List, Set, Tuple, Optional
|
|
from config.config import Config, OptionDescription, BoolOption, IntOption, ChoiceOption, StrOption
|
|
|
|
# Liste des modules essentiels et par défaut
|
|
essential_modules: Set[str] = {
|
|
"exceptions", "_file", "sys", "__builtin__", "posix", "_warnings", "itertools"
|
|
}
|
|
|
|
default_modules: Set[str] = essential_modules | {
|
|
"_codecs", "gc", "_weakref", "marshal", "errno", "imp", "math", "cmath",
|
|
"_sre", "_pickle_support", "operator", "parser", "symbol", "token", "_ast",
|
|
"_io", "_random", "__pypy__", "_testing", "time"
|
|
}
|
|
|
|
working_modules: Set[str] = default_modules | {
|
|
"_socket", "unicodedata", "mmap", "fcntl", "_locale", "pwd",
|
|
"select", "zipimport", "_lsprof", "crypt", "signal", "_rawffi", "termios",
|
|
"zlib", "bz2", "struct", "_hashlib", "_md5", "_sha", "_minimal_curses",
|
|
"cStringIO", "thread", "itertools", "pyexpat", "_ssl", "cpyext", "array",
|
|
"binascii", "_multiprocessing", '_warnings', "_collections",
|
|
"_multibytecodec", "micronumpy", "_continuation", "_cffi_backend",
|
|
"_csv", "_cppyy", "_pypyjson", "_jitlog"
|
|
}
|
|
|
|
translation_modules: Set[str] = default_modules | {
|
|
"fcntl", "time", "select", "signal", "_rawffi", "zlib", "struct", "_md5",
|
|
"cStringIO", "array", "binascii", "termios", "_minimal_curses"
|
|
}
|
|
|
|
reverse_debugger_disable_modules: Set[str] = {
|
|
"_continuation", "_vmprof", "_multiprocessing", "micronumpy"
|
|
}
|
|
|
|
# Ajustements spécifiques à la plateforme
|
|
if sys.platform == "win32":
|
|
working_modules.add("_winreg")
|
|
working_modules -= {"crypt", "fcntl", "pwd", "termios", "_minimal_curses"}
|
|
translation_modules -= {"fcntl", "termios", "_minimal_curses"}
|
|
default_modules.add("_locale")
|
|
|
|
if "_cppyy" in working_modules:
|
|
working_modules.remove("_cppyy")
|
|
if "faulthandler" in working_modules:
|
|
working_modules.remove("faulthandler")
|
|
if "_vmprof" in working_modules:
|
|
working_modules.remove("_vmprof")
|
|
|
|
if sys.platform == "sunos5":
|
|
working_modules -= {'fcntl', "_minimal_curses", "termios"}
|
|
if "_cppyy" in working_modules:
|
|
working_modules.remove("_cppyy")
|
|
|
|
# Dépendances entre modules
|
|
module_dependencies: Dict[str, List[Tuple[str, Any]]] = {
|
|
'_multiprocessing': [('objspace.usemodules.time', True),
|
|
('objspace.usemodules.thread', True)],
|
|
'cpyext': [('objspace.usemodules.array', True)],
|
|
'_cppyy': [('objspace.usemodules.cpyext', True)],
|
|
'faulthandler': [('objspace.usemodules._vmprof', True)],
|
|
}
|
|
|
|
module_suggests: Dict[str, List[Tuple[str, Any]]] = {
|
|
"_rawffi": [("objspace.usemodules.struct", True)],
|
|
"cpyext": [("translation.secondaryentrypoints", "cpyext,main")],
|
|
}
|
|
|
|
if sys.platform == "win32":
|
|
module_suggests["cpyext"].append(("translation.shared", True))
|
|
|
|
# Définition des options PyPy
|
|
pypy_optiondescription = OptionDescription("objspace", "Object Space Options", [
|
|
OptionDescription("usemodules", "Which Modules should be used", [
|
|
BoolOption(
|
|
modname,
|
|
f"use module {modname}",
|
|
default=modname in default_modules,
|
|
cmdline=f"--withmod-{modname}",
|
|
requires=module_dependencies.get(modname, []),
|
|
suggests=module_suggests.get(modname, []),
|
|
negation=modname not in essential_modules
|
|
)
|
|
for modname in [] # Remplacez par votre liste de modules si nécessaire
|
|
]),
|
|
|
|
BoolOption(
|
|
"allworkingmodules",
|
|
"use as many working modules as possible",
|
|
default=True,
|
|
cmdline="--allworkingmodules",
|
|
negation=True
|
|
),
|
|
|
|
StrOption(
|
|
"extmodules",
|
|
"Comma-separated list of third-party builtin modules",
|
|
cmdline="--ext",
|
|
default=None
|
|
),
|
|
|
|
BoolOption(
|
|
"translationmodules",
|
|
"use only those modules needed to run translate.py on pypy",
|
|
default=False,
|
|
cmdline="--translationmodules",
|
|
suggests=[("objspace.allworkingmodules", False)]
|
|
),
|
|
|
|
BoolOption(
|
|
"lonepycfiles",
|
|
"Import pyc files with no matching py file",
|
|
default=False
|
|
),
|
|
|
|
StrOption(
|
|
"soabi",
|
|
"Tag to differentiate extension modules for different Python interpreters",
|
|
cmdline="--soabi",
|
|
default=None
|
|
),
|
|
|
|
BoolOption(
|
|
"honor__builtins__",
|
|
"Honor the __builtins__ key of a module dictionary",
|
|
default=False
|
|
),
|
|
|
|
BoolOption(
|
|
"disable_call_speedhacks",
|
|
"make sure that all calls go through space.call_args",
|
|
default=False
|
|
),
|
|
|
|
BoolOption(
|
|
"disable_entrypoints",
|
|
"Disable external entry points, notably cpyext module and cffi's embedding mode",
|
|
default=False
|
|
),
|
|
|
|
ChoiceOption(
|
|
"hash",
|
|
"The hash function to use for strings: fnv (CPython 2.7) or siphash24 (CPython >= 3.4)",
|
|
["fnv", "siphash24"],
|
|
default="fnv",
|
|
cmdline="--hash"
|
|
),
|
|
|
|
OptionDescription("std", "Standard Object Space Options", [
|
|
BoolOption("withtproxy", "support transparent proxies", default=True),
|
|
BoolOption("withprebuiltint", "prebuild commonly used int objects", default=False),
|
|
IntOption("prebuiltintfrom", "lowest integer which is prebuilt", default=-5, cmdline="--prebuiltintfrom"),
|
|
IntOption("prebuiltintto", "highest integer which is prebuilt", default=100, cmdline="--prebuiltintto"),
|
|
BoolOption("withsmalllong", "use a version of 'long' in a C long long", default=False),
|
|
BoolOption("withspecialisedtuple", "use specialised tuples", default=False),
|
|
BoolOption("withliststrategies", "enable optimized ways to store lists of primitives", default=True),
|
|
BoolOption("withmethodcachecounter", "try to cache methods and provide a counter in __pypy__", default=False),
|
|
IntOption("methodcachesizeexp", "2 ** methodcachesizeexp is the size of the method cache", default=11),
|
|
BoolOption("intshortcut", "special case integer addition/subtraction", default=False),
|
|
BoolOption("optimized_list_getitem", "special case 'list[integer]' expressions", default=False),
|
|
BoolOption("newshortcut", "cache and shortcut calling __new__ from builtin types", default=False),
|
|
]),
|
|
])
|
|
|
|
def get_pypy_config(overrides: Optional[Dict] = None, translating: bool = False) -> Config:
|
|
"""Create and return a PyPy configuration object."""
|
|
return Config(pypy_optiondescription)
|
|
|
|
def set_pypy_opt_level(config: Config, level: str):
|
|
"""Apply PyPy-specific optimization suggestions based on optimization level."""
|
|
if level in ['2', '3', 'jit']:
|
|
config.objspace.std.suggest(intshortcut=True)
|
|
config.objspace.std.suggest(optimized_list_getitem=True)
|
|
config.objspace.std.suggest(withspecialisedtuple=True)
|
|
|
|
if level == '3':
|
|
config.translation.suggest(
|
|
profopt="-c 'from richards import main;main(); from test import pystone; pystone.main()'"
|
|
)
|
|
|
|
if level == 'mem':
|
|
config.objspace.std.suggest(withprebuiltint=True)
|
|
config.objspace.std.suggest(withliststrategies=True)
|
|
|
|
if level == 'jit':
|
|
pass # No specific optimizations at the moment
|
|
|
|
def enable_allworkingmodules(config: Config):
|
|
"""Enable all working modules in the configuration."""
|
|
modules = working_modules.copy()
|
|
# Sandbox and reverse debugger adjustments
|
|
if getattr(config.translation, 'sandbox', False):
|
|
modules = default_modules
|
|
if getattr(config.translation, 'reverse_debugger', False):
|
|
for mod in reverse_debugger_disable_modules:
|
|
setattr(config.objspace.usemodules, mod, False)
|
|
|
|
# Enable non-essential modules
|
|
modules = [name for name in modules if name not in essential_modules]
|
|
config.objspace.usemodules.suggest(**{mod: True for mod in modules})
|
|
|
|
def enable_translationmodules(config: Config):
|
|
"""Enable translation-specific modules in the configuration."""
|
|
modules = [name for name in translation_modules if name not in essential_modules]
|
|
config.objspace.usemodules.suggest(**{mod: True for mod in modules})
|
|
|
|
if __name__ == '__main__':
|
|
config = get_pypy_config()
|
|
print(config.getpaths()) |