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

rename PyLong to PyInt #4347

Merged
merged 3 commits into from
Jul 14, 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
2 changes: 1 addition & 1 deletion guide/src/conversions/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The table below contains the Python type and the corresponding function argument
| `str` | `String`, `Cow<str>`, `&str`, `char`, `OsString`, `PathBuf`, `Path` | `PyString`, `PyUnicode` |
| `bytes` | `Vec<u8>`, `&[u8]`, `Cow<[u8]>` | `PyBytes` |
| `bool` | `bool` | `PyBool` |
| `int` | `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`, `num_bigint::BigInt`[^1], `num_bigint::BigUint`[^1] | `PyLong` |
| `int` | `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`, `num_bigint::BigInt`[^1], `num_bigint::BigUint`[^1] | `PyInt` |
| `float` | `f32`, `f64` | `PyFloat` |
| `complex` | `num_complex::Complex`[^2] | `PyComplex` |
| `fractions.Fraction`| `num_rational::Ratio`[^8] | - |
Expand Down
1 change: 1 addition & 0 deletions newsfragments/4347.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate `PyLong` in favor of `PyInt`.
20 changes: 10 additions & 10 deletions src/conversions/num_bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::types::{bytes::PyBytesMethods, PyBytes};
use crate::{
ffi,
instance::Bound,
types::{any::PyAnyMethods, PyLong},
types::{any::PyAnyMethods, PyInt},
FromPyObject, IntoPy, Py, PyAny, PyObject, PyResult, Python, ToPyObject,
};

Expand Down Expand Up @@ -120,7 +120,7 @@ macro_rules! bigint_conversion {
} else {
None
};
py.get_type_bound::<PyLong>()
py.get_type_bound::<PyInt>()
.call_method("from_bytes", (bytes_obj, "little"), kwargs.as_ref())
.expect("int.from_bytes() failed during to_object()") // FIXME: #1813 or similar
.into()
Expand All @@ -144,8 +144,8 @@ impl<'py> FromPyObject<'py> for BigInt {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<BigInt> {
let py = ob.py();
// fast path - checking for subclass of `int` just checks a bit in the type object
let num_owned: Py<PyLong>;
let num = if let Ok(long) = ob.downcast::<PyLong>() {
let num_owned: Py<PyInt>;
let num = if let Ok(long) = ob.downcast::<PyInt>() {
long
} else {
num_owned = unsafe { Py::from_owned_ptr_or_err(py, ffi::PyNumber_Index(ob.as_ptr()))? };
Expand Down Expand Up @@ -192,8 +192,8 @@ impl<'py> FromPyObject<'py> for BigUint {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<BigUint> {
let py = ob.py();
// fast path - checking for subclass of `int` just checks a bit in the type object
let num_owned: Py<PyLong>;
let num = if let Ok(long) = ob.downcast::<PyLong>() {
let num_owned: Py<PyInt>;
let num = if let Ok(long) = ob.downcast::<PyInt>() {
long
} else {
num_owned = unsafe { Py::from_owned_ptr_or_err(py, ffi::PyNumber_Index(ob.as_ptr()))? };
Expand All @@ -218,7 +218,7 @@ impl<'py> FromPyObject<'py> for BigUint {

#[cfg(not(any(Py_LIMITED_API, Py_3_13)))]
#[inline]
fn int_to_u32_vec<const SIGNED: bool>(long: &Bound<'_, PyLong>) -> PyResult<Vec<u32>> {
fn int_to_u32_vec<const SIGNED: bool>(long: &Bound<'_, PyInt>) -> PyResult<Vec<u32>> {
let mut buffer = Vec::new();
let n_bits = int_n_bits(long)?;
if n_bits == 0 {
Expand Down Expand Up @@ -252,7 +252,7 @@ fn int_to_u32_vec<const SIGNED: bool>(long: &Bound<'_, PyLong>) -> PyResult<Vec<

#[cfg(all(not(Py_LIMITED_API), Py_3_13))]
#[inline]
fn int_to_u32_vec<const SIGNED: bool>(long: &Bound<'_, PyLong>) -> PyResult<Vec<u32>> {
fn int_to_u32_vec<const SIGNED: bool>(long: &Bound<'_, PyInt>) -> PyResult<Vec<u32>> {
let mut buffer = Vec::new();
let mut flags = ffi::Py_ASNATIVEBYTES_LITTLE_ENDIAN;
if !SIGNED {
Expand Down Expand Up @@ -290,7 +290,7 @@ fn int_to_u32_vec<const SIGNED: bool>(long: &Bound<'_, PyLong>) -> PyResult<Vec<

#[cfg(Py_LIMITED_API)]
fn int_to_py_bytes<'py>(
long: &Bound<'py, PyLong>,
long: &Bound<'py, PyInt>,
n_bytes: usize,
is_signed: bool,
) -> PyResult<Bound<'py, PyBytes>> {
Expand All @@ -313,7 +313,7 @@ fn int_to_py_bytes<'py>(

#[inline]
#[cfg(any(not(Py_3_13), Py_LIMITED_API))]
fn int_n_bits(long: &Bound<'_, PyLong>) -> PyResult<usize> {
fn int_n_bits(long: &Bound<'_, PyInt>) -> PyResult<usize> {
let py = long.py();
#[cfg(not(Py_LIMITED_API))]
{
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/std/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ macro_rules! extract_int {
// See https://github.com/PyO3/pyo3/pull/3742 for detials
if cfg!(Py_3_10) && !$force_index_call {
err_if_invalid_value($obj.py(), $error_val, unsafe { $pylong_as($obj.as_ptr()) })
} else if let Ok(long) = $obj.downcast::<crate::types::PyLong>() {
} else if let Ok(long) = $obj.downcast::<crate::types::PyInt>() {
// fast path - checking for subclass of `int` just checks a bit in the type $object
err_if_invalid_value($obj.py(), $error_val, unsafe { $pylong_as(long.as_ptr()) })
} else {
Expand Down
22 changes: 11 additions & 11 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed {
///
/// ```rust
/// use pyo3::prelude::*;
/// use pyo3::types::{PyBool, PyLong};
/// use pyo3::types::{PyBool, PyInt};
///
/// Python::with_gil(|py| {
/// let b = PyBool::new_bound(py, true);
Expand All @@ -1504,8 +1504,8 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed {
///
/// // `bool` is a subtype of `int`, so `downcast` will accept a `bool` as an `int`
/// // but `downcast_exact` will not.
/// assert!(any.downcast::<PyLong>().is_ok());
/// assert!(any.downcast_exact::<PyLong>().is_err());
/// assert!(any.downcast::<PyInt>().is_ok());
/// assert!(any.downcast_exact::<PyInt>().is_err());
///
/// assert!(any.downcast_exact::<PyBool>().is_ok());
/// });
Expand Down Expand Up @@ -2269,7 +2269,7 @@ impl<'py> Bound<'py, PyAny> {
mod tests {
use crate::{
basic::CompareOp,
types::{IntoPyDict, PyAny, PyAnyMethods, PyBool, PyList, PyLong, PyModule, PyTypeMethods},
types::{IntoPyDict, PyAny, PyAnyMethods, PyBool, PyInt, PyList, PyModule, PyTypeMethods},
Bound, PyTypeInfo, Python, ToPyObject,
};

Expand Down Expand Up @@ -2424,7 +2424,7 @@ class SimpleClass:
fn test_hasattr() {
Python::with_gil(|py| {
let x = 5.to_object(py).into_bound(py);
assert!(x.is_instance_of::<PyLong>());
assert!(x.is_instance_of::<PyInt>());

assert!(x.hasattr("to_bytes").unwrap());
assert!(!x.hasattr("bbbbbbytes").unwrap());
Expand Down Expand Up @@ -2471,7 +2471,7 @@ class SimpleClass:
fn test_any_is_instance_of() {
Python::with_gil(|py| {
let x = 5.to_object(py).into_bound(py);
assert!(x.is_instance_of::<PyLong>());
assert!(x.is_instance_of::<PyInt>());

let l = vec![&x, &x].to_object(py).into_bound(py);
assert!(l.is_instance_of::<PyList>());
Expand All @@ -2490,11 +2490,11 @@ class SimpleClass:
fn test_any_is_exact_instance_of() {
Python::with_gil(|py| {
let x = 5.to_object(py).into_bound(py);
assert!(x.is_exact_instance_of::<PyLong>());
assert!(x.is_exact_instance_of::<PyInt>());

let t = PyBool::new_bound(py, true);
assert!(t.is_instance_of::<PyLong>());
assert!(!t.is_exact_instance_of::<PyLong>());
assert!(t.is_instance_of::<PyInt>());
assert!(!t.is_exact_instance_of::<PyInt>());
assert!(t.is_exact_instance_of::<PyBool>());

let l = vec![&x, &x].to_object(py).into_bound(py);
Expand All @@ -2506,8 +2506,8 @@ class SimpleClass:
fn test_any_is_exact_instance() {
Python::with_gil(|py| {
let t = PyBool::new_bound(py, true);
assert!(t.is_instance(&py.get_type_bound::<PyLong>()).unwrap());
assert!(!t.is_exact_instance(&py.get_type_bound::<PyLong>()));
assert!(t.is_instance(&py.get_type_bound::<PyInt>()).unwrap());
assert!(!t.is_exact_instance(&py.get_type_bound::<PyInt>()));
assert!(t.is_exact_instance(&py.get_type_bound::<PyBool>()));
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub use self::memoryview::PyMemoryView;
pub use self::module::{PyModule, PyModuleMethods};
pub use self::none::PyNone;
pub use self::notimplemented::PyNotImplemented;
pub use self::num::PyLong;
pub use self::num::PyLong as PyInt;
#[allow(deprecated)]
pub use self::num::{PyInt, PyLong};
#[cfg(not(any(PyPy, GraalPy)))]
pub use self::pysuper::PySuper;
pub use self::sequence::{PySequence, PySequenceMethods};
Expand Down
10 changes: 7 additions & 3 deletions src/types/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ use crate::{ffi, PyAny};
/// Represents a Python `int` object.
///
/// Values of this type are accessed via PyO3's smart pointers, e.g. as
/// [`Py<PyLong>`][crate::Py] or [`Bound<'py, PyLong>`][crate::Bound].
/// [`Py<PyInt>`][crate::Py] or [`Bound<'py, PyInt>`][crate::Bound].
///
/// You can usually avoid directly working with this type
/// by using [`ToPyObject`](crate::conversion::ToPyObject)
/// and [`extract`](super::PyAnyMethods::extract)
/// with the primitive Rust integer types.
#[repr(transparent)]
pub struct PyLong(PyAny);
pub struct PyInt(PyAny);

pyobject_native_type_core!(PyLong, pyobject_native_static_type_object!(ffi::PyLong_Type), #checkfunction=ffi::PyLong_Check);
pyobject_native_type_core!(PyInt, pyobject_native_static_type_object!(ffi::PyLong_Type), #checkfunction=ffi::PyLong_Check);

/// Deprecated alias for [`PyInt`].
#[deprecated(since = "0.23.0", note = "use `PyInt` instead")]
pub type PyLong = PyInt;
8 changes: 3 additions & 5 deletions src/types/typeobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,15 @@ impl<'py> PyTypeMethods<'py> for Bound<'py, PyType> {

#[cfg(test)]
mod tests {
use crate::types::{
PyAnyMethods, PyBool, PyInt, PyLong, PyModule, PyTuple, PyType, PyTypeMethods,
};
use crate::types::{PyAnyMethods, PyBool, PyInt, PyModule, PyTuple, PyType, PyTypeMethods};
use crate::PyAny;
use crate::Python;

#[test]
fn test_type_is_subclass() {
Python::with_gil(|py| {
let bool_type = py.get_type_bound::<PyBool>();
let long_type = py.get_type_bound::<PyLong>();
let long_type = py.get_type_bound::<PyInt>();
assert!(bool_type.is_subclass(&long_type).unwrap());
});
}
Expand All @@ -263,7 +261,7 @@ mod tests {
Python::with_gil(|py| {
assert!(py
.get_type_bound::<PyBool>()
.is_subclass_of::<PyLong>()
.is_subclass_of::<PyInt>()
.unwrap());
});
}
Expand Down
Loading