1
+ #[ cfg( Py_3_12 ) ]
2
+ use crate :: pycell:: impl_:: PyClassObjectContents ;
1
3
use crate :: {
2
4
conversion:: IntoPyObject ,
3
5
exceptions:: { PyAttributeError , PyNotImplementedError , PyRuntimeError , PyValueError } ,
@@ -8,10 +10,7 @@ use crate::{
8
10
pyclass_init:: PyObjectInit ,
9
11
pymethods:: { PyGetterDef , PyMethodDefType } ,
10
12
} ,
11
- pycell:: {
12
- impl_:: { InternalPyClassObjectLayout , PyClassObjectContents } ,
13
- PyBorrowError ,
14
- } ,
13
+ pycell:: { impl_:: InternalPyClassObjectLayout , PyBorrowError } ,
15
14
types:: { any:: PyAnyMethods , PyBool } ,
16
15
Borrowed , BoundObject , Py , PyAny , PyClass , PyErr , PyRef , PyResult , PyTypeInfo , Python ,
17
16
} ;
@@ -1197,20 +1196,9 @@ pub enum PyObjectOffset {
1197
1196
/// An offset relative to the start of the subclass-specific data.
1198
1197
/// Only allowed when basicsize is negative (which is only allowed for python >=3.12).
1199
1198
/// <https://docs.python.org/3.12/c-api/structures.html#c.Py_RELATIVE_OFFSET>
1200
- #[ cfg( Py_3_12 ) ]
1201
1199
Relative ( ffi:: Py_ssize_t ) ,
1202
1200
}
1203
1201
1204
- impl PyObjectOffset {
1205
- pub fn to_value_and_is_relative ( & self ) -> ( ffi:: Py_ssize_t , bool ) {
1206
- match self {
1207
- PyObjectOffset :: Absolute ( offset) => ( * offset, false ) ,
1208
- #[ cfg( Py_3_12 ) ]
1209
- PyObjectOffset :: Relative ( offset) => ( * offset, true ) ,
1210
- }
1211
- }
1212
- }
1213
-
1214
1202
impl std:: ops:: Add < usize > for PyObjectOffset {
1215
1203
type Output = PyObjectOffset ;
1216
1204
@@ -1221,7 +1209,6 @@ impl std::ops::Add<usize> for PyObjectOffset {
1221
1209
1222
1210
match self {
1223
1211
PyObjectOffset :: Absolute ( offset) => PyObjectOffset :: Absolute ( offset + rhs) ,
1224
- #[ cfg( Py_3_12 ) ]
1225
1212
PyObjectOffset :: Relative ( offset) => PyObjectOffset :: Relative ( offset + rhs) ,
1226
1213
}
1227
1214
}
@@ -1321,11 +1308,16 @@ impl<
1321
1308
pub fn generate ( & self , name : & ' static CStr , doc : & ' static CStr ) -> PyMethodDefType {
1322
1309
use crate :: pyclass:: boolean_struct:: private:: Boolean ;
1323
1310
if ClassT :: Frozen :: VALUE {
1324
- let ( offset, is_relative) = Offset :: offset ( ) . to_value_and_is_relative ( ) ;
1325
- let flags = if is_relative {
1326
- ffi:: Py_READONLY | ffi:: Py_RELATIVE_OFFSET
1327
- } else {
1328
- ffi:: Py_READONLY
1311
+ let ( offset, flags) = match Offset :: offset ( ) {
1312
+ PyObjectOffset :: Absolute ( offset) => ( offset, ffi:: Py_READONLY ) ,
1313
+ #[ cfg( Py_3_12 ) ]
1314
+ PyObjectOffset :: Relative ( offset) => {
1315
+ ( offset, ffi:: Py_READONLY | ffi:: Py_RELATIVE_OFFSET )
1316
+ }
1317
+ #[ cfg( not( Py_3_12 ) ) ]
1318
+ PyObjectOffset :: Relative ( _) => {
1319
+ panic ! ( "relative offsets not valid before python 3.12" ) ;
1320
+ }
1329
1321
} ;
1330
1322
PyMethodDefType :: StructMember ( ffi:: PyMemberDef {
1331
1323
name : name. as_ptr ( ) ,
@@ -1560,6 +1552,10 @@ where
1560
1552
let contents = class_obj. contents_mut ( ) as * mut PyClassObjectContents < ClassT > ;
1561
1553
( contents. cast :: < u8 > ( ) , offset)
1562
1554
}
1555
+ #[ cfg( not( Py_3_12 ) ) ]
1556
+ PyObjectOffset :: Relative ( _) => {
1557
+ panic ! ( "relative offsets not valid before python 3.12" ) ;
1558
+ }
1563
1559
} ;
1564
1560
// Safety: conditions for pointer addition must be met
1565
1561
unsafe { base. add ( offset as usize ) } . cast :: < FieldT > ( )
0 commit comments