Skip to content

Commit 3cd1a22

Browse files
committed
Deserializing null avoids Py_None()
1 parent 377bc10 commit 3cd1a22

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/decode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use smallvec::SmallVec;
88
use std::borrow::Cow;
99
use std::fmt;
1010
use std::marker::PhantomData;
11+
use crate::typeref;
1112

1213
import_exception!(json, JSONDecodeError);
1314

@@ -57,7 +58,7 @@ impl<'de, 'a> Visitor<'de> for JsonValue<'a> {
5758
}
5859

5960
fn visit_unit<E>(self) -> Result<Self::Value, E> {
60-
Ok(unsafe { pyo3::ffi::Py_None() })
61+
Ok(unsafe { typeref::NONE })
6162
}
6263

6364
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>

src/typeref.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use pyo3::prelude::*;
44
use pyo3::types::*;
55
use std::sync::Once;
66

7+
pub static mut NONE: *mut pyo3::ffi::PyObject = 0 as *mut pyo3::ffi::PyObject;
78
pub static mut STR_PTR: *mut pyo3::ffi::PyTypeObject = 0 as *mut pyo3::ffi::PyTypeObject;
89
pub static mut BYTES_PTR: *mut pyo3::ffi::PyTypeObject = 0 as *mut pyo3::ffi::PyTypeObject;
910
pub static mut DICT_PTR: *mut pyo3::ffi::PyTypeObject = 0 as *mut pyo3::ffi::PyTypeObject;
@@ -18,6 +19,7 @@ static INIT: Once = Once::new();
1819

1920
pub fn init_typerefs(py: Python) {
2021
INIT.call_once(|| unsafe {
22+
NONE = pyo3::ffi::Py_None();
2123
STR_PTR = PyUnicode::new(py, "python").as_ref(py).get_type_ptr();
2224
BYTES_PTR = PyBytes::new(py, b"python").as_ref(py).get_type_ptr();
2325
DICT_PTR = PyDict::new(py).as_ref().get_type_ptr();

test/test_type.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,24 @@ def test_bool(self):
3131
self.assertEqual(orjson.dumps(obj), ref.encode('utf-8'))
3232
self.assertEqual(orjson.loads(ref), obj)
3333

34-
def test_bool_array(self):
34+
def test_bool_true_array(self):
3535
"""
36-
bool array
36+
bool true array
3737
"""
3838
obj = [True] * 256
3939
ref = ('[' + ('true,' * 255) + 'true]').encode('utf-8')
4040
self.assertEqual(orjson.dumps(obj), ref)
4141
self.assertEqual(orjson.loads(ref), obj)
4242

43+
def test_bool_false_array(self):
44+
"""
45+
bool false array
46+
"""
47+
obj = [False] * 256
48+
ref = ('[' + ('false,' * 255) + 'false]').encode('utf-8')
49+
self.assertEqual(orjson.dumps(obj), ref)
50+
self.assertEqual(orjson.loads(ref), obj)
51+
4352
def test_none(self):
4453
"""
4554
null

0 commit comments

Comments
 (0)