Skip to content

Commit

Permalink
Merge pull request #237 from pypa/pathlike_ext
Browse files Browse the repository at this point in the history
ENH: Extension should be able to accept PathLike sources objects
  • Loading branch information
jaraco committed Aug 2, 2024
2 parents 9bebfda + 3b86d4b commit 127371a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit 127371a

Please sign in to comment.