Skip to content

Commit 1eeb2ba

Browse files
Jiri Bajertcalmant
authored andcommitted
NumPy is imported only when needed
1 parent 1b3bb82 commit 1eeb2ba

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

javaobj/v1/unmarshaller.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
# Standard library
3939
from typing import Any, Union
40+
import contextlib
4041
import os
4142
import struct
4243

@@ -65,11 +66,7 @@
6566
hexdump,
6667
)
6768

68-
# Numpy array support
69-
try:
70-
import numpy
71-
except ImportError:
72-
numpy = None
69+
numpy = None # Imported only when really used
7370

7471
# ------------------------------------------------------------------------------
7572

@@ -113,6 +110,13 @@ def __init__(self, stream, use_numpy_arrays=False):
113110
"""
114111
self.use_numpy_arrays = use_numpy_arrays
115112

113+
# Numpy array support
114+
if self.use_numpy_arrays:
115+
with contextlib.suppress(ImportError):
116+
global numpy
117+
import numpy as np
118+
numpy = np
119+
116120
# Check stream
117121
if stream is None:
118122
raise IOError("No input stream given")

0 commit comments

Comments
 (0)