Skip to content

Commit 14513d6

Browse files
committed
fixes
1 parent 370fa76 commit 14513d6

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

src/pycell/layout.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ impl<T: PyNativeType + PyTypeInfo> PyObjectRecursiveOperations
165165
// the 'most derived class' of `obj`. i.e. the result of calling `type(obj)`.
166166
let actual_type = unsafe { PyType::from_borrowed_type_ptr(py, ffi::Py_TYPE(obj)) };
167167

168-
if type_ptr == std::ptr::addr_of_mut!(ffi::PyBaseObject_Type) {
168+
if std::ptr::eq(type_ptr, std::ptr::addr_of_mut!(ffi::PyBaseObject_Type)) {
169169
// the `PyBaseObject_Type` destructor (tp_dealloc) just calls tp_free so we can do this directly
170170
let tp_free = actual_type
171171
.get_slot(TP_FREE)
172172
.expect("base type should have tp_free");
173173
return unsafe { tp_free(obj.cast()) };
174174
}
175175

176-
if type_ptr == std::ptr::addr_of_mut!(ffi::PyType_Type) {
176+
if std::ptr::eq(type_ptr, std::ptr::addr_of_mut!(ffi::PyType_Type)) {
177177
let tp_dealloc = unsafe {
178178
PyType::from_borrowed_type_ptr(py, type_ptr)
179179
.get_slot(TP_DEALLOC)
@@ -189,23 +189,21 @@ impl<T: PyNativeType + PyTypeInfo> PyObjectRecursiveOperations
189189

190190
// More complex native types (e.g. `extends=PyDict`) require calling the base's dealloc.
191191
#[cfg(not(Py_LIMITED_API))]
192-
{
192+
unsafe {
193193
// FIXME: should this be using actual_type.tp_dealloc?
194-
if let Some(dealloc) = unsafe { (*type_ptr).tp_dealloc } {
194+
if let Some(dealloc) = (*type_ptr).tp_dealloc {
195195
// Before CPython 3.11 BaseException_dealloc would use Py_GC_UNTRACK which
196196
// assumes the exception is currently GC tracked, so we have to re-track
197197
// before calling the dealloc so that it can safely call Py_GC_UNTRACK.
198198
#[cfg(not(any(Py_3_11, PyPy)))]
199199
if ffi::PyType_FastSubclass(type_ptr, ffi::Py_TPFLAGS_BASE_EXC_SUBCLASS) == 1 {
200200
ffi::PyObject_GC_Track(obj.cast());
201201
}
202-
unsafe { dealloc(obj) };
202+
dealloc(obj);
203203
} else {
204-
unsafe {
205-
(*actual_type.as_type_ptr())
206-
.tp_free
207-
.expect("type missing tp_free")(obj.cast())
208-
};
204+
(*actual_type.as_type_ptr())
205+
.tp_free
206+
.expect("type missing tp_free")(obj.cast());
209207
}
210208
}
211209

@@ -286,7 +284,9 @@ pub(crate) mod static_layout {
286284

287285
impl<T: PyClassImpl> PyStaticClassLayout<T> {
288286
/// A layout is valid if `ob_base` does not have the type [InvalidStaticLayout].
289-
pub const IS_VALID: bool = offset_of!(Self, contents) > 0;
287+
pub fn is_valid() -> bool {
288+
offset_of!(Self, contents) > 0
289+
}
290290
}
291291

292292
unsafe impl<T: PyClassImpl> PyLayout<T> for PyStaticClassLayout<T> {}
@@ -302,7 +302,7 @@ pub(crate) mod static_layout {
302302
unsafe impl<T, U> PyLayout<T> for PyStaticNativeLayout<U> where U: PySizedLayout<T> {}
303303

304304
/// a struct for use with opaque native types to indicate that they
305-
/// cannot be used as part of a static layout (see `PyStaticLayout::IS_VALID`).
305+
/// cannot be used as part of a static layout (see `PyStaticLayout::is_valid`).
306306
#[repr(C)]
307307
pub struct InvalidStaticLayout;
308308

@@ -374,7 +374,7 @@ impl PyObjectLayout {
374374
} else {
375375
let obj: *mut static_layout::PyStaticClassLayout<T> = obj.cast();
376376
debug_assert!(
377-
static_layout::PyStaticClassLayout::<T>::IS_VALID,
377+
static_layout::PyStaticClassLayout::<T>::is_valid(),
378378
"invalid static layout found"
379379
);
380380
unsafe { addr_of_mut!((*obj).contents) }

src/pyclass/create_type_object.rs

-6
Original file line numberDiff line numberDiff line change
@@ -422,18 +422,12 @@ impl PyTypeBuilder {
422422
Some(PyObjectOffset::Absolute(offset)) => {
423423
(*type_object).tp_dictoffset = offset;
424424
}
425-
Some(PyObjectOffset::Relative(_)) => {
426-
panic!("PyObjectOffset::Relative requires >=3.12")
427-
}
428425
None => {}
429426
}
430427
match weaklist_offset {
431428
Some(PyObjectOffset::Absolute(offset)) => {
432429
(*type_object).tp_weaklistoffset = offset;
433430
}
434-
Some(PyObjectOffset::Relative(_)) => {
435-
panic!("PyObjectOffset::Relative requires >=3.12")
436-
}
437431
None => {}
438432
}
439433
}));

src/types/datetime.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::types::any::PyAnyMethods;
2020
#[cfg(not(Py_LIMITED_API))]
2121
use crate::{ffi_ptr_ext::FfiPtrExt, py_result_ext::PyResultExt, types::PyTuple, IntoPyObject};
2222
#[cfg(Py_LIMITED_API)]
23-
use crate::{sync::GILOnceCell, types::IntoPyDict, types::PyType, Py, PyTypeCheck};
23+
use crate::{sync::GILOnceCell, types::IntoPyDict, Py, PyTypeCheck};
2424
use crate::{Bound, PyAny, PyErr, Python};
2525
#[cfg(not(Py_LIMITED_API))]
2626
use std::os::raw::c_int;
@@ -228,6 +228,7 @@ pyobject_native_type!(
228228
#module=Some("datetime"),
229229
#checkfunction=PyDate_Check
230230
);
231+
#[cfg(not(Py_LIMITED_API))]
231232
pyobject_native_type_object_methods!(
232233
PyDate,
233234
#create=|py| unsafe {
@@ -332,6 +333,7 @@ pyobject_native_type!(
332333
#module=Some("datetime"),
333334
#checkfunction=PyDateTime_Check
334335
);
336+
#[cfg(not(Py_LIMITED_API))]
335337
pyobject_native_type_object_methods!(
336338
PyDateTime,
337339
#create=|py| unsafe {
@@ -588,6 +590,7 @@ pyobject_native_type!(
588590
#module=Some("datetime"),
589591
#checkfunction=PyTime_Check
590592
);
593+
#[cfg(not(Py_LIMITED_API))]
591594
pyobject_native_type_object_methods!(
592595
PyTime,
593596
#create=|py| unsafe {
@@ -779,6 +782,7 @@ pyobject_native_type!(
779782
#module=Some("datetime"),
780783
#checkfunction=PyTZInfo_Check
781784
);
785+
#[cfg(not(Py_LIMITED_API))]
782786
pyobject_native_type_object_methods!(
783787
PyTzInfo,
784788
#create=|py| unsafe {
@@ -878,6 +882,7 @@ pyobject_native_type!(
878882
#module=Some("datetime"),
879883
#checkfunction=PyDelta_Check
880884
);
885+
#[cfg(not(Py_LIMITED_API))]
881886
pyobject_native_type_object_methods!(
882887
PyDelta,
883888
#create=|py| unsafe {

0 commit comments

Comments
 (0)