Commit Graph

399 Commits

Author SHA1 Message Date
David Hewitt 7d357ad992 release: 0.19.1 2023-07-03 16:18:24 +01:00
mejrs 51a6863440 Give a better error message for Python in traverse 2023-06-19 23:25:51 +02:00
Alex Gaynor afbb1d435c
Upgrade to syn 2 2023-06-14 07:03:33 -04:00
David Hewitt 4f3fcdbf0f remove all functionality deprecated in PyO3 0.18 2023-06-13 22:27:43 +01:00
Adam Reichold 0d30bb7d3d Bump Rust edition to 2021 and make current Clippy happy. 2023-06-07 21:15:38 +02:00
bors[bot] dbf7b233aa
Merge #3203
3203: support ordering magic methods for `#[pyclass]` r=adamreichold a=davidhewitt

Closes #2089 

This adds `__lt__`, `__le__`, `__eq__`, `__ne__`, `__gt__`, and `__ge__` as per the Python implementations of what we call `__richcmp__`.

There's a UI test confirming that the user cannot implement split forms and `__richcmp__` simultaneously.

There's also a benchmark comparing implementing these split methods against using `__richcmp__`. I couldn't see a meaningful performance difference, so I'm tempted to deprecate `__richcmp__`, given that's not a magic method which exists in Python. Potentially we can provide options such as the opt-in `#[pyclass(eq, ord)]` to avoid boilerplate for people who don't want to implement six different methods.



Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-06-05 07:13:23 +00:00
Adam Reichold 5738edfdd4 Bump MSRV to 1.56 2023-06-04 23:02:30 +01:00
David Hewitt 8850d5d384 support ordering magic methods for `#[pyclass]` 2023-06-04 13:58:18 +01:00
David Hewitt 45b5ede26a remove copyright headers from source files 2023-06-03 22:38:54 +01:00
David Hewitt 2500e22e13 release: 0.19.0 2023-05-31 13:53:37 +01:00
Kang Seonghoon c2d4e8ad1f Use a full path for `#[new]` method which is also `#[classmethod]`. 2023-05-29 11:01:48 +09:00
Adam Reichold 18397828e5 Move locking the GIL and handling the resulting panics into a library function. 2023-05-25 20:01:35 +02:00
Adam Reichold 810ad00a76 Do not apply deferred ref count updates and prevent the GIL from being acquired inside of implementations. 2023-05-25 20:01:35 +02:00
Kang Seonghoon 24343f2caa Fix a span site of `_slf` for custom receivers. 2023-05-23 10:25:57 +09:00
Stu Hood 20c5618160
Add support for combining the `#[new]` and `#[classmethod]` method types. 2023-05-16 15:51:14 -07:00
Adam Reichold 91cdfe4f7d Fix patch version of our dependency on syn. 2023-05-10 22:31:15 +02:00
bors[bot] c27a6333d6
Merge #3029
3029: use dynamic trampoline for all getters and setters r=adamreichold a=davidhewitt

This is an extension to the "trampoline" changes made in #2705 to re-use a single trampoline for all `#[getter]`s (and similar for all `#[setters]`). It works by setting the currently-unused `closure` member of the `ffi::PyGetSetDef` structure to point at a new `struct GetSetDefClosure` which contains function pointers to the `getter` / `setter` implementations.

A universal trampoline for all `getter`, for example, then works by reading the actual getter implementation out of the `GetSetDefClosure`.

Advantages of doing this:
- Very minimal simplification to the macro code / generated code size. It made a 4.4% reduction to `test_getter_setter` generated size, which is an exaggerated result as most code will probably have lots of bulk that isn't just the macro code.

Disadvantages:
- Additional level of complexity in the `getter` and `setter` trampolines and accompanying code.
- To keep the `GetSetDefClosure` objects alive, I've added them to the static `LazyTypeObject` inner.
- Very slight performance overhead at runtime (shouldn't be more than an additional pointer read). It's so slight I couldn't measure it.

Overall I'm happy to either merge or close this based on what reviewers think!

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-05-09 08:50:45 +00:00
David Hewitt 1ea57cb9ad use dynamic trampoline for all getters and setters 2023-05-09 08:09:59 +01:00
Adam Reichold a60945faa2 Do not store return values in locals so that holders benefit from lifetime extension for temporaries. 2023-05-06 16:26:18 +02:00
bors[bot] 1cdac4fde4
Merge #2980
2980: support `text_signature` on `#[new]` r=adamreichold a=davidhewitt

Closes #2866 

This is a breaking change for 0.19.0, because it starts autogenerating `text_signature` for `#[new]`. This could affect runtime behaviour if the user is relying on the class docs at runtime for some reason.

Guide & tests all updated accordingly.

`#[pyclass(text_signature = "...")]` is deprecated by this PR, however if it's set, it will be used in preference to `#[new]`.

(The signature / `text_signature` from `#[new]` will simply be ignored in this case. I figure that when users fix their deprecation warnings by removing `#[pyclass(text_signature = "...")]` then the `#[new]` signatures will start flowing properly, and this is good enough.)

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-05-04 16:51:54 +00:00
David Hewitt 8bd17f02c7 support `text_signature` on `#[new]` 2023-05-04 07:15:11 +01:00
BlueGlassBlock f75ea3d93a
test: improve cov 2023-04-27 12:44:38 +08:00
BlueGlassBlock 1d6ab21781
fix: change to from_item_all 2023-04-26 19:47:07 +08:00
BlueGlassBlock 30b2ed1d85
feat: add #[pyo3(item_all)] for FromPyObject 2023-04-25 19:37:05 +08:00
Adam Reichold 2c1762949e Fix some Clippy warnings only visible in the CI. 2023-04-14 18:41:45 +02:00
David Hewitt bfcc1f8c27 update release notes for 0.18.3 2023-04-13 22:47:55 +01:00
messense 57dbc94293
Improve default value for `None` in `text_signature` 2023-03-29 10:14:44 +08:00
bors[bot] ebedcfb8b2
Merge #3050
3050: Improve default values for str, numbers and bool in `text_signature` r=davidhewitt a=messense

xref #2863

Co-authored-by: messense <messense@icloud.com>
2023-03-25 07:07:06 +00:00
bors[bot] e2bcca4771
Merge #3063
3063: update release notes for 0.18.2 r=davidhewitt a=davidhewitt

Changelog entry & cargo.toml changes from 0.18.2 

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-03-24 21:44:48 +00:00
David Hewitt e7e58ba51e update release notes for 0.18.2 2023-03-24 20:40:06 +00:00
bors[bot] e5e8c7a6d0
Merge #2975 #3022 #3023
2975: RFC: Add GILProtected synchronization primitive and use it for LazyTypeObjectInner. r=davidhewitt a=adamreichold

I would also like to use that type in rust-numpy and it seems we can avoid ~~both a manual unsafe impl and~~ a full blown mutex if we apply it to `LazyTypeObjectInner`.

One downside might be that it ties us closer to the GIL when we want to enable nogil experimentation, but on the other hand, it may also help by reifying the GIL usage. (This is currently limited to comments in unsafe code in rust-numpy for example.)

3022: Fix function name shadowing r=davidhewitt a=mejrs

Fixes https://github.com/PyO3/pyo3/issues/3017

3023: Emit a better error for bad argument names r=davidhewitt a=mejrs

This will emit a better error for code like 
```rust
#[pyfunction]
fn output([a,b,c]: [u8;3]) {}
```



Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
Co-authored-by: mejrs <59372212+mejrs@users.noreply.github.com>
2023-03-23 08:04:11 +00:00
mejrs 1ecc71638d Emit a better error for bad argument names 2023-03-23 07:43:54 +00:00
mejrs 4966d93494 Fix function name shadowing 2023-03-23 07:33:14 +00:00
Will Stott bcdb17db5b Fix compile error when trying to use static slot methods 2023-03-18 11:16:52 +00:00
messense 79a70f3923
Improve default values for str, numbers and bool in `text_signature` 2023-03-16 11:06:51 +08:00
David Hewitt 7a2b1f0e13 fix `non_snake_case` lint for `#[pyfunction]` generated code 2023-02-28 08:44:17 +00:00
David Hewitt 226bf97ec0 Fix `clippy::redundant_closure` lint firing for pyfunction defaults 2023-02-27 22:10:06 +00:00
David Hewitt c7cc48f8e4 use PyO3 types within LazyTypeObject 2023-02-18 09:06:36 +00:00
David Hewitt 00ddd21535 change PyModule::add_class to return an error if class creation fails 2023-02-14 22:08:35 +00:00
David Hewitt 201964ce3b don't parse doc attributes unnecessarily 2023-02-13 21:05:44 +00:00
David Hewitt dbeb3b4453 move some private internals out of public implementation 2023-02-11 21:28:27 +00:00
David Hewitt 92cca896de release notes for 0.18.1 2023-02-07 21:38:07 +00:00
David Hewitt 96efb0eda9 add better error message for Python in signature 2023-02-03 06:57:30 +00:00
bors[bot] d118ee3a73
Merge #2923 #2924
2923: hygiene: fix `#[pymethods(crate = "...")]` r=davidhewitt a=davidhewitt

Got to the bottom of the hygiene issue in test of #2914 

Turns out that `#[pymethods] #[pyo3(crate = "...")]` works, but `#[pymethods(crate = "...")]` was ignoring the argument.

Added a tweak to fix this and a snippet in the hygiene test (which fails on `main`). 

2924: remove unneeded into_iter calls r=davidhewitt a=davidhewitt

Clippy complaining about these to me this morning locally.

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-01-27 09:31:20 +00:00
David Hewitt 1a4153f718 remove unneeded into_iter calls 2023-01-27 08:20:03 +00:00
David Hewitt e4a3d2893a refactor docstring generation code 2023-01-27 06:38:04 +00:00
David Hewitt 89d4ae1dbf rename `wrap_pyfunction` impl to `wrap_pyfunction_impl` 2023-01-19 08:51:44 +00:00
David Hewitt 224a4160b4 release: 0.18.0 2023-01-17 19:04:30 +00:00
Sunyeop Lee 8026f3521e Improve derive(FromPyObject) to apply intern! when applicable 2023-01-15 13:39:06 +00:00
David Hewitt 8f48d157d6 deprecate required arguments after option arguments without signature 2023-01-15 10:17:20 +00:00
bors[bot] 8af48bbb53
Merge #2796
2796: Forward cfgs on pyclass fields to the method defs r=davidhewitt a=mejrs

With this and the cfg_attr PR, I don't need cfg_eval at all anymore :)


- [x] needs some more tests

Co-authored-by: mejrs <>
2022-12-29 16:28:26 +00:00
David Hewitt f2608a923c remove functionality deprecated in 0.16 2022-12-28 12:23:53 +00:00
David Hewitt 5039fd746a add automatic text signature generation 2022-12-24 09:43:02 +00:00
mejrs 44310ec7e0 Forward cfgs on pyclass fields to the method defs 2022-12-06 22:38:32 +01:00
bors[bot] 08423557d4
Merge #2739
2739: error when `#[pyo3(signature = ())]` used on invalid methods r=davidhewitt a=davidhewitt

A follow-up to #2702 to reject some invalid applications of `#[pyo3(signature = (...))]` attribute, specifically on magic methods and getters / setters / class attributes.

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2022-11-22 19:41:27 +00:00
David Hewitt 8ca41be87b
Merge pull request #2749 from PyO3/2748-fix
Fix being able to call arg-less `#[new]` with any args from Python
2022-11-20 11:39:42 +00:00
Georg Brandl ea9da80ab1 macros: fix the check for applying `METH_NOARGS`
to only consider the Python argument list.

Fixes #2750
2022-11-20 09:00:42 +01:00
Georg Brandl 2a630a2a52 Fix being able to call arg-less `#[new]` with any args from Python
Fixes #2748
2022-11-20 08:25:19 +01:00
David Hewitt 00fc0358b9 error when `#[pyo3(signature = ())]` used on invalid methods 2022-11-17 08:28:34 +00:00
David Hewitt 548e90fcec release: 0.17.3 2022-11-01 20:47:46 +00:00
David Hewitt 8e8b484169
add `#[pyo3(signature = (...))]` attribute (#2702) 2022-10-25 07:23:21 +01:00
David Hewitt 747d791f1f
introduce trampolines for methods (#2705) 2022-10-25 07:22:36 +01:00
David Hewitt 446c0e82f0
Merge pull request #2692 from mejrs/all
Implement get/set all on pyclass
2022-10-18 21:39:44 +01:00
mejrs 08c8b845aa Create better error spans/messages 2022-10-18 21:14:22 +02:00
mejrs d254134154 Implement get/set all on pyclass 2022-10-17 02:37:43 +02:00
Bruno Kolenbrander 4a04603c2c
Don't use intocallback in method macros (#2664)
* Don't use intocallback in method macros

Co-authored-by: mejrs <>
2022-10-16 10:35:58 +01:00
David Hewitt bd40011e93 release: 0.17.2 2022-10-04 08:13:53 +01:00
David Hewitt c5ba1f0632
pyclass: better error and explanation why lifetimes are disallowed (#2633)
* pyclass: better error and explanation why lifetimes are disallowed

* extend detail on lifetimes
2022-09-22 11:14:06 +02:00
David Hewitt c445eba28d pyfunction: fix compile error for Option<&T> argument with a default 2022-09-20 15:44:55 +02:00
Jonathan Coates 26a9603519 Some improvements to __richcmp__ on enums
- Implement __ne__ as well as __eq__.
 - Return NotImplemented when types cannot be converted, rather than
   throwing.
 - Compare the integer ids inside the __eq__/__ne__ implementation.
   Previously a match block was generated.
2022-09-16 15:30:56 +01:00
David Hewitt 5718adeec7 pyproto: remove deprecated feature 2022-09-06 08:38:44 +01:00
David Hewitt caaf7bbda7 release: 0.17.1 2022-08-28 08:27:56 +01:00
David Hewitt 058af11c4d pyfunction: fix clippy lint on **kwargs argument 2022-08-26 20:27:44 +01:00
David Hewitt 9d543b3f4b pyfunction: fix from_py_with on Option<T> argument 2022-08-25 18:57:17 +01:00
David Hewitt 04c1ac87f1 release: 0.17.0 2022-08-23 21:21:19 +01:00
David Hewitt d0492b7c72
Merge pull request #2570 from davidhewitt/pyclass-frozen-tidy
pyclass: tidy up frozen implementation
2022-08-21 09:35:51 +01:00
David Hewitt 24456f3f41 pyclass: tidy up frozen implementation 2022-08-21 08:21:06 +01:00
David Hewitt fd8026c7bb pyclass: add `sequence` option to implement `sq_length` 2022-08-20 07:14:26 +01:00
David Hewitt a3f093d7f0 safety: abort on uncaught panics 2022-08-14 13:47:46 +01:00
David Hewitt fa19f322d2
Merge pull request #2503 from davidhewitt/extract_argument_holder
pyfunction: use extract_argument with holder to avoid extractext
2022-07-17 07:14:53 +01:00
David Hewitt d5e99b635d refactor: remove all 0.15 deprecations 2022-07-15 06:33:14 +01:00
David Hewitt 209c942277 pyfunction: use extract_argument with holder to avoid extractext 2022-07-14 08:42:22 +01:00
David Hewitt c4a2c6c912 clippy: fix some warnings from beta toolchain 2022-07-12 23:02:47 +01:00
David Hewitt 4da9c3a55f llvm-lines: use iterator to collect class items 2022-06-25 22:03:28 +01:00
David Hewitt e8e1729723
Merge pull request #2470 from davidhewitt/monomorphizations
opt: remove some generic code bloat
2022-06-22 23:44:57 +01:00
David Hewitt 3d9b78062e opt: remove some generic code bloat 2022-06-22 22:53:32 +01:00
Gabriel Smith 75656949f9 macros: Support #[pyo3(name)] on enum variants 2022-06-22 09:36:40 -04:00
Gabriel Smith 2122faa547 macros: Use macro-defined name for enums in __repr__ 2022-06-21 16:31:23 -04:00
David Hewitt 7725f17c46 pyclass: switch from immutable to frozen 2022-06-19 21:18:15 +01:00
David Hewitt f81a01b604 macros: emit pymethod associated methods as a single block 2022-06-16 22:15:29 +01:00
Ivan Krivosheev f19561c745 Allow #[classattr] take Python argument 2022-06-16 13:08:53 +03:00
David Hewitt 7c56a03d64 frompyobject: fix `from_py_with` ignored for transparent structs 2022-06-09 06:49:25 +01:00
David Hewitt 0aa4f95a98 frompyobject: improve error messages of derived impls 2022-06-02 11:13:35 +01:00
David Hewitt e4ec720d51 frompyobject: tidy up generated code 2022-06-02 08:54:20 +01:00
David Hewitt cfb91057af frompyobject: improve error message for tuple case 2022-06-02 08:54:20 +01:00
David Hewitt eafbbc5417
Merge pull request #2399 from davidhewitt/avoid-duplicate-pymethods
pymethods: prevent methods sharing the same name
2022-05-24 22:22:31 +01:00
David Hewitt 879eb14e27
Merge pull request #2395 from davidhewitt/fix-class-raw-ident
macros: fix raw-ident pyclasses having r# at the start of the Python name
2022-05-24 22:08:56 +01:00
David Hewitt 71abeeff8b macros: fix raw-ident pyclasses having r# at the start of the Python name 2022-05-24 21:34:23 +01:00
David Hewitt a306365db8 pymethods: prevent methods sharing the same name 2022-05-24 21:15:30 +01:00
David Hewitt 126bf49b8b
Merge pull request #2377 from herquan/herquan_cr1
Add macro append_to_inittab (issue #2359)
2022-05-24 08:02:47 +01:00