Skip to content

Commit 69f64b6

Browse files
bpo-35596: Use unchecked PYCs for the embeddable distro to avoid zipimport restrictions (GH-11465)
Also adds extra steps to the CI build for Windows on Azure Pipelines to validate that the various layouts at least execute. (cherry picked from commit 872bd2b) Co-authored-by: Steve Dower <[email protected]>
1 parent 5d1e012 commit 69f64b6

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

.azure-pipelines/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,13 @@ jobs:
134134

135135
steps:
136136
- template: ./windows-steps.yml
137+
138+
- template: ./windows-layout-steps.yml
139+
parameters:
140+
kind: nuget
141+
- template: ./windows-layout-steps.yml
142+
parameters:
143+
kind: embed
144+
- template: ./windows-layout-steps.yml
145+
parameters:
146+
kind: appx
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parameters:
2+
kind: nuget
3+
extraOpts: --precompile
4+
5+
steps:
6+
- script: .\python.bat PC\layout -vv -s "$(Build.SourcesDirectory)" -b "$(Py_OutDir)\$(arch)" -t "$(Py_IntDir)\layout-tmp-${{ parameters['kind'] }}-$(arch)" --copy "$(Py_OutDir)\layout-${{ parameters['kind'] }}-$(arch)" ${{ parameters['extraOpts'] }} --preset-${{ parameters['kind'] }} --include-tests
7+
displayName: Create ${{ parameters['kind'] }} layout
8+
9+
- script: .\python.exe -m test.pythoninfo
10+
workingDirectory: $(Py_OutDir)\layout-${{ parameters['kind'] }}-$(arch)
11+
displayName: Show layout info (${{ parameters['kind'] }})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Use unchecked PYCs for the embeddable distro to avoid zipimport
2+
restrictions.

PC/layout/main.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,18 @@ def _c(d):
240240
yield "DLLs/{}".format(ns.include_cat.name), ns.include_cat
241241

242242

243-
def _compile_one_py(src, dest, name, optimize):
243+
def _compile_one_py(src, dest, name, optimize, checked=True):
244244
import py_compile
245245

246246
if dest is not None:
247247
dest = str(dest)
248248

249+
mode = (
250+
py_compile.PycInvalidationMode.CHECKED_HASH
251+
if checked
252+
else py_compile.PycInvalidationMode.UNCHECKED_HASH
253+
)
254+
249255
try:
250256
return Path(
251257
py_compile.compile(
@@ -254,24 +260,24 @@ def _compile_one_py(src, dest, name, optimize):
254260
str(name),
255261
doraise=True,
256262
optimize=optimize,
257-
invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH,
263+
invalidation_mode=mode,
258264
)
259265
)
260266
except py_compile.PyCompileError:
261267
log_warning("Failed to compile {}", src)
262268
return None
263269

264270

265-
def _py_temp_compile(src, ns, dest_dir=None):
271+
def _py_temp_compile(src, ns, dest_dir=None, checked=True):
266272
if not ns.precompile or src not in PY_FILES or src.parent in DATA_DIRS:
267273
return None
268274

269275
dest = (dest_dir or ns.temp) / (src.stem + ".py")
270-
return _compile_one_py(src, dest.with_suffix(".pyc"), dest, optimize=2)
276+
return _compile_one_py(src, dest.with_suffix(".pyc"), dest, optimize=2, checked=checked)
271277

272278

273-
def _write_to_zip(zf, dest, src, ns):
274-
pyc = _py_temp_compile(src, ns)
279+
def _write_to_zip(zf, dest, src, ns, checked=True):
280+
pyc = _py_temp_compile(src, ns, checked=checked)
275281
if pyc:
276282
try:
277283
zf.write(str(pyc), dest.with_suffix(".pyc"))
@@ -321,7 +327,7 @@ def generate_source_files(ns):
321327
ns.temp.mkdir(parents=True, exist_ok=True)
322328
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
323329
for dest, src in get_lib_layout(ns):
324-
_write_to_zip(zf, dest, src, ns)
330+
_write_to_zip(zf, dest, src, ns, checked=False)
325331

326332
if ns.include_underpth:
327333
log_info("Generating {} in {}", PYTHON_PTH_NAME, ns.temp)

0 commit comments

Comments
 (0)