Skip to content

Commit

Permalink
use_immortal!()
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Jul 7, 2023
1 parent 2e9ff51 commit 8686b22
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/deserialize/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ pub fn deserialize(
} else if buffer == b"{}" {
return Ok(nonnull!(ffi!(PyDict_New())));
} else if buffer == b"\"\"" {
ffi!(Py_INCREF(EMPTY_UNICODE));
unsafe { return Ok(nonnull!(EMPTY_UNICODE)) }
unsafe { return Ok(nonnull!(use_immortal!(EMPTY_UNICODE))) }
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/deserialize/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ pub fn parse_bool(val: bool) -> NonNull<pyo3_ffi::PyObject> {

#[inline(always)]
pub fn parse_true() -> NonNull<pyo3_ffi::PyObject> {
ffi!(Py_INCREF(TRUE));
nonnull!(TRUE)
nonnull!(use_immortal!(TRUE))
}

#[inline(always)]
pub fn parse_false() -> NonNull<pyo3_ffi::PyObject> {
ffi!(Py_INCREF(FALSE));
nonnull!(FALSE)
nonnull!(use_immortal!(FALSE))
}
#[inline(always)]
pub fn parse_i64(val: i64) -> NonNull<pyo3_ffi::PyObject> {
Expand All @@ -68,6 +66,5 @@ pub fn parse_f64(val: f64) -> NonNull<pyo3_ffi::PyObject> {

#[inline(always)]
pub fn parse_none() -> NonNull<pyo3_ffi::PyObject> {
ffi!(Py_INCREF(NONE));
nonnull!(NONE)
nonnull!(use_immortal!(NONE))
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ fn raise_loads_exception(err: deserialize::DeserializeError) -> *mut PyObject {
PyUnicode_FromStringAndSize(as_str.as_ptr() as *const c_char, as_str.len() as isize)
},
None => {
ffi!(Py_INCREF(crate::typeref::EMPTY_UNICODE));
unsafe { crate::typeref::EMPTY_UNICODE }
use_immortal!(crate::typeref::EMPTY_UNICODE)
}
};
unsafe {
Expand Down
3 changes: 1 addition & 2 deletions src/str/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ fn find_str_kind(buf: &str, num_chars: usize) -> PyUnicodeKind {

pub fn unicode_from_str(buf: &str) -> *mut pyo3_ffi::PyObject {
if buf.is_empty() {
ffi!(Py_INCREF(EMPTY_UNICODE));
unsafe { EMPTY_UNICODE }
use_immortal!(EMPTY_UNICODE)
} else {
let num_chars = bytecount::num_chars(buf.as_bytes());
match find_str_kind(buf, num_chars) {
Expand Down
17 changes: 17 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,20 @@ macro_rules! pydict_contains {
unsafe { pyo3_ffi::PyDict_Contains((*$obj1).tp_dict, $obj2) == 1 }
};
}

#[cfg(Py_3_12)]
macro_rules! use_immortal {
($op:expr) => {
unsafe { $op }
};
}

#[cfg(not(Py_3_12))]
macro_rules! use_immortal {
($op:expr) => {
unsafe {
ffi!(Py_INCREF($op));
$op
}
};
}

0 comments on commit 8686b22

Please sign in to comment.