1
- From 1e4e88533f860a0a06668b973264db17cb52387e Mon Sep 17 00:00:00 2001
2
- From: Tim Felgentreff <
[email protected] >
3
- Date: Tue, 19 Sep 2023 07:05:59 +0200
4
- Subject: [PATCH] get it to build on graalpy
5
-
6
- ---
7
- Cargo.lock | 6 ++----
8
- Cargo.toml | 4 ++--
9
- src/ffi/fragment.rs | 3 +++
10
- src/ffi/list.rs | 3 +++
11
- src/lib.rs | 18 +++++++++++++++---
12
- src/serialize/datetime.rs | 4 ++++
13
- src/serialize/default.rs | 4 ++--
14
- src/serialize/numpy.rs | 6 ++++++
15
- src/serialize/tuple.rs | 3 +++
16
- src/util.rs | 4 ++--
17
- 10 files changed, 42 insertions(+), 13 deletions(-)
18
-
19
1
diff --git a/Cargo.lock b/Cargo.lock
20
2
index f8ec582..e835fd4 100644
21
3
--- a/Cargo.lock
@@ -62,6 +44,47 @@ index 8ca77f7..9b15f81 100644
62
44
version_check = { version = "0.9" }
63
45
64
46
[profile.dev]
47
+ diff --git a/src/deserialize/cache.rs b/src/deserialize/cache.rs
48
+ index 62142f7..d136b2b 100644
49
+ --- a/src/deserialize/cache.rs
50
+ +++ b/src/deserialize/cache.rs
51
+ @@ -24,7 +24,7 @@ impl CachedKey {
52
+ }
53
+ pub fn get(&mut self) -> *mut pyo3_ffi::PyObject {
54
+ let ptr = self.ptr as *mut pyo3_ffi::PyObject;
55
+ - debug_assert!(ffi!(Py_REFCNT(ptr)) >= 1);
56
+ + // debug_assert!(ffi!(Py_REFCNT(ptr)) >= 1);
57
+ ffi!(Py_INCREF(ptr));
58
+ ptr
59
+ }
60
+ diff --git a/src/deserialize/deserializer.rs b/src/deserialize/deserializer.rs
61
+ index 0675cce..b9b01ab 100644
62
+ --- a/src/deserialize/deserializer.rs
63
+ +++ b/src/deserialize/deserializer.rs
64
+ @@ -8,7 +8,7 @@ use std::ptr::NonNull;
65
+ pub fn deserialize(
66
+ ptr: *mut pyo3_ffi::PyObject,
67
+ ) -> Result<NonNull<pyo3_ffi::PyObject>, DeserializeError<'static>> {
68
+ - debug_assert!(ffi!(Py_REFCNT(ptr)) >= 1);
69
+ + // debug_assert!(ffi!(Py_REFCNT(ptr)) >= 1);
70
+ let buffer = read_input_to_buf(ptr)?;
71
+
72
+ if unlikely!(buffer.len() == 2) {
73
+ diff --git a/src/deserialize/pyobject.rs b/src/deserialize/pyobject.rs
74
+ index 49766b1..3a00296 100644
75
+ --- a/src/deserialize/pyobject.rs
76
+ +++ b/src/deserialize/pyobject.rs
77
+ @@ -29,8 +29,8 @@ pub fn get_unicode_key(key_str: &str) -> *mut pyo3_ffi::PyObject {
78
+ );
79
+ pykey = entry.get();
80
+ }
81
+ - debug_assert!(ffi!(Py_REFCNT(pykey)) >= 1);
82
+ - debug_assert!(unsafe { (*pykey.cast::<pyo3_ffi::PyASCIIObject>()).hash != -1 });
83
+ + // debug_assert!(ffi!(Py_REFCNT(pykey)) >= 1);
84
+ + // debug_assert!(unsafe { (*pykey.cast::<pyo3_ffi::PyASCIIObject>()).hash != -1 });
85
+ pykey
86
+ }
87
+
65
88
diff --git a/src/ffi/fragment.rs b/src/ffi/fragment.rs
66
89
index 44d4517..006111a 100644
67
90
--- a/src/ffi/fragment.rs
@@ -93,10 +116,10 @@ index 44b386c..5800ac3 100644
93
116
Some(nonnull!(elem))
94
117
}
95
118
diff --git a/src/lib.rs b/src/lib.rs
96
- index 7728d45..5f9547a 100644
119
+ index 7728d45..35ba2b0 100644
97
120
--- a/src/lib.rs
98
121
+++ b/src/lib.rs
99
- @@ -214,9 +214,18 @@ fn raise_loads_exception(err: deserialize::DeserializeError) -> *mut PyObject {
122
+ @@ -214,11 +214,20 @@ fn raise_loads_exception(err: deserialize::DeserializeError) -> *mut PyObject {
100
123
PyUnicode_FromStringAndSize(msg.as_ptr() as *const c_char, msg.len() as isize);
101
124
let args = PyTuple_New(3);
102
125
let pos = PyLong_FromLongLong(pos);
@@ -116,8 +139,38 @@ index 7728d45..5f9547a 100644
116
139
+ PyTuple_SetItem(args, 2, pos);
117
140
+ }
118
141
PyErr_SetObject(typeref::JsonDecodeError, args);
119
- debug_assert!(ffi!(Py_REFCNT(args)) == 2);
142
+ - debug_assert!(ffi!(Py_REFCNT(args)) == 2);
143
+ + // debug_assert!(ffi!(Py_REFCNT(args)) == 2);
120
144
Py_DECREF(args);
145
+ };
146
+ null_mut()
147
+ @@ -232,7 +241,7 @@ fn raise_dumps_exception_fixed(msg: &str) -> *mut PyObject {
148
+ let err_msg =
149
+ PyUnicode_FromStringAndSize(msg.as_ptr() as *const c_char, msg.len() as isize);
150
+ PyErr_SetObject(typeref::JsonEncodeError, err_msg);
151
+ - debug_assert!(ffi!(Py_REFCNT(err_msg)) == 2);
152
+ + // debug_assert!(ffi!(Py_REFCNT(err_msg)) == 2);
153
+ Py_DECREF(err_msg);
154
+ };
155
+ null_mut()
156
+ @@ -249,7 +258,7 @@ fn raise_dumps_exception_dynamic(err: &String) -> *mut PyObject {
157
+ let err_msg =
158
+ PyUnicode_FromStringAndSize(err.as_ptr() as *const c_char, err.len() as isize);
159
+ PyErr_SetObject(typeref::JsonEncodeError, err_msg);
160
+ - debug_assert!(ffi!(Py_REFCNT(err_msg)) == 2);
161
+ + // debug_assert!(ffi!(Py_REFCNT(err_msg)) == 2);
162
+ Py_DECREF(err_msg);
163
+
164
+ if !cause_exc.is_null() {
165
+ @@ -275,7 +284,7 @@ fn raise_dumps_exception_dynamic(err: &String) -> *mut PyObject {
166
+ let err_msg =
167
+ PyUnicode_FromStringAndSize(err.as_ptr() as *const c_char, err.len() as isize);
168
+ PyErr_SetObject(typeref::JsonEncodeError, err_msg);
169
+ - debug_assert!(ffi!(Py_REFCNT(err_msg)) == 2);
170
+ + // debug_assert!(ffi!(Py_REFCNT(err_msg)) == 2);
171
+ Py_DECREF(err_msg);
172
+ let mut tp: *mut PyObject = null_mut();
173
+ let mut val: *mut PyObject = null_mut();
121
174
@@ -330,7 +339,10 @@ pub unsafe extern "C" fn dumps(
122
175
}
123
176
if !kwnames.is_null() {
@@ -129,6 +182,46 @@ index 7728d45..5f9547a 100644
129
182
if arg == typeref::DEFAULT {
130
183
if unlikely!(num_args & 2 == 2) {
131
184
return raise_dumps_exception_fixed(
185
+ @@ -354,7 +366,7 @@ pub unsafe extern "C" fn dumps(
186
+ let mut optsbits: i32 = 0;
187
+ if let Some(opts) = optsptr {
188
+ if opts.as_ptr() == typeref::NONE {
189
+ - } else if (*opts.as_ptr()).ob_type != typeref::INT_TYPE {
190
+ + } else if Py_TYPE(opts.as_ptr()) != typeref::INT_TYPE {
191
+ return raise_dumps_exception_fixed("Invalid opts");
192
+ } else {
193
+ optsbits = PyLong_AsLong(optsptr.unwrap().as_ptr()) as i32;
194
+ diff --git a/src/serialize/dataclass.rs b/src/serialize/dataclass.rs
195
+ index aa805c4..8f134d5 100644
196
+ --- a/src/serialize/dataclass.rs
197
+ +++ b/src/serialize/dataclass.rs
198
+ @@ -178,7 +178,7 @@ impl Serialize for DataclassFallbackSerializer {
199
+ S: Serializer,
200
+ {
201
+ let fields = ffi!(PyObject_GetAttr(self.ptr, DATACLASS_FIELDS_STR));
202
+ - debug_assert!(ffi!(Py_REFCNT(fields)) >= 2);
203
+ + // debug_assert!(ffi!(Py_REFCNT(fields)) >= 2);
204
+ ffi!(Py_DECREF(fields));
205
+ let len = ffi!(Py_SIZE(fields)) as usize;
206
+ if unlikely!(len == 0) {
207
+ @@ -187,7 +187,7 @@ impl Serialize for DataclassFallbackSerializer {
208
+ let mut map = serializer.serialize_map(None).unwrap();
209
+ for (attr, field) in PyDictIter::from_pyobject(fields) {
210
+ let field_type = ffi!(PyObject_GetAttr(field.as_ptr(), FIELD_TYPE_STR));
211
+ - debug_assert!(ffi!(Py_REFCNT(field_type)) >= 2);
212
+ + // debug_assert!(ffi!(Py_REFCNT(field_type)) >= 2);
213
+ ffi!(Py_DECREF(field_type));
214
+ if unsafe { field_type as *mut pyo3_ffi::PyTypeObject != FIELD_TYPE } {
215
+ continue;
216
+ @@ -202,7 +202,7 @@ impl Serialize for DataclassFallbackSerializer {
217
+ }
218
+
219
+ let value = ffi!(PyObject_GetAttr(self.ptr, attr.as_ptr()));
220
+ - debug_assert!(ffi!(Py_REFCNT(value)) >= 2);
221
+ + // debug_assert!(ffi!(Py_REFCNT(value)) >= 2);
222
+ ffi!(Py_DECREF(value));
223
+ let pyvalue = PyObjectSerializer::new(
224
+ value,
132
225
diff --git a/src/serialize/datetime.rs b/src/serialize/datetime.rs
133
226
index adf35fb..13d5747 100644
134
227
--- a/src/serialize/datetime.rs
@@ -165,6 +258,19 @@ index 6f2d098..eefca82 100644
165
258
let default_obj = unsafe {
166
259
pyo3_ffi::PyObject_Vectorcall(
167
260
callable.as_ptr(),
261
+ diff --git a/src/serialize/dict.rs b/src/serialize/dict.rs
262
+ index a2517e0..9d8e1c7 100644
263
+ --- a/src/serialize/dict.rs
264
+ +++ b/src/serialize/dict.rs
265
+ @@ -293,7 +293,7 @@ impl DictNonStrKey {
266
+ }
267
+ ObType::Enum => {
268
+ let value = ffi!(PyObject_GetAttr(key, VALUE_STR));
269
+ - debug_assert!(ffi!(Py_REFCNT(value)) >= 2);
270
+ + // debug_assert!(ffi!(Py_REFCNT(value)) >= 2);
271
+ let ret = Self::pyobject_to_string(value, opts);
272
+ ffi!(Py_DECREF(value));
273
+ ret
168
274
diff --git a/src/serialize/numpy.rs b/src/serialize/numpy.rs
169
275
index 2ae9204..9dc97b4 100644
170
276
--- a/src/serialize/numpy.rs
@@ -184,6 +290,35 @@ index 2ae9204..9dc97b4 100644
184
290
let uni = crate::str::unicode_to_str(descr_str).unwrap();
185
291
if uni.len() < 5 {
186
292
return Self::NaT;
293
+ diff --git a/src/serialize/pyenum.rs b/src/serialize/pyenum.rs
294
+ index 1972f8a..d930c90 100644
295
+ --- a/src/serialize/pyenum.rs
296
+ +++ b/src/serialize/pyenum.rs
297
+ @@ -39,7 +39,7 @@ impl Serialize for EnumSerializer {
298
+ S: Serializer,
299
+ {
300
+ let value = ffi!(PyObject_GetAttr(self.ptr, VALUE_STR));
301
+ - debug_assert!(ffi!(Py_REFCNT(value)) >= 2);
302
+ + // debug_assert!(ffi!(Py_REFCNT(value)) >= 2);
303
+ let ret = PyObjectSerializer::new(
304
+ value,
305
+ self.opts,
306
+ diff --git a/src/serialize/serializer.rs b/src/serialize/serializer.rs
307
+ index 0d7d0b3..ea7d8aa 100644
308
+ --- a/src/serialize/serializer.rs
309
+ +++ b/src/serialize/serializer.rs
310
+ @@ -106,10 +106,7 @@ macro_rules! is_subclass_by_flag {
311
+
312
+ macro_rules! is_subclass_by_type {
313
+ ($ob_type:expr, $type:ident) => {
314
+ - (*($ob_type as *mut pyo3_ffi::PyTypeObject))
315
+ - .ob_base
316
+ - .ob_base
317
+ - .ob_type
318
+ + pyo3_ffi::Py_TYPE($ob_type as *mut pyo3_ffi::PyObject)
319
+ == $type
320
+ };
321
+ }
187
322
diff --git a/src/serialize/tuple.rs b/src/serialize/tuple.rs
188
323
index 7ad76e4..46881cf 100644
189
324
--- a/src/serialize/tuple.rs
@@ -199,10 +334,139 @@ index 7ad76e4..46881cf 100644
199
334
let value = PyObjectSerializer::new(
200
335
elem,
201
336
self.opts,
337
+ diff --git a/src/str/ffi.rs b/src/str/ffi.rs
338
+ index 2f37b6b..013273d 100644
339
+ --- a/src/str/ffi.rs
340
+ +++ b/src/str/ffi.rs
341
+ @@ -37,18 +37,6 @@ pub fn unicode_to_str_via_ffi(op: *mut PyObject) -> Option<&'static str> {
342
+ #[inline]
343
+ pub fn unicode_to_str(op: *mut PyObject) -> Option<&'static str> {
344
+ unsafe {
345
+ - if unlikely!((*op.cast::<PyASCIIObject>()).compact() == 0) {
346
+ - unicode_to_str_via_ffi(op)
347
+ - } else if (*op.cast::<PyASCIIObject>()).ascii() == 1 {
348
+ - let ptr = op.cast::<PyASCIIObject>().offset(1) as *const u8;
349
+ - let len = (*op.cast::<PyASCIIObject>()).length as usize;
350
+ - Some(str_from_slice!(ptr, len))
351
+ - } else if (*op.cast::<PyCompactUnicodeObject>()).utf8_length != 0 {
352
+ - let ptr = (*op.cast::<PyCompactUnicodeObject>()).utf8 as *const u8;
353
+ - let len = (*op.cast::<PyCompactUnicodeObject>()).utf8_length as usize;
354
+ - Some(str_from_slice!(ptr, len))
355
+ - } else {
356
+ - unicode_to_str_via_ffi(op)
357
+ - }
358
+ + unicode_to_str_via_ffi(op)
359
+ }
360
+ }
361
+ diff --git a/src/typeref.rs b/src/typeref.rs
362
+ index 664f8a3..f88d85c 100644
363
+ --- a/src/typeref.rs
364
+ +++ b/src/typeref.rs
365
+ @@ -157,26 +157,26 @@ fn _init_typerefs_impl() -> bool {
366
+ TRUE = Py_True();
367
+ FALSE = Py_False();
368
+ EMPTY_UNICODE = PyUnicode_New(0, 255);
369
+ - STR_TYPE = (*EMPTY_UNICODE).ob_type;
370
+ - BYTES_TYPE = (*PyBytes_FromStringAndSize("".as_ptr() as *const c_char, 0)).ob_type;
371
+ + STR_TYPE = Py_TYPE(EMPTY_UNICODE);
372
+ + BYTES_TYPE = Py_TYPE(PyBytes_FromStringAndSize("".as_ptr() as *const c_char, 0));
373
+
374
+ {
375
+ let bytearray = PyByteArray_FromStringAndSize("".as_ptr() as *const c_char, 0);
376
+ - BYTEARRAY_TYPE = (*bytearray).ob_type;
377
+ + BYTEARRAY_TYPE = Py_TYPE(bytearray);
378
+
379
+ let memoryview = PyMemoryView_FromObject(bytearray);
380
+ - MEMORYVIEW_TYPE = (*memoryview).ob_type;
381
+ + MEMORYVIEW_TYPE = Py_TYPE(memoryview);
382
+ Py_DECREF(memoryview);
383
+ Py_DECREF(bytearray);
384
+ }
385
+
386
+ - DICT_TYPE = (*PyDict_New()).ob_type;
387
+ - LIST_TYPE = (*PyList_New(0)).ob_type;
388
+ - TUPLE_TYPE = (*PyTuple_New(0)).ob_type;
389
+ - NONE_TYPE = (*NONE).ob_type;
390
+ - BOOL_TYPE = (*TRUE).ob_type;
391
+ - INT_TYPE = (*PyLong_FromLongLong(0)).ob_type;
392
+ - FLOAT_TYPE = (*PyFloat_FromDouble(0.0)).ob_type;
393
+ + DICT_TYPE = Py_TYPE(PyDict_New());
394
+ + LIST_TYPE = Py_TYPE(PyList_New(0));
395
+ + TUPLE_TYPE = Py_TYPE(PyTuple_New(0));
396
+ + NONE_TYPE = Py_TYPE(NONE);
397
+ + BOOL_TYPE = Py_TYPE(TRUE);
398
+ + INT_TYPE = Py_TYPE(PyLong_FromLongLong(0));
399
+ + FLOAT_TYPE = Py_TYPE(PyFloat_FromDouble(0.0));
400
+ DATETIME_TYPE = look_up_datetime_type();
401
+ DATE_TYPE = look_up_date_type();
402
+ TIME_TYPE = look_up_time_type();
403
+ @@ -303,7 +303,7 @@ unsafe fn look_up_uuid_type() -> *mut PyTypeObject {
404
+ let uuid_mod = PyImport_ImportModule("uuid\0".as_ptr() as *const c_char);
405
+ let uuid_mod_dict = PyObject_GenericGetDict(uuid_mod, null_mut());
406
+ let uuid = PyMapping_GetItemString(uuid_mod_dict, "NAMESPACE_DNS\0".as_ptr() as *const c_char);
407
+ - let ptr = (*uuid).ob_type;
408
+ + let ptr = Py_TYPE(uuid);
409
+ Py_DECREF(uuid);
410
+ Py_DECREF(uuid_mod_dict);
411
+ Py_DECREF(uuid_mod);
412
+ @@ -324,7 +324,7 @@ unsafe fn look_up_datetime_type() -> *mut PyTypeObject {
413
+ NONE,
414
+ (*(PyDateTimeAPI())).DateTimeType,
415
+ );
416
+ - let ptr = (*datetime).ob_type;
417
+ + let ptr = Py_TYPE(datetime);
418
+ Py_DECREF(datetime);
419
+ ptr
420
+ }
421
+ @@ -333,7 +333,7 @@ unsafe fn look_up_datetime_type() -> *mut PyTypeObject {
422
+ #[cfg_attr(feature = "optimize", optimize(size))]
423
+ unsafe fn look_up_date_type() -> *mut PyTypeObject {
424
+ let date = ((*PyDateTimeAPI()).Date_FromDate)(1, 1, 1, (*(PyDateTimeAPI())).DateType);
425
+ - let ptr = (*date).ob_type;
426
+ + let ptr = Py_TYPE(date);
427
+ Py_DECREF(date);
428
+ ptr
429
+ }
430
+ @@ -342,7 +342,7 @@ unsafe fn look_up_date_type() -> *mut PyTypeObject {
431
+ #[cfg_attr(feature = "optimize", optimize(size))]
432
+ unsafe fn look_up_time_type() -> *mut PyTypeObject {
433
+ let time = ((*PyDateTimeAPI()).Time_FromTime)(0, 0, 0, 0, NONE, (*(PyDateTimeAPI())).TimeType);
434
+ - let ptr = (*time).ob_type;
435
+ + let ptr = Py_TYPE(time);
436
+ Py_DECREF(time);
437
+ ptr
438
+ }
202
439
diff --git a/src/util.rs b/src/util.rs
203
- index fa3b7e1..2b2df61 100644
440
+ index fa3b7e1..fddf97b 100644
204
441
--- a/src/util.rs
205
442
+++ b/src/util.rs
443
+ @@ -8,7 +8,7 @@ macro_rules! is_type {
444
+
445
+ macro_rules! ob_type {
446
+ ($obj:expr) => {
447
+ - unsafe { (*$obj).ob_type }
448
+ + unsafe { pyo3_ffi::Py_TYPE($obj) }
449
+ };
450
+ }
451
+
452
+ @@ -61,7 +61,7 @@ macro_rules! reverse_pydict_incref {
453
+ ($op:expr) => {
454
+ unsafe {
455
+ if crate::ffi::_Py_IsImmortal($op) == 0 {
456
+ - debug_assert!(ffi!(Py_REFCNT($op)) >= 2);
457
+ + // debug_assert!(ffi!(Py_REFCNT($op)) >= 2);
458
+ (*$op).ob_refcnt.ob_refcnt -= 1;
459
+ }
460
+ }
461
+ @@ -72,7 +72,7 @@ macro_rules! reverse_pydict_incref {
462
+ macro_rules! reverse_pydict_incref {
463
+ ($op:expr) => {
464
+ unsafe {
465
+ - debug_assert!(ffi!(Py_REFCNT($op)) >= 2);
466
+ + // debug_assert!(ffi!(Py_REFCNT($op)) >= 2);
467
+ (*$op).ob_refcnt -= 1;
468
+ }
469
+ };
206
470
@@ -100,7 +100,7 @@ macro_rules! ffi {
207
471
};
208
472
}
@@ -221,5 +485,3 @@ index fa3b7e1..2b2df61 100644
221
485
macro_rules! call_method {
222
486
($obj1:expr, $obj2:expr) => {
223
487
unsafe {
224
- - -
225
- 2.34.1
0 commit comments