Skip to content

Commit

Permalink
Add missing #[allow(unsafe_code)] attributes (#4396)
Browse files Browse the repository at this point in the history
Fixes #4394.
  • Loading branch information
LilyFoote authored and davidhewitt committed Sep 3, 2024
1 parent 9e36baa commit ebd4747
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions newsfragments/4396.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hide confusing warnings about unsafe usage in `#[pyclass]` implementation.
3 changes: 2 additions & 1 deletion pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ fn impl_pytypeinfo(

#[cfg(feature = "gil-refs")]
let has_py_gil_ref = quote! {
#[allow(deprecated)]
#[allow(deprecated, unsafe_code)]
unsafe impl #pyo3_path::type_object::HasPyGilRef for #cls {
type AsRefTarget = #pyo3_path::PyCell<Self>;
}
Expand All @@ -1671,6 +1671,7 @@ fn impl_pytypeinfo(
quote! {
#has_py_gil_ref

#[allow(unsafe_code)]
unsafe impl #pyo3_path::type_object::PyTypeInfo for #cls {
const NAME: &'static str = #cls_name;
const MODULE: ::std::option::Option<&'static str> = #module;
Expand Down
2 changes: 2 additions & 0 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,13 +788,15 @@ pub fn impl_py_getter_def(
use #pyo3_path::impl_::pyclass::Probe;

struct Offset;
#[allow(unsafe_code)]
unsafe impl #pyo3_path::impl_::pyclass::OffsetCalculator<#cls, #ty> for Offset {
fn offset() -> usize {
#pyo3_path::impl_::pyclass::class_offset::<#cls>() +
#pyo3_path::impl_::pyclass::offset_of!(#cls, #field)
}
}

#[allow(unsafe_code)]
const GENERATOR: #pyo3_path::impl_::pyclass::PyClassGetterGenerator::<
#cls,
#ty,
Expand Down
1 change: 1 addition & 0 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ macro_rules! impl_exception_boilerplate {
#[cfg(feature = "gil-refs")]
impl ::std::error::Error for $name {
fn source(&self) -> ::std::option::Option<&(dyn ::std::error::Error + 'static)> {
#[allow(unsafe_code)]
unsafe {
#[allow(deprecated)]
let cause: &$crate::exceptions::PyBaseException = self
Expand Down
6 changes: 5 additions & 1 deletion src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ slot_fragment_trait! {
#[macro_export]
macro_rules! generate_pyclass_getattro_slot {
($cls:ty) => {{
#[allow(unsafe_code)]
unsafe extern "C" fn __wrap(
_slf: *mut $crate::ffi::PyObject,
attr: *mut $crate::ffi::PyObject,
Expand Down Expand Up @@ -436,6 +437,7 @@ macro_rules! define_pyclass_setattr_slot {
#[macro_export]
macro_rules! $generate_macro {
($cls:ty) => {{
#[allow(unsafe_code)]
unsafe extern "C" fn __wrap(
_slf: *mut $crate::ffi::PyObject,
attr: *mut $crate::ffi::PyObject,
Expand Down Expand Up @@ -552,6 +554,7 @@ macro_rules! define_pyclass_binary_operator_slot {
#[macro_export]
macro_rules! $generate_macro {
($cls:ty) => {{
#[allow(unsafe_code)]
unsafe extern "C" fn __wrap(
_slf: *mut $crate::ffi::PyObject,
_other: *mut $crate::ffi::PyObject,
Expand Down Expand Up @@ -744,6 +747,7 @@ slot_fragment_trait! {
#[macro_export]
macro_rules! generate_pyclass_pow_slot {
($cls:ty) => {{
#[allow(unsafe_code)]
unsafe extern "C" fn __wrap(
_slf: *mut $crate::ffi::PyObject,
_other: *mut $crate::ffi::PyObject,
Expand Down Expand Up @@ -868,7 +872,7 @@ macro_rules! generate_pyclass_richcompare_slot {
($cls:ty) => {{
#[allow(unknown_lints, non_local_definitions)]
impl $cls {
#[allow(non_snake_case)]
#[allow(non_snake_case, unsafe_code)]
unsafe extern "C" fn __pymethod___richcmp____(
slf: *mut $crate::ffi::PyObject,
other: *mut $crate::ffi::PyObject,
Expand Down
1 change: 1 addition & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ macro_rules! wrap_pymodule {
#[macro_export]
macro_rules! append_to_inittab {
($module:ident) => {
#[allow(unsafe_code)]
unsafe {
if $crate::ffi::Py_IsInitialized() != 0 {
::std::panic!(
Expand Down
1 change: 1 addition & 0 deletions src/tests/hygiene/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_implicit_prelude]
#![allow(dead_code, unused_variables, clippy::unnecessary_wraps)]
#![deny(unsafe_code)]

// The modules in this test are used to check PyO3 macro expansion is hygienic. By locating the test
// inside the crate the global `::pyo3` namespace is not available, so in combination with
Expand Down
7 changes: 7 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub trait DerefToPyAny {
macro_rules! pyobject_native_type_base(
($name:ty $(;$generics:ident)* ) => {
#[cfg(feature = "gil-refs")]
#[allow(unsafe_code)]
unsafe impl<$($generics,)*> $crate::PyNativeType for $name {
type AsRefSource = Self;
}
Expand Down Expand Up @@ -162,6 +163,7 @@ macro_rules! pyobject_native_type_base(
{
#[inline]
fn to_object(&self, py: $crate::Python<'_>) -> $crate::PyObject {
#[allow(unsafe_code)]
unsafe { $crate::PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}
Expand Down Expand Up @@ -192,6 +194,7 @@ macro_rules! pyobject_native_type_named (
}
}

#[allow(unsafe_code)]
unsafe impl<$($generics,)*> $crate::AsPyPointer for $name {
/// Gets the underlying FFI pointer, returns a borrowed pointer.
#[inline]
Expand All @@ -206,6 +209,7 @@ macro_rules! pyobject_native_type_named (
impl<$($generics,)*> $crate::IntoPy<$crate::Py<$name>> for &'_ $name {
#[inline]
fn into_py(self, py: $crate::Python<'_>) -> $crate::Py<$name> {
#[allow(unsafe_code)]
unsafe { $crate::Py::from_borrowed_ptr(py, self.as_ptr()) }
}
}
Expand All @@ -217,6 +221,7 @@ macro_rules! pyobject_native_type_named (
#[inline]
fn from(other: &$name) -> Self {
use $crate::PyNativeType;
#[allow(unsafe_code)]
unsafe { $crate::Py::from_borrowed_ptr(other.py(), other.as_ptr()) }
}
}
Expand All @@ -226,6 +231,7 @@ macro_rules! pyobject_native_type_named (
#[cfg(feature = "gil-refs")]
impl<'a, $($generics,)*> ::std::convert::From<&'a $name> for &'a $crate::PyAny {
fn from(ob: &'a $name) -> Self {
#[allow(unsafe_code)]
unsafe{&*(ob as *const $name as *const $crate::PyAny)}
}
}
Expand All @@ -246,6 +252,7 @@ macro_rules! pyobject_native_static_type_object(
#[macro_export]
macro_rules! pyobject_native_type_info(
($name:ty, $typeobject:expr, $module:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
#[allow(unsafe_code)]
unsafe impl<$($generics,)*> $crate::type_object::PyTypeInfo for $name {
const NAME: &'static str = stringify!($name);
const MODULE: ::std::option::Option<&'static str> = $module;
Expand Down

0 comments on commit ebd4747

Please sign in to comment.