Skip to content

Commit 0e03b39

Browse files
authored
Add missing #[allow(unsafe_code)] attributes (#4396)
Fixes #4394.
1 parent 0ba244c commit 0e03b39

File tree

7 files changed

+13
-1
lines changed

7 files changed

+13
-1
lines changed

newsfragments/4396.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hide confusing warnings about unsafe usage in `#[pyclass]` implementation.

pyo3-macros-backend/src/pyclass.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,7 @@ fn impl_pytypeinfo(cls: &syn::Ident, attr: &PyClassArgs, ctx: &Ctx) -> TokenStre
17441744
};
17451745

17461746
quote! {
1747+
#[allow(unsafe_code)]
17471748
unsafe impl #pyo3_path::type_object::PyTypeInfo for #cls {
17481749
const NAME: &'static str = #cls_name;
17491750
const MODULE: ::std::option::Option<&'static str> = #module;

pyo3-macros-backend/src/pymethod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,13 +772,15 @@ pub fn impl_py_getter_def(
772772
use #pyo3_path::impl_::pyclass::Probe;
773773

774774
struct Offset;
775+
#[allow(unsafe_code)]
775776
unsafe impl #pyo3_path::impl_::pyclass::OffsetCalculator<#cls, #ty> for Offset {
776777
fn offset() -> usize {
777778
#pyo3_path::impl_::pyclass::class_offset::<#cls>() +
778779
#pyo3_path::impl_::pyclass::offset_of!(#cls, #field)
779780
}
780781
}
781782

783+
#[allow(unsafe_code)]
782784
const GENERATOR: #pyo3_path::impl_::pyclass::PyClassGetterGenerator::<
783785
#cls,
784786
#ty,

src/impl_/pyclass.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ slot_fragment_trait! {
346346
#[macro_export]
347347
macro_rules! generate_pyclass_getattro_slot {
348348
($cls:ty) => {{
349+
#[allow(unsafe_code)]
349350
unsafe extern "C" fn __wrap(
350351
_slf: *mut $crate::ffi::PyObject,
351352
attr: *mut $crate::ffi::PyObject,
@@ -429,6 +430,7 @@ macro_rules! define_pyclass_setattr_slot {
429430
#[macro_export]
430431
macro_rules! $generate_macro {
431432
($cls:ty) => {{
433+
#[allow(unsafe_code)]
432434
unsafe extern "C" fn __wrap(
433435
_slf: *mut $crate::ffi::PyObject,
434436
attr: *mut $crate::ffi::PyObject,
@@ -545,6 +547,7 @@ macro_rules! define_pyclass_binary_operator_slot {
545547
#[macro_export]
546548
macro_rules! $generate_macro {
547549
($cls:ty) => {{
550+
#[allow(unsafe_code)]
548551
unsafe extern "C" fn __wrap(
549552
_slf: *mut $crate::ffi::PyObject,
550553
_other: *mut $crate::ffi::PyObject,
@@ -737,6 +740,7 @@ slot_fragment_trait! {
737740
#[macro_export]
738741
macro_rules! generate_pyclass_pow_slot {
739742
($cls:ty) => {{
743+
#[allow(unsafe_code)]
740744
unsafe extern "C" fn __wrap(
741745
_slf: *mut $crate::ffi::PyObject,
742746
_other: *mut $crate::ffi::PyObject,
@@ -861,7 +865,7 @@ macro_rules! generate_pyclass_richcompare_slot {
861865
($cls:ty) => {{
862866
#[allow(unknown_lints, non_local_definitions)]
863867
impl $cls {
864-
#[allow(non_snake_case)]
868+
#[allow(non_snake_case, unsafe_code)]
865869
unsafe extern "C" fn __pymethod___richcmp____(
866870
slf: *mut $crate::ffi::PyObject,
867871
other: *mut $crate::ffi::PyObject,

src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ macro_rules! wrap_pymodule {
184184
#[macro_export]
185185
macro_rules! append_to_inittab {
186186
($module:ident) => {
187+
#[allow(unsafe_code)]
187188
unsafe {
188189
if $crate::ffi::Py_IsInitialized() != 0 {
189190
::std::panic!(

src/tests/hygiene/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![no_implicit_prelude]
22
#![allow(dead_code, unused_variables, clippy::unnecessary_wraps)]
3+
#![deny(unsafe_code)]
34

45
// The modules in this test are used to check PyO3 macro expansion is hygienic. By locating the test
56
// inside the crate the global `::pyo3` namespace is not available, so in combination with

src/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ macro_rules! pyobject_native_type_named (
133133
}
134134
}
135135

136+
#[allow(unsafe_code)]
136137
unsafe impl<$($generics,)*> $crate::AsPyPointer for $name {
137138
/// Gets the underlying FFI pointer, returns a borrowed pointer.
138139
#[inline]
@@ -160,6 +161,7 @@ macro_rules! pyobject_native_static_type_object(
160161
#[macro_export]
161162
macro_rules! pyobject_native_type_info(
162163
($name:ty, $typeobject:expr, $module:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
164+
#[allow(unsafe_code)]
163165
unsafe impl<$($generics,)*> $crate::type_object::PyTypeInfo for $name {
164166
const NAME: &'static str = stringify!($name);
165167
const MODULE: ::std::option::Option<&'static str> = $module;

0 commit comments

Comments
 (0)