Skip to content

Commit 46d5dd9

Browse files
author
Subbarao Garlapati
committed
Fix Static Python benchmarks in OSS builds
- Rename opcodes version directories from 3.14 to 3_14 so they can be imported as namespace packages in editable installs. - Use importlib.import_module fallback instead of file-path manipulation. - Fix benchmark imports to work in both buck (relative) and OSS (absolute). - Add .pys stubs to package_data for non-editable installs.
1 parent 79cb0d9 commit 46d5dd9

9 files changed

Lines changed: 25 additions & 8 deletions

File tree

cinderx/Interpreter/regen-opcodes-314.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
set -e
55

66
root=$(sl root)
7-
buck2 run fbcode//cinderx/PythonLib/opcodes:assign_opcode_numbers314 -- $root/fbcode/cinderx/PythonLib/opcodes/3.14/opcode.py
7+
buck2 run fbcode//cinderx/PythonLib/opcodes:assign_opcode_numbers314 -- $root/fbcode/cinderx/PythonLib/opcodes/3_14/opcode.py
88

99
buck2 run :gen-opcodes-314 -- $root/fbcode/cinderx/Interpreter/3.14/cinder_opcode_ids.h

cinderx/Interpreter/regen-opcodes-315.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
set -e
55

66
root=$(sl root)
7-
buck2 run fbcode//cinderx/PythonLib/opcodes:assign_opcode_numbers314 -- $root/fbcode/cinderx/PythonLib/opcodes/3.14/opcode.py
7+
buck2 run fbcode//cinderx/PythonLib/opcodes:assign_opcode_numbers314 -- $root/fbcode/cinderx/PythonLib/opcodes/3_14/opcode.py
88

99
buck2 run :gen-opcodes-314 -- $root/fbcode/cinderx/Interpreter/3.14/cinder_opcode_ids.h

cinderx/PythonLib/cinderx/compiler/opcodes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@
3333
_inline_cache_entries = dict(_inline_cache_entries)
3434
_specializations = dict(_specializations)
3535

36-
from cinderx import opcode as cinderx_opcode
36+
try:
37+
from cinderx import opcode as cinderx_opcode
38+
except ImportError:
39+
# Editable install: opcode.py hasn't been copied by build_py.
40+
import importlib
41+
42+
_ver_name = f"{sys.version_info.major}_{sys.version_info.minor}"
43+
cinderx_opcode = importlib.import_module(f"opcodes.{_ver_name}.opcode")
3744

3845
cinderx_opcode.init(
3946
STATIC_OPNAMES if sys.version_info >= (3, 14) else opname,

cinderx/benchmarks/richards_static.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
import cinderx.jit
1010

11-
from .richards_static_lib import Richards
11+
try:
12+
from .richards_static_lib import Richards # buck
13+
except ImportError:
14+
from richards_static_lib import Richards # OSS standalone script
1215

1316

1417
if __name__ == "__main__":

cinderx/benchmarks/richards_static_basic.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
import cinderx.jit
1010

11-
from .richards_static_basic_lib import Richards
11+
try:
12+
from .richards_static_basic_lib import Richards # buck
13+
except ImportError:
14+
from richards_static_basic_lib import Richards # OSS standalone script
1215

1316

1417
if __name__ == "__main__":

setup.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,13 @@ def run(self) -> None:
319319

320320
super().run()
321321

322-
# Copy opcodes/${PY_VERSION}/opcode.py to cinderx/opcode.py.
322+
# Copy opcodes/${PY_VERSION_UNDERSCORED}/opcode.py to cinderx/opcode.py.
323323
py_version = compute_py_version()
324324
out_path = self.get_module_outfile(self.build_lib, ["cinderx"], "opcode")
325325
os.makedirs(os.path.dirname(out_path), exist_ok=True)
326+
ver_dir = f"{py_version.replace('.', '_')}"
326327
self.copy_file(
327-
os.path.join(PYTHON_LIB_DIR, f"opcodes/{py_version}/opcode.py"),
328+
os.path.join(PYTHON_LIB_DIR, f"opcodes/{ver_dir}/opcode.py"),
328329
out_path,
329330
preserve_mode=False,
330331
)
@@ -488,7 +489,10 @@ def main() -> None:
488489
},
489490
packages=find_packages(where=PYTHON_LIB_DIR, exclude=["test_cinderx*"]),
490491
package_dir={"": PYTHON_LIB_DIR},
491-
package_data={"cinderx": [".dev_build"]},
492+
package_data={
493+
"cinderx": [".dev_build"],
494+
"cinderx.compiler.strict": ["stubs/**/*.pys"],
495+
},
492496
)
493497

494498

0 commit comments

Comments
 (0)