Commit Graph

2497 Commits

Author SHA1 Message Date
Ralf W. Grosse-Kunstleve
fbabf99624 Merge branch 'master' into sh_merge_master 2021-09-20 08:00:31 -07:00
Aaron Gokaslan
d0f3c51f01
Enable defining custom __new__ (#3265)
* Enable defining custom __new__

* See if xfail needed

* Qualify auto self

* Unconditionally defining PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING. Returning pointer from "__init__" instead of reference.

* Use new style __init__

* Simplify __new__ creation

* Reviewer suggestions

* Match indentation

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
2021-09-20 10:42:14 -04:00
Ralf W. Grosse-Kunstleve
10350ab854 Tracking ci.yml changes from master. 2021-09-20 07:19:46 -07:00
Ralf W. Grosse-Kunstleve
03d409b4a0 Merge branch 'master' into sh_merge_master 2021-09-20 05:57:17 -07:00
Ralf W. Grosse-Kunstleve
b08b84989d
Introducing PYBIND11_INTERNALS_SH_DEF, replacing the approach of PR #2939. For compatibility with PR #3275. (#3283) 2021-09-20 05:54:13 -07: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
04dd3262f0
docs: update CHANGELOG (#3276) 2021-09-17 17:28:26 -04:00
Matthias Köppe
e0031bfceb include/pybind11/numpy.h: gcc 4.8.4 does not have is_trivially_copyable (#3270) 2021-09-15 14:01:24 -07:00
pre-commit-ci[bot]
3ed31e9289
[pre-commit.ci] pre-commit autoupdate (#3266)
updates:
- [github.com/asottile/pyupgrade: v2.25.0 → v2.26.0](https://github.com/asottile/pyupgrade/compare/v2.25.0...v2.26.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-09-13 15:58:30 -04:00
Ralf W. Grosse-Kunstleve
ce62ec56d7 Merge branch 'master' into sh_merge_master 2021-09-12 19:55:42 -07:00
Ralf W. Grosse-Kunstleve
6c65ab5950
Follow-on to PR #3254, to address user code breakages. (#3263)
* Restoring `const` removed from pytypes.h in PR #3254, adding tests reflective of user code that breaks when those `const` are removed.

* clang-tidy NOLINTs (and one collateral fix).

* Inserting PYBIND11_CONST_FOR_STRICT_PLATFORMS

* Trying `defined(__APPLE__)`

* Trying again: `auto it` for strict platforms.

* Adding NOLINTNEXTLINE(bugprone-macro-parentheses), expanding comments.

* Labeling all changes with `PR #3263`, for easy reference, and to make it easy to undo these changes if we decide to do so in the future.
2021-09-12 19:53:26 -07:00
Aaron Gokaslan
9978ed588b
Fix capsule bug (#3261)
Thanks Aaron for jumping in fixing this!
2021-09-10 11:23:32 -07:00
Laramie Leavitt
0e599589fe
Fix thread safety for pybind11 loader_life_support (#3237)
* Fix thread safety for pybind11 loader_life_support

Fixes issue: https://github.com/pybind/pybind11/issues/2765

This converts the vector of PyObjects to either a single void* or
a per-thread void* depending on the WITH_THREAD define.

The new field is used by each thread to construct a stack
of loader_life_support frames that can extend the life of python
objects.

The pointer is updated when the loader_life_support object is allocated
(which happens before a call) as well as on release.

Each loader_life_support maintains a set of PyObject references
that need to be lifetime extended; this is done by storing them
in a c++ std::unordered_set and clearing the references when the
method completes.

* Also update the internals version as the internal struct is no longer compatible

* Add test demonstrating threading works correctly.

It may be appropriate to run this under msan/tsan/etc.

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

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

* Update test to use lifetime-extended references rather than
std::string_view, as that's a C++ 17 feature.

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

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

* Make loader_life_support members private

* Update version to dev2

* Update test to use python threading rather than concurrent.futures

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

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

* Remove unnecessary env in test

* Remove unnecessary pytest in test

* Use native C++ thread_local in place of python per-thread data structures to retain compatability

* clang-format test_thread.cpp

* Add a note about debugging the py::cast() error

* thread_test.py now propagates exceptions on join() calls.

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

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

* remove unused sys / merge

* Update include order in test_thread.cpp

* Remove spurious whitespace

* Update comment / whitespace.

* Address review comments

* lint cleanup

* Fix test IntStruct constructor.

* Add explicit to constructor

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
2021-09-10 12:29:21 -04:00
Ralf W. Grosse-Kunstleve
121b91f99c
Fixing NOLINT mishap (#3260)
* Removing NOLINT pointed out by Aaron.

* Removing another NOLINT.
2021-09-10 10:16:09 -04:00
Aaron Gokaslan
ae07d4c6c6
maint(Clang-Tidy): readability-const-return (#3254)
* Enable clang-tidy readability-const-return

* PyTest functional

* Fix regression

* Fix actual regression

* Remove one more NOLINT

* Update comment
2021-09-09 21:27:36 -07:00
Jeremy Maitin-Shepard
4d5ad03e1f
Avoid use of temporary bytes object in string_caster for UTF-8 (#3257)
Fixes #3252
2021-09-09 15:56:10 -04:00
Aaron Gokaslan
4c6bee3514
fix: Set __file__ constant when using eval_file (#1300) (#3233)
* Set __file__ constant when using eval_file

* Use const ref

* Use a move instead

* Revert

* Improve test

* Guard test with Python version

* Fix tests

* Dont support Python2 API

* Drop Python2 eval __file__ support

* Hack

* Semisupport Python2

* Take2

* Remove Python2 support
2021-09-09 14:06:33 -04:00
Ralf W. Grosse-Kunstleve
b4e1ac9a94 clang-tidy fixes related to PR #3250 2021-09-08 22:05:23 -07:00
Ralf W. Grosse-Kunstleve
94a5c673bc Merge branch 'master' into sh_merge_master 2021-09-08 18:56:35 -07:00
Ralf W. Grosse-Kunstleve
6abf2baa62
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy

* clang-tidy explicit attr.h (all automatic)

* clang-tidy explicit cast.h (all automatic)

* clang-tidy detail/init.h (1 NOLINT)

* clang-tidy detail/type_caster_base.h (2 NOLINT)

* clang-tidy pybind11.h (7 NOLINT)

* clang-tidy detail/common.h (3 NOLINT)

* clang-tidy detail/descr.h (2 NOLINT)

* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)

* clang-tidy eigen.h (7 NOLINT, 0 explicit)

* Adding 2 explicit in functional.h

* Adding 4 explicit in iostream.h

* clang-tidy numpy.h (1 NOLINT, 1 explicit)

* clang-tidy embed.h (0 NOLINT, 1 explicit)

* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)

* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)

* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)

* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)

* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)

* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)

* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)

* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)

* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)

* clang-tidy tests/object.h (0 NOLINT, 2 explicit)

* clang-tidy batch of fully automatic fixes.

* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-08 18:53:38 -07:00
Thomas Ballinger
39a0aac88e
docs fix to avoid nonexistent SmartCompile (#3241) 2021-09-08 14:00:00 -04:00
Aaron Gokaslan
d71ba0cb73
(perf): Add a missing noexcept to a pytype constructor (#3236)
* Add a missing noexcept to pytypes constructor.

Adds a few missing noexcept to PyType constructors for perf reasons.

* Revert exception ctor
2021-09-02 15:18:42 -04:00
Ralf W. Grosse-Kunstleve
49173e472f
Minor follow-on to PR #1334 (Fix enum value's __int__ returning non-int when underlying type is bool or of char type) (#3232)
* Minor tweaks.

* Restoring tests/pybind11_tests.h version from master, removing just the comment and empty line that was added in PR #3087; those were made obsolete by the pragma cleanup that concluded with PR #3186.

* More-to-the-point test for Python 3.
2021-08-31 08:52:04 -07:00
Ralf W. Grosse-Kunstleve
a46f623782
Minor tweaks. (#3230)
* Minor tweaks.

* Restoring tests/pybind11_tests.h version from master, removing just the comment and empty line that was added in PR #3087; those were made obsolete by the pragma cleanup that concluded with PR #3186.

* [ci skip] Restoring tests/test_enum.py from master.
2021-08-31 06:44:14 -07:00
Tailing Yuan
d6474ed7d2
fix: memory leak in cpp_function (#3228) (#3229)
* fix: memory leak in cpp_function (#3228)

* add a test case to check objects are deconstructed in cpp_function

* update the test case about cpp_function

* fix the test case about cpp_function: remove "noexcept"

* Actually calling func. CHECK(stat.alive() == 2); Manually verified that the new tests fails without the change in pybind11.h

* Moving new test to test_callbacks.cpp,py, with small enhancements.

* Removing new test from test_interpreter.cpp (after it was moved to test_callbacks.cpp,py). This restores test_interpreter.cpp to the current state on master.

* Using py::detail::silence_unused_warnings(py_func); to make the intent clear.

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
2021-08-30 21:48:33 -04:00
pre-commit-ci[bot]
76d939de53
[pre-commit.ci] pre-commit autoupdate (#3231)
updates:
- [github.com/asottile/pyupgrade: v2.24.0 → v2.25.0](https://github.com/asottile/pyupgrade/compare/v2.24.0...v2.25.0)
- [github.com/psf/black: 21.7b0 → 21.8b0](https://github.com/psf/black/compare/21.7b0...21.8b0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-08-30 16:00:33 -04:00
Ralf W. Grosse-Kunstleve
f33da1831a Merge branch 'master' into sh_merge_master 2021-08-28 16:46:08 -07:00
Ralf W. Grosse-Kunstleve
777352fcd1
Adding ssize_t_cast to support passing size_t or ssize_t values where ssize_t is needed. (#3219)
* Trivial change to avoid (ssize_t) cast.

* Demo for safe_ssize_t idea.

* Removing safe_ssize_t.cpp (proof-of-concept code) to not upset the GHA Format workflow.

* Completing changes in pytypes.h

* New ssize_t_cast (better replacement for safe_ssize_t).

* clang-format-diff (no manual changes).

* bytes_ssize_t -Wnarrowing reproducer (see PR #2692).

* Backing out tuple(), list() ssize_t support, for compatibility with older compilers (to resolve link failures).

* Bug fix: missing `py::` for `py::ssize_t`

* Restoring tuple(), list() ssize_t support, but passing `size` by value, for compatibility with older compilers (to resolve link failures).

* Full test coverage of all functions with modified signatures.
2021-08-28 16:40:46 -07:00
Ralf W. Grosse-Kunstleve
c8953eb982 Adding NOLINT related to https://github.com/pybind/pybind11/pull/3210 2021-08-26 16:19:39 -07:00
Ralf W. Grosse-Kunstleve
4ed3417faa Tracking ci.yml changes from master. 2021-08-26 15:03:41 -07:00
Ralf W. Grosse-Kunstleve
a655f95a83 Merge branch 'master' into sh_merge_master 2021-08-26 14:46:44 -07:00
Ye Zhihao
cb60ed49e4
Fix enum value's __int__ returning non-int when underlying type is bool or of char type (#1334)
* Use equivalent_integer for enum's Scalar decision

* Add test for char underlying enum

* Support translating bool type in enum's Scalar

* Add test for bool underlying enum

* Fix comment in test

* Switch from `PYBIND11_CPP20` macro to `PYBIND11_HAS_U8STRING`

* Refine tests

Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
2021-08-26 14:34:24 -07:00
Dan
930bb16c79
Call PySys_SetArgv when initializing interpreter. (#2341)
* Call PySys_SetArgv when initializing interpreter.

* Document argc/argv parameters in initialize_interpreter.

* Remove manual memory management from set_interpreter_argv in favor of smart pointers.

* Use size_t for indexers in set_interpreter_argv.

* Minimize macros for flow control in set_interpreter_argv.

* Fix 'unused variable' warning on Py2

* whitespace

* Define wide_char_arg_deleter outside set_interpreter_argv.

* Do sys.path workaround in C++ rather than eval.

* Factor out wchar conversion to a separate function.

* Restore widened_argv variable declaration.

* Fix undeclared widened_arg variable on some paths.

* Use delete[] to match new wchar_t[].

* Fix compiler errors

* Use PY_VERSION_HEX for a cleaner CVE-2008-5983 mode check.

* Fix typo

* Use explicit type for deleter so delete[] works cross-compiler.

* Always use PySys_SetArgvEx because pybind11 doesn't support pythons that don't include it.

* Remove pointless ternary operator.

* Use unique_ptr.reset instead of a second initialization.

* Rename add_program_dir_to_path parameter to clarify intent.

* Add defined() check before evaluating HAVE_BROKEN_MBSTOWCS.

* Apply clang-tidy fixes

* Pre-commit

* refactor: use const for set_interpreter_argv

* Try to fix const issue and allocate vector properly

* fix: copy strings on Python 2

* Applying clang-format-diff relative to master.

The only manual change is an added empty line between pybind11 and system `#include`s.

```
git diff -U0 --no-color master | python3 $HOME/clone/llvm-project/clang/tools/clang-format/clang-format-diff.py -p1 -style=file -i
```

Co-authored-by: Boris Staletic <boris.staletic@gmail.com>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
2021-08-26 14:12:54 -07:00
Nick Cullen
503ff2a6fb
view for numpy arrays (#987)
* reshape

* more tests

* Update numpy.h

* Update test_numpy_array.py

* array view

* test

* Update test_numpy_array.cpp

* Update numpy.h

* Update numpy.h

* Update test_numpy_array.cpp

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

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

* Fix merge bug

* Make clang-tidy happy

* Add xfail for PyPy

* Fix casting issue

* Fix formatting

* Apply clang-tidy

* Address reviews on additional tests

* Fix ordering

* Do a little more reordering

* Fix typo

* Try improving tests

* Fix error in reshape

* Add one more reshape test

* Fix bugs and add test

* Relax test

* streamlining new tests; removing a few stray msg

* Fix style revert

* Fix clang-tidy

* Misc tweaks:
* Comment: matching style in file (///), responsibility sentence, consistent punctuation.
* Replacing `unsigned char` with `uint8_t` for max consistency.
* Removing `1` from `array_view1` because there is only one.

* Partial clang-format-diff.

Co-authored-by: ncullen93 <ncullen.th@dartmouth.edu>
Co-authored-by: NC Cullen <nicholas.c.cullen.th@dartmouth.edu>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf Grosse-Kunstleve <rwgk@google.com>
2021-08-26 14:11:01 -07:00
Henry Schreiner
db44afa33b
tests: fix pytest usage on Python 3.10 (#3221) 2021-08-26 10:52:13 -07:00
Nick Cullen
59ad1e7d05
reshape for numpy arrays (#984)
* reshape

* more tests

* Update numpy.h

* Update test_numpy_array.py

* Update numpy.h

* Update numpy.h

* Update test_numpy_array.cpp

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

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

* Fix merge bug

* Make clang-tidy happy

* Add xfail for PyPy

* Fix casting issue

* Address reviews on additional tests

* Fix ordering

* Do a little more reordering

* Fix typo

* Try improving tests

* Fix error in reshape

* Add one more reshape test

* streamlining new tests; removing a few stray msg

Co-authored-by: ncullen93 <ncullen.th@dartmouth.edu>
Co-authored-by: NC Cullen <nicholas.c.cullen.th@dartmouth.edu>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf Grosse-Kunstleve <rwgk@google.com>
2021-08-26 08:12:35 -07:00
Jouke Witteveen
031a700dfd
Add make_simple_namespace function and tests (#2840)
Co-authored-by: Jouke Witteveen <j.witteveen@cosine.nl>
2021-08-26 08:04:22 -07:00
Ralf W. Grosse-Kunstleve
c8ce4b8df8
Clone of @virtuald's PR #2112 with minor enhancements. (#3215)
* Add py::raise_from to enable chaining exceptions on Python 3.3+

* Use 'raise from' in initialization

* Documenting the exact base version of _PyErr_FormatVFromCause, adding back `assert`s.

Co-authored-by: Dustin Spicuzza <dustin@virtualroadside.com>
2021-08-23 17:30:01 -07:00
Aaron Gokaslan
6cbabc4b8c
maint(clang-tidy): Enable cpp-coreguideline slicing checks (#3210)
* maint(clang-tidy): add a clang-tidy slicing check

* Add self + touch up readme

* Fix typo
2021-08-23 18:42:19 -04:00
Henry Schreiner
fdac5fbf7c
chore: support targeting different Python versions with nox (#3214) 2021-08-23 18:05:54 -04:00
pre-commit-ci[bot]
b3d18f382f
[pre-commit.ci] pre-commit autoupdate (#3213)
updates:
- [github.com/asottile/pyupgrade: v2.23.3 → v2.24.0](https://github.com/asottile/pyupgrade/compare/v2.23.3...v2.24.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-08-23 15:22:12 -04:00
Liam Keegan
4f29b8a45b
ci: extend msys2 mingw CI (#3207)
* extend msys2 CI

- add 32-bit job
- add c++11/17 c++/interface tests copied from standard ci
- add numpy/scipy

* account for padding of PartialStruct in numpy dtypes test with mingw32

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

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

* msys2 ci: add c++14 tests

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-08-19 14:42:55 -04:00
Ralf W. Grosse-Kunstleve
b176057abc Merge branch 'master' into smart_holder 2021-08-19 11:40:24 -07:00
Ralf W. Grosse-Kunstleve
998d45e431
Cleanup of file-scoped and globally-scoped warning suppression pragmas across pybind11 header files. (#3201)
* Removing all MSVC C4127 warning suppression pragmas.

* Removing MSVC /WX (WERROR). To get a full list of all warnings.

* Inserting PYBIND11_SILENCE_MSVC_C4127. Changing one runtime if to #if.

* Changing PYBIND11_SILENCE_MSVC_C4127 macro to use absolute namespace (for use outside pybind11 include directory).

* Restoring MSVC /WX (WERROR).

* Removing globally-scoped suppression for clang -Wunsequenced. Based on an experiment under PR #3202 it is obsolete and can simply be removed.
2021-08-19 11:37:04 -07:00
Ralf W. Grosse-Kunstleve
774b5ff90b
Removing obsolete eigen.h warning suppression pragmas. (#3198)
* Removing all pragma from eigen.h

* Removing -Werror or equivalent from tests/CMakeLists.txt

* Restoring tests/CMakeLists.txt from master.

* Adding 4 PYBIND11_SILENCE_MSVC_C4127.

* Compatibility with -Wconversion, -Wdeprecated

* Introducing PYBIND11_COMPATIBILITY_WDEPRECATED_COPY

* Systematically using --verbose for compilations where possible (cmake 3.14 or newer).

Also changing all `cmake -t` to `--target`, `-v` to `--verbose`, `check` to `pytest`, for consistency (to make it easier to pin-point all commands of a certain type).

Also removing one `-j 2` for `pytest` in hopes of reducing flakes due to races in test_iostream and in prints from destructors.

* Commenting out pragmas as an experiment to reproduce previous observation.

* Removing all (newly added, but already commented-out) pragma code, adding HINT use -isystem (as cmake does).

* Restoring ci.yml from master. Those changes are better handled separately. BTW: in the last CI run there was still a test_iostream flake, even without the -j 2 for running the tests (verfied by inspecting the log).
2021-08-17 16:49:39 -07:00
Ralf W. Grosse-Kunstleve
a0a30c4532 Merge branch 'master' into sh_merge_master 2021-08-14 12:46:18 -07:00
Ralf W. Grosse-Kunstleve
e41fb99e7e clang-tidy fixes (mostly manual) related to PR #3166 2021-08-14 12:45:50 -07:00
Aaron Gokaslan
617cb653ec
[Bugfix] Fix errant const methods (#3194)
* Fix errant const methods

* Remove NOLINT since clang-tidy is pretty conservative

* Missed one

* Fix a few more errors

* Add reviewer suggested comments

* Run clang-format
2021-08-14 12:25:54 -04:00
Ralf W. Grosse-Kunstleve
e3e1d29fdb Removing obsolete detail/pragma_warning_block.h 2021-08-14 08:42:38 -07:00
Ralf W. Grosse-Kunstleve
68d1aa83ac Tracking ci.yml changes from master. 2021-08-14 08:38:56 -07:00