Skip to content

Commit d75d6e1

Browse files
DinoVfacebook-github-bot
authored andcommitted
Add option to run compiled code in strict module
Summary: Currently there's no good way to run a one-off script in a strict module inside of Cinder, where doing so with static Python will unlock extra optimization opportunities. This enables that with: `./python -X jit -OO -m compiler --strict --static Tools/benchmarks/richards_static.py` Reviewed By: carljm Differential Revision: D28270863 fbshipit-source-id: f679a0e
1 parent 0a43a95 commit d75d6e1

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Lib/compiler/__main__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
import marshal
66
import os
77
import re
8+
import sys
89
from dis import dis
910

1011
from . import pycodegen, static
1112

13+
try:
14+
# pyre-ignore[21]: Could not find a module corresponding to import `cinder`.
15+
from cinder import StrictModule
16+
except ImportError:
17+
StrictModule = None
18+
1219
# https://www.python.org/dev/peps/pep-0263/
1320
coding_re = re.compile(rb"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)")
1421

@@ -57,6 +64,7 @@ def open_with_coding(fname):
5764
default=-1,
5865
help="set optimization level to compile with",
5966
)
67+
argparser.add_argument("--strict", action="store_true", help="run in strict module")
6068
args = argparser.parse_args()
6169

6270
with open_with_coding(args.input) as f:
@@ -93,4 +101,11 @@ def open_with_coding(fname):
93101
f.write(hdr)
94102
marshal.dump(codeobj, f)
95103
else:
96-
exec(codeobj)
104+
if args.strict and StrictModule is not None:
105+
d = {}
106+
mod = StrictModule(d, False)
107+
d["__name__"] = "__main__"
108+
sys.modules["__main__"] = mod
109+
exec(codeobj, d, d)
110+
else:
111+
exec(codeobj)

0 commit comments

Comments
 (0)