Skip to content

Commit 0613b5a

Browse files
authored
Merge pull request #1805 from mejrs/proc_macro_hygiene
Test proc macro hygiene for `#[pyclass]` macro.
2 parents f72b2c8 + 21196e6 commit 0613b5a

File tree

6 files changed

+166
-118
lines changed

6 files changed

+166
-118
lines changed

pyo3-macros-backend/src/method.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,21 @@ impl SelfType {
133133
match self {
134134
SelfType::Receiver { mutable: false } => {
135135
quote! {
136-
let _cell = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
136+
let _cell = _py.from_borrowed_ptr::<::pyo3::PyCell<#cls>>(_slf);
137137
let _ref = _cell.try_borrow()?;
138138
let _slf = &_ref;
139139
}
140140
}
141141
SelfType::Receiver { mutable: true } => {
142142
quote! {
143-
let _cell = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
143+
let _cell = _py.from_borrowed_ptr::<::pyo3::PyCell<#cls>>(_slf);
144144
let mut _ref = _cell.try_borrow_mut()?;
145145
let _slf = &mut _ref;
146146
}
147147
}
148148
SelfType::TryFromPyCell(span) => {
149149
quote_spanned! { *span =>
150-
let _cell = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
150+
let _cell = _py.from_borrowed_ptr::<::pyo3::PyCell<#cls>>(_slf);
151151
#[allow(clippy::useless_conversion)] // In case _slf is PyCell<Self>
152152
let _slf = std::convert::TryFrom::try_from(_cell)?;
153153
}

pyo3-macros-backend/src/proto_method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub(crate) fn impl_method_proto(
9595
};
9696

9797
Ok(quote! {
98-
impl<'p> #module::#proto<'p> for #cls {
98+
impl<'p> ::#module::#proto<'p> for #cls {
9999
#(#impl_types)*
100100
#res_type_def
101101
}

pyo3-macros-backend/src/pyclass.rs

Lines changed: 72 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Default for PyClassArgs {
4747
freelist: None,
4848
name: None,
4949
module: None,
50-
base: parse_quote! { pyo3::PyAny },
50+
base: parse_quote! { ::pyo3::PyAny },
5151
has_dict: false,
5252
has_weaklist: false,
5353
is_gc: false,
@@ -345,22 +345,22 @@ fn impl_methods_inventory(cls: &syn::Ident) -> TokenStream {
345345
quote! {
346346
#[doc(hidden)]
347347
pub struct #inventory_cls {
348-
methods: Vec<pyo3::class::PyMethodDefType>,
348+
methods: ::std::vec::Vec<::pyo3::class::PyMethodDefType>,
349349
}
350-
impl pyo3::class::impl_::PyMethodsInventory for #inventory_cls {
351-
fn new(methods: Vec<pyo3::class::PyMethodDefType>) -> Self {
350+
impl ::pyo3::class::impl_::PyMethodsInventory for #inventory_cls {
351+
fn new(methods: ::std::vec::Vec<::pyo3::class::PyMethodDefType>) -> Self {
352352
Self { methods }
353353
}
354-
fn get(&'static self) -> &'static [pyo3::class::PyMethodDefType] {
354+
fn get(&'static self) -> &'static [::pyo3::class::PyMethodDefType] {
355355
&self.methods
356356
}
357357
}
358358

359-
impl pyo3::class::impl_::HasMethodsInventory for #cls {
359+
impl ::pyo3::class::impl_::HasMethodsInventory for #cls {
360360
type Methods = #inventory_cls;
361361
}
362362

363-
pyo3::inventory::collect!(#inventory_cls);
363+
::pyo3::inventory::collect!(#inventory_cls);
364364
}
365365
}
366366

@@ -380,31 +380,31 @@ fn impl_class(
380380

381381
let alloc = attr.freelist.as_ref().map(|freelist| {
382382
quote! {
383-
impl pyo3::class::impl_::PyClassWithFreeList for #cls {
383+
impl ::pyo3::class::impl_::PyClassWithFreeList for #cls {
384384
#[inline]
385-
fn get_free_list(_py: pyo3::Python<'_>) -> &mut pyo3::impl_::freelist::FreeList<*mut pyo3::ffi::PyObject> {
386-
static mut FREELIST: *mut pyo3::impl_::freelist::FreeList<*mut pyo3::ffi::PyObject> = 0 as *mut _;
385+
fn get_free_list(_py: ::pyo3::Python<'_>) -> &mut ::pyo3::impl_::freelist::FreeList<*mut ::pyo3::ffi::PyObject> {
386+
static mut FREELIST: *mut ::pyo3::impl_::freelist::FreeList<*mut ::pyo3::ffi::PyObject> = 0 as *mut _;
387387
unsafe {
388388
if FREELIST.is_null() {
389-
FREELIST = Box::into_raw(Box::new(
390-
pyo3::impl_::freelist::FreeList::with_capacity(#freelist)));
389+
FREELIST = ::std::boxed::Box::into_raw(::std::boxed::Box::new(
390+
::pyo3::impl_::freelist::FreeList::with_capacity(#freelist)));
391391
}
392392
&mut *FREELIST
393393
}
394394
}
395395
}
396396

397-
impl pyo3::class::impl_::PyClassAllocImpl<#cls> for pyo3::class::impl_::PyClassImplCollector<#cls> {
397+
impl ::pyo3::class::impl_::PyClassAllocImpl<#cls> for ::pyo3::class::impl_::PyClassImplCollector<#cls> {
398398
#[inline]
399-
fn alloc_impl(self) -> Option<pyo3::ffi::allocfunc> {
400-
Some(pyo3::class::impl_::alloc_with_freelist::<#cls>)
399+
fn alloc_impl(self) -> ::std::option::Option<::pyo3::ffi::allocfunc> {
400+
::std::option::Option::Some(::pyo3::class::impl_::alloc_with_freelist::<#cls>)
401401
}
402402
}
403403

404-
impl pyo3::class::impl_::PyClassFreeImpl<#cls> for pyo3::class::impl_::PyClassImplCollector<#cls> {
404+
impl ::pyo3::class::impl_::PyClassFreeImpl<#cls> for ::pyo3::class::impl_::PyClassImplCollector<#cls> {
405405
#[inline]
406-
fn free_impl(self) -> Option<pyo3::ffi::freefunc> {
407-
Some(pyo3::class::impl_::free_with_freelist::<#cls>)
406+
fn free_impl(self) -> ::std::option::Option<::pyo3::ffi::freefunc> {
407+
::std::option::Option::Some(::pyo3::class::impl_::free_with_freelist::<#cls>)
408408
}
409409
}
410410
}
@@ -414,23 +414,23 @@ fn impl_class(
414414

415415
// insert space for weak ref
416416
let weakref = if attr.has_weaklist {
417-
quote! { pyo3::pyclass_slots::PyClassWeakRefSlot }
417+
quote! { ::pyo3::pyclass_slots::PyClassWeakRefSlot }
418418
} else if attr.has_extends {
419-
quote! { <Self::BaseType as pyo3::class::impl_::PyClassBaseType>::WeakRef }
419+
quote! { <Self::BaseType as ::pyo3::class::impl_::PyClassBaseType>::WeakRef }
420420
} else {
421-
quote! { pyo3::pyclass_slots::PyClassDummySlot }
421+
quote! { ::pyo3::pyclass_slots::PyClassDummySlot }
422422
};
423423
let dict = if attr.has_dict {
424-
quote! { pyo3::pyclass_slots::PyClassDictSlot }
424+
quote! { ::pyo3::pyclass_slots::PyClassDictSlot }
425425
} else if attr.has_extends {
426-
quote! { <Self::BaseType as pyo3::class::impl_::PyClassBaseType>::Dict }
426+
quote! { <Self::BaseType as ::pyo3::class::impl_::PyClassBaseType>::Dict }
427427
} else {
428-
quote! { pyo3::pyclass_slots::PyClassDummySlot }
428+
quote! { ::pyo3::pyclass_slots::PyClassDummySlot }
429429
};
430430
let module = if let Some(m) = &attr.module {
431-
quote! { Some(#m) }
431+
quote! { ::std::option::Option::Some(#m) }
432432
} else {
433-
quote! { None }
433+
quote! { ::std::option::Option::None }
434434
};
435435

436436
// Enforce at compile time that PyGCProtocol is implemented
@@ -439,9 +439,9 @@ fn impl_class(
439439
let closure_token = syn::Ident::new(&closure_name, Span::call_site());
440440
quote! {
441441
fn #closure_token() {
442-
use pyo3::class;
442+
use ::pyo3::class;
443443

444-
fn _assert_implements_protocol<'p, T: pyo3::class::PyGCProtocol<'p>>() {}
444+
fn _assert_implements_protocol<'p, T: ::pyo3::class::PyGCProtocol<'p>>() {}
445445
_assert_implements_protocol::<#cls>();
446446
}
447447
}
@@ -450,30 +450,33 @@ fn impl_class(
450450
};
451451

452452
let (impl_inventory, for_each_py_method) = match methods_type {
453-
PyClassMethodsType::Specialization => (None, quote! { visitor(collector.py_methods()); }),
453+
PyClassMethodsType::Specialization => (
454+
::std::option::Option::None,
455+
quote! { visitor(collector.py_methods()); },
456+
),
454457
PyClassMethodsType::Inventory => (
455458
Some(impl_methods_inventory(cls)),
456459
quote! {
457-
for inventory in pyo3::inventory::iter::<<Self as pyo3::class::impl_::HasMethodsInventory>::Methods>() {
458-
visitor(pyo3::class::impl_::PyMethodsInventory::get(inventory));
460+
for inventory in ::pyo3::inventory::iter::<<Self as ::pyo3::class::impl_::HasMethodsInventory>::Methods>() {
461+
visitor(::pyo3::class::impl_::PyMethodsInventory::get(inventory));
459462
}
460463
},
461464
),
462465
};
463466

464467
let base = &attr.base;
465468
let base_nativetype = if attr.has_extends {
466-
quote! { <Self::BaseType as pyo3::class::impl_::PyClassBaseType>::BaseNativeType }
469+
quote! { <Self::BaseType as ::pyo3::class::impl_::PyClassBaseType>::BaseNativeType }
467470
} else {
468-
quote! { pyo3::PyAny }
471+
quote! { ::pyo3::PyAny }
469472
};
470473

471474
// If #cls is not extended type, we allow Self->PyObject conversion
472475
let into_pyobject = if !attr.has_extends {
473476
quote! {
474-
impl pyo3::IntoPy<pyo3::PyObject> for #cls {
475-
fn into_py(self, py: pyo3::Python) -> pyo3::PyObject {
476-
pyo3::IntoPy::into_py(pyo3::Py::new(py, self).unwrap(), py)
477+
impl ::pyo3::IntoPy<::pyo3::PyObject> for #cls {
478+
fn into_py(self, py: ::pyo3::Python) -> ::pyo3::PyObject {
479+
::pyo3::IntoPy::into_py(::pyo3::Py::new(py, self).unwrap(), py)
477480
}
478481
}
479482
}
@@ -482,68 +485,68 @@ fn impl_class(
482485
};
483486

484487
let thread_checker = if attr.has_unsendable {
485-
quote! { pyo3::class::impl_::ThreadCheckerImpl<#cls> }
488+
quote! { ::pyo3::class::impl_::ThreadCheckerImpl<#cls> }
486489
} else if attr.has_extends {
487490
quote! {
488-
pyo3::class::impl_::ThreadCheckerInherited<#cls, <#cls as pyo3::class::impl_::PyClassImpl>::BaseType>
491+
::pyo3::class::impl_::ThreadCheckerInherited<#cls, <#cls as ::pyo3::class::impl_::PyClassImpl>::BaseType>
489492
}
490493
} else {
491-
quote! { pyo3::class::impl_::ThreadCheckerStub<#cls> }
494+
quote! { ::pyo3::class::impl_::ThreadCheckerStub<#cls> }
492495
};
493496

494497
let is_gc = attr.is_gc;
495498
let is_basetype = attr.is_basetype;
496499
let is_subclass = attr.has_extends;
497500

498501
Ok(quote! {
499-
unsafe impl pyo3::type_object::PyTypeInfo for #cls {
500-
type AsRefTarget = pyo3::PyCell<Self>;
502+
unsafe impl ::pyo3::type_object::PyTypeInfo for #cls {
503+
type AsRefTarget = ::pyo3::PyCell<Self>;
501504

502505
const NAME: &'static str = #cls_name;
503-
const MODULE: Option<&'static str> = #module;
506+
const MODULE: ::std::option::Option<&'static str> = #module;
504507

505508
#[inline]
506-
fn type_object_raw(py: pyo3::Python<'_>) -> *mut pyo3::ffi::PyTypeObject {
509+
fn type_object_raw(py: ::pyo3::Python<'_>) -> *mut ::pyo3::ffi::PyTypeObject {
507510
#deprecations
508511

509-
use pyo3::type_object::LazyStaticType;
512+
use ::pyo3::type_object::LazyStaticType;
510513
static TYPE_OBJECT: LazyStaticType = LazyStaticType::new();
511514
TYPE_OBJECT.get_or_init::<Self>(py)
512515
}
513516
}
514517

515-
impl pyo3::PyClass for #cls {
518+
impl ::pyo3::PyClass for #cls {
516519
type Dict = #dict;
517520
type WeakRef = #weakref;
518521
type BaseNativeType = #base_nativetype;
519522
}
520523

521-
impl<'a> pyo3::derive_utils::ExtractExt<'a> for &'a #cls
524+
impl<'a> ::pyo3::derive_utils::ExtractExt<'a> for &'a #cls
522525
{
523-
type Target = pyo3::PyRef<'a, #cls>;
526+
type Target = ::pyo3::PyRef<'a, #cls>;
524527
}
525528

526-
impl<'a> pyo3::derive_utils::ExtractExt<'a> for &'a mut #cls
529+
impl<'a> ::pyo3::derive_utils::ExtractExt<'a> for &'a mut #cls
527530
{
528-
type Target = pyo3::PyRefMut<'a, #cls>;
531+
type Target = ::pyo3::PyRefMut<'a, #cls>;
529532
}
530533

531534
#into_pyobject
532535

533536
#impl_inventory
534537

535-
impl pyo3::class::impl_::PyClassImpl for #cls {
538+
impl ::pyo3::class::impl_::PyClassImpl for #cls {
536539
const DOC: &'static str = #doc;
537540
const IS_GC: bool = #is_gc;
538541
const IS_BASETYPE: bool = #is_basetype;
539542
const IS_SUBCLASS: bool = #is_subclass;
540543

541-
type Layout = pyo3::PyCell<Self>;
544+
type Layout = ::pyo3::PyCell<Self>;
542545
type BaseType = #base;
543546
type ThreadChecker = #thread_checker;
544547

545-
fn for_each_method_def(visitor: &mut dyn FnMut(&[pyo3::class::PyMethodDefType])) {
546-
use pyo3::class::impl_::*;
548+
fn for_each_method_def(visitor: &mut dyn ::std::ops::FnMut(&[::pyo3::class::PyMethodDefType])) {
549+
use ::pyo3::class::impl_::*;
547550
let collector = PyClassImplCollector::<Self>::new();
548551
#for_each_py_method;
549552
visitor(collector.py_class_descriptors());
@@ -554,30 +557,30 @@ fn impl_class(
554557
visitor(collector.mapping_protocol_methods());
555558
visitor(collector.number_protocol_methods());
556559
}
557-
fn get_new() -> Option<pyo3::ffi::newfunc> {
558-
use pyo3::class::impl_::*;
560+
fn get_new() -> ::std::option::Option<::pyo3::ffi::newfunc> {
561+
use ::pyo3::class::impl_::*;
559562
let collector = PyClassImplCollector::<Self>::new();
560563
collector.new_impl()
561564
}
562-
fn get_alloc() -> Option<pyo3::ffi::allocfunc> {
563-
use pyo3::class::impl_::*;
565+
fn get_alloc() -> ::std::option::Option<::pyo3::ffi::allocfunc> {
566+
use ::pyo3::class::impl_::*;
564567
let collector = PyClassImplCollector::<Self>::new();
565568
collector.alloc_impl()
566569
}
567-
fn get_free() -> Option<pyo3::ffi::freefunc> {
568-
use pyo3::class::impl_::*;
570+
fn get_free() -> ::std::option::Option<::pyo3::ffi::freefunc> {
571+
use ::pyo3::class::impl_::*;
569572
let collector = PyClassImplCollector::<Self>::new();
570573
collector.free_impl()
571574
}
572-
fn get_call() -> Option<pyo3::ffi::PyCFunctionWithKeywords> {
573-
use pyo3::class::impl_::*;
575+
fn get_call() -> ::std::option::Option<::pyo3::ffi::PyCFunctionWithKeywords> {
576+
use ::pyo3::class::impl_::*;
574577
let collector = PyClassImplCollector::<Self>::new();
575578
collector.call_impl()
576579
}
577580

578-
fn for_each_proto_slot(visitor: &mut dyn FnMut(&[pyo3::ffi::PyType_Slot])) {
581+
fn for_each_proto_slot(visitor: &mut dyn ::std::ops::FnMut(&[::pyo3::ffi::PyType_Slot])) {
579582
// Implementation which uses dtolnay specialization to load all slots.
580-
use pyo3::class::impl_::*;
583+
use ::pyo3::class::impl_::*;
581584
let collector = PyClassImplCollector::<Self>::new();
582585
visitor(collector.object_protocol_slots());
583586
visitor(collector.number_protocol_slots());
@@ -590,8 +593,8 @@ fn impl_class(
590593
visitor(collector.buffer_protocol_slots());
591594
}
592595

593-
fn get_buffer() -> Option<&'static pyo3::class::impl_::PyBufferProcs> {
594-
use pyo3::class::impl_::*;
596+
fn get_buffer() -> ::std::option::Option<&'static ::pyo3::class::impl_::PyBufferProcs> {
597+
use ::pyo3::class::impl_::*;
595598
let collector = PyClassImplCollector::<Self>::new();
596599
collector.buffer_procs()
597600
}
@@ -645,11 +648,11 @@ fn impl_descriptors(
645648
.collect::<syn::Result<_>>()?;
646649

647650
Ok(quote! {
648-
impl pyo3::class::impl_::PyClassDescriptors<#cls>
649-
for pyo3::class::impl_::PyClassImplCollector<#cls>
651+
impl ::pyo3::class::impl_::PyClassDescriptors<#cls>
652+
for ::pyo3::class::impl_::PyClassImplCollector<#cls>
650653
{
651-
fn py_class_descriptors(self) -> &'static [pyo3::class::methods::PyMethodDefType] {
652-
static METHODS: &[pyo3::class::methods::PyMethodDefType] = &[#(#py_methods),*];
654+
fn py_class_descriptors(self) -> &'static [::pyo3::class::methods::PyMethodDefType] {
655+
static METHODS: &[::pyo3::class::methods::PyMethodDefType] = &[#(#py_methods),*];
653656
METHODS
654657
}
655658
}

0 commit comments

Comments
 (0)