Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PyO3 0.20.x #802

Closed

Conversation

musicinmybrain
Copy link

Pull Request Check List

  • Added tests for changed code. N/A, just a dependency update
  • Updated documentation for changed code. N/A, just a dependency update

I need to use the current release of PyO3 0.20.x to update the python-pendulum package to 3.0.0 in Fedora Linux, and based on PyO3 release notes and on testing, there’s no reason upstream can’t allow a current version too.

@Secrus
Copy link
Collaborator

Secrus commented Nov 21, 2024

Please rebase your changes with master to fix the pipeline

@musicinmybrain
Copy link
Author

Done, and PYO3_PYTHON=/usr/bin/python3.12 cargo test passes, but PyO3 0.20.x doesn’t support the current Python 3.13 release, and there might be some API changes to contend with between 0.20.x and the current PyO3 0.23.x. Let me see if I can do any further upgrades without hitting the limits of my Rust and PyO3 experience.

@musicinmybrain musicinmybrain changed the title Allow PyO3 0.20.x, including the latest version Allow PyO3 0.20.x Nov 21, 2024
@musicinmybrain
Copy link
Author

For PyO3 0.21, it’s necessary to deal with the new Bound<'py, T> smart pointer replacing the existing “GIL Refs” API, https://pyo3.rs/v0.21.0/migration.html#from-020-to-021; see also https://pyo3.rs/v0.21.0/migration.html#migrating-from-the-gil-refs-api-to-boundt.

I started to attempt a PyO3 0.21 port with

diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 46b00c5..31f55dc 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -14,7 +14,7 @@ strip = true
 overflow-checks = false
 
 [dependencies]
-pyo3 = { version = ">= 0.19, < 0.21", features = ["extension-module", "generate-import-lib"] }
+pyo3 = { version = "0.22", features = ["extension-module", "generate-import-lib"] }
 mimalloc = { version = "0.1.39", optional = true, default-features = false }
 
 [features]
diff --git a/rust/src/python/helpers.rs b/rust/src/python/helpers.rs
index 4a53e59..3d08da4 100644
--- a/rust/src/python/helpers.rs
+++ b/rust/src/python/helpers.rs
@@ -74,10 +74,10 @@ impl PartialOrd for DateTimeInfo<'_> {
     }
 }
 
-pub fn get_tz_name<'py>(py: Python, dt: &'py PyAny) -> PyResult<&'py str> {
+pub fn get_tz_name<'py>(py: Python, dt: Bound<'py, PyAny>) -> PyResult<&'py str> {
     let tz: &str = "";
 
-    if !PyDateTime::is_type_of(dt) {
+    if !PyDateTime::is_type_of_bound(&dt) {
         return Ok(tz);
     }
 
@@ -169,7 +169,7 @@ pub fn local_time(
 }
 
 #[pyfunction]
-pub fn precise_diff<'py>(py: Python, dt1: &'py PyAny, dt2: &'py PyAny) -> PyResult<PreciseDiff> {
+pub fn precise_diff<'py>(py: Python, dt1: Bound<'py, PyAny>, dt2: Bound<'py, PyAny>) -> PyResult<PreciseDiff> {
     let mut sign = 1;
     let mut dtinfo1 = DateTimeInfo {
         year: dt1.downcast::<PyDate>()?.get_year(),
diff --git a/rust/src/python/types/timezone.rs b/rust/src/python/types/timezone.rs
index 1a8bbad..7c6c289 100644
--- a/rust/src/python/types/timezone.rs
+++ b/rust/src/python/types/timezone.rs
@@ -10,6 +10,7 @@ pub struct FixedTimezone {
 
 #[pymethods]
 impl FixedTimezone {
+    #[pyo3(signature = (offset, name=None))]
     #[new]
     pub fn new(offset: i32, name: Option<String>) -> Self {
         Self { offset, name }

but quite a few further changes are needed, starting with

error[E0308]: mismatched types
  --> src/python/helpers.rs:95:41
   |
95 |                   let tzname: &PyString = tzinfo
   |  _____________________________---------___^
   | |                             |
   | |                             expected due to this
96 | |                     .getattr(intern!(py, "key"))
97 | |                     .unwrap()
98 | |                     .downcast()  
99 | |                     .unwrap();   
   | |_____________________________^ expected `&PyString`, found `&Bound<'_, _>`
   |
   = note: expected reference `&PyString`
              found reference `&pyo3::Bound<'_, _>`

which is obviously related to the same API changes, but which my Rust experience is not adequate to immediately understand, and which I don’t have time to dig into right now.

Still, migrating to a current PyO3 version that supports Python 3.13 would be a great idea, and I imagine you will have less trouble dealing with the API changes than I do.

Copy link

codspeed-hq bot commented Nov 22, 2024

CodSpeed Performance Report

Merging #802 will not alter performance

Comparing musicinmybrain:pyo3-0.20 (c2a1721) with master (b84b976)

Summary

✅ 1 untouched benchmarks

@Secrus
Copy link
Collaborator

Secrus commented Dec 17, 2024

We have jumped straight to 0.22. Thank you for the effort anyway! 😃

@Secrus Secrus closed this Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants