Skip to content

Commit 127371a

Browse files
committed
Merge pull request pypa/distutils#237 from pypa/pathlike_ext
ENH: Extension should be able to accept PathLike sources objects
2 parents 9bebfda + 3b86d4b commit 127371a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

distutils/extension.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ 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]
29+
sources : [string | os.PathLike]
3030
list of source filenames, relative to the distribution root
3131
(where the setup script lives), in Unix form (slash-separated)
3232
for portability. Source files may be C, C++, SWIG (.i),
@@ -106,11 +106,16 @@ def __init__(
106106
):
107107
if not isinstance(name, str):
108108
raise AssertionError("'name' must be a string")
109-
if not (isinstance(sources, list) and all(isinstance(v, str) for v in sources)):
110-
raise AssertionError("'sources' must be a list of strings")
109+
if not (
110+
isinstance(sources, list)
111+
and all(isinstance(v, (str, os.PathLike)) for v in sources)
112+
):
113+
raise AssertionError(
114+
"'sources' must be a list of strings or PathLike objects."
115+
)
111116

112117
self.name = name
113-
self.sources = sources
118+
self.sources = list(map(os.fspath, sources))
114119
self.include_dirs = include_dirs or []
115120
self.define_macros = define_macros or []
116121
self.undef_macros = undef_macros or []

distutils/tests/test_extension.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Tests for distutils.extension."""
22

33
import os
4+
import pathlib
45
import warnings
6+
57
from distutils.extension import Extension, read_setup_file
68

79
import pytest
@@ -68,13 +70,15 @@ def test_extension_init(self):
6870
assert ext.name == 'name'
6971

7072
# the second argument, which is the list of files, must
71-
# be a list of strings
73+
# be a list of strings or PathLike objects
7274
with pytest.raises(AssertionError):
7375
Extension('name', 'file')
7476
with pytest.raises(AssertionError):
7577
Extension('name', ['file', 1])
7678
ext = Extension('name', ['file1', 'file2'])
7779
assert ext.sources == ['file1', 'file2']
80+
ext = Extension('name', [pathlib.Path('file1'), pathlib.Path('file2')])
81+
assert ext.sources == ['file1', 'file2']
7882

7983
# others arguments have defaults
8084
for attr in (

0 commit comments

Comments
 (0)