Skip to content

Commit

Permalink
Suppress non_local_definitions lint as we often want the non-local ef…
Browse files Browse the repository at this point in the history
…fects in macro code (PyO3#4074)

* Resolve references to legacy numerical constants and use the associated constants instead

* Suppress non_local_definitions lint as we often want the non-local effects in macro code
  • Loading branch information
adamreichold authored Apr 13, 2024
1 parent 4e5167d commit 721100a
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pyo3-ffi/src/pyport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub type Py_ssize_t = ::libc::ssize_t;
pub type Py_hash_t = Py_ssize_t;
pub type Py_uhash_t = ::libc::size_t;

pub const PY_SSIZE_T_MIN: Py_ssize_t = std::isize::MIN as Py_ssize_t;
pub const PY_SSIZE_T_MAX: Py_ssize_t = std::isize::MAX as Py_ssize_t;
pub const PY_SSIZE_T_MIN: Py_ssize_t = isize::MIN as Py_ssize_t;
pub const PY_SSIZE_T_MAX: Py_ssize_t = isize::MAX as Py_ssize_t;

#[cfg(target_endian = "big")]
pub const PY_BIG_ENDIAN: usize = 1;
Expand Down
2 changes: 2 additions & 0 deletions pyo3-macros-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub fn pymodule_module_impl(mut module: syn::ItemMod) -> Result<TokenStream> {

#initialization

#[allow(unknown_lints, non_local_definitions)]
impl MakeDef {
const fn make_def() -> #pyo3_path::impl_::pymodule::ModuleDef {
use #pyo3_path::impl_::pymodule as impl_;
Expand Down Expand Up @@ -333,6 +334,7 @@ pub fn pymodule_function_impl(mut function: syn::ItemFn) -> Result<TokenStream>
// this avoids complications around the fact that the generated module has a different scope
// (and `super` doesn't always refer to the outer scope, e.g. if the `#[pymodule] is
// inside a function body)
#[allow(unknown_lints, non_local_definitions)]
impl #ident::MakeDef {
const fn make_def() -> #pyo3_path::impl_::pymodule::ModuleDef {
fn __pyo3_pymodule(module: &#pyo3_path::Bound<'_, #pyo3_path::types::PyModule>) -> #pyo3_path::PyResult<()> {
Expand Down
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ pub fn impl_wrap_pyfunction(
// this avoids complications around the fact that the generated module has a different scope
// (and `super` doesn't always refer to the outer scope, e.g. if the `#[pyfunction] is
// inside a function body)
#[allow(unknown_lints, non_local_definitions)]
impl #name::MakeDef {
const _PYO3_DEF: #pyo3_path::impl_::pymethods::PyMethodDef = #methoddef;
}
Expand Down
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ pub fn impl_py_method_def_new(
#deprecations

use #pyo3_path::impl_::pyclass::*;
#[allow(unknown_lints, non_local_definitions)]
impl PyClassNewTextSignature<#cls> for PyClassImplCollector<#cls> {
#[inline]
fn new_text_signature(self) -> ::std::option::Option<&'static str> {
Expand Down
7 changes: 1 addition & 6 deletions src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::err::{PyErr, PyResult};
use crate::exceptions::PyOverflowError;
use crate::ffi::{self, Py_hash_t};
use crate::{IntoPy, PyObject, Python};
use std::isize;
use std::os::raw::c_int;

/// A type which can be the return type of a python C-API callback
Expand Down Expand Up @@ -85,11 +84,7 @@ impl IntoPyCallbackOutput<()> for () {
impl IntoPyCallbackOutput<ffi::Py_ssize_t> for usize {
#[inline]
fn convert(self, _py: Python<'_>) -> PyResult<ffi::Py_ssize_t> {
if self <= (isize::MAX as usize) {
Ok(self as isize)
} else {
Err(PyOverflowError::new_err(()))
}
self.try_into().map_err(|_err| PyOverflowError::new_err(()))
}
}

Expand Down
28 changes: 14 additions & 14 deletions src/conversions/std/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ mod test_128bit_integers {
#[test]
fn test_i128_max() {
Python::with_gil(|py| {
let v = std::i128::MAX;
let v = i128::MAX;
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<i128>(py).unwrap());
assert_eq!(v as u128, obj.extract::<u128>(py).unwrap());
Expand All @@ -456,7 +456,7 @@ mod test_128bit_integers {
#[test]
fn test_i128_min() {
Python::with_gil(|py| {
let v = std::i128::MIN;
let v = i128::MIN;
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<i128>(py).unwrap());
assert!(obj.extract::<i64>(py).is_err());
Expand All @@ -467,7 +467,7 @@ mod test_128bit_integers {
#[test]
fn test_u128_max() {
Python::with_gil(|py| {
let v = std::u128::MAX;
let v = u128::MAX;
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<u128>(py).unwrap());
assert!(obj.extract::<i128>(py).is_err());
Expand Down Expand Up @@ -495,7 +495,7 @@ mod test_128bit_integers {
#[test]
fn test_nonzero_i128_max() {
Python::with_gil(|py| {
let v = NonZeroI128::new(std::i128::MAX).unwrap();
let v = NonZeroI128::new(i128::MAX).unwrap();
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroI128>(py).unwrap());
assert_eq!(
Expand All @@ -509,7 +509,7 @@ mod test_128bit_integers {
#[test]
fn test_nonzero_i128_min() {
Python::with_gil(|py| {
let v = NonZeroI128::new(std::i128::MIN).unwrap();
let v = NonZeroI128::new(i128::MIN).unwrap();
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroI128>(py).unwrap());
assert!(obj.extract::<NonZeroI64>(py).is_err());
Expand All @@ -520,7 +520,7 @@ mod test_128bit_integers {
#[test]
fn test_nonzero_u128_max() {
Python::with_gil(|py| {
let v = NonZeroU128::new(std::u128::MAX).unwrap();
let v = NonZeroU128::new(u128::MAX).unwrap();
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroU128>(py).unwrap());
assert!(obj.extract::<NonZeroI128>(py).is_err());
Expand Down Expand Up @@ -573,7 +573,7 @@ mod tests {
#[test]
fn test_u32_max() {
Python::with_gil(|py| {
let v = std::u32::MAX;
let v = u32::MAX;
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<u32>(py).unwrap());
assert_eq!(u64::from(v), obj.extract::<u64>(py).unwrap());
Expand All @@ -584,7 +584,7 @@ mod tests {
#[test]
fn test_i64_max() {
Python::with_gil(|py| {
let v = std::i64::MAX;
let v = i64::MAX;
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<i64>(py).unwrap());
assert_eq!(v as u64, obj.extract::<u64>(py).unwrap());
Expand All @@ -595,7 +595,7 @@ mod tests {
#[test]
fn test_i64_min() {
Python::with_gil(|py| {
let v = std::i64::MIN;
let v = i64::MIN;
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<i64>(py).unwrap());
assert!(obj.extract::<i32>(py).is_err());
Expand All @@ -606,7 +606,7 @@ mod tests {
#[test]
fn test_u64_max() {
Python::with_gil(|py| {
let v = std::u64::MAX;
let v = u64::MAX;
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<u64>(py).unwrap());
assert!(obj.extract::<i64>(py).is_err());
Expand Down Expand Up @@ -664,7 +664,7 @@ mod tests {
#[test]
fn test_nonzero_u32_max() {
Python::with_gil(|py| {
let v = NonZeroU32::new(std::u32::MAX).unwrap();
let v = NonZeroU32::new(u32::MAX).unwrap();
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroU32>(py).unwrap());
assert_eq!(NonZeroU64::from(v), obj.extract::<NonZeroU64>(py).unwrap());
Expand All @@ -675,7 +675,7 @@ mod tests {
#[test]
fn test_nonzero_i64_max() {
Python::with_gil(|py| {
let v = NonZeroI64::new(std::i64::MAX).unwrap();
let v = NonZeroI64::new(i64::MAX).unwrap();
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroI64>(py).unwrap());
assert_eq!(
Expand All @@ -689,7 +689,7 @@ mod tests {
#[test]
fn test_nonzero_i64_min() {
Python::with_gil(|py| {
let v = NonZeroI64::new(std::i64::MIN).unwrap();
let v = NonZeroI64::new(i64::MIN).unwrap();
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroI64>(py).unwrap());
assert!(obj.extract::<NonZeroI32>(py).is_err());
Expand All @@ -700,7 +700,7 @@ mod tests {
#[test]
fn test_nonzero_u64_max() {
Python::with_gil(|py| {
let v = NonZeroU64::new(std::u64::MAX).unwrap();
let v = NonZeroU64::new(u64::MAX).unwrap();
let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroU64>(py).unwrap());
assert!(obj.extract::<NonZeroI64>(py).is_err());
Expand Down
1 change: 1 addition & 0 deletions src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ slot_fragment_trait! {
#[macro_export]
macro_rules! generate_pyclass_richcompare_slot {
($cls:ty) => {{
#[allow(unknown_lints, non_local_definitions)]
impl $cls {
#[allow(non_snake_case)]
unsafe extern "C" fn __pymethod___richcmp____(
Expand Down
2 changes: 1 addition & 1 deletion src/pycell/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct BorrowFlag(usize);

impl BorrowFlag {
pub(crate) const UNUSED: BorrowFlag = BorrowFlag(0);
const HAS_MUTABLE_BORROW: BorrowFlag = BorrowFlag(usize::max_value());
const HAS_MUTABLE_BORROW: BorrowFlag = BorrowFlag(usize::MAX);
const fn increment(self) -> Self {
Self(self.0 + 1)
}
Expand Down
1 change: 1 addition & 0 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2491,6 +2491,7 @@ class SimpleClass:

#[cfg(feature = "macros")]
#[test]
#[allow(unknown_lints, non_local_definitions)]
fn test_hasattr_error() {
use crate::exceptions::PyValueError;
use crate::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_proto_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use pyo3::exceptions::{PyAttributeError, PyIndexError, PyValueError};
use pyo3::types::{PyDict, PyList, PyMapping, PySequence, PySlice, PyType};
use pyo3::{prelude::*, py_run};
use std::{isize, iter};
use std::iter;

#[path = "../src/tests/common.rs"]
mod common;
Expand Down

0 comments on commit 721100a

Please sign in to comment.