Skip to content

Commit

Permalink
defer to PyO3 i64 extraction to avoid implicit integer casts
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed May 14, 2024
1 parent e1fc99d commit 6712079
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 24 deletions.
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',
]
timeout = 30
xfail_strict = true
Expand Down
22 changes: 2 additions & 20 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,8 @@ 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
}
v.extract().ok()
}

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

0 comments on commit 6712079

Please sign in to comment.