Skip to content

Commit 1adbe60

Browse files
Restrict importlib.machinery.SourceLoader.source_to_code and subclasses to only take bytes instead of Buffer as path.
These also route to `compile()`.
1 parent 81eff74 commit 1adbe60

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

stdlib/_frozen_importlib_external.pyi

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,14 @@ class SourceLoader(_LoaderBasics):
9999
def set_data(self, path: str, data: bytes) -> None: ...
100100
def get_source(self, fullname: str) -> str | None: ...
101101
def path_stats(self, path: str) -> Mapping[str, Any]: ...
102-
def source_to_code(
103-
self, data: ReadableBuffer | str | _ast.Module | _ast.Expression | _ast.Interactive, path: ReadableBuffer | StrPath
104-
) -> types.CodeType: ...
102+
if sys.version_info >= (3, 12):
103+
def source_to_code(
104+
self, data: ReadableBuffer | str | _ast.Module | _ast.Expression | _ast.Interactive, path: bytes | StrPath
105+
) -> types.CodeType: ...
106+
else:
107+
def source_to_code(
108+
self, data: ReadableBuffer | str | _ast.Module | _ast.Expression | _ast.Interactive, path: ReadableBuffer | StrPath
109+
) -> types.CodeType: ...
105110
def get_code(self, fullname: str) -> types.CodeType | None: ...
106111

107112
class FileLoader:
@@ -123,13 +128,22 @@ class FileLoader:
123128
class SourceFileLoader(importlib.abc.FileLoader, FileLoader, importlib.abc.SourceLoader, SourceLoader): # type: ignore[misc] # incompatible method arguments in base classes
124129
def set_data(self, path: str, data: ReadableBuffer, *, _mode: int = 0o666) -> None: ...
125130
def path_stats(self, path: str) -> Mapping[str, Any]: ...
126-
def source_to_code( # type: ignore[override] # incompatible with InspectLoader.source_to_code
127-
self,
128-
data: ReadableBuffer | str | _ast.Module | _ast.Expression | _ast.Interactive,
129-
path: ReadableBuffer | StrPath,
130-
*,
131-
_optimize: int = -1,
132-
) -> types.CodeType: ...
131+
if sys.version_info >= (3, 12):
132+
def source_to_code( # type: ignore[override] # incompatible with InspectLoader.source_to_code
133+
self,
134+
data: ReadableBuffer | str | _ast.Module | _ast.Expression | _ast.Interactive,
135+
path: bytes | StrPath,
136+
*,
137+
_optimize: int = -1,
138+
) -> types.CodeType: ...
139+
else:
140+
def source_to_code( # type: ignore[override] # incompatible with InspectLoader.source_to_code
141+
self,
142+
data: ReadableBuffer | str | _ast.Module | _ast.Expression | _ast.Interactive,
143+
path: ReadableBuffer | StrPath,
144+
*,
145+
_optimize: int = -1,
146+
) -> types.CodeType: ...
133147

134148
class SourcelessFileLoader(importlib.abc.FileLoader, FileLoader, _LoaderBasics):
135149
def get_code(self, fullname: str) -> types.CodeType | None: ...

0 commit comments

Comments
 (0)