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

defer to PyO3 i64 extraction to avoid implicit integer casts #1288

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ testpaths = 'tests'
log_format = '%(name)s %(levelname)s: %(message)s'
filterwarnings = [
'error',
# Work around https://github.com/pytest-dev/pytest/issues/10977 for Python 3.12
'ignore:(ast\.Str|ast\.NameConstant|ast\.Num|Attribute s) is deprecated and will be removed.*:DeprecationWarning:',
# issue with pytz - https://github.com/stub42/pytz/issues/105 for Python 3.12
'ignore:datetime\.utcfromtimestamp\(\) is deprecated.*:DeprecationWarning:',
# Python 3.9 and below allowed truncation of float to integers in some
# cases, by not making this an error we can test for this behaviour
'ignore:(.+)Implicit conversion to integers using __int__ is deprecated',
Comment on lines +76 to +78
Copy link
Contributor Author

@davidhewitt davidhewitt May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By adding this ignore it allows the deprecated behaviour on older versions, thus testing them. Without the Rust change in this PR I get test failures locally which are consistent with pydantic/pydantic#9018.

]
timeout = 30
xfail_strict = true
Expand Down
29 changes: 10 additions & 19 deletions src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Cow;
use pyo3::exceptions::PyKeyError;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyString};
use pyo3::{ffi, intern, FromPyObject};
use pyo3::{intern, FromPyObject};

use jiter::{cached_py_string, pystring_fast_new, StringCacheMode};

Expand Down Expand Up @@ -124,26 +124,17 @@ pub fn safe_repr<'py>(v: &Bound<'py, PyAny>) -> ReprOutput<'py> {
}
}

/// Extract an i64 from a python object more quickly, see
/// https://github.com/PyO3/pyo3/pull/3742#discussion_r1451763928
#[cfg(not(any(target_pointer_width = "32", windows, PyPy)))]
pub fn extract_i64(obj: &Bound<'_, PyAny>) -> Option<i64> {
let val = unsafe { ffi::PyLong_AsLong(obj.as_ptr()) };
if val == -1 && PyErr::occurred(obj.py()) {
unsafe { ffi::PyErr_Clear() };
None
} else {
Some(val)
}
}

#[cfg(any(target_pointer_width = "32", windows, PyPy))]
pub fn extract_i64(v: &Bound<'_, PyAny>) -> Option<i64> {
if v.is_instance_of::<pyo3::types::PyInt>() {
v.extract().ok()
} else {
None
#[cfg(PyPy)]
if !v.is_instance_of::<pyo3::types::PyInt>() {
// PyPy used __int__ to cast floats to ints after CPython removed it,
// see https://github.com/pypy/pypy/issues/4949
//
// Can remove this after PyPy 7.3.17 is released
return None;
}

v.extract().ok()
}

pub(crate) fn new_py_string<'py>(py: Python<'py>, s: &str, cache_str: StringCacheMode) -> Bound<'py, PyString> {
Expand Down
Loading