Skip to content

Commit 5a0a5c1

Browse files
dimblebybostonrwalker
authored andcommitted
Clean up and fix errors
typechecking poetry.core.masonry Fix bugs causing tests to fail [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Add missing imoports to build.py; Use dict instead of toml.items.Table to store intermediate build info in build_ext_modules process [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Move distutils.extension.Extension import into type-checking block Fix mypy errors Define missing BuildFailed exception in setup.py; Rename test fixtures to avoid errors with Windows filenames being too long Add get_export_symbols() function to ExtBuilder in setup.py for Windows compat [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Try another fix to Windows build errors Try another fix to Windows build errors [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Remove get_export_symbols() function; Add stub functions to init C libs (for Windows compat) Add libraries to export_symbols (for Windows) [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Rename folders to avoid path length limit when compiling/linking; Fix typos Fix linking issues on Windows Removed generated binary files Revert changes to extended_no_setup test fixture Rename ext_pyproj_lib_build_py fixture Clarify module descriptions in README.rst files Fix 'extended is not a package' errors when attempting to build extended_no_setup test fixture Fix paths Fix call to SdistBuilder.build() Clean up rebase [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Fix exception message
1 parent 07d21cd commit 5a0a5c1

File tree

48 files changed

+139
-71
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+139
-71
lines changed

src/poetry/core/masonry/builder.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from pathlib import Path
43
from typing import TYPE_CHECKING
54

65

src/poetry/core/masonry/builders/builder.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from collections import defaultdict
99
from pathlib import Path
1010
from typing import TYPE_CHECKING
11-
from typing import Any, Collection, Optional
11+
from typing import Any
1212

1313

1414
if TYPE_CHECKING:
@@ -326,7 +326,7 @@ def convert_script_files(self) -> list[Path]:
326326
source = specification["reference"]
327327

328328
try:
329-
abs_path = self.get_source_paths(name, [source])[0]
329+
_ = self.get_source_paths(name, [source])[0]
330330
except IndexError:
331331
raise RuntimeError(
332332
f"{source} in script/module specification ({name}) is not"
@@ -337,12 +337,14 @@ def convert_script_files(self) -> list[Path]:
337337

338338
if not abs_path.exists():
339339
raise RuntimeError(
340-
f"{abs_path} in script/module specification ({name}) is not found."
340+
f"{abs_path} in script/module specification ({name}) is not"
341+
" found."
341342
)
342343

343344
if not abs_path.is_file():
344345
raise RuntimeError(
345-
f"{abs_path} in script/module specification ({name}) is not a file."
346+
f"{abs_path} in script/module specification ({name}) is not a"
347+
" file."
346348
)
347349

348350
script_files.append(abs_path)
@@ -368,7 +370,7 @@ def convert_libraries(self) -> list[tuple[str, dict[str, Any]]]:
368370
libraries = []
369371

370372
for name, build_info in self._poetry.local_config.get("libraries", {}).items():
371-
373+
build_info = dict(build_info)
372374
build_info["sources"] = [
373375
str(src)
374376
for src in self.get_source_paths(
@@ -397,7 +399,7 @@ def convert_ext_modules(self) -> list[tuple[str, dict[str, Any]]]:
397399
for name, build_info in self._poetry.local_config.get(
398400
"ext_modules", {}
399401
).items():
400-
402+
build_info = dict(build_info)
401403
build_info["sources"] = [
402404
str(src)
403405
for src in self.get_source_paths(
@@ -468,7 +470,6 @@ def get_source_paths(
468470
sources: list[Path] = []
469471

470472
for src in source_paths:
471-
472473
if not src.exists():
473474
continue
474475

src/poetry/core/masonry/builders/sdist.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
EXT_BUILDER_DEF = """\
5454
5555
56+
class BuildFailed(Exception):
57+
pass
58+
59+
5660
class ExtBuilder(build_ext):
5761
5862
def run(self):
@@ -233,11 +237,16 @@ def build_setup(self) -> bytes:
233237
ext_modules_line += constructor
234238
ext_modules_line += "]\n"
235239
imports.append("from distutils.extension import Extension")
240+
imports.append(
241+
"from distutils.errors import DistutilsPlatformError,"
242+
" DistutilsExecError, CCompilerError"
243+
)
244+
imports.append("import os")
236245
before.append(ext_modules_line)
237246
extra.append("'ext_modules': ext_modules,")
238247
imports.append("from distutils.command.build_ext import build_ext")
239248
before.append(EXT_BUILDER_DEF)
240-
extra.append("'cmdclass': {'build_ext': ExtBuilder},") # noqa: FS003
249+
extra.append("'cmdclass': {'build_ext': ExtBuilder},")
241250

242251
if self._package.python_versions != "*":
243252
python_requires = self._meta.requires_python
@@ -361,7 +370,6 @@ def find_nearest_pkg(rel_path: str) -> tuple[str, str]:
361370
return pkgdir, sorted(packages), pkg_data
362371

363372
def find_files_to_add(self, exclude_build: bool = False) -> set[BuildIncludeFile]:
364-
365373
to_add = super().find_files_to_add(exclude_build)
366374

367375
# add any additional files, starting with all LICENSE files

src/poetry/core/masonry/builders/wheel.py

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def _build(self, wheel: zipfile.ZipFile) -> None:
154154
current_path = os.getcwd()
155155
try:
156156
os.chdir(str(self._path))
157+
assert self._package.build_script is not None
157158
self._run_build_script(self._package.build_script)
158159
finally:
159160
os.chdir(current_path)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <Python.h>
2+
#if defined _WIN32 || defined __CYGWIN__
3+
extern "C" {
4+
#endif
5+
#include <../lib/mylib/include/mylib.h>
6+
#if defined _WIN32 || defined __CYGWIN__
7+
}
8+
#endif
9+
10+
static PyObject *hello(PyObject *self);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#ifndef MYLIB
3+
#define MYLIB
4+
#if defined _WIN32 || defined __CYGWIN__
5+
#ifdef __GNUC__
6+
#define MYLIB_EXPORT __attribute__ ((dllexport))
7+
#else
8+
#define MYLIB_EXPORT __declspec(dllexport)
9+
#endif
10+
#else
11+
#define MYLIB_EXPORT __attribute__ ((visibility("default")))
12+
#endif
13+
#endif
14+
15+
MYLIB_EXPORT char* mylib_say_hello();
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <Python.h>
2-
#include <mylib.h>
31
#include <extended.hpp>
42

53

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
libraries = [
2+
(
3+
"mylib",
4+
{
5+
"sources": ["lib/mylib/src/mylib.c"],
6+
"include_dirs": [
7+
"include",
8+
"lib/mylib/include",
9+
],
10+
},
11+
)
12+
]
13+
14+
15+
def build(setup_kwargs):
16+
setup_kwargs.update({"libraries": libraries})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <Python.h>
2+
#if defined _WIN32 || defined __CYGWIN__
3+
extern "C" {
4+
#endif
5+
#include <../lib/mylib/include/mylib.h>
6+
#if defined _WIN32 || defined __CYGWIN__
7+
}
8+
#endif
9+
10+
static PyObject *hello(PyObject *self);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#ifndef MYLIB
3+
#define MYLIB
4+
#if defined _WIN32 || defined __CYGWIN__
5+
#ifdef __GNUC__
6+
#define MYLIB_EXPORT __attribute__ ((dllexport))
7+
#else
8+
#define MYLIB_EXPORT __declspec(dllexport)
9+
#endif
10+
#else
11+
#define MYLIB_EXPORT __attribute__ ((visibility("default")))
12+
#endif
13+
#endif
14+
15+
MYLIB_EXPORT char* mylib_say_hello();

tests/masonry/builders/fixtures/extended_with_lib_defined_in_pyproject/src/extended/extended.cpp tests/masonry/builders/fixtures/ext_build_py_lib/src/extended/extended.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <Python.h>
2-
#include <mylib.h>
31
#include <extended.hpp>
42

53

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <Python.h>
2+
#if defined _WIN32 || defined __CYGWIN__
3+
extern "C" {
4+
#endif
5+
#include <../lib/mylib/include/mylib.h>
6+
#if defined _WIN32 || defined __CYGWIN__
7+
}
8+
#endif
9+
10+
static PyObject *hello(PyObject *self);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#ifndef MYLIB
3+
#define MYLIB
4+
#if defined _WIN32 || defined __CYGWIN__
5+
#ifdef __GNUC__
6+
#define MYLIB_EXPORT __attribute__ ((dllexport))
7+
#else
8+
#define MYLIB_EXPORT __declspec(dllexport)
9+
#endif
10+
#else
11+
#define MYLIB_EXPORT __attribute__ ((visibility("default")))
12+
#endif
13+
#endif
14+
15+
MYLIB_EXPORT char* mylib_say_hello();

tests/masonry/builders/fixtures/extended_with_lib_defined_in_pyproject/pyproject.toml tests/masonry/builders/fixtures/ext_pyproj_lib/pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ homepage = "https://python-poetry.org/"
1414

1515
[tool.poetry.libraries.mylib]
1616
sources = [
17-
"lib/mylib/*.c"
17+
"lib/mylib/src/*.c"
1818
]
1919
include_dirs = [
20-
"include"
20+
"lib/mylib/include"
2121
]
2222

2323
[tool.poetry.ext_modules."extended.extended"]
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <Python.h>
2-
#include <mylib.h>
31
#include <extended.hpp>
42

53

tests/masonry/builders/fixtures/extended_defined_in_build_py_lib_in_pyproject/include/extended.hpp

-3
This file was deleted.

tests/masonry/builders/fixtures/extended_defined_in_build_py_lib_in_pyproject/include/mylib.h

-2
This file was deleted.

tests/masonry/builders/fixtures/extended_defined_in_pyproject/README.rst

-4
This file was deleted.

tests/masonry/builders/fixtures/extended_defined_in_pyproject_lib_in_build_py/README.rst

-4
This file was deleted.

tests/masonry/builders/fixtures/extended_defined_in_pyproject_lib_in_build_py/build.py

-13
This file was deleted.

tests/masonry/builders/fixtures/extended_defined_in_pyproject_lib_in_build_py/include/extended.hpp

-3
This file was deleted.

tests/masonry/builders/fixtures/extended_defined_in_pyproject_lib_in_build_py/include/mylib.h

-2
This file was deleted.

tests/masonry/builders/fixtures/extended_with_lib_defined_in_pyproject/README.rst

-4
This file was deleted.

tests/masonry/builders/fixtures/extended_with_lib_defined_in_pyproject/include/extended.hpp

-3
This file was deleted.

tests/masonry/builders/fixtures/extended_with_lib_defined_in_pyproject/include/mylib.h

-2
This file was deleted.

tests/masonry/builders/test_complete.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ def clear_samples_dist() -> None:
5353
("dir", "has_build_script"),
5454
[
5555
("extended", True),
56-
("extended_defined_in_pyproject", False),
56+
("ext_pyproj", False),
5757
],
5858
)
5959
def test_wheel_c_extension(dir: str, has_build_script: bool) -> None:
6060
module_path = fixtures_dir / dir
6161

62-
# Perform "clean' build
62+
# Perform "clean" build
6363
setup_file = module_path / "setup.py"
6464
if setup_file.exists():
6565
setup_file.unlink()
@@ -119,11 +119,11 @@ def test_wheel_c_extension(dir: str, has_build_script: bool) -> None:
119119
reason="Disable test on Windows for Python <=3.6 and for PyPy",
120120
)
121121
def test_wheel_c_extension_with_no_setup() -> None:
122-
module_path = fixtures_dir / "extended_with_no_setup"
122+
module_path = fixtures_dir / "extended_no_setup"
123123
builder = Builder(Factory().create_poetry(module_path))
124124
builder.build(fmt="all")
125125

126-
sdist = fixtures_dir / "extended_with_no_setup" / "dist" / "extended-0.1.tar.gz"
126+
sdist = fixtures_dir / "extended_no_setup" / "dist" / "extended-0.1.tar.gz"
127127

128128
assert sdist.exists()
129129

@@ -229,9 +229,9 @@ def test_wheel_c_extension_src_layout() -> None:
229229
reason="Disable test on Windows for Python <=3.6 and for PyPy",
230230
)
231231
def test_wheel_c_cpp_extensions_defined_in_pyproject() -> None:
232-
module_path = fixtures_dir / "extended_with_lib_defined_in_pyproject"
232+
module_path = fixtures_dir / "ext_pyproj_lib"
233233

234-
# Perform "clean' build
234+
# Perform "clean" build
235235
setup_file = module_path / "setup.py"
236236
if setup_file.exists():
237237
setup_file.unlink()
@@ -245,10 +245,10 @@ def test_wheel_c_cpp_extensions_defined_in_pyproject() -> None:
245245

246246
with tarfile.open(str(sdist), "r") as tar:
247247
assert "extended-0.1/setup.py" in tar.getnames()
248-
assert "extended-0.1/lib/mylib/mylib.c" in tar.getnames()
248+
assert "extended-0.1/lib/mylib/src/mylib.c" in tar.getnames()
249249
assert "extended-0.1/src/extended/extended.cpp" in tar.getnames()
250-
assert "extended-0.1/include/mylib.h" in tar.getnames()
251250
assert "extended-0.1/include/extended.hpp" in tar.getnames()
251+
assert "extended-0.1/lib/mylib/include/mylib.h" in tar.getnames()
252252

253253
whl = list((module_path / "dist").glob("extended-0.1-cp*-cp*-*.whl"))[0]
254254

@@ -270,7 +270,7 @@ def test_wheel_c_cpp_extensions_defined_in_pyproject() -> None:
270270
re.match(
271271
f"""(?m)^\
272272
Wheel-Version: 1.0
273-
Generator: poetry {__version__}
273+
Generator: poetry-core {__version__}
274274
Root-Is-Purelib: false
275275
Tag: cp[23]_?\\d+-cp[23]_?\\d+m?u?-.+
276276
$""",
@@ -295,8 +295,8 @@ def test_wheel_c_cpp_extensions_defined_in_pyproject() -> None:
295295
@pytest.mark.parametrize(
296296
"dir",
297297
[
298-
"extended_defined_in_pyproject_lib_in_build_py",
299-
"extended_defined_in_build_py_lib_in_pyproject",
298+
"ext_build_py_lib",
299+
"ext_build_py",
300300
],
301301
)
302302
def test_extensions_defined_in_pyproject_with_build_py_raises_error(dir: str) -> None:

0 commit comments

Comments
 (0)