Commit Graph

6378 Commits

Author SHA1 Message Date
Bruno Kolenbrander 6a0221ba2c
document FnType and refactor FnType::self_arg (#4276) 2024-06-23 10:36:19 +00:00
Aneesh Agrawal c67625d683
Revamp PyType name functions to match PEP 737 (#4196)
* Revamp PyType name functions to match PEP 737

PyType::name uses `tp_name`, which is not consistent.
[PEP 737](https://peps.python.org/pep-0737/) adds a new path forward,
so update PyType::name and add PyType::{module,fully_qualified_name}
to match the PEP.

* refactor conditional code to handle multiple Python versions better

* return `Bound<'py, str>`

* fixup

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-06-22 22:10:27 +00:00
David Hewitt a2f9399906
make datetime from timestamp tests compare against Python result (#4275)
* attemp to fix range for st.datetime

* remove example

* try fixing to utc

* make datetime from timestamp tests compare against Python result

---------

Co-authored-by: Cheukting <cheukting.ho@gmail.com>
2024-06-22 22:09:59 +00:00
Code Apprentice 908ef6ad84
Implement PartialEq for PyBytes and [u8] (#4259)
* Copy pasta implementation from types/string.rs

* changelog

* I think I don't need a special implementation for 3.10 or ABI

* Copy pasta tests

* Fix comment with correct type

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* Fix implementation

* Use slice in tests

* Try renaming changelog file

* Fix doc example

* Fix doc example

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

---------

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>
2024-06-22 22:08:57 +00:00
Weijie Guo c4d18e5ee3
Change `search_lib_dir`'s return type to Result (#4043)
* Change `search_lib_dir`'s return type to Result

* add changelog

* add coverage and a hint to `PYO3_CROSS_LIB_DIR`

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-06-22 14:01:33 +00:00
David Hewitt 0b967d427a
use `ffi::MemberGef` for `#[pyo3(get)]` fields of `Py<T>` (#4254)
* use `ffi::MemberGef` for `#[pyo3(get)]` fields of `Py<T>`

* tidy up implementation

* make it work on MSRV :(

* fix docs and newsfragment

* clippy

* internal docs and coverage

* review: mejrs
2024-06-21 23:33:34 +00:00
David Hewitt 41fb9572b6
docs: update docstring on `Python` for `Bound` API (#4274) 2024-06-21 11:30:57 +00:00
Alex Gaynor 9ff3d237c1
Update dependencies to reflect minimal versions (#4272)
Based on testing locally with `rm -f Cargo.lock && cargo +nightly check --tests --all -Z minimal-versions`
2024-06-21 10:05:09 +00:00
Icxolu 56341cbc81
emit c-string literals on Rust 1.77 or later (#4269)
* emit c-string literals on Rust 1.77 or later

* only clone `PyO3CratePath` instead of the whole `Ctx`

Co-authored-by: mejrs <59372212+mejrs@users.noreply.github.com>

---------

Co-authored-by: mejrs <59372212+mejrs@users.noreply.github.com>
Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-06-21 08:49:33 +00:00
David Hewitt ca82681615
ci: minor cleanups following 1.63 MSRV (#4239)
* ci: minor cleanups following 1.63 MSRV

* correct `invalid_pymethods_duplicates` UI test

* fix `nightly` feature
2024-06-21 07:02:31 +00:00
Bruno Kolenbrander a983b2fe7b
Bump diagnostic_namespace rust version (#4268) 2024-06-20 21:56:17 +00:00
David Hewitt 30add032b5
improve code generated by `c_str!` macro (#4270)
* improve code generated by `c_str!` macro

* fix clippy
2024-06-20 21:41:16 +00:00
Bruno Kolenbrander b25b3b3a7b
Improve the span and message for return types of pymethod/functions (#4220)
* Improve the span and message for return types of pymethod/functions

* Don't pass the span

* fixup trybuild output

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-06-20 09:23:40 +00:00
WÁNG Xuěruì e6b2216b04
Add several missing wrappers to PyAnyMethods (#4264) 2024-06-20 08:16:06 +00:00
David Hewitt 0e142f05dd
add `c_str!` macro to create `&'static CStr` (#4255)
* add `c_str!` macro to create `&'static CStr`

* newsfragment, just export as `pyo3::ffi::c_str`

* fix doc link

* fix doc

* further `c_str!` based cleanups

* [review]: mejrs

Co-authored-by: Bruno Kolenbrander <59372212+mejrs@users.noreply.github.com>

* rustfmt

* build fixes

* clippy

* allow lint on MSRV

* fix GraalPy import

---------

Co-authored-by: Bruno Kolenbrander <59372212+mejrs@users.noreply.github.com>
2024-06-18 18:09:36 +00:00
Alex Gaynor ddff8bea25
Stabilize declarative modules (#4257) 2024-06-17 01:28:20 +00:00
Alex Gaynor baae9291cc
Update tests for numpy 2.0 (#4258) 2024-06-17 00:05:55 +00:00
Code Apprentice 79591f2161
Add error messages for unsupported macro features on compilation (#4194)
* First implementation

* tweak error message wording

* Fix boolean logic

* Remove redundant parens

* Add test for weakref error

* Fix test

* Reword error message

* Add expected error output

* Rinse and repeat for `dict`

* Add test output file

* Ignore Rust Rover config files

* cargo fmt

* Add newsfragment

* Update newsfragments/4194.added.md

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Use ensure_spanned! macro

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Use ensure_spanned! macro for weakref error, too

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Revert "Ignore Rust Rover config files"

This reverts commit 6c8a2eec581ed250ec792d8465772d649b0a3199.

* Update wording for error message

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* Update weakref error message, too

* Refactor constant to a pyversions module

* Fix compiler errors

* Another wording update

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* And make weakref wording the same

* Fix compiler error due to using weakref in our own code

* Fix after merge

* apply conditional pyclass

* update conditional compilation in tests

---------

Co-authored-by: cojmeister <luqas.c@gmail.com>
Co-authored-by: David Hewitt <mail@davidhewitt.dev>
Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>
2024-06-16 10:23:03 +00:00
David Hewitt 9648d595a5
implement `PartialEq<str>` for `Bound<'py, PyString>` (#4245)
* implement `PartialEq<str>` for `Bound<'py, PyString>`

* fixup conditional code

* document equality semantics for `Bound<'_, PyString>`

* fix doc example
2024-06-16 08:19:21 +00:00
David Hewitt 0b2f19b3c9
fix `__dict__` on Python 3.9 with limited API (#4251)
* fix `__dict__` on Python 3.9 with limited API

* [review] Icxolu suggestions

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* [review] Icxolu

* missing import

---------

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>
2024-06-16 07:57:44 +00:00
David Hewitt 591cdb0bf8
implement `PyModuleMethods::filename` on PyPy (#4249) 2024-06-14 19:08:35 +00:00
Icxolu 5749a08b63
ci: updates for Rust 1.79 (#4244)
* ci: updates for Rust 1.79

* ci: fix beta clippy

* ci: fix `dead_code` warning on nightly
2024-06-13 18:24:13 +00:00
David Hewitt f66124a79b
pypy/graalpy: set `Py_LIMITED_API` when `abi3` requested (#4237)
* pypy/graalpy: set `Py_LIMITED_API` when `abi3` requested

* add newsfragment
2024-06-10 07:26:05 +00:00
JRRudy1 d2dca2169c
Added `as_super` methods to `PyRef` and `PyRefMut`. (#4219)
* Added `PyRef::as_super` and `PyRefMut::as_super` methods, including docstrings and tests. The implementation of these methods also required adding `#[repr(transparent)]` to the `PyRef` and `PyRefMut` structs.

* Added newsfragment entry.

* Changed the `AsRef<U>`/`AsMut<U>` impls for `PyRef` and `PyRefMut` to use the new `as_super` methods. Added the `PyRefMut::downgrade` associated function for converting `&PyRefMut` to `&PyRef`. Updated tests and docstrings to better demonstrate the new functionality.

* Fixed newsfragment filename.

* Removed unnecessary re-borrows flagged by clippy.

* retrigger checks

* Updated `PyRef::as_super`, `PyRefMut::as_super`, and `PyRefMut::downgrade` to use `.cast()` instead of `as _` pointer casts. Fixed typo.

* Updated `PyRef::as_super` and `PyRefMut::downgrade` to use `ptr_from_ref` for the initial cast to `*const _` instead of `as _` casts.

* Added `pyo3::internal_tricks::ptr_from_mut` function alongside the `ptr_from_ref` added in PR #4240. Updated `PyRefMut::as_super` to use this method instead of `as *mut _`.

* Updated the user guide to recommend `as_super` for accessing the base class instead of `as_ref`, and updated the subsequent example/doctest to demonstrate this functionality.

* Improved tests for the `as_super` methods.

* Updated newsfragment to include additional changes.

* Fixed formatting.

---------

Co-authored-by: jrudolph <jrudolph@anl.gov>
2024-06-09 07:17:23 +00:00
David Hewitt 9c67057745
refactor: use `ptr_from_ref` and ptr `.cast()` (#4240)
* refactor: use `ptr_from_ref` and ptr `.cast()`

* fix unused imports
2024-06-07 22:47:27 +00:00
Michael Gilbert b8fb367582
feat: Add 'ord' option for PyClass and corresponding tests (#4202)
* feat: Add 'ord' option for PyClass and corresponding tests

Updated the macros back-end to include 'ord' as an option for PyClass allowing for Python-style ordering comparison of enum variants. Additionally, test cases to verify the proper functioning of this new feature have been introduced.

* update: fix formatting with cargo fmt

* update: documented added feature in newsfragments

* update: updated saved errors for comparison test for invalid pyclass args

* update: removed nested match arms and extended cases for ordering instead

* update: alphabetically ordered entries

* update: added section to class documentation with example for using ord argument.

* refactor: reduced duplication of code using closure to process tokens.

* update: used ensure_spanned macro to emit compile time errors for uses of ord on complex enums or structs, updated test errors for bad compile cases

* fix: remove errant character

* update: added note about PartialOrd being required.

* feat: implemented ordering for structs and complex enums.  Retained the equality logic for simple enums until PartialEq is deprecated.

* update: adjusted compile time error checks for missing PartialOrd implementations.  Refactored growing set of comparison tests for simple and complex enums and structs into separate test file.

* fix: updated with clippy findings

* update: added not to pyclass parameters on ord (assumes that eq will be implemented and merged first)

* update: rebased on main after merging of `eq` feature

* update: format update

* update: update all test output and doc tests

* Update guide/src/class.md

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* Update pyo3-macros-backend/src/pyclass.rs

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* Update newsfragments/4202.added.md

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* Update guide/pyclass-parameters.md

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* update: added note about `ord` implementation with example.

* fix doc formatting

---------

Co-authored-by: Michael Gilbert <git.3mc1o@aleeas.com>
Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>
2024-06-07 19:08:53 +00:00
David Hewitt fbb6f20c2b
migration: close all 0.20->0.21 items (#4238) 2024-06-07 14:26:36 +00:00
Thomas Tanon 74619143b6
Declarative modules: make sure to emit doc comments and other attributes (#4236)
* Declarative modules: make sure to emmit doc comments and other attributes

* Adds a test

* Apply suggestions from code review

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-06-06 21:19:37 +00:00
Adam Reichold c644c0b0b8
Lazy-initialize the global reference pool to reduce its overhead when unused (#4178)
* Add benchmarks exercising the global reference count decrement pool.

* Lazy-initialize the global reference pool to reduce its overhead when unused
2024-06-06 08:45:16 +00:00
Thomas Tanon 11d67b3acc
Automated module= field creation in declarative modules (#4213)
* Automated module= field creation in declarative modules

Sets automatically the "module" field of all contained classes and submodules in a declarative module

Adds the "module" field to pymodule attributes in order to set the name of the parent modules. By default, the module is assumed to be a root module

* fix guide test error

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-06-06 07:54:26 +00:00
Cheuk Ting Ho 37a5f6a94e
remove internal APIs from pyo3-ffi (#4201)
* remove internal APIs from pyo3-ffi

* fix formating

* add conditional import

* remove _Py_c_neg/abs/pow

* fix formating

* adding changelog

* expose PyAnyMethods::neg/pos/abs and use them

* Update src/types/any.rs

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Update src/types/any.rs

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Adding details to changelog

* update docs

* remove PyREsultExt import for GraalPy

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-06-05 21:21:44 +00:00
A. Cody Schuffelen 93ef056711
Use `Ident::parse_any` for `name` attributes (#4226)
This makes it possible to use rust keywords as the name of python class
methods and standalone functions. For example:

```
struct MyClass {
}

impl MyClass {
    #[new]
    fn new() -> Self {
        MyClass {}
    }

    #[pyo3(name = "struct")]
    fn struct_method(&self) -> usize {
        42
    }
}

fn struct_function() -> usize {
    42
}
```

From the [`syn::Ident`
documentation](https://docs.rs/syn/2.0.66/syn/struct.Ident.html):

> An identifier constructed with `Ident::new` is permitted to be a Rust
keyword, though parsing one through its
[`Parse`](https://docs.rs/syn/2.0.66/syn/parse/trait.Parse.html)
implementation rejects Rust keywords. Use `input.call(Ident::parse_any)`
when parsing to match the behaviour of `Ident::new`.

Fixes issue #4225
2024-06-03 19:45:36 +00:00
Icxolu 7e5884c40b
fix incorrect `__richcmp__` for `eq_int` only simple enums (#4224)
* fix incorrect `__richcmp__` for `eq_int` only simple enums

* add tests for deprecated simple enum eq behavior

* only emit deprecation warning if neither `eq` nor `eq_int` were given

* require `eq` for `eq_int`
2024-06-03 18:49:36 +00:00
Icxolu 36cdeb29c1
fix incorrect raw identifier handling for the `name` attribute of `pyfunction`s (#4229) 2024-06-03 17:32:35 +00:00
liammcinroy b4b780b475
Allow module= attribute in complex enum variants (#4228)
* Allow module= attribute in complex enum variants

* stderr test update

* towncrier

* inherit `module`, rather than specifying everywhere.

* clippy fix
2024-06-03 07:57:04 +00:00
David Hewitt 88b6f23e3b
fix calling POOL.update_counts() when no `gil-refs` feature (#4200)
* fix calling POOL.update_counts() when no `gil-refs` feature

* fixup conditional compilation

* always increment gil count

* correct test

* clippy fix

* fix clippy

* Deduplicate construction of GILGuard::Assumed.

* Remove unsafe-block-with-unsafe-function triggering errors in our MSRV builds.

---------

Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2024-06-02 11:11:14 +00:00
JRRudy1 5d47c4ae4c
Added `ToPyObject` and `IntoPy<PyObject>` impls for `PyBackedStr`. (#4205)
* Added `ToPyObject` and `Into<PyObject>` impls for `PyBackedStr`.

* Create 4205.added.md

* Added attributes limiting the `ToPyObject` and `IntoPy<PyObject>` impls to the case where `cfg(any(Py_3_10, not(Py_LIMITED_API)))`. When this cfg does not apply, the conversion is less trivial since the `storage` is actually `PyBytes`, not `PyString`.

* Fixed imports format.

* Updated the `ToPyObject` and `IntoPy<PyObject>` impls to support the `cfg(not(any(Py_3_10, not(Py_LIMITED_API))))` case by converting the `PyBytes` back to `PyString`.

* Added `ToPyObject` and `IntoPy<PyObject>` impls for `PyBackedBytes`.

* Added tests for the `PyBackedBytes` conversion impls.

* Updated newsfragment entry to include the `PyBackedBytes` impls.

* Changed the `IntoPy` and `ToPyObject` impls for `PyBackedBytes` to produce `PyBytes` regardless of the backing variant. Updated tests to demonstrate this.

* retrigger checks

* Updated `PyBackedStr` conversion tests to extract the result as a `PyBackedStr` instead of `&str` since the latter is not supported under some `cfg`'s.

* Fixed `IntoPy<PyObject> for PyBackedBytes` impl to create `bytes` for both storage types as intended. Updated test to properly catch the error.

---------

Co-authored-by: jrudolph <jrudolph@anl.gov>
2024-06-01 21:09:14 +00:00
Icxolu a7a5c10b8a
add pyclass `hash` option (#4206)
* add pyclass `hash` option

* add newsfragment

* require `frozen` option for `hash`

* simplify `hash` without `frozen` error message

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* require `eq` for `hash`

* prevent manual `__hash__` with `#pyo3(hash)`

* combine error messages

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-06-01 14:20:20 +00:00
Jasper van Brakel 25c1db4767
Add weakref Python types (#3835)
* Add vscode folder to gitignore

* Initial work on PyWeakRef (weakref.ReferenceType)

* Add documentation for PyWeakRef::upgrade

* Add missing docs for PyWeakRef

* Add PyWeakProxy

* Add PyWeakCallableProxy

* Add PyWeakRefMethods::upgrade_exact and prevent unnecessary panicing

* Change PyWeakRefMethods to exclude infeasible errors.

As a result of the checks  made about the objectpointers in PyO3, all
 errors in PyWeakref_GetObject are already caught. Therefor there is no
 need to check for these errors in the non-ffi layer.

* Add towncrier changes

* Update weakref type doctests to use `py.run_bound`

* Fix to adhere to MSRV

* Make weakref tests independent from macros feature

* Change Weakref tests

* Remove forgotten Debug marcos

* Processed .gitignore and PyResultExt feedback

* Change new methods of weakref types to remove dangling pointers

* Change to reflect deprecation of PyErr::value for PyErr::value_bound

* Change Tests so different class name in older python versions is accounted for

* Change formatting

* Make tests ABI3 compatible

* Prevent the use of PyClass in test for weakref under abi3 Python 3.7 and 3.8

* Disable weakref types when targeting PyPy

* Remove needless borrow from CallableProxy test

* Add Borrowed variants of upgrade and upgrade exact to trait

* Added tests for weakref borrow_upgrade methods

* Change PyWeakRefMethods method names to be more consistent

* Change weakref constructors to take PyAny for main target

* Add track_caller to all panicing weakref methods

* Add PyWeakRefMethods::upgrade*_as_unchecked

* Fix PyWeakProxy and PyWeakCallableProxy Documentation

* Replace deprecated wrap_pyfunction with bound equivalent

* Add (Generic) PyWeakref Type

* Reworked Proxy types into one (PyWeakrefProxy)

* Rename PyWeakRef to PyWeakrefReference

* Change PyWeakrefReference to only use type pointer when it exists

* Remove `#[track_caller]` annotations for now

* Make the gil-refs function feature dependent

* Remove unused AsPyPointer import

* Change docs links to PyNone to not include private module

* Fix string based examples

* Change tests to work for Python 3.13

* Fix cargo clippy for Python 3.13

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-05-31 19:13:50 +00:00
Icxolu d1a7cf400a
add pyclass `eq` option (#4210)
* add pyclass `eq` option

* prevent manual impl of `__richcmp__` or `__eq__` with `#[pyclass(eq)]`

* add simple enum `eq_int` option

* rearrange names to fix deprecation warning

* add newsfragment and migration

* update docs

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-05-31 14:13:30 +00:00
David Brochart cb347370ff
Fix typo (#4222) 2024-05-31 09:44:09 +00:00
David Hewitt 4fe5e8c689
ci: turn off gh-pages benchmarks (#4209)
* ci: turn off gh-pages benchmarks

* update benchmark badge
2024-05-28 08:19:50 +00:00
JRRudy1 934c663612
Added `From<Bound<'py, T>>` impl for `PyClassInitializer<T>`. (#4214)
* Added `From<Bound<'py, T>>` impl for PyClassInitializer<T>.

* Added newsfragment entry.

* Added tests for pyclass constructors returning `Py<Self>` and `Bound<Self>`.

* Fixed tests.

* Updated tests to properly cover the new impl.

---------

Co-authored-by: jrudolph <jrudolph@anl.gov>
2024-05-28 01:49:52 +00:00
David Hewitt 388d1760b5
ci: start testing on 3.13-dev (#4184)
* ci: start testing on 3.13-dev

* ffi fixes for 3.13 beta 1

* support 3.13

* move gevent to be binary-only

* adjust for div_ceil

* fixup pytests
2024-05-25 22:41:26 +00:00
Cheuk Ting Ho d21045cbc1
adding new getter for type obj (#4197)
* adding new getter for type obj

* fixing limited api build

* fix formating ssues from clippy

* add changelog info

* Update newsfragments/4197.added.md

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Update src/types/typeobject.rs

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Update src/types/typeobject.rs

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Update src/types/typeobject.rs

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Update src/types/typeobject.rs

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* using uncheck downcast

* fix formating

* move import

* Update src/types/typeobject.rs

Co-authored-by: Matt Hooks <me@matthooks.com>

* Update src/types/typeobject.rs

Co-authored-by: Matt Hooks <me@matthooks.com>

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
Co-authored-by: Matt Hooks <me@matthooks.com>
2024-05-25 22:39:48 +00:00
David Hewitt 2c654b2906
ci: adjust test to avoid type inference (#4199) 2024-05-21 19:27:20 +00:00
Cheuk Ting Ho 81ba9a8cd5
Include import hook in getting-started.md (#4198) 2024-05-21 18:24:06 +00:00
David Hewitt 3e4b3c5c52
docs: attempt to clarify magic methods supported by PyO3 (#4190)
* docs: attempt to clarify magic methods supported by PyO3

* Update guide/src/class/protocols.md

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

---------

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>
2024-05-19 20:13:11 +00:00
Adam Reichold 674708cb4c
Remove OWNED_OBJECTS thread local when GILPool is disabled. (#4193) 2024-05-19 13:40:55 +00:00
David Hewitt ac273a1612
docs: minor updates to pyenv installs (#4189) 2024-05-19 13:39:29 +00:00