Commit Graph

121 Commits

Author SHA1 Message Date
Lalaland fab1eebe2c
First draft of Eigen::Tensor support (#4201)
* First draft of Eigen::Tensor support

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix build errors

* Weird allocator stuff?

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove unused + additional allocator junk

* Disable warning

* Use constexpr

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* clang tidy fixes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Resolve comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove auto constexpr function

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Try again for older C++

* Oops forgot constexpr

* Move to new files as suggested

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix weird tests

* Fix nits

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Oops, forgot import

* Fix clang 3.6 bug

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* More comprehensive test suite

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Refactor allocators to make things more clear

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Switch to std::copy

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Switch to DSizes instead of array

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Address feedback

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix python + dummy c++ change to trigger build

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Alignment

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add include guard

* Forgot inline

* Fix compiler warning

* Remove bad test

* Better type signatures

* Add guards to make compiler requirements more explicit

* style: pre-commit fixes

* Force rerun of tests due to flake

* style: pre-commit fixes

* Keep pragmas & all related comments together, add PLEASE KEEP IN SYNC

* Move headers out of detail

* style: pre-commit fixes

* Fix cmake

* Improve casting

* style: pre-commit fixes

* Add a ton more tests + refactor

* Improve names

* style: pre-commit fixes

* Update include/pybind11/eigen/tensor.h

Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>

* Fix tests

* style: pre-commit fixes

* Update

* Add a test to verify that strange numpy arrays work

* Fix dumb compiler warning

* Better tests

* Better tests

* Fix tests

* style: pre-commit fixes

* More test fixes

* style: pre-commit fixes

* A ton more test coverage

* Fix tests

* style: pre-commit fixes

* style: pre-commit fixes

* Add back constexpr

* Another test

* style: pre-commit fixes

* Improve tests

* Whoops

* Less magic numbers

* Update tests/test_eigen_tensor.py

Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>

* Update tests/test_eigen_tensor.py

Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>

* style: pre-commit fixes

* Fix tests

* style: pre-commit fixes

* Fix memory leak

* style: pre-commit fixes

* Fix order

* style: pre-commit fixes

* Add test to make sure unsafe casts fail

* Minor bug fix to work on 32 bit machines

* Implement convert flag

* style: pre-commit fixes

* Switch to correct TensorMap const use

* style: pre-commit fixes

* Support older versions of eigen

* Weird c++ compilers

* Fix Eigen bug

* Fix another eigen bug

* Yet another eigen bug

* Potential flakes?

* style: pre-commit fixes

* Rerun tests with dummy exception to find out what is going on

* Another dummy test run

* Ablate more

* Found the broken test?

* One step closer

* one step further

* Double check

* one thing at a time

* Give up and disable the test

* Clang lies about being gcc

* Oops, fix matrix test

* style: pre-commit fixes

* Add tests to verify scalar conversions

* style: pre-commit fixes

* Fix nits

* Support no_array

* Fix tests

* style: pre-commit fixes

* Silence compiler warning

* Improve build system for ancient compilers

* Make clang happy

* Make gcc happy

* Implement Skylion's suggestions

* Fix warning

* Inline const pointer check

* Implement suggestions

* style: pre-commit fixes

* Improve tests

* Typo

* style: pre-commit fixes

* Support Google's build environment

* style: pre-commit fixes

* Update include/pybind11/eigen/tensor.h

Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>

* style: pre-commit fixes

* Test cleanup per Skylion

* Switch to remvove_cv_t

* Cleaner test

* style: pre-commit fixes

* Remove tensor from eigen.h, update tests

* style: pre-commit fixes

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Sergiu Deitsch <sergiud@users.noreply.github.com>
2022-10-18 16:54:16 -07:00
Eli Schwartz 5bdd3d59be
feat(cmake): add installation support for pkg-config dependency detection (#4077)
* add installation support for pkg-config dependency detection

pkg-config is a buildsystem-agnostic alternative to
`pybind11Config.cmake` that can be used from build systems other than
cmake.

Fixes #230

* tests: add test for pkg config

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
2022-08-09 00:02:45 -04:00
Henry Schreiner 15f8d7c12e
fix(build): cleaner CMake printouts & IDE folders (#3479) 2021-11-18 10:01:24 -05:00
Henry Schreiner e7e2c79f3f
fix: improve support for Python 3.11-dev (#3368)
* ci: support Python 3.11-dev

Also update 3.10 to final, better PyPy usage

* fix: use PyFrame_GetCode on Python 3.9+

* ci: some bitiness of pypy not supported on win

* chore: update CMake support to 3.22rc1 to quiet warning

* fix: use dev version of py to fix Py 3.11

* tests: print proper Eigen version

* ci: include pypy2, not sure why

* ci: avoid running on Python 3.11 for now

* ci: fix runs

* ci: simpler PyPy usage, drop unmaintained scipy + pypy index

* ci: only binary numpy, wait on pypy 3.8

* refactor: address review
2021-10-26 14:50:34 -04:00
Jeremy Maitin-Shepard 14976c853b
Eliminate duplicate TLS keys for loader_life_support stack (#3275)
* Eliminate duplicate TLS keys for loader_life_support stack

This revises the existing fix for
https://github.com/pybind/pybind11/issues/2765 in
https://github.com/pybind/pybind11/pull/3237 to reduce the amount of
TLS storage used.

The shared TLS key is stored in two different ways, depending on
`PYBIND11_INTERNALS_VERSION`.  If `PYBIND11_INTERNALS_VERSION ==
4` (as is currently set), the TLS key is stored in the
`internal::shared_data` map to avoid breaking ABI compatibility.  If
`PYBIND11_INTERNALS_VERSION > 4`, the TLS key is stored directly in
the `internals` struct.

* Fix test_pytypes.py::test_issue2361 failure on PyPy3.7

* Add github actions tests for unstable ABI
2021-09-20 04:57:38 -07:00
Henry Schreiner c14b193308
chore: increase CMake upper limit (#3124) 2021-07-28 18:04:14 -07:00
Henry Schreiner 7cc0ebb475
fix: the CMake config in Python package had a hard coded path (#3144) 2021-07-27 17:23:52 -04:00
Ralf W. Grosse-Kunstleve 6d1b197b46
Splitting out pybind11/stl/filesystem.h. (#3077)
* Splitting out pybind11/stl/filesystem.h.

To solve breakages like: https://github.com/deepmind/open_spiel/runs/2999582108

Mostly following the suggestion here: https://github.com/pybind/pybind11/pull/2730#issuecomment-750507575

Except using pybind11/stl/filesystem.h instead of pybind11/stlfs.h, as decided via chat.

stl.h restored to the exact state before merging PR #2730 via:
```
git checkout 733f8de24f stl.h
```

* Properly including new stl subdirectory in pip wheel config.

This now passes interactively:
```
pytest tests/extra_python_package/
```

* iwyu cleanup.

iwyuh.py -c -std=c++17 -DPYBIND11_TEST_BOOST -Ipybind11/include -I/usr/include/python3.9 -I/usr/include/eigen3 include/pybind11/stl/filesystem.h

* Adding PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL.

* Eliminating else after return.
2021-07-08 09:02:48 -07:00
Dustin Spicuzza c0fbb02c9f
Extract gil management functions to separate header (#2845) 2021-02-22 19:15:40 -08:00
Ralf W. Grosse-Kunstleve 0c42250a4e
Splitting out detail/type_caster_base.h from cast.h, with iwyu cleanup. (#2841)
* Splitting out detail/type_caster_base.h from cast.h.
* Include cleanup guided by include-what-you-use 0.12 based on clang version 9.0.1-11.
2021-02-22 18:38:18 -08:00
Henry Schreiner 130c99544d
fix: support basic dual includes (#2804) 2021-01-19 18:49:03 -05:00
Henry Schreiner eb83feefff
style: avoid using unintialized variables (#2806)
* style: avoid using unintialized variables

Tested with cmake --warn-unintialized -S . -B build

* refactor: use function for possibly uninit vars
2021-01-19 18:48:22 -05:00
Henry Schreiner b8dc60ec8f
fix: Python include directory was missing from DIRS (#2636) 2020-11-02 20:45:54 -05:00
Henry Schreiner edda89ba68
fix: install to DATAROOTDIR instead of hardcoding share (#2584)
Closes #2571; this is defined to share, and unlike DATADIR, is not sometimes overwritten incorrectly for storing CMake files.
2020-10-13 13:08:54 -04:00
Henry Schreiner fd61f5038e
feat: setup.py redesign and helpers (#2433)
* feat: setup.py redesign and helpers

* refactor: simpler design with two outputs

* refactor: helper file update and Windows support

* fix: review points from @YannickJadoul

* refactor: fixes to naming and more docs

* feat: more customization points

* feat: add entry point pybind11-config

* refactor: Try Extension-focused method

* refactor: rename alt/inplace to global

* fix: allow usage with git modules, better docs

* feat: global as an extra (@YannickJadoul's suggestion)

* feat: single version location

* fix: remove the requirement that setuptools must be imported first

* fix: some review points from @wjacob

* fix: use .in, add procedure to docs

* refactor: avoid monkeypatch copy

* docs: minor typos corrected

* fix: minor points from @YannickJadoul

* fix: typo on Windows C++ mode

* fix: MSVC 15 update 3+ have c++14 flag

See <https://docs.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=vs-2019>

* docs: discuss making SDists by hand

* ci: use pep517.build instead of manual setup.py

* refactor: more comments from @YannickJadoul

* docs: updates from @ktbarrett

* fix: change to newly recommended tool instead of pep517.build

This was intended as a proof of concept; build seems to be the correct replacement.

See https://github.com/pypa/pep517/pull/83

* docs: updates from @wjakob

* refactor: dual version locations

* docs: typo spotted by @wjakob
2020-09-16 17:13:41 -04:00
Wenzel Jakob 064a03a49b main CMakeLists.txt file: be less noisy 2020-09-06 16:46:38 +02:00
Henry Schreiner c58f7b745b
fix: reduce target collision in add_submodule mode (#2423)
* fix: reduce target collision in add_submodule mode

Closes #2420

* fix: update CMakeLists.txt
2020-08-22 09:06:01 -04:00
Henry Schreiner 24dffe46af
fix: PYBIND11_MASTER_PROJECT always ON (#2412) 2020-08-19 16:49:08 -04:00
Henry Schreiner 04fdc44f50
tests: avoid putting build products into source directory (#2353)
* tests: keep source dir clean

* ci: make first build inplace

* ci: drop dev setting (wasn't doing anything)

* tests: warn if source directory is dirty
2020-08-19 13:11:57 -04:00
Henry Schreiner 1729aae96f
feat: new FindPython support (#2370)
* feat: FindPython support

* refactor: rename to PYBIND11_FINDPYTHON

* docs: Caps fixes

* feat: NOPYTHON mode

* test: check simple call

* docs: add changelog/upgrade guide

* feat: Support Python3 and Python2

* refactor: Use targets in tests

* fix: support CMake 3.4+

* feat: classic search also finds virtual environments

* docs: some updates from @wjakob's review

* fix: wrong name for QUIET mode variable, reported by @skoslowski

* refactor: cleaner output messaging

* fix: support debug Python's in FindPython mode too

* fixup! refactor: cleaner output messaging

* fix: missing pybind11_FOUND and pybind11_INCLUDE_DIR restored to subdir mode

* fix: nicer reporting of Python / PyPy

* fix: out-of-order variable fix

* docs: minor last-minute cleanup
2020-08-19 12:26:26 -04:00
Paul Fultz II 69821d9e75
Disable testing when using BUILD_TESTING (#1682) 2020-08-18 08:34:18 -04:00
Henry Schreiner df115977df chore: cleanup 2020-08-06 11:54:41 -04:00
Henry Schreiner 1caf1d0613
fix: check list validity and add missing file (#2352)
Added a check on CMake 3.12+ to make sure the file listing is up to date. And caught a missing file with it!
2020-08-01 15:24:30 -04:00
Henry Schreiner b8047245bb
fix: uninstall cleanup (#2346) 2020-07-31 10:16:40 +02:00
acxz 6f6e9394ce
feat: add uninstall target for CMake (#2265)
* add uninstall target for cmake

* only add target when built as master project

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2020-07-30 21:16:50 -04:00
Henry Schreiner 94db5c5ed1 format: apply cmake-format 2020-07-30 20:27:55 -04:00
Henry Schreiner 1651c32492 update: address review points 2020-07-30 20:27:55 -04:00
Henry Schreiner f64d5aa6da fix: typo for warning on C++17, support Win + 2.7 2020-07-30 20:27:55 -04:00
Henry Schreiner 6ec1775fff feat: drop CMake 3.6 and below, modernize CMake
fix: include PYTHON_IS_DEBUG
2020-07-30 20:27:55 -04:00
Axel Huebl 97b20e537a CMake: Remember Python Version (#1434)
It is useful not only to remember the python libs and includes but
also the interpreter version in cache.

If users call pybind11 throught `add_subdirectories` they will
otherwise have no access to the selected interpreter version.
The interpreter version is useful for downstream projects, e.g.
to select default `lib/pythonX.Y/site-packages/` install paths.
2018-06-24 15:22:41 +02:00
Dean Moldovan a80af9557d Add a dummy common.h header with a deprecation warning 2017-09-06 15:22:26 +02:00
Florian Apolloner 29b99a11a4 Specify CXX as project language for CMake >= 3.4 (#1027) 2017-08-30 14:17:54 +02:00
Dean Moldovan 024932b379 Move everything related to `internals` into a separate detail header 2017-08-23 10:44:52 +02:00
Jason Rhinelander 464d98962d Allow binding factory functions as constructors
This allows you to use:

    cls.def(py::init(&factory_function));

where `factory_function` returns a pointer, holder, or value of the
class type (or a derived type).  Various compile-time checks
(static_asserts) are performed to ensure the function is valid, and
various run-time type checks where necessary.

Some other details of this feature:
- The `py::init` name doesn't conflict with the templated no-argument
  `py::init<...>()`, but keeps the naming consistent: the existing
  templated, no-argument one wraps constructors, the no-template,
  function-argument one wraps factory functions.
- If returning a CppClass (whether by value or pointer) when an CppAlias
  is required (i.e. python-side inheritance and a declared alias), a
  dynamic_cast to the alias is attempted (for the pointer version); if
  it fails, or if returned by value, an Alias(Class &&) constructor
  is invoked.  If this constructor doesn't exist, a runtime error occurs.
- for holder returns when an alias is required, we try a dynamic_cast of
  the wrapped pointer to the alias to see if it is already an alias
  instance; if it isn't, we raise an error.
- `py::init(class_factory, alias_factory)` is also available that takes
  two factories: the first is called when an alias is not needed, the
  second when it is.
- Reimplement factory instance clearing.  The previous implementation
  failed under python-side multiple inheritance: *each* inherited
  type's factory init would clear the instance instead of only setting
  its own type value.  The new implementation here clears just the
  relevant value pointer.
- dealloc is updated to explicitly set the leftover value pointer to
  nullptr and the `holder_constructed` flag to false so that it can be
  used to clear preallocated value without needing to rebuild the
  instance internals data.
- Added various tests to test out new allocation/deallocation code.
- With preallocation now done lazily, init factory holders can
  completely avoid the extra overhead of needing an extra
  allocation/deallocation.
- Updated documentation to make factory constructors the default
  advanced constructor style.
- If an `__init__` is called a second time, we have two choices: we can
  throw away the first instance, replacing it with the second; or we can
  ignore the second call.  The latter is slightly easier, so do that.
2017-08-17 09:33:27 -04:00
Jason Rhinelander 0d703f6e7c Don't force hidden visibility on the embed target, just the module target
Embedding may well be used in places where hidden visibility isn't
desired.  It should be relatively safe to allow it there; any potential
conflict would come in if modules are loaded into that embedded
interpreter, but as long as the modules are compiled with hidden
visibility they shouldn't conflict.

There could still be warnings if the embedded code attempts to export
classes with internal (hidden) pybind members, but that seems a
legitimate warning (and already has a FAQ entry).
2017-08-17 01:57:31 -04:00
Dean Moldovan f580649871 Move internal headers into `detail` subdirectory 2017-08-17 04:06:35 +02:00
Jason Rhinelander 97aa54fefa Compile with hidden visibility always; set via cmake property rather than compiler flag
This updates the compilation to always apply hidden visibility to
resolve the issues with default visibility causing problems under debug
compilations.  Moreover using the cmake property makes it easier for a
caller to override if absolutely needed for some reason.

For `pybind11_add_module` we use cmake to set the property; for the
targets, we append to compilation option to non-MSVC compilers.
2017-08-14 11:44:17 -04:00
Dean Moldovan 3dde6ddc53 Add test for custom CMake export group 2017-08-07 23:08:20 +02:00
Ben Boeckel 017a747da6 CMake: support a custom export group (#970)
When Pybind11 is used via `add_subdirectory`, when targets are installed
from the parent project, CMake wants all of the dependencies built by
the project in the same export set. Projects may now set
`PYBIND11_EXPORT_NAME` to have Pybind11 put it targets into the
project's export set. If so, do not install Pybind11's export file.
2017-08-07 22:32:35 +02:00
Dean Moldovan 22c413b196 Add C++ interface for the Python interpreter 2017-05-28 02:12:24 +02:00
Dean Moldovan 7f5c85c861 Add CMake target for embedding the Python interpreter
All targets provided by pybind11:

* pybind11::module - the existing target for creating extension modules
* pybind11::embed - new target for embedding the interpreter
* pybind11::pybind11 - common "base" target (headers only)
2017-05-28 02:12:24 +02:00
Jason Rhinelander 2b941b38b4 Add missing header to setup.py 2017-05-07 12:19:19 -04:00
Jason Rhinelander dbb4c5b531 Move buffer_info to its own header
Upcoming changes to buffer_info make it need some things declared in
common.h; it also feels a bit misplaced in common.h (which is arguably
too large already), so move it out.  (Separating this and the subsequent
changes into separate commits to make the changes easier to distinguish
from the move.)
2017-04-13 09:57:02 -04:00
Ghislain Antony Vaillant 5b50376490 Arch-indep CMake packaging (#764)
* Arch-indep CMake packaging

Since pybind11 is a header-only library, the CMake packaging does not have to carry any architecture specific checks. Without this patch, the detection of pybind11 will fail on 32-bit architectures if the project was built on a 64-bit machine and vice-versa. This fix is similar to what is applied to `Eigen` and other header-only C++ libraries.
2017-03-24 23:09:38 +01:00
Jason Rhinelander 60d0e0db3e Independent tests (#665)
* Make tests buildable independently

This makes "tests" buildable as a separate project that uses
find_package(pybind11 CONFIG) when invoked independently.

This also moves the WERROR option into tests/CMakeLists.txt, as that's
the only place it is used.

* Use Eigen 3.3.1's cmake target, if available

This changes the eigen finding code to attempt to use Eigen's
system-installed Eigen3Config first.  In Eigen 3.3.1, it exports a cmake
Eigen3::Eigen target to get dependencies from (rather than setting the
include path directly).

If it fails, we fall back to the trying to load allowing modules (i.e.
allowing our tools/FindEigen3.cmake).  If we either fallback, or the
eigen version is older than 3.3.1 (or , we still set the include
directory manually; otherwise, for CONFIG + new Eigen, we get it via
the target.

This is also needed to allow 'tests' to be built independently, when
the find_package(Eigen3) is going to find via the system-installed
Eigen3Config.cmake.

* Add a install-then-build test, using clang on linux

This tests that `make install` to the actual system, followed by a build
of the tests (without the main pybind11 repository available) works as
expected.

To also expand the testing variety a bit, it also builds using
clang-3.9 instead of gcc.

* Don't try loading Eigen3Config in cmake < 3.0

It could FATAL_ERROR as the newer cmake includes a cmake 3.0 required
line.

If doing an independent, out-of-tree "tests" build, the regular
find_package(Eigen3) is likely to fail with the same error, but I think
we can just let that be: if you want a recent Eigen with proper cmake
loading support *and* want to do an independent tests build, you'll
need at least cmake 3.0.
2017-02-24 23:07:53 +01:00
Dean Moldovan c91f8bd627 Reimplement static properties by extending PyProperty_Type
Instead of creating a new unique metaclass for each type, the builtin
`property` type is subclassed to support static properties. The new
setter/getters always pass types instead of instances in their `self`
argument. A metaclass is still required to support this behavior, but
it doesn't store any data anymore, so a new one doesn't need to be
created for each class. There is now only one common metaclass which
is shared by all pybind11 types.
2017-02-23 15:45:26 +01:00
Sylvain Corlay d5ce82b6f5 CMake installation path consistency (#652)
* Add flag for installation of headers under python include directory

* Allow the disabling of distutils install_headers
2017-02-14 13:16:14 +01:00
Matthew Woehlke e15fa9f99a Avoid C-style const casts (#659)
* Avoid C-style const casts

Replace C-style casts that discard `const` with `const_cast` (and, where
necessary, `reinterpret_cast` as well).

* Warn about C-style const-discarding casts

Change pybind11_enable_warnings to also enable `-Wcast-qual` (warn if a
C-style cast discards `const`) by default. The previous commit should
have gotten rid of all of these (at least, all the ones that tripped in
my build, which included the tests), and this should discourage more
from newly appearing.
2017-02-08 23:43:08 +01:00
Jason Rhinelander a3fec901b8 Avoid CMP0048 warning (#570)
Fixes #567.

If pybind's CMakeLists gets loaded via an include_directory from another
CMakeLists with a higher minimum version (e.g. 3.0), the project()
command without a version produces a CMP0048 warning.

This commit explicitly requests the new behaviour if the policy exists,
as it won't cause problems (we set VERSION later).
2016-12-26 11:26:21 +01:00
Dean Moldovan 71e8a7962c Rename target from pybind11::pybind11 to pybind11::module
Makes room for an eventual pybind11::embedded target.
2016-12-19 16:34:48 +01:00