Skip to content

Commit

Permalink
Merge pull request #230 from MridulS/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 authored Mar 2, 2024
2 parents 08699b6 + eb27adf commit 9fc9671
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
14 changes: 10 additions & 4 deletions distutils/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
modules in setup scripts."""

import os
import pathlib
import warnings

# This class is really only used by the "build_ext" command, so it might
Expand All @@ -26,7 +27,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 +107,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(pathlib.Path, sources))
self.include_dirs = include_dirs or []
self.define_macros = define_macros or []
self.undef_macros = undef_macros or []
Expand Down
3 changes: 2 additions & 1 deletion distutils/tests/test_build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import textwrap
import site
import contextlib
import pathlib
import platform
import tempfile
import importlib
Expand Down Expand Up @@ -335,7 +336,7 @@ def test_get_source_files(self):
dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = self.build_ext(dist)
cmd.ensure_finalized()
assert cmd.get_source_files() == ['xxx']
assert cmd.get_source_files() == [pathlib.Path('xxx')]

def test_unicode_module_names(self):
modules = [
Expand Down
7 changes: 5 additions & 2 deletions distutils/tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import warnings
from pathlib import Path

from distutils.extension import read_setup_file, Extension

Expand Down Expand Up @@ -68,13 +69,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']
assert ext.sources == [Path('file1'), Path('file2')]
ext = Extension('name', [Path('file1'), Path('file2')])
assert ext.sources == [Path('file1'), Path('file2')]

# others arguments have defaults
for attr in (
Expand Down

0 comments on commit 9fc9671

Please sign in to comment.