Daankular commited on
Commit
63cc737
·
1 Parent(s): 59014df

Fix basicsr Python 3.13 exec()+locals() crash at runtime startup

Browse files

basicsr's setup.py get_version() does exec(compile(f.read(),...)) then
locals()['__version__'], which is broken on Python 3.13 — exec() no
longer populates the caller's locals() dict.

Fix: clone BasicSR, patch setup.py to use an explicit namespace dict
(_ns={};exec(...,_ns);return _ns['__version__']), then install from
local path. Dependent face-enhancement packages (realesrgan, gfpgan,
facexlib, face-alignment) are installed in a separate step after.

Files changed (1) hide show
  1. app.py +45 -3
app.py CHANGED
@@ -32,13 +32,17 @@ _RUNTIME_PKG_MARKER = Path("/tmp/.runtime_pkgs_installed")
32
  # 1. Packages requiring --no-build-isolation
33
  # - hmr2/skel: declare `chumpy @ git+...` direct-ref dep; chumpy's setup.py does
34
  # `from pip._internal.req import parse_requirements` which fails in isolated builds.
35
- # - basicsr/realesrgan/gfpgan/facexlib/face-alignment: setup.py imports torch to read
36
- # __version__, which fails when torch is not yet present in the isolated build env.
37
  # Do NOT list chumpy explicitly — hmr2 pulls it as a transitive dep.
 
 
 
38
  _NO_ISOLATION_PACKAGES = [
39
  "hmr2 @ git+https://github.com/shubham-goel/4D-Humans.git@efe18deff163b29dff87ddbd575fa29b716a356c",
40
  "skel @ git+https://github.com/MarilynKeller/SKEL.git@c32cf16581295bff19399379efe5b776d707cd95",
41
- "basicsr",
 
 
 
42
  "realesrgan",
43
  "gfpgan",
44
  "facexlib",
@@ -74,6 +78,44 @@ def _install_runtime_packages():
74
  [sys.executable, "-m", "pip", "install", "--quiet", "--upgrade",
75
  "numpy>=2", "moderngl-window>=3.0.0"], check=True,
76
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  subprocess.run(
78
  [sys.executable, "-m", "pip", "install", "--quiet", "--no-deps"]
79
  + _NO_DEPS_PACKAGES, check=True,
 
32
  # 1. Packages requiring --no-build-isolation
33
  # - hmr2/skel: declare `chumpy @ git+...` direct-ref dep; chumpy's setup.py does
34
  # `from pip._internal.req import parse_requirements` which fails in isolated builds.
 
 
35
  # Do NOT list chumpy explicitly — hmr2 pulls it as a transitive dep.
36
+ # NOTE: basicsr/realesrgan/gfpgan/facexlib/face-alignment are NOT listed here —
37
+ # basicsr's setup.py get_version() uses exec()+locals() which is broken on Python 3.13
38
+ # (exec() no longer populates caller's locals()). Handled separately below.
39
  _NO_ISOLATION_PACKAGES = [
40
  "hmr2 @ git+https://github.com/shubham-goel/4D-Humans.git@efe18deff163b29dff87ddbd575fa29b716a356c",
41
  "skel @ git+https://github.com/MarilynKeller/SKEL.git@c32cf16581295bff19399379efe5b776d707cd95",
42
+ ]
43
+
44
+ # Face enhancement packages that depend on basicsr
45
+ _FACE_ENHANCE_PACKAGES = [
46
  "realesrgan",
47
  "gfpgan",
48
  "facexlib",
 
78
  [sys.executable, "-m", "pip", "install", "--quiet", "--upgrade",
79
  "numpy>=2", "moderngl-window>=3.0.0"], check=True,
80
  )
81
+ # basicsr Python 3.13 fix: setup.py get_version() uses exec(compile(...))+locals()
82
+ # which is broken on Python 3.13 — exec() no longer populates the caller's locals().
83
+ # Fix: clone the repo, patch setup.py to use an explicit namespace dict, then install.
84
+ _basicsr_src = Path("/tmp/basicsr-build")
85
+ if not _basicsr_src.exists():
86
+ subprocess.run(
87
+ ["git", "clone", "--depth=1",
88
+ "https://github.com/XPixelGroup/BasicSR.git", str(_basicsr_src)],
89
+ check=True,
90
+ )
91
+ # Patch get_version() in setup.py: replace locals()['__version__'] pattern
92
+ _basicsr_setup = _basicsr_src / "setup.py"
93
+ _setup_src = _basicsr_setup.read_text(encoding="utf-8")
94
+ # The original pattern: exec(compile(f.read(), ...)); return locals()['__version__']
95
+ # Replace with an explicit namespace so it works on Python 3.13
96
+ _patched = _setup_src.replace(
97
+ "exec(compile(f.read(), version_file, 'exec'))\n return locals()['__version__']",
98
+ "_ns = {}\n exec(compile(f.read(), version_file, 'exec'), _ns)\n return _ns['__version__']",
99
+ )
100
+ if _patched == _setup_src:
101
+ # Fallback: patch any exec()+locals() version-reading pattern more broadly
102
+ import re as _re
103
+ _patched = _re.sub(
104
+ r"exec\(compile\(([^)]+),\s*'exec'\)\)\s*\n(\s*)return locals\(\)\['__version__'\]",
105
+ r"_ns = {}\n exec(compile(\1, 'exec'), _ns)\n\2return _ns['__version__']",
106
+ _setup_src,
107
+ )
108
+ _basicsr_setup.write_text(_patched, encoding="utf-8")
109
+ subprocess.run(
110
+ [sys.executable, "-m", "pip", "install", "--quiet", "--no-build-isolation",
111
+ str(_basicsr_src)],
112
+ check=True,
113
+ )
114
+ # Install face enhancement packages that depend on basicsr
115
+ subprocess.run(
116
+ [sys.executable, "-m", "pip", "install", "--quiet", "--no-build-isolation"]
117
+ + _FACE_ENHANCE_PACKAGES, check=True,
118
+ )
119
  subprocess.run(
120
  [sys.executable, "-m", "pip", "install", "--quiet", "--no-deps"]
121
  + _NO_DEPS_PACKAGES, check=True,