Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use compile() with dont_inherit and optimize #2387

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cx_Freeze/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def code_object_replace_function(
if code is None:
return code

new_code = compile(dedent(source), code.co_filename, "exec")
new_code = compile(
dedent(source), code.co_filename, "exec", dont_inherit=True
)
new_co_func = None
for constant in new_code.co_consts:
if isinstance(constant, CodeType) and constant.co_name == name:
Expand Down
4 changes: 3 additions & 1 deletion cx_Freeze/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,9 @@ def _load_module_code(
elif isinstance(loader, importlib.machinery.ExtensionFileLoader):
logging.debug("Adding module [%s] [EXTENSION]", name)
elif module.source_is_string:
module.code = compile("", path, "exec")
module.code = compile(
"", path, "exec", dont_inherit=True, optimize=self.optimize
)
else:
msg = f"Unknown module loader in {path}"
raise ImportError(msg, name=name)
Expand Down
12 changes: 9 additions & 3 deletions cx_Freeze/hooks/multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

from __future__ import annotations

import os
from textwrap import dedent
from typing import TYPE_CHECKING

from cx_Freeze._compat import IS_MINGW, IS_WINDOWS

if TYPE_CHECKING:
from cx_Freeze.finder import ModuleFinder
from cx_Freeze.module import Module


def load_multiprocessing(_, module: Module) -> None:
def load_multiprocessing(finder: ModuleFinder, module: Module) -> None:
"""The forkserver method calls utilspawnv_passfds in ensure_running to
pass a command line to python. In cx_Freeze the running executable
is called, then we need to catch this and use exec function.
Expand Down Expand Up @@ -56,7 +56,13 @@ def _get_freeze_context(self, method=None):
# cx_Freeze patch end
"""
code_string = module.file.read_text(encoding="utf_8") + dedent(source)
module.code = compile(code_string, os.fspath(module.file), "exec")
module.code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)


def load_multiprocessing_connection(_, module: Module) -> None:
Expand Down
11 changes: 8 additions & 3 deletions cx_Freeze/hooks/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from __future__ import annotations

import json
import os
import sys
from importlib.machinery import EXTENSION_SUFFIXES
from pathlib import Path
Expand Down Expand Up @@ -94,7 +93,7 @@ def load_numpy_core_overrides(finder: ModuleFinder, module: Module) -> None:
code_string = module.file.read_text(encoding="utf_8")
module.code = compile(
code_string.replace("dispatcher.__doc__", "dispatcher.__doc__ or ''"),
os.fspath(module.file),
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=min(finder.optimize, 1),
Expand Down Expand Up @@ -187,7 +186,13 @@ def init_numpy_blas():
code_string = code_string.replace(
"__file__", "__file__.replace('library.zip/', '')"
)
module.code = compile(code_string, os.fspath(module.file), "exec")
module.code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)


def load_numpy_core_numerictypes(_, module: Module) -> None:
Expand Down
8 changes: 7 additions & 1 deletion cx_Freeze/hooks/pyqt5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ def load_pyqt5(finder: ModuleFinder, module: Module) -> None:
# cx_Freeze patch end
"""
)
module.code = compile(code_string, module.file.as_posix(), "exec")
module.code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)


def load_pyqt5_qtwebenginecore(finder: ModuleFinder, module: Module) -> None:
Expand Down
8 changes: 7 additions & 1 deletion cx_Freeze/hooks/pyqt6/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ def load_pyqt6(finder: ModuleFinder, module: Module) -> None:
# cx_Freeze patch end
"""
)
module.code = compile(code_string, module.file.as_posix(), "exec")
module.code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)


def load_pyqt6_qtwidgets(finder: ModuleFinder, module: Module) -> None:
Expand Down
8 changes: 7 additions & 1 deletion cx_Freeze/hooks/pyside2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ def load_pyside2(finder: ModuleFinder, module: Module) -> None:
# cx_Freeze patch end
"""
)
code = compile(code_string, module.file.as_posix(), "exec")
code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)

# shiboken2 in zip_include_packages
shiboken2 = finder.include_package("shiboken2")
Expand Down
8 changes: 7 additions & 1 deletion cx_Freeze/hooks/pyside6/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ def load_pyside6(finder: ModuleFinder, module: Module) -> None:
# cx_Freeze patch end
"""
)
code = compile(code_string, module.file.as_posix(), "exec")
code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)

# shiboken6 in zip_include_packages
shiboken6 = finder.include_package("shiboken6")
Expand Down
36 changes: 21 additions & 15 deletions cx_Freeze/hooks/rns.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,54 @@

from __future__ import annotations

import os
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from cx_Freeze.finder import ModuleFinder
from cx_Freeze.module import Module


def load_rns(_, module: Module) -> None:
def load_rns(finder: ModuleFinder, module: Module) -> None:
"""Patch RNS."""
_fix_init(module)
_fix_init(finder, module)


def load_rns_cryptography(_, module: Module) -> None:
def load_rns_cryptography(finder: ModuleFinder, module: Module) -> None:
"""Patch RNS.Cryptography."""
_fix_init(module)
_fix_init(finder, module)


def load_rns_interfaces(_, module: Module) -> None:
def load_rns_interfaces(finder: ModuleFinder, module: Module) -> None:
"""Patch RNS.Interface."""
_fix_init(module)
_fix_init(finder, module)


def load_rns_interfaces_android(_, module: Module) -> None:
def load_rns_interfaces_android(finder: ModuleFinder, module: Module) -> None:
"""Patch RNS.Interface."""
_fix_init(module)
_fix_init(finder, module)


def load_rns_utilities(_, module: Module) -> None:
def load_rns_utilities(finder: ModuleFinder, module: Module) -> None:
"""Patch RNS.Utilities."""
_fix_init(module)
_fix_init(finder, module)


def load_rns_vendor(_, module: Module) -> None:
def load_rns_vendor(finder: ModuleFinder, module: Module) -> None:
"""Patch RNS.vendor."""
_fix_init(module)
_fix_init(finder, module)


def _fix_init(module: Module) -> None:
def _fix_init(finder: ModuleFinder, module: Module) -> None:
"""Patch the __init__ of the modules."""
code_string = module.file.read_text(encoding="utf_8")
code_string = code_string.replace('"/*.py"', '"/*.pyc"')
code_string = code_string.replace("'__init__.py'", '"__init__.pyc"')
code_string = code_string.replace('"__init__.py"', '"__init__.pyc"')
code_string = code_string.replace("basename(f)[:-3]", "basename(f)[:-4]")
module.code = compile(code_string, os.fspath(module.file), "exec")
module.code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)
9 changes: 7 additions & 2 deletions cx_Freeze/hooks/scipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import os
from importlib.machinery import EXTENSION_SUFFIXES
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -56,7 +55,13 @@ def load_scipy__distributor_init(finder: ModuleFinder, module: Module) -> None:
code_string = code_string.replace(
"__file__", "__file__.replace('library.zip/', '')"
)
module.code = compile(code_string, os.fspath(module.file), "exec")
module.code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)


def load_scipy_interpolate(
Expand Down
9 changes: 7 additions & 2 deletions cx_Freeze/hooks/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import os
from typing import TYPE_CHECKING

if TYPE_CHECKING:
Expand All @@ -25,4 +24,10 @@ def load_sklearn__distributor_init(
code_string = code_string.replace(
"libs_path =", "libs_path = __import__('sys').frozen_dir #"
)
module.code = compile(code_string, os.fspath(module.file), "exec")
module.code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)
9 changes: 7 additions & 2 deletions cx_Freeze/hooks/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import os
from typing import TYPE_CHECKING

from cx_Freeze._compat import IS_MINGW, IS_WINDOWS
Expand Down Expand Up @@ -42,7 +41,13 @@ def load_tensorflow(finder: ModuleFinder, module: Module) -> None:
"_current_file_location = ",
"_current_file_location = __file__.replace('library.zip/', '') #",
)
module.code = compile(code_string, os.fspath(module.file), "exec")
module.code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)

# installed version of tensorflow is a variant?
if module.distribution is None:
Expand Down
19 changes: 16 additions & 3 deletions cx_Freeze/hooks/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ def load_torch(finder: ModuleFinder, module: Module) -> None:
# patch the code to ignore CUDA_PATH_Vxx_x installation directory
code_string = module.file.read_text(encoding="utf_8")
code_string = code_string.replace("CUDA_PATH", "NO_CUDA_PATH")
module.code = compile(code_string, module.file.as_posix(), "exec")
module.code = compile(
code_string,
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)

# include the binaries (torch 2.1+)
source_bin = module.file.parent / "bin"
if source_bin.exists():
Expand Down Expand Up @@ -79,11 +86,17 @@ def load_torch(finder: ModuleFinder, module: Module) -> None:
finder.include_files(source, target)


def load_torch__dynamo_skipfiles(_, module: Module) -> None:
def load_torch__dynamo_skipfiles(finder: ModuleFinder, module: Module) -> None:
"""Patch to work with Python 3.11+."""
code_string = module.file.read_text(encoding="utf_8")
code_string = code_string.replace(
"return _strip_init_py(m.__file__)",
'return _strip_init_py(getattr(m, "__file__", ""))',
)
module.code = compile(code_string, os.fspath(module.file), "exec")
module.code = compile(
code_string,
os.fspath(module.file),
"exec",
dont_inherit=True,
optimize=finder.optimize,
)
6 changes: 4 additions & 2 deletions cx_Freeze/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ def stub_code(self) -> CodeType | None:
if source_file.exists():
imports_only = source_file.read_text(encoding="utf_8")
if imports_only:
return compile(imports_only, stub_name, "exec")
return compile(
imports_only, stub_name, "exec", dont_inherit=True
)
# search for a stub file along side the python extension module
source_file = filename.parent / stub_name
if not source_file.exists():
Expand All @@ -222,7 +224,7 @@ def stub_code(self) -> CodeType | None:
else:
imports_only = self.get_imports_from_file(source_file)
if imports_only:
return compile(imports_only, stub_name, "exec")
return compile(imports_only, stub_name, "exec", dont_inherit=True)
return None

def get_imports_from_file(self, source_file: Path) -> str | None:
Expand Down