Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept any non-string iterable for distutils.Extension's sources #311

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions distutils/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ class Extension:
name : string
the full name of the extension, including any packages -- ie.
*not* a filename or pathname, but Python dotted name
sources : [string | os.PathLike]
list of source filenames, relative to the distribution root
(where the setup script lives), in Unix form (slash-separated)
for portability. Source files may be C, C++, SWIG (.i),
platform-specific resource files, or whatever else is recognized
by the "build_ext" command as source for a Python extension.
sources : Iterable[string | os.PathLike]
iterable of source filenames (except strings, which could be misinterpreted
as a single filename), relative to the distribution root (where the setup
script lives), in Unix form (slash-separated) for portability. Can be any
non-string iterable (list, tuple, set, etc.) containing strings or
PathLike objects. Source files may be C, C++, SWIG (.i), platform-specific
resource files, or whatever else is recognized by the "build_ext" command
as source for a Python extension.
include_dirs : [string]
list of directories to search for C/C++ header files (in Unix
form for portability)
Expand Down Expand Up @@ -106,12 +108,23 @@ def __init__(
):
if not isinstance(name, str):
raise AssertionError("'name' must be a string") # noqa: TRY004
if not (
isinstance(sources, list)
and all(isinstance(v, (str, os.PathLike)) for v in sources)
):

# we handle the string case first; though strings are iterable, we disallow them
if isinstance(sources, str):
raise AssertionError( # noqa: TRY004
"'sources' must be an iterable of strings or PathLike objects, not a string"
)

# mow we check if it's iterable and contains valid types
agriyakhetarpal marked this conversation as resolved.
Show resolved Hide resolved
try:
sources = list(sources) # convert to list for consistency
if not all(isinstance(v, (str, os.PathLike)) for v in sources):
raise AssertionError(
"All elements in 'sources' must be strings or PathLike objects"
)
agriyakhetarpal marked this conversation as resolved.
Show resolved Hide resolved
except TypeError:
raise AssertionError(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question for the distutils maintainers: I can't help but feel that AssertionError is misused here, shouldn't these all be TypeError ? (it's noqa'd but not explained)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the same suggestion! :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Peeking at the history, these used to be actual assert statements (7d70ca7), so my guess is that the AssertionErrors were kept for backwards compatibility with code that might try to catch these errors.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, what I infer from the commit message is that that they were added because in-line assert statements are completely removed from the bytecode if used with -O

"'sources' must be a list of strings or PathLike objects."
"'sources' must be an iterable of strings or PathLike objects"
)

self.name = name
Expand Down
12 changes: 11 additions & 1 deletion distutils/tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_extension_init(self):
assert ext.name == 'name'

# the second argument, which is the list of files, must
# be a list of strings or PathLike objects
# be a list of strings or PathLike objects, and not a string
with pytest.raises(AssertionError):
Extension('name', 'file')
with pytest.raises(AssertionError):
Expand All @@ -79,6 +79,16 @@ def test_extension_init(self):
ext = Extension('name', [pathlib.Path('file1'), pathlib.Path('file2')])
assert ext.sources == ['file1', 'file2']

# any non-string iterable of strings or PathLike objects should work
ext = Extension('name', ('file1', 'file2')) # tuple
assert ext.sources == ['file1', 'file2']
ext = Extension('name', {'file1', 'file2'}) # set
assert sorted(ext.sources) == ['file1', 'file2']
ext = Extension('name', iter(['file1', 'file2'])) # iterator
assert ext.sources == ['file1', 'file2']
ext = Extension('name', [pathlib.Path('file1'), 'file2']) # mixed types
assert ext.sources == ['file1', 'file2']

# others arguments have defaults
for attr in (
'include_dirs',
Expand Down