From c4ef993c78b43f63e84e16f4f5368903295aa542 Mon Sep 17 00:00:00 2001 From: Jiri Bajer Date: Tue, 2 Feb 2021 15:29:43 +0100 Subject: [PATCH 1/2] NumPy is imported only when needed --- javaobj/v1/unmarshaller.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/javaobj/v1/unmarshaller.py b/javaobj/v1/unmarshaller.py index 4895a26..d427a14 100644 --- a/javaobj/v1/unmarshaller.py +++ b/javaobj/v1/unmarshaller.py @@ -37,6 +37,7 @@ # Standard library from typing import Any, Union +import contextlib import os import struct @@ -65,11 +66,7 @@ hexdump, ) -# Numpy array support -try: - import numpy -except ImportError: - numpy = None +numpy = None # Imported only when really used # ------------------------------------------------------------------------------ @@ -113,6 +110,13 @@ def __init__(self, stream, use_numpy_arrays=False): """ self.use_numpy_arrays = use_numpy_arrays + # Numpy array support + if self.use_numpy_arrays: + with contextlib.suppress(ImportError): + global numpy + import numpy as np + numpy = np + # Check stream if stream is None: raise IOError("No input stream given") From e83318753a5d013f0be080172c33ec4cab504672 Mon Sep 17 00:00:00 2001 From: Jiri Bajer Date: Tue, 2 Feb 2021 19:13:19 +0100 Subject: [PATCH 2/2] Made ignoring of import errors compatible with Python 2.x --- javaobj/v1/unmarshaller.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/javaobj/v1/unmarshaller.py b/javaobj/v1/unmarshaller.py index d427a14..3d0efb8 100644 --- a/javaobj/v1/unmarshaller.py +++ b/javaobj/v1/unmarshaller.py @@ -37,7 +37,6 @@ # Standard library from typing import Any, Union -import contextlib import os import struct @@ -112,10 +111,12 @@ def __init__(self, stream, use_numpy_arrays=False): # Numpy array support if self.use_numpy_arrays: - with contextlib.suppress(ImportError): + try: global numpy import numpy as np numpy = np + except ImportError: + pass # Check stream if stream is None: