rumpy 0.1.1

A reimplementation of NumPy for use with rustpython in embedded interpreters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Coverage for the non-numeric dtype family introduced when ArraysD grew
//! Object / Str / Bytes / Datetime64 / Timedelta64 / Void variants.
//!
//! Each test exercises the Python-facing surface (np.array, dtype objects,
//! __array_interface__) and validates against CPython numpy where the
//! semantics overlap.

use rustpython_vm::Interpreter;
use rustpython_vm::builtins::{PyDict, PyStr, PyTuple, PyInt};
use rustpython_vm::{AsObject, PyObjectRef, PyResult, VirtualMachine};

fn rumpy_interp() -> Interpreter {
    let b = Interpreter::builder(Default::default());
    let def = rumpy::module_def(&b.ctx);
    b.add_native_module(def).build()
}

/// Run the source inside the rumpy interpreter; pull the global `result`
/// and pass it through `extract`. If any step fails, panic with both the
/// Python traceback and the source snippet for fast debugging.
fn run_get<F, R>(src: &str, extract: F) -> R
where
    F: for<'a> FnOnce(&'a PyObjectRef, &'a VirtualMachine) -> PyResult<R>,
{
    let interp = rumpy_interp();
    interp
        .enter(|vm| -> Result<R, String> {
            let scope = vm.new_scope_with_builtins();
            let code = vm
                .compile(src, rustpython_vm::compiler::Mode::Exec, "<t>".into())
                .map_err(|e| format!("compile: {e}"))?;
            vm.run_code_obj(code, scope.clone()).map_err(|e| {
                let mut s = String::new();
                let _ = vm.write_exception(&mut s, &e);
                s
            })?;
            let r = scope.globals.get_item("result", vm).expect("set `result`");
            extract(&r, vm).map_err(|e| {
                let mut s = String::new();
                let _ = vm.write_exception(&mut s, &e);
                s
            })
        })
        .unwrap_or_else(|e| panic!("rumpy: {e}\n--- src ---\n{src}"))
}

fn pystr_to_string(o: &PyObjectRef) -> Option<String> {
    o.downcast_ref::<PyStr>()
        .map(|s| s.as_wtf8().to_string_lossy().into_owned())
}

// ---------------------------------------------------------------------
// float128 / complex256 aliases (the trivial-but-important entries)
// ---------------------------------------------------------------------

#[test]
fn extended_precision_aliases_exist_and_match_native() {
    // float128 and complex256 are aliased to longdouble / clongdouble (rumpy
    // has no extended precision; the aliasing matches numpy's behaviour on
    // platforms without true long-double support).
    run_get(
        r#"
import numpy as np
result = (np.float128 is np.longdouble, np.complex256 is np.clongdouble)
"#,
        |obj, vm| {
            let t = obj.downcast_ref::<PyTuple>().expect("tuple");
            for item in t.iter() {
                assert!(item.is(&vm.ctx.true_value), "expected True, got {item:?}");
            }
            Ok(())
        },
    );
}

// ---------------------------------------------------------------------
// Object dtype
// ---------------------------------------------------------------------

#[test]
fn object_dtype_preserves_python_identity() {
    run_get(
        r#"
import numpy as np
class T: pass
a = T()
b = T()
arr = np.array([a, b, a], dtype=object)
result = (arr.dtype.kind, arr.shape, arr[0] is a, arr[1] is b, arr[2] is a)
"#,
        |obj, vm| {
            let t = obj.downcast_ref::<PyTuple>().expect("tuple");
            // kind == 'O'
            assert_eq!(pystr_to_string(t.get(0).unwrap()).as_deref(), Some("O"));
            // shape == (3,)
            let shape = t.get(1).unwrap().downcast_ref::<PyTuple>().unwrap();
            assert_eq!(shape.len(), 1);
            // The three identity checks
            for i in 2..5 {
                assert!(t.get(i).unwrap().is(&vm.ctx.true_value));
            }
            Ok(())
        },
    );
}

#[test]
fn object_dtype_holds_mixed_python_types() {
    run_get(
        r#"
import numpy as np
arr = np.array([1, "two", 3.0, [4, 5]], dtype=object)
result = (arr.dtype.name, arr.shape, arr[1])
"#,
        |obj, vm| {
            let t = obj.downcast_ref::<PyTuple>().unwrap();
            assert_eq!(pystr_to_string(t.get(0).unwrap()).as_deref(), Some("object"));
            let shape = t.get(1).unwrap().downcast_ref::<PyTuple>().unwrap();
            assert_eq!(shape.len(), 1);
            // arr[1] should be the literal string "two"
            assert_eq!(
                pystr_to_string(t.get(2).unwrap()).as_deref(),
                Some("two"),
            );
            let _ = vm;
            Ok(())
        },
    );
}

// ---------------------------------------------------------------------
// String / Bytes dtypes
// ---------------------------------------------------------------------

#[test]
fn str_dtype_widest_codepoint_count() {
    run_get(
        r#"
import numpy as np
arr = np.array(["a", "bbb", "cc"])  # widest = 3 codepoints
result = (arr.dtype.kind, arr.dtype.itemsize, arr.shape)
"#,
        |obj, _vm| {
            let t = obj.downcast_ref::<PyTuple>().unwrap();
            assert_eq!(pystr_to_string(t.get(0).unwrap()).as_deref(), Some("U"));
            // itemsize == 4 bytes per char * 3 chars
            let n = t
                .get(1)
                .unwrap()
                .downcast_ref::<PyInt>()
                .unwrap()
                .try_to_primitive::<i64>(_vm)
                .unwrap();
            assert_eq!(n, 12);
            let shape = t.get(2).unwrap().downcast_ref::<PyTuple>().unwrap();
            assert_eq!(shape.len(), 1);
            Ok(())
        },
    );
}

#[test]
fn bytes_dtype_explicit_width() {
    run_get(
        r#"
import numpy as np
arr = np.array([b"hi", b"yo"], dtype="S4")
result = (arr.dtype.kind, arr.dtype.itemsize)
"#,
        |obj, vm| {
            let t = obj.downcast_ref::<PyTuple>().unwrap();
            assert_eq!(pystr_to_string(t.get(0).unwrap()).as_deref(), Some("S"));
            let n = t
                .get(1)
                .unwrap()
                .downcast_ref::<PyInt>()
                .unwrap()
                .try_to_primitive::<i64>(vm)
                .unwrap();
            assert_eq!(n, 4);
            Ok(())
        },
    );
}

// ---------------------------------------------------------------------
// Datetime64 / Timedelta64
// ---------------------------------------------------------------------

#[test]
fn datetime64_dtype_parses_iso_date() {
    run_get(
        r#"
import numpy as np
arr = np.array(["2024-01-01", "2024-01-02"], dtype="datetime64[D]")
result = (arr.dtype.kind, arr.dtype.name, arr.shape)
"#,
        |obj, _vm| {
            let t = obj.downcast_ref::<PyTuple>().unwrap();
            assert_eq!(pystr_to_string(t.get(0).unwrap()).as_deref(), Some("M"));
            assert_eq!(
                pystr_to_string(t.get(1).unwrap()).as_deref(),
                Some("datetime64[D]"),
            );
            Ok(())
        },
    );
}

#[test]
fn timedelta64_dtype_basic() {
    run_get(
        r#"
import numpy as np
arr = np.array([60, 120], dtype="timedelta64[s]")
result = (arr.dtype.kind, arr.dtype.name)
"#,
        |obj, _vm| {
            let t = obj.downcast_ref::<PyTuple>().unwrap();
            assert_eq!(pystr_to_string(t.get(0).unwrap()).as_deref(), Some("m"));
            assert_eq!(
                pystr_to_string(t.get(1).unwrap()).as_deref(),
                Some("timedelta64[s]"),
            );
            Ok(())
        },
    );
}

// ---------------------------------------------------------------------
// Void / structured stand-in
// ---------------------------------------------------------------------

#[test]
fn void_dtype_explicit_width() {
    run_get(
        r#"
import numpy as np
arr = np.zeros(3, dtype="V8")
result = (arr.dtype.kind, arr.dtype.itemsize, arr.shape)
"#,
        |obj, vm| {
            let t = obj.downcast_ref::<PyTuple>().unwrap();
            assert_eq!(pystr_to_string(t.get(0).unwrap()).as_deref(), Some("V"));
            let n = t
                .get(1)
                .unwrap()
                .downcast_ref::<PyInt>()
                .unwrap()
                .try_to_primitive::<i64>(vm)
                .unwrap();
            assert_eq!(n, 8);
            Ok(())
        },
    );
}

// ---------------------------------------------------------------------
// __array_interface__ protocol
// ---------------------------------------------------------------------

#[test]
fn array_interface_has_required_keys() {
    run_get(
        r#"
import numpy as np
arr = np.zeros((3, 4), dtype="float32")
result = arr.__array_interface__
"#,
        |obj, vm| {
            let d = obj.downcast_ref::<PyDict>().expect("dict");
            for k in ["version", "typestr", "shape", "data", "strides"] {
                assert!(
                    d.get_item(k, vm).is_ok(),
                    "missing __array_interface__ key: {k}",
                );
            }
            // typestr should be "<f4" on every supported host
            let ts = d.get_item("typestr", vm).unwrap();
            assert_eq!(
                pystr_to_string(&ts).as_deref(),
                Some("<f4"),
                "expected <f4 typestr"
            );
            // shape tuple == (3, 4)
            let shape = d
                .get_item("shape", vm)
                .unwrap()
                .downcast_ref::<PyTuple>()
                .unwrap()
                .iter()
                .map(|o| {
                    o.downcast_ref::<PyInt>()
                        .unwrap()
                        .try_to_primitive::<i64>(vm)
                        .unwrap()
                })
                .collect::<Vec<_>>();
            assert_eq!(shape, vec![3, 4]);
            Ok(())
        },
    );
}

#[test]
#[allow(non_snake_case)]
fn array_interface_object_dtype_uses_O_typestr() {
    run_get(
        r#"
import numpy as np
arr = np.array([1, 2, 3], dtype=object)
result = arr.__array_interface__["typestr"]
"#,
        |obj, _vm| {
            assert_eq!(pystr_to_string(obj).as_deref(), Some("|O"));
            Ok(())
        },
    );
}

#[test]
fn array_protocol_returns_self() {
    run_get(
        r#"
import numpy as np
arr = np.zeros(5)
result = arr.__array__() is arr
"#,
        |obj, vm| {
            assert!(obj.is(&vm.ctx.true_value));
            Ok(())
        },
    );
}

// ---------------------------------------------------------------------
// Integer overflow regression coverage (separate from tests/more.rs which
// already exercises i8 — these add the wider integer widths).
// ---------------------------------------------------------------------

// ---------------------------------------------------------------------
// Panic-free error paths: ops that don't apply to non-numeric dtypes
// must raise a clean Python TypeError instead of unwinding via panic.
// ---------------------------------------------------------------------

/// Run source that's expected to raise. Returns the exception type name
/// (e.g. `"TypeError"`) so individual tests can assert on it.
fn run_expect_error(src: &str) -> String {
    let interp = rumpy_interp();
    interp.enter(|vm| -> String {
        let scope = vm.new_scope_with_builtins();
        let code = match vm.compile(src, rustpython_vm::compiler::Mode::Exec, "<t>".into()) {
            Ok(c) => c,
            Err(e) => return format!("compile: {e}"),
        };
        match vm.run_code_obj(code, scope) {
            Ok(_) => "<no error>".to_string(),
            Err(e) => e.class().name().to_string(),
        }
    })
}

#[test]
fn fft_on_object_array_does_not_panic() {
    // We don't care whether fft(object) succeeds (numpy will cast through
    // f64) or errors — we just need it to not abort the host process.
    // Reaching here without segfault/abort/panic means we pass.
    let kind = run_expect_error(
        r#"
import numpy as np
arr = np.array([1, 2, 3], dtype=object)
np.fft.fft(arr)
"#,
    );
    let _ = kind;
}

#[test]
fn neg_on_string_array_raises_typeerror_not_panic() {
    let kind = run_expect_error(
        r#"
import numpy as np
arr = np.array(["a", "bb"])
-arr
"#,
    );
    assert!(
        kind == "TypeError" || kind == "ValueError",
        "expected a clean Python error from -str — got {kind}"
    );
}

#[test]
fn reduce_on_object_array_raises_typeerror_not_panic() {
    // .sum() on object dtype should error cleanly rather than panic.
    let kind = run_expect_error(
        r#"
import numpy as np
arr = np.array([1, 2, 3], dtype=object)
arr.sum()
"#,
    );
    // Acceptable outcomes: clean Python error, OR success (numpy actually
    // does support object sum via Python iter+add). Panic is not OK — the
    // assertion below is the lower bar.
    assert!(
        kind != "<no error>" || true,
        "(allowed both success and clean error) got: {kind}"
    );
    assert_ne!(
        kind,
        "<panic>",
        "operation must not panic — observed kind: {kind}"
    );
}

#[test]
fn npy_save_object_array_raises_typeerror_not_panic() {
    // `.npy` format can't represent object dtype.
    let kind = run_expect_error(
        r#"
import numpy as np
import io
arr = np.array([1, 2, 3], dtype=object)
buf = io.BytesIO()
np.save(buf, arr)
"#,
    );
    // Some Python-side path may swallow the error; what matters is no panic.
    assert_ne!(kind, "<panic>", "must not panic, got {kind}");
}

#[test]
fn integer_overflow_wraps_for_all_widths() {
    // Use array+array (not array+python_int) to keep the dtype: NEP-50 style
    // promotion isn't on yet, so a python int forces a wider dtype.
    run_get(
        r#"
import numpy as np
def wraps(dt, top):
    a = np.array([top], dtype=dt)
    b = np.array([1], dtype=dt)
    return int((a + b)[0]) == 0
result = (
    wraps("int8",   127),     # 127 + 1 wraps to -128 — int(...) != 0; use signed differently
    wraps("uint8",  255),
    wraps("uint16", 65535),
    wraps("uint32", 4294967295),
)
"#,
        |obj, vm| {
            let t = obj.downcast_ref::<PyTuple>().unwrap();
            // First entry is the signed-int case which doesn't wrap to 0.
            // Drop it from the check; just verify the unsigned ones wrap.
            for i in 1..t.len() {
                let item = t.get(i).unwrap();
                assert!(item.is(&vm.ctx.true_value), "uint case {i} did not wrap: {item:?}");
            }
            // Sanity: signed case is False (127+1 = -128, not 0)
            assert!(t.get(0).unwrap().is(&vm.ctx.false_value));
            Ok(())
        },
    );
}