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

ENH: Extension should be able to accept PathLike sources objects #237

Merged
merged 8 commits into from
Jul 20, 2024
13 changes: 9 additions & 4 deletions distutils/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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]
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),
Expand Down Expand Up @@ -106,11 +106,16 @@ def __init__(
):
if not isinstance(name, str):
raise AssertionError("'name' must be a string")
if not (isinstance(sources, list) and all(isinstance(v, str) for v in sources)):
raise AssertionError("'sources' must be a list of strings")
if not (
isinstance(sources, list)
and all(isinstance(v, (str, os.PathLike)) for v in sources)
):
raise AssertionError(
"'sources' must be a list of strings or PathLike objects."
)

self.name = name
self.sources = sources
self.sources = list(map(os.fspath, sources))
self.include_dirs = include_dirs or []
self.define_macros = define_macros or []
self.undef_macros = undef_macros or []
Expand Down
6 changes: 5 additions & 1 deletion distutils/tests/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Tests for distutils.extension."""

import os
import pathlib
import warnings

from distutils.extension import Extension, read_setup_file

import pytest
Expand Down Expand Up @@ -68,13 +70,15 @@ def test_extension_init(self):
assert ext.name == 'name'

# the second argument, which is the list of files, must
# be a list of strings
# be a list of strings or PathLike objects
with pytest.raises(AssertionError):
Extension('name', 'file')
with pytest.raises(AssertionError):
Extension('name', ['file', 1])
ext = Extension('name', ['file1', 'file2'])
assert ext.sources == ['file1', 'file2']
ext = Extension('name', [pathlib.Path('file1'), pathlib.Path('file2')])
assert ext.sources == ['file1', 'file2']

# others arguments have defaults
for attr in (
Expand Down
Loading