Skip to content

Commit 8686b22

Browse files
committed
use_immortal!()
1 parent 2e9ff51 commit 8686b22

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

src/deserialize/deserializer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ pub fn deserialize(
1616
} else if buffer == b"{}" {
1717
return Ok(nonnull!(ffi!(PyDict_New())));
1818
} else if buffer == b"\"\"" {
19-
ffi!(Py_INCREF(EMPTY_UNICODE));
20-
unsafe { return Ok(nonnull!(EMPTY_UNICODE)) }
19+
unsafe { return Ok(nonnull!(use_immortal!(EMPTY_UNICODE))) }
2120
}
2221
}
2322

src/deserialize/pyobject.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,12 @@ pub fn parse_bool(val: bool) -> NonNull<pyo3_ffi::PyObject> {
4242

4343
#[inline(always)]
4444
pub fn parse_true() -> NonNull<pyo3_ffi::PyObject> {
45-
ffi!(Py_INCREF(TRUE));
46-
nonnull!(TRUE)
45+
nonnull!(use_immortal!(TRUE))
4746
}
4847

4948
#[inline(always)]
5049
pub fn parse_false() -> NonNull<pyo3_ffi::PyObject> {
51-
ffi!(Py_INCREF(FALSE));
52-
nonnull!(FALSE)
50+
nonnull!(use_immortal!(FALSE))
5351
}
5452
#[inline(always)]
5553
pub fn parse_i64(val: i64) -> NonNull<pyo3_ffi::PyObject> {
@@ -68,6 +66,5 @@ pub fn parse_f64(val: f64) -> NonNull<pyo3_ffi::PyObject> {
6866

6967
#[inline(always)]
7068
pub fn parse_none() -> NonNull<pyo3_ffi::PyObject> {
71-
ffi!(Py_INCREF(NONE));
72-
nonnull!(NONE)
69+
nonnull!(use_immortal!(NONE))
7370
}

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ fn raise_loads_exception(err: deserialize::DeserializeError) -> *mut PyObject {
206206
PyUnicode_FromStringAndSize(as_str.as_ptr() as *const c_char, as_str.len() as isize)
207207
},
208208
None => {
209-
ffi!(Py_INCREF(crate::typeref::EMPTY_UNICODE));
210-
unsafe { crate::typeref::EMPTY_UNICODE }
209+
use_immortal!(crate::typeref::EMPTY_UNICODE)
211210
}
212211
};
213212
unsafe {

src/str/create.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ fn find_str_kind(buf: &str, num_chars: usize) -> PyUnicodeKind {
2626

2727
pub fn unicode_from_str(buf: &str) -> *mut pyo3_ffi::PyObject {
2828
if buf.is_empty() {
29-
ffi!(Py_INCREF(EMPTY_UNICODE));
30-
unsafe { EMPTY_UNICODE }
29+
use_immortal!(EMPTY_UNICODE)
3130
} else {
3231
let num_chars = bytecount::num_chars(buf.as_bytes());
3332
match find_str_kind(buf, num_chars) {

src/util.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,20 @@ macro_rules! pydict_contains {
144144
unsafe { pyo3_ffi::PyDict_Contains((*$obj1).tp_dict, $obj2) == 1 }
145145
};
146146
}
147+
148+
#[cfg(Py_3_12)]
149+
macro_rules! use_immortal {
150+
($op:expr) => {
151+
unsafe { $op }
152+
};
153+
}
154+
155+
#[cfg(not(Py_3_12))]
156+
macro_rules! use_immortal {
157+
($op:expr) => {
158+
unsafe {
159+
ffi!(Py_INCREF($op));
160+
$op
161+
}
162+
};
163+
}

0 commit comments

Comments
 (0)