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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions distutils/_msvccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ def compile( # noqa: C901
src = os.path.abspath(src)

if ext in self._c_extensions:
input_opt = "/Tc" + src
input_opt = "/Tc" + str(src)
elif ext in self._cpp_extensions:
input_opt = "/Tp" + src
input_opt = "/Tp" + str(src)
add_cpp_opts = True
elif ext in self._rc_extensions:
# compile .RC to .RES file
Expand Down
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
2 changes: 1 addition & 1 deletion distutils/filelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def exclude_pattern(self, pattern, anchor=1, prefix=None, is_regex=0):
pattern_re = translate_pattern(pattern, anchor, prefix, is_regex)
self.debug_print("exclude_pattern: applying regex r'%s'" % pattern_re.pattern)
for i in range(len(self.files) - 1, -1, -1):
if pattern_re.search(self.files[i]):
if pattern_re.search(str(self.files[i])):
self.debug_print(" removing " + self.files[i])
del self.files[i]
files_found = True
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