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 Aug 2, 2024
1 parent 0ba244c commit 0e03b39
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 1 deletion.
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.
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,7 @@ fn impl_pytypeinfo(cls: &syn::Ident, attr: &PyClassArgs, ctx: &Ctx) -> TokenStre
};

quote! {
#[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 @@ -772,13 +772,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
6 changes: 5 additions & 1 deletion src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,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 @@ -429,6 +430,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 @@ -545,6 +547,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 @@ -737,6 +740,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 @@ -861,7 +865,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 @@ -184,6 +184,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
2 changes: 2 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,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 Down Expand Up @@ -160,6 +161,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 0e03b39

Please sign in to comment.