Skip to content

Commit 2832ff0

Browse files
committed
Lazy import numpy
1 parent 891496f commit 2832ff0

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

src/encode.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ pub fn pyobject_to_obtype_unlikely(obj: *mut pyo3::ffi::PyObject, opts: Opt) ->
128128
&& ffi!(PyDict_Contains((*ob_type).tp_dict, DATACLASS_FIELDS_STR)) == 1
129129
{
130130
ObType::Dataclass
131-
} else if opts & SERIALIZE_NUMPY != 0 && Some(NonNull::new_unchecked(ob_type)) == ARRAY_TYPE
131+
} else if opts & SERIALIZE_NUMPY != 0
132+
&& ARRAY_TYPE.is_some()
133+
&& ob_type == ARRAY_TYPE.unwrap().as_ptr()
132134
{
133135
ObType::Array
134136
} else {

src/typeref.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
22

3+
use once_cell::unsync::Lazy;
34
use pyo3::ffi::*;
45
use std::os::raw::c_char;
56
use std::ptr::NonNull;
@@ -24,7 +25,8 @@ pub static mut TIME_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
2425
pub static mut TUPLE_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
2526
pub static mut UUID_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
2627
pub static mut ENUM_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
27-
pub static mut ARRAY_TYPE: Option<NonNull<PyTypeObject>> = None;
28+
pub static mut ARRAY_TYPE: Lazy<Option<NonNull<PyTypeObject>>> =
29+
Lazy::new(|| unsafe { look_up_array_type() });
2830

2931
pub static mut BYTES_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
3032
pub static mut BYTEARRAY_TYPE: *mut PyTypeObject = 0 as *mut PyTypeObject;
@@ -68,7 +70,6 @@ pub fn init_typerefs() {
6870
TIME_TYPE = look_up_time_type();
6971
UUID_TYPE = look_up_uuid_type();
7072
ENUM_TYPE = look_up_enum_type();
71-
ARRAY_TYPE = look_up_array_type();
7273
INT_ATTR_STR = PyUnicode_InternFromString("int\0".as_ptr() as *const c_char);
7374
UTCOFFSET_METHOD_STR = PyUnicode_InternFromString("utcoffset\0".as_ptr() as *const c_char);
7475
NORMALIZE_METHOD_STR = PyUnicode_InternFromString("normalize\0".as_ptr() as *const c_char);

0 commit comments

Comments
 (0)