-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathtest_files.py
More file actions
471 lines (381 loc) · 14.7 KB
/
Copy pathtest_files.py
File metadata and controls
471 lines (381 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
from __future__ import annotations
import contextlib
import importlib
import os
import re
import shutil
import subprocess
import sys
import sysconfig
import tarfile
import zipfile
from collections.abc import Generator
from pathlib import Path
import pytest
# These tests must be run explicitly
DIR = Path(__file__).parent.resolve()
MAIN_DIR = DIR.parent.parent
FILENAME_VERSION = re.compile(r"[-_]((\d+\.\d+\.\d+)(?:[a-z]+\d*)?)(?:-|\.tar\.gz$)")
# Newer pytest has global path setting, but keeping old pytest for now
sys.path.append(str(MAIN_DIR / "tools"))
from make_global import get_global
HAS_UV = shutil.which("uv") is not None
UV_ARGS = ["--installer=uv"] if HAS_UV else []
PKGCONFIG = """\
prefix=${{pcfiledir}}/../../
includedir=${{prefix}}/include
srcdir=${{prefix}}/share/pybind11/src
Name: pybind11
Description: Seamless operability between C++11 and Python
Version: {VERSION}
Cflags: -I${{includedir}}
"""
main_headers = {
"include/pybind11/attr.h",
"include/pybind11/buffer_info.h",
"include/pybind11/cast.h",
"include/pybind11/chrono.h",
"include/pybind11/common.h",
"include/pybind11/complex.h",
"include/pybind11/critical_section.h",
"include/pybind11/eigen.h",
"include/pybind11/embed.h",
"include/pybind11/eval.h",
"include/pybind11/functional.h",
"include/pybind11/gil.h",
"include/pybind11/gil_safe_call_once.h",
"include/pybind11/gil_simple.h",
"include/pybind11/iostream.h",
"include/pybind11/native_enum.h",
"include/pybind11/numpy.h",
"include/pybind11/operators.h",
"include/pybind11/options.h",
"include/pybind11/pybind11-inl.h",
"include/pybind11/pybind11.h",
"include/pybind11/pytypes-inl.h",
"include/pybind11/pytypes.h",
"include/pybind11/subinterpreter.h",
"include/pybind11/stl.h",
"include/pybind11/stl_bind.h",
"include/pybind11/trampoline_self_life_support.h",
"include/pybind11/type_caster_pyobject_ptr.h",
"include/pybind11/typing.h",
"include/pybind11/warnings.h",
}
conduit_headers = {
"include/pybind11/conduit/README.txt",
"include/pybind11/conduit/pybind11_conduit_v1.h",
"include/pybind11/conduit/pybind11_platform_abi_id.h",
"include/pybind11/conduit/wrap_include_python_h.h",
}
detail_headers = {
"include/pybind11/detail/argument_vector.h",
"include/pybind11/detail/class-inl.h",
"include/pybind11/detail/class.h",
"include/pybind11/detail/common-inl.h",
"include/pybind11/detail/common.h",
"include/pybind11/detail/cpp_conduit.h",
"include/pybind11/detail/descr.h",
"include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h",
"include/pybind11/detail/function_record_pyobject.h",
"include/pybind11/detail/function_ref.h",
"include/pybind11/detail/holder_caster_foreign_helpers.h",
"include/pybind11/detail/init.h",
"include/pybind11/detail/internals-inl.h",
"include/pybind11/detail/internals.h",
"include/pybind11/detail/native_enum_data.h",
"include/pybind11/detail/pybind11_namespace_macros.h",
"include/pybind11/detail/struct_smart_holder.h",
"include/pybind11/detail/type_caster_base-inl.h",
"include/pybind11/detail/type_caster_base.h",
"include/pybind11/detail/typeid.h",
"include/pybind11/detail/using_smart_holder.h",
"include/pybind11/detail/value_and_holder.h",
"include/pybind11/detail/exception_translation-inl.h",
"include/pybind11/detail/exception_translation.h",
}
eigen_headers = {
"include/pybind11/eigen/common.h",
"include/pybind11/eigen/matrix.h",
"include/pybind11/eigen/tensor.h",
}
stl_headers = {
"include/pybind11/stl/filesystem.h",
}
cmake_files = {
"share/cmake/pybind11/FindPythonLibsNew.cmake",
"share/cmake/pybind11/pybind11Common.cmake",
"share/cmake/pybind11/pybind11Config.cmake",
"share/cmake/pybind11/pybind11ConfigVersion.cmake",
"share/cmake/pybind11/pybind11GuessPythonExtSuffix.cmake",
"share/cmake/pybind11/pybind11NewTools.cmake",
"share/cmake/pybind11/pybind11Targets.cmake",
"share/cmake/pybind11/pybind11Tools.cmake",
}
pkgconfig_files = {
"share/pkgconfig/pybind11.pc",
}
sdist_src_files = {
"src/class.cpp",
"src/common.cpp",
"src/exception_translation.cpp",
"src/internals.cpp",
"src/type_caster_base.cpp",
"src/pybind11.cpp",
"src/pybind11_combined.cpp",
"src/pytypes.cpp",
}
src_files = {f"share/pybind11/{n}" for n in sdist_src_files}
py_files = {
"__init__.py",
"__main__.py",
"_version.py",
"commands.py",
"py.typed",
"setup_helpers.py",
"share/__init__.py",
"share/pkgconfig/__init__.py",
}
headers = main_headers | conduit_headers | detail_headers | eigen_headers | stl_headers
generated_files = cmake_files | pkgconfig_files | src_files
all_files = headers | generated_files | py_files
sdist_files = {
"pyproject.toml",
"LICENSE",
"README.rst",
"PKG-INFO",
"SECURITY.md",
}
@contextlib.contextmanager
def preserve_file(filename: Path) -> Generator[str, None, None]:
old_stat = filename.stat()
old_file = filename.read_text(encoding="utf-8")
try:
yield old_file
finally:
filename.write_text(old_file, encoding="utf-8")
os.utime(filename, (old_stat.st_atime, old_stat.st_mtime))
@contextlib.contextmanager
def build_global() -> Generator[None, None, None]:
"""
Build global SDist and wheel.
"""
pyproject = MAIN_DIR / "pyproject.toml"
with preserve_file(pyproject):
newer_txt = get_global()
pyproject.write_text(newer_txt, encoding="utf-8")
yield
def read_tz_file(tar: tarfile.TarFile, name: str) -> bytes:
start = tar.getnames()[0].split("/")[0] + "/"
inner_file = tar.extractfile(tar.getmember(f"{start}{name}"))
assert inner_file
with contextlib.closing(inner_file) as f:
return f.read()
def normalize_line_endings(value: bytes) -> bytes:
return value.replace(os.linesep.encode("utf-8"), b"\n")
def test_build_sdist(monkeypatch, tmpdir):
monkeypatch.chdir(MAIN_DIR)
subprocess.run(
[sys.executable, "-m", "build", "--sdist", f"--outdir={tmpdir}", *UV_ARGS],
check=True,
)
(sdist,) = tmpdir.visit("*.tar.gz")
version = FILENAME_VERSION.search(sdist.basename).group(1)
with tarfile.open(str(sdist), "r:gz") as tar:
simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
(pkg_info_path,) = (n for n in simpler if n.endswith("PKG-INFO"))
pyproject_toml = read_tz_file(tar, "pyproject.toml")
pkg_info = read_tz_file(tar, pkg_info_path).decode("utf-8")
files = headers | sdist_src_files | sdist_files
assert files <= simpler
assert b'name = "pybind11"' in pyproject_toml
assert f"Version: {version}" in pkg_info
assert "License-Expression: BSD-3-Clause" in pkg_info
assert "License-File: LICENSE" in pkg_info
assert "Provides-Extra: global" in pkg_info
assert f'Requires-Dist: pybind11-global=={version}; extra == "global"' in pkg_info
def test_build_global_dist(monkeypatch, tmpdir):
monkeypatch.chdir(MAIN_DIR)
with build_global():
subprocess.run(
[
sys.executable,
"-m",
"build",
"--sdist",
"--outdir",
str(tmpdir),
*UV_ARGS,
],
check=True,
)
(sdist,) = tmpdir.visit("*.tar.gz")
version = FILENAME_VERSION.search(sdist.basename).group(2)
with tarfile.open(str(sdist), "r:gz") as tar:
simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
(pkg_info_path,) = (n for n in simpler if n.endswith("PKG-INFO"))
pyproject_toml = read_tz_file(tar, "pyproject.toml")
pkg_info = read_tz_file(tar, pkg_info_path).decode("utf-8")
files = headers | sdist_src_files | sdist_files
assert files <= simpler
assert b'name = "pybind11-global"' in pyproject_toml
assert f"Version: {version}" in pkg_info
assert "License-Expression: BSD-3-Clause" in pkg_info
assert "License-File: LICENSE" in pkg_info
assert "Provides-Extra: global" not in pkg_info
assert 'Requires-Dist: pybind11-global; extra == "global"' not in pkg_info
def tests_build_wheel(monkeypatch, tmpdir):
monkeypatch.chdir(MAIN_DIR)
subprocess.run(
[sys.executable, "-m", "build", "--wheel", "--outdir", str(tmpdir), *UV_ARGS],
check=True,
)
(wheel,) = tmpdir.visit("*.whl")
version, simple_version = FILENAME_VERSION.search(wheel.basename).groups()
files = {f"pybind11/{n}" for n in all_files}
files |= {
"dist-info/licenses/LICENSE",
"dist-info/METADATA",
"dist-info/RECORD",
"dist-info/WHEEL",
"dist-info/entry_points.txt",
}
with zipfile.ZipFile(str(wheel)) as z:
names = z.namelist()
share = zipfile.Path(z, "pybind11/share")
pkgconfig = (share / "pkgconfig/pybind11.pc").read_text(encoding="utf-8")
cmakeconfig = (share / "cmake/pybind11/pybind11Config.cmake").read_text(
encoding="utf-8"
)
(pkg_info_path,) = (n for n in names if n.endswith("METADATA"))
pkg_info = zipfile.Path(z, pkg_info_path).read_text(encoding="utf-8")
trimmed = {n for n in names if "dist-info" not in n}
trimmed |= {f"dist-info/{n.split('/', 1)[-1]}" for n in names if "dist-info" in n}
assert files == trimmed
assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in cmakeconfig
pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version)
assert pkgconfig_expected == pkgconfig
assert f"Version: {version}" in pkg_info
assert "License-Expression: BSD-3-Clause" in pkg_info
assert "License-File: LICENSE" in pkg_info
assert "Provides-Extra: global" in pkg_info
assert f'Requires-Dist: pybind11-global=={version}; extra == "global"' in pkg_info
def tests_build_global_wheel(monkeypatch, tmpdir):
monkeypatch.chdir(MAIN_DIR)
with build_global():
subprocess.run(
[
sys.executable,
"-m",
"build",
"--wheel",
"--outdir",
str(tmpdir),
*UV_ARGS,
],
check=True,
)
(wheel,) = tmpdir.visit("*.whl")
version, simple_version = FILENAME_VERSION.search(wheel.basename).groups()
files = {f"data/data/{n}" for n in headers}
files |= {f"data/headers/{n[8:]}" for n in headers}
files |= {f"data/data/{n}" for n in generated_files}
files |= {
"dist-info/licenses/LICENSE",
"dist-info/METADATA",
"dist-info/WHEEL",
"dist-info/RECORD",
}
with zipfile.ZipFile(str(wheel)) as z:
names = z.namelist()
beginning = names[0].split("/", 1)[0].rsplit(".", 1)[0]
share = zipfile.Path(z, f"{beginning}.data/data/share")
pkgconfig = (share / "pkgconfig/pybind11.pc").read_text(encoding="utf-8")
cmakeconfig = (share / "cmake/pybind11/pybind11Config.cmake").read_text(
encoding="utf-8"
)
(pkg_info_path,) = (n for n in names if n.endswith("METADATA"))
pkg_info = zipfile.Path(z, pkg_info_path).read_text(encoding="utf-8")
assert f"Version: {version}" in pkg_info
assert "License-Expression: BSD-3-Clause" in pkg_info
assert "License-File: LICENSE" in pkg_info
assert "Provides-Extra: global" not in pkg_info
assert 'Requires-Dist: pybind11-global; extra == "global"' not in pkg_info
trimmed = {n[len(beginning) + 1 :] for n in names}
assert files == trimmed
assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in cmakeconfig
pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version)
assert pkgconfig_expected == pkgconfig
def test_version_matches():
header = MAIN_DIR / "include/pybind11/detail/common.h"
text = header.read_text()
# Extract the relevant macro values
regex_prefix = r"#\s*define\s+PYBIND11_VERSION_"
micro = re.search(rf"{regex_prefix}MICRO\s+(\d+)\b", text).group(1)
release_level = re.search(rf"{regex_prefix}RELEASE_LEVEL\s+(\w+)\b", text).group(1)
release_serial = re.search(
rf"{regex_prefix}RELEASE_SERIAL\s+(\d+)\b",
text,
).group(1)
patch = re.search(rf"{regex_prefix}PATCH\s+([\w.-]+)\b", text).group(1)
# Map release level macro to string
level_map = {
"PY_RELEASE_LEVEL_ALPHA": "a",
"PY_RELEASE_LEVEL_BETA": "b",
"PY_RELEASE_LEVEL_GAMMA": "rc",
"PY_RELEASE_LEVEL_FINAL": "",
}
level_str = level_map[release_level]
if release_level == "PY_RELEASE_LEVEL_FINAL":
assert level_str == ""
assert release_serial == "0"
expected_patch = micro
else:
expected_patch = f"{micro}{level_str}{release_serial}"
assert patch == expected_patch
def run_command_line(*args: str) -> str:
env = os.environ.copy()
env["PYTHONPATH"] = str(MAIN_DIR)
result = subprocess.run(
[sys.executable, "-m", "pybind11", *args],
capture_output=True,
text=True,
check=True,
env=env,
)
return result.stdout
def test_cli_cflags():
out = run_command_line("--cflags")
assert "-std=c++17" in out
assert f"-I{sysconfig.get_path('include')}" in out
def test_cli_ldflags_embed():
out = run_command_line("--ldflags", "--embed")
assert "-lpython" in out
if sysconfig.get_config_var("LIBDIR"):
assert "-L" in out
@pytest.mark.skipif(os.name != "posix", reason="quote style is platform-specific")
def test_cflags_quotes_paths_with_spaces(monkeypatch):
monkeypatch.syspath_prepend(str(MAIN_DIR))
commands = importlib.import_module("pybind11.commands")
monkeypatch.setattr(sysconfig, "get_path", lambda name: f"/spa ced/{name}")
assert "'-I/spa ced/include'" in commands.get_cflags()
@pytest.mark.skipif(os.name != "posix", reason="Unix link flags only")
def test_ldflags_other_unix(monkeypatch):
monkeypatch.syspath_prepend(str(MAIN_DIR))
commands = importlib.import_module("pybind11.commands")
monkeypatch.setattr(sys, "platform", "freebsd14")
monkeypatch.setattr(commands, "_config", lambda name: "") # noqa: ARG005
out = commands.get_ldflags()
assert "-shared" in out
assert "-fPIC" in out
def test_cli_file():
out = run_command_line("--file", "example.cpp").rstrip()
ext_suffix = sysconfig.get_config_var("EXT_SUFFIX")
assert "-std=c++17" in out
assert out.index("-std=c++17") < out.index("example.cpp")
assert out.endswith(f"-o example{ext_suffix}")
if sys.platform.startswith(("linux", "darwin")):
assert out.index("example.cpp") < out.index("-shared")
def test_cli_file_embed():
out = run_command_line("--file", "example.cpp", "--embed").rstrip()
assert out.endswith("-o example")