1
1
#[ cfg( not( PyPy ) ) ]
2
2
use crate :: Py_hash_t ;
3
3
use crate :: { PyObject , Py_UCS1 , Py_UCS2 , Py_UCS4 , Py_UNICODE , Py_ssize_t } ;
4
+ #[ cfg( not( Py_3_12 ) ) ]
4
5
use libc:: wchar_t;
5
6
use std:: os:: raw:: { c_char, c_int, c_uint, c_void} ;
6
7
@@ -116,6 +117,28 @@ where
116
117
}
117
118
}
118
119
120
+ const STATE_INTERNED_INDEX : usize = 0 ;
121
+ #[ cfg( not( Py_3_12 ) ) ]
122
+ const STATE_INTERNED_WIDTH : u8 = 2 ;
123
+ #[ cfg( Py_3_12 ) ]
124
+ const STATE_INTERNED_WIDTH : u8 = 1 ;
125
+
126
+ const STATE_KIND_INDEX : usize = STATE_INTERNED_WIDTH as usize ;
127
+ const STATE_KIND_WIDTH : u8 = 3 ;
128
+
129
+ const STATE_COMPACT_INDEX : usize = ( STATE_INTERNED_WIDTH + STATE_KIND_WIDTH ) as usize ;
130
+ const STATE_COMPACT_WIDTH : u8 = 1 ;
131
+
132
+ const STATE_ASCII_INDEX : usize =
133
+ ( STATE_INTERNED_WIDTH + STATE_KIND_WIDTH + STATE_COMPACT_WIDTH ) as usize ;
134
+ const STATE_ASCII_WIDTH : u8 = 1 ;
135
+
136
+ #[ cfg( not( Py_3_12 ) ) ]
137
+ const STATE_READY_INDEX : usize =
138
+ ( STATE_INTERNED_WIDTH + STATE_KIND_WIDTH + STATE_COMPACT_WIDTH + STATE_ASCII_WIDTH ) as usize ;
139
+ #[ cfg( not( Py_3_12 ) ) ]
140
+ const STATE_READY_WIDTH : u8 = 1 ;
141
+
119
142
// generated by bindgen v0.63.0 (with small adaptations)
120
143
// The same code is generated for Python 3.7, 3.8, 3.9, 3.10, and 3.11, but the "ready" field
121
144
// has been removed from Python 3.12.
@@ -137,57 +160,67 @@ struct PyASCIIObjectState {
137
160
impl PyASCIIObjectState {
138
161
#[ inline]
139
162
unsafe fn interned ( & self ) -> c_uint {
140
- std:: mem:: transmute ( self . _bitfield . get ( 0usize , 2u8 ) as u32 )
163
+ std:: mem:: transmute (
164
+ self . _bitfield
165
+ . get ( STATE_INTERNED_INDEX , STATE_INTERNED_WIDTH ) as u32 ,
166
+ )
141
167
}
142
168
143
169
#[ inline]
144
170
unsafe fn set_interned ( & mut self , val : c_uint ) {
145
171
let val: u32 = std:: mem:: transmute ( val) ;
146
- self . _bitfield . set ( 0usize , 2u8 , val as u64 )
172
+ self . _bitfield
173
+ . set ( STATE_INTERNED_INDEX , STATE_INTERNED_WIDTH , val as u64 )
147
174
}
148
175
149
176
#[ inline]
150
177
unsafe fn kind ( & self ) -> c_uint {
151
- std:: mem:: transmute ( self . _bitfield . get ( 2usize , 3u8 ) as u32 )
178
+ std:: mem:: transmute ( self . _bitfield . get ( STATE_KIND_INDEX , STATE_KIND_WIDTH ) as u32 )
152
179
}
153
180
154
181
#[ inline]
155
182
unsafe fn set_kind ( & mut self , val : c_uint ) {
156
183
let val: u32 = std:: mem:: transmute ( val) ;
157
- self . _bitfield . set ( 2usize , 3u8 , val as u64 )
184
+ self . _bitfield
185
+ . set ( STATE_KIND_INDEX , STATE_KIND_WIDTH , val as u64 )
158
186
}
159
187
160
188
#[ inline]
161
189
unsafe fn compact ( & self ) -> c_uint {
162
- std:: mem:: transmute ( self . _bitfield . get ( 5usize , 1u8 ) as u32 )
190
+ std:: mem:: transmute ( self . _bitfield . get ( STATE_COMPACT_INDEX , STATE_COMPACT_WIDTH ) as u32 )
163
191
}
164
192
165
193
#[ inline]
166
194
unsafe fn set_compact ( & mut self , val : c_uint ) {
167
195
let val: u32 = std:: mem:: transmute ( val) ;
168
- self . _bitfield . set ( 5usize , 1u8 , val as u64 )
196
+ self . _bitfield
197
+ . set ( STATE_COMPACT_INDEX , STATE_COMPACT_WIDTH , val as u64 )
169
198
}
170
199
171
200
#[ inline]
172
201
unsafe fn ascii ( & self ) -> c_uint {
173
- std:: mem:: transmute ( self . _bitfield . get ( 6usize , 1u8 ) as u32 )
202
+ std:: mem:: transmute ( self . _bitfield . get ( STATE_ASCII_INDEX , STATE_ASCII_WIDTH ) as u32 )
174
203
}
175
204
176
205
#[ inline]
177
206
unsafe fn set_ascii ( & mut self , val : c_uint ) {
178
207
let val: u32 = std:: mem:: transmute ( val) ;
179
- self . _bitfield . set ( 6usize , 1u8 , val as u64 )
208
+ self . _bitfield
209
+ . set ( STATE_ASCII_INDEX , STATE_ASCII_WIDTH , val as u64 )
180
210
}
181
211
212
+ #[ cfg( not( Py_3_12 ) ) ]
182
213
#[ inline]
183
214
unsafe fn ready ( & self ) -> c_uint {
184
- std:: mem:: transmute ( self . _bitfield . get ( 7usize , 1u8 ) as u32 )
215
+ std:: mem:: transmute ( self . _bitfield . get ( STATE_READY_INDEX , STATE_READY_WIDTH ) as u32 )
185
216
}
186
217
218
+ #[ cfg( not( Py_3_12 ) ) ]
187
219
#[ inline]
188
220
unsafe fn set_ready ( & mut self , val : c_uint ) {
189
221
let val: u32 = std:: mem:: transmute ( val) ;
190
- self . _bitfield . set ( 7usize , 1u8 , val as u64 )
222
+ self . _bitfield
223
+ . set ( STATE_READY_INDEX , STATE_READY_WIDTH , val as u64 )
191
224
}
192
225
}
193
226
@@ -226,14 +259,16 @@ pub struct PyASCIIObject {
226
259
/// unsigned int ready:1;
227
260
/// unsigned int :24;
228
261
pub state : u32 ,
262
+ #[ cfg( not( Py_3_12 ) ) ]
229
263
pub wstr : * mut wchar_t ,
230
264
}
231
265
232
266
/// Interacting with the bitfield is not actually well-defined, so we mark these APIs unsafe.
233
267
impl PyASCIIObject {
234
268
/// Get the `interned` field of the [`PyASCIIObject`] state bitfield.
235
269
///
236
- /// Returns one of: [`SSTATE_NOT_INTERNED`], [`SSTATE_INTERNED_MORTAL`], [`SSTATE_INTERNED_IMMORTAL`]
270
+ /// Returns one of: [`SSTATE_NOT_INTERNED`], [`SSTATE_INTERNED_MORTAL`],
271
+ /// or on CPython earlier than 3.12, [`SSTATE_INTERNED_IMMORTAL`]
237
272
#[ inline]
238
273
pub unsafe fn interned ( & self ) -> c_uint {
239
274
PyASCIIObjectState :: from ( self . state ) . interned ( )
@@ -242,7 +277,8 @@ impl PyASCIIObject {
242
277
/// Set the `interned` field of the [`PyASCIIObject`] state bitfield.
243
278
///
244
279
/// Calling this function with an argument that is not [`SSTATE_NOT_INTERNED`],
245
- /// [`SSTATE_INTERNED_MORTAL`], or [`SSTATE_INTERNED_IMMORTAL`] is invalid.
280
+ /// [`SSTATE_INTERNED_MORTAL`], or on CPython earlier than 3.12,
281
+ /// [`SSTATE_INTERNED_IMMORTAL`] is invalid.
246
282
#[ inline]
247
283
pub unsafe fn set_interned ( & mut self , val : c_uint ) {
248
284
let mut state = PyASCIIObjectState :: from ( self . state ) ;
@@ -309,6 +345,7 @@ impl PyASCIIObject {
309
345
/// Get the `ready` field of the [`PyASCIIObject`] state bitfield.
310
346
///
311
347
/// Returns either `0` or `1`.
348
+ #[ cfg( not( Py_3_12 ) ) ]
312
349
#[ inline]
313
350
pub unsafe fn ready ( & self ) -> c_uint {
314
351
PyASCIIObjectState :: from ( self . state ) . ready ( )
@@ -317,6 +354,7 @@ impl PyASCIIObject {
317
354
/// Set the `ready` flag of the [`PyASCIIObject`] state bitfield.
318
355
///
319
356
/// Calling this function with an argument that is neither `0` nor `1` is invalid.
357
+ #[ cfg( not( Py_3_12 ) ) ]
320
358
#[ inline]
321
359
pub unsafe fn set_ready ( & mut self , val : c_uint ) {
322
360
let mut state = PyASCIIObjectState :: from ( self . state ) ;
@@ -330,6 +368,7 @@ pub struct PyCompactUnicodeObject {
330
368
pub _base : PyASCIIObject ,
331
369
pub utf8_length : Py_ssize_t ,
332
370
pub utf8 : * mut c_char ,
371
+ #[ cfg( not( Py_3_12 ) ) ]
333
372
pub wstr_length : Py_ssize_t ,
334
373
}
335
374
@@ -359,6 +398,7 @@ extern "C" {
359
398
360
399
pub const SSTATE_NOT_INTERNED : c_uint = 0 ;
361
400
pub const SSTATE_INTERNED_MORTAL : c_uint = 1 ;
401
+ #[ cfg( not( Py_3_12 ) ) ]
362
402
pub const SSTATE_INTERNED_IMMORTAL : c_uint = 2 ;
363
403
364
404
#[ inline]
@@ -449,6 +489,14 @@ pub unsafe fn PyUnicode_GET_LENGTH(op: *mut PyObject) -> Py_ssize_t {
449
489
( * ( op as * mut PyASCIIObject ) ) . length
450
490
}
451
491
492
+ #[ cfg( Py_3_12 ) ]
493
+ #[ inline]
494
+ pub unsafe fn PyUnicode_IS_READY ( _op : * mut PyObject ) -> c_uint {
495
+ // kept in CPython for backwards compatibility
496
+ 1
497
+ }
498
+
499
+ #[ cfg( not( Py_3_12 ) ) ]
452
500
#[ inline]
453
501
pub unsafe fn PyUnicode_IS_READY ( op : * mut PyObject ) -> c_uint {
454
502
( * ( op as * mut PyASCIIObject ) ) . ready ( )
0 commit comments