Skip to content

Commit 8f5d781

Browse files
committed
chore: remove imports--numpy, cppyy
tests: add template and dtpye less tests chore: remove cppyy import chore: remove numpy--import recover: deleted valgrind--file
1 parent eaf4ff3 commit 8f5d781

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

python/cppyy/_cpython_cppyy.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,18 @@ def __call__(self, *args):
115115
if args:
116116
args0 = args[0]
117117

118-
if type(args0).__module__ == 'numpy' and type(args0).__name__ == 'ndarray':
119-
import numpy as np
120-
# Get the data type of the array
121-
t = args0.dtype.type
122-
if np.issubdtype(t, np.integer):
123-
t = 'int'
124-
elif np.issubdtype(t, np.floating):
125-
t = 'doublex'
126-
elif np.issubdtype(t, np.complexfloating):
127-
t = 'std::complex<double>'
118+
if (
119+
type(args0).__module__ == "numpy"
120+
and type(args0).__name__ == "ndarray"
121+
and hasattr(args0, "dtype")
122+
):
123+
t = args0.dtype.type.__name__
124+
if t.startswith("int"):
125+
t = "int"
126+
elif t.startswith("float"):
127+
t = "double"
128+
elif t.startswith("complex"):
129+
t = "std::complex<double>"
128130

129131
# Handle arrays of arbitrary dimension recursively
130132
return _np_vector(args0, t)
@@ -225,28 +227,26 @@ def _end_capture_stderr():
225227
return ""
226228

227229
def _np_vector(arr, dtype):
228-
import cppyy
229-
230230
vector_type_cache = {}
231231

232232
def _build_nested_vector_type(ndim, dtype):
233233
key = (ndim, dtype)
234234
if key in vector_type_cache:
235235
return vector_type_cache[key]
236236

237-
vector_t = cppyy.gbl.std.vector[dtype]
237+
vector_t = gbl.std.vector[dtype]
238238
for _ in range(ndim - 1):
239-
vector_t = cppyy.gbl.std.vector[vector_t]
239+
vector_t = gbl.std.vector[vector_t]
240240

241241
vector_type_cache[key] = vector_t
242242
return vector_t
243243

244244
ndim = arr.ndim
245245

246246
if ndim == 1:
247-
vector_t = cppyy.gbl.std.vector[dtype]
247+
vector_t = gbl.std.vector[dtype]
248248
vector = vector_t()
249-
vector.reserve(arr.size) # Pre-allocate memory for better performance
249+
vector.reserve(arr.size)
250250

251251
try:
252252
vector.insert(vector.end(), arr.flat)
@@ -255,12 +255,10 @@ def _build_nested_vector_type(ndim, dtype):
255255
vector.push_back(elem.item())
256256
return vector
257257

258-
# Build nested vector types dynamically
259258
nested_vector_type = _build_nested_vector_type(ndim, dtype)
260259
nested_vector = nested_vector_type()
261260
nested_vector.reserve(arr.shape[0]) # Pre-allocate outer vector
262261

263-
# Recursively process sub-arrays
264262
for subarr in arr:
265263
inner_vector = _np_vector(subarr, dtype)
266264
nested_vector.push_back(inner_vector)

test/test_stltypes.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,52 @@ def test23_copy_conversion(self):
789789
for f, d in zip(x, v):
790790
assert f == d
791791

792+
def test24_numpy_template_less(self):
793+
import cppyy
794+
795+
try:
796+
import numpy as np
797+
except ImportError:
798+
skip("numpy is not installed")
799+
800+
rng = np.random.default_rng(seed=42)
801+
x = rng.random((10, 3, 3, 3)) # It is default to dtype=np.float32
802+
v = cppyy.gbl.std.vector(x)
803+
804+
assert len(v) == 10
805+
assert type(v[0][0][0][0]) is float
806+
807+
808+
def test25_numpy_dtype_and_template_less(self):
809+
import cppyy
810+
811+
try:
812+
import numpy as np
813+
except ImportError:
814+
skip("numpy is not installed")
815+
816+
rng = np.random.default_rng(seed=42)
817+
x = rng.integers(low=0, high=100, size=(10, 3, 3, 3))
818+
v = cppyy.gbl.std.vector(x)
819+
820+
assert len(v) == 10
821+
assert type(v[0][0][0][0]) is int
822+
823+
824+
def test26_numpy_passing_dtype(self):
825+
import cppyy
826+
827+
try:
828+
import numpy as np
829+
except ImportError:
830+
skip("numpy is not installed")
831+
832+
rng = np.random.default_rng(seed=42)
833+
x = rng.integers(low=0, high=100, size=(10, 3, 3, 3), dtype=np.int32)
834+
v = cppyy.gbl.std.vector(x)
835+
836+
assert len(v) == 10
837+
assert type(v[0][0][0][0]) is int
792838

793839
class TestSTLSTRING:
794840
def setup_class(cls):

0 commit comments

Comments
 (0)