Skip to content

Commit 34ca4d9

Browse files
authored
Merge branch 'main' into version-readme-cards
2 parents aa6033f + 1658f65 commit 34ca4d9

4 files changed

Lines changed: 23 additions & 13 deletions

File tree

kernel-builder/src/pyproject/templates/torch/noarch/setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env python
22

3-
from typing import Any
4-
from pathlib import Path
53
import shutil
64
import sys
5+
from pathlib import Path
6+
from typing import Any
77

88
from setuptools import setup
99
from setuptools.command.build import build
@@ -73,9 +73,10 @@ def build_backend(
7373
raise FileNotFoundError(f"torch-ext/{module_name} does not exist")
7474
for item in torch_ext_dir.rglob("*"):
7575
if item.is_file():
76-
if item.suffix == ".pyc" or item.parent.name == "__pycache__":
77-
continue
7876
rel_path = item.relative_to(torch_ext_dir)
77+
# Skip Python bytecode.
78+
if item.suffix == ".pyc" or "__pycache__" in rel_path.parts:
79+
continue
7980
dest = variant_dir / rel_path
8081
dest.parent.mkdir(parents=True, exist_ok=True)
8182
shutil.copy2(item, dest)

kernels-data/src/digest.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ impl Digest {
3030
// hashes. It is protected by the detached signature.
3131
|| entry.path().file_name().and_then(|f| f.to_str()) == Some("metadata.json")
3232
|| entry.path().file_name().and_then(|f| f.to_str()) == Some("metadata.json.sigstore")
33-
// Python likes to create .pyc files in __pycache__/ directories.
33+
// Exclude Python bytecode, these files are generated by the
34+
// interpreter since they are never included in builds or downloads.
3435
|| entry.path().components().any(|c| c.as_os_str() == "__pycache__")
36+
|| entry.path().extension().and_then(|e| e.to_str()) == Some("pyc")
3537
{
3638
continue;
3739
}
@@ -130,15 +132,13 @@ mod tests {
130132
fs::write(dir.path().join("metadata.json"), b"{}")?;
131133
fs::write(dir.path().join("metadata.json.sigstore"), b"sig")?;
132134
fs::write(pycache_dir.join("mod.cpython-311.pyc"), b"bytecode")?;
133-
// A .pyc outside __pycache__/ must be included — it was not generated
134-
// locally by Python and should be covered by the digest.
135135
fs::write(dir.path().join("payload.pyc"), b"smuggled")?;
136136

137137
let digest = Digest::hash_variant(DigestAlgorithm::SHA256, dir.path())?;
138138

139-
assert_eq!(digest.files().len(), 2);
139+
assert_eq!(digest.files().len(), 1);
140140
assert!(digest.files().contains_key("_extension.so"));
141-
assert!(digest.files().contains_key("payload.pyc"));
141+
assert!(!digest.files().contains_key("payload.pyc"));
142142
assert!(!digest.files().contains_key("metadata.json"));
143143
assert!(!digest.files().contains_key("metadata.json.sigstore"));
144144
assert!(

kernels/src/kernels/lockfile.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ def get_kernel_locks(repo_id: str, version_spec: int) -> KernelLock:
7272
if sibling.blob_id is None:
7373
raise ValueError(f"Cannot get blob ID for {sibling.rfilename}")
7474

75-
if sibling.rfilename.endswith(".pyc") or "__pycache__" in sibling.rfilename:
75+
# Exclude Python bytecode. If bytecode exists, it is generated
76+
# by the interpreter, since we exclude bytecode from builds
77+
# and downloads.
78+
if sibling.rfilename.endswith(".pyc") or "__pycache__" in sibling.rfilename.split("/"):
7679
continue
7780

7881
path = Path(sibling.rfilename)

kernels/src/kernels/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636

3737
KNOWN_BACKENDS = {"cpu", "cuda", "metal", "neuron", "rocm", "xpu", "npu"}
3838

39+
# Exclude patter for bytecode. These are not included in kernel builds,
40+
# but builds not done using kernel-builder might accidentally include
41+
# bytcode. So these patterns are used to ensure that they are never
42+
# downloaded.
43+
_BYTECODE_IGNORE_PATTERNS = ["*.pyc", "**/__pycache__/**"]
44+
3945

4046
def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) -> None:
4147
"""Check whether a kernel repository is trusted.
@@ -302,7 +308,7 @@ def install_kernel(
302308
_validate_variant_dependencies(Path(metadata_path).parent)
303309

304310
allow_patterns = [f"build/{variant.variant_str}/*"]
305-
ignore_patterns = ["*.pyc", "**/__pycache__/**"]
311+
ignore_patterns = _BYTECODE_IGNORE_PATTERNS
306312

307313
repo_path = Path(
308314
str(
@@ -368,7 +374,7 @@ def _resolve_local_variant_path(
368374
)
369375

370376
allow_patterns = [f"build/{variant.variant_str}/*"]
371-
ignore_patterns = ["*.pyc", "**/__pycache__/**"]
377+
ignore_patterns = _BYTECODE_IGNORE_PATTERNS
372378
repo_path = Path(
373379
str(
374380
api.snapshot_download(
@@ -420,7 +426,7 @@ def install_kernel_all_variants(
420426
repo_id,
421427
repo_type="kernel",
422428
allow_patterns="build/*",
423-
ignore_patterns=["*.pyc", "**/__pycache__/**"],
429+
ignore_patterns=_BYTECODE_IGNORE_PATTERNS,
424430
cache_dir=CACHE_DIR,
425431
revision=revision,
426432
local_files_only=local_files_only,

0 commit comments

Comments
 (0)