Skip to content

Commit 7df26f4

Browse files
committed
refactor: more comments from @YannickJadoul
1 parent ae310fb commit 7df26f4

File tree

7 files changed

+53
-58
lines changed

7 files changed

+53
-58
lines changed

docs/compiling.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ Copy manually
127127
-------------
128128

129129
You can also copy ``setup_helpers.py`` directly to your project; it was
130-
designed to be usable standalone, like the old example ``setup.py``. The
131-
``CppExtension`` class is identical to ``Pybind11Extension``, except it does
132-
not include the pybind11 package headers, so you can use it with git submodules
133-
and a specific git version. If you use this, you will need to import from a
134-
local file in ``setup.py`` and ensure the helper file is part of your MANIFEST.
130+
designed to be usable standalone, like the old example ``setup.py``. You can
131+
set ``include_pybind11=False`` to skip including the pybind11 package headers,
132+
so you can use it with git submodules and a specific git version. If you use
133+
this, you will need to import from a local file in ``setup.py`` and ensure the
134+
helper file is part of your MANIFEST.
135135

136136

137137
.. versionchanged:: 2.6

docs/upgrade.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ The Python package now includes the headers as data in the package itself, as
8282
well as in the "headers" wheel slot. ``pybind11 --includes`` and
8383
``pybind11.get_include()`` report the new location, which is always correct
8484
regardless of how pybind11 was installed, making the old ``user=`` argument
85-
meaningless. If you are not using the helper, you are encouraged to switch to
86-
the package location.
85+
meaningless. If you are not using the function to get the location already, you
86+
are encouraged to switch to the package location.
8787

8888

8989
v2.2

pybind11/setup_helpers.py

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,11 @@
6464
# directory into your path if it sits beside your setup.py.
6565

6666

67-
# Just in case someone clever tries to multithread
68-
tmp_chdir_lock = threading.Lock()
69-
cpp_cache_lock = threading.Lock()
70-
71-
72-
@contextlib.contextmanager
73-
def tmp_chdir():
74-
"Prepare and enter a temporary directory, cleanup when done"
75-
76-
# Threadsafe
77-
with tmp_chdir_lock:
78-
olddir = os.getcwd()
79-
try:
80-
tmpdir = tempfile.mkdtemp()
81-
os.chdir(tmpdir)
82-
yield tmpdir
83-
finally:
84-
os.chdir(olddir)
85-
shutil.rmtree(tmpdir)
86-
87-
88-
class CppExtension(_Extension):
67+
class Pybind11Extension(_Extension):
8968
"""
90-
Build a C++11+ Extension module. This automatically adds the recommended
91-
flags when you init the extension and assumes C++ sources - you can further
92-
modify the options yourself.
69+
Build a C++11+ Extension module with pybind11. This automatically adds the
70+
recommended flags when you init the extension and assumes C++ sources - you
71+
can further modify the options yourself.
9372
9473
The customizations are:
9574
@@ -105,6 +84,9 @@ class CppExtension(_Extension):
10584
property more than once, as flags are added when you set it. Set the
10685
property to None to disable the addition of C++ standard flags.
10786
87+
If you want to add pybind11 headers manually, for example for an exact
88+
git checkout, then set ``include_pybind11=False``.
89+
10890
Warning: do not use property-based access to the instance on Python 2 -
10991
this is an ugly old-style class due to Distutils.
11092
"""
@@ -127,12 +109,27 @@ def __init__(self, *args, **kwargs):
127109
if "language" not in kwargs:
128110
kwargs["language"] = "c++"
129111

112+
include_pybind11 = kwargs.pop("include_pybind11", True)
113+
130114
# Can't use super here because distutils has old-style classes in
131115
# Python 2!
132116
_Extension.__init__(self, *args, **kwargs)
133117

118+
# Include the installed package pybind11 headers
119+
if include_pybind11:
120+
# If using setup_requires, this fails the first time - that's okay
121+
try:
122+
import pybind11
123+
124+
pyinc = pybind11.get_include()
125+
126+
if pyinc not in self.include_dirs:
127+
self.include_dirs.append(pyinc)
128+
except ImportError:
129+
pass
130+
134131
# Have to use the accessor manually to support Python 2 distutils
135-
CppExtension.cxx_std.__set__(self, cxx_std)
132+
Pybind11Extension.cxx_std.__set__(self, cxx_std)
136133

137134
if WIN:
138135
self._add_cflags("/EHsc", "/bigobj")
@@ -183,28 +180,25 @@ def cxx_std(self, level):
183180
self.extra_compile_args.append("-Wno-deprecated-register")
184181

185182

186-
class Pybind11Extension(CppExtension):
187-
"""
188-
A pybind11 Extension subclass. Includes the header directory from the
189-
package.
190-
"""
191-
192-
def __init__(self, *args, **kwargs):
193-
CppExtension.__init__(self, *args, **kwargs)
194-
195-
# If using setup_requires, this fails the first time - that's okay
196-
try:
197-
import pybind11
198-
199-
pyinc = pybind11.get_include()
183+
# Just in case someone clever tries to multithread
184+
tmp_chdir_lock = threading.Lock()
185+
cpp_cache_lock = threading.Lock()
200186

201-
if pyinc not in self.include_dirs:
202-
self.include_dirs.append(pyinc)
203-
except ImportError:
204-
pass
205187

188+
@contextlib.contextmanager
189+
def tmp_chdir():
190+
"Prepare and enter a temporary directory, cleanup when done"
206191

207-
Pybind11Extension.__doc__ += CppExtension.__doc__
192+
# Threadsafe
193+
with tmp_chdir_lock:
194+
olddir = os.getcwd()
195+
try:
196+
tmpdir = tempfile.mkdtemp()
197+
os.chdir(tmpdir)
198+
yield tmpdir
199+
finally:
200+
os.chdir(olddir)
201+
shutil.rmtree(tmpdir)
208202

209203

210204
# cf http://bugs.python.org/issue26689

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
# PYBIND11_GLOBAL_SDIST will build a different sdist, with the python-headers
1919
# files, and the sys.prefix files (CMake and headers).
2020

21-
alt_sdist = os.environ.get("PYBIND11_GLOBAL_SDIST", False)
21+
global_sdist = os.environ.get("PYBIND11_GLOBAL_SDIST", False)
2222

2323
version_py = "pybind11/_version.py"
24-
setup_py = "tools/setup_global.py.in" if alt_sdist else "tools/setup_main.py.in"
24+
setup_py = "tools/setup_global.py.in" if global_sdist else "tools/setup_main.py.in"
2525
extra_cmd = 'cmdclass["sdist"] = SDist\n'
2626

2727
to_src = (

tests/extra_python_package/test_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_build_sdist(monkeypatch, tmpdir):
159159
assert pyproject_toml == contents
160160

161161

162-
def test_build_alt_dist(monkeypatch, tmpdir):
162+
def test_build_global_dist(monkeypatch, tmpdir):
163163

164164
monkeypatch.chdir(MAIN_DIR)
165165
monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
@@ -252,7 +252,7 @@ def tests_build_wheel(monkeypatch, tmpdir):
252252
assert files == trimmed
253253

254254

255-
def tests_build_alt_wheel(monkeypatch, tmpdir):
255+
def tests_build_global_wheel(monkeypatch, tmpdir):
256256
monkeypatch.chdir(MAIN_DIR)
257257
monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
258258

tools/setup_global.py.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
# Setup script for PyPI; use CMakeFile.txt to build extension modules
4+
# Setup script for pybind11-global (in the sdist or in tools/setup_global.py in the repository)
5+
# This package is targeted for easy use from CMake.
56

67
import contextlib
78
import glob

tools/setup_main.py.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
# Setup script for PyPI - placed in the sdist
4+
# Setup script (in the sdist or in tools/setup_main.py in the repository)
55

66
from setuptools import setup
77

0 commit comments

Comments
 (0)