Skip to content

Commit 68cb6ba

Browse files
Accept an Iterable at runtime for Extension
1 parent 2517976 commit 68cb6ba

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

distutils/extension.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ class Extension:
2626
name : string
2727
the full name of the extension, including any packages -- ie.
2828
*not* a filename or pathname, but Python dotted name
29-
sources : [string | os.PathLike]
30-
list of source filenames, relative to the distribution root
31-
(where the setup script lives), in Unix form (slash-separated)
32-
for portability. Source files may be C, C++, SWIG (.i),
33-
platform-specific resource files, or whatever else is recognized
34-
by the "build_ext" command as source for a Python extension.
29+
sources : Iterable[string | os.PathLike]
30+
iterable of source filenames (except strings, which could be misinterpreted
31+
as a single filename), relative to the distribution root (where the setup
32+
script lives), in Unix form (slash-separated) for portability. Can be any
33+
non-string iterable (list, tuple, set, etc.) containing strings or
34+
PathLike objects. Source files may be C, C++, SWIG (.i), platform-specific
35+
resource files, or whatever else is recognized by the "build_ext" command
36+
as source for a Python extension.
3537
include_dirs : [string]
3638
list of directories to search for C/C++ header files (in Unix
3739
form for portability)
@@ -106,12 +108,23 @@ def __init__(
106108
):
107109
if not isinstance(name, str):
108110
raise AssertionError("'name' must be a string") # noqa: TRY004
109-
if not (
110-
isinstance(sources, list)
111-
and all(isinstance(v, (str, os.PathLike)) for v in sources)
112-
):
111+
112+
# we handle the string case first; though strings are iterable, we disallow them
113+
if isinstance(sources, str):
114+
raise AssertionError( # noqa: TRY004
115+
"'sources' must be an iterable of strings or PathLike objects, not a string"
116+
)
117+
118+
# mow we check if it's iterable and contains valid types
119+
try:
120+
sources = list(sources) # convert to list for consistency
121+
if not all(isinstance(v, (str, os.PathLike)) for v in sources):
122+
raise AssertionError(
123+
"All elements in 'sources' must be strings or PathLike objects"
124+
)
125+
except TypeError:
113126
raise AssertionError(
114-
"'sources' must be a list of strings or PathLike objects."
127+
"'sources' must be an iterable of strings or PathLike objects"
115128
)
116129

117130
self.name = name

0 commit comments

Comments
 (0)