pyodide / pyodide/pyodide

Cannot .toJs() a numpy.ndarray containing BigInt values - Error: Unrecognized format character 'O'

Open
#4,051 5 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
14.8k
Forks
1k
Avg merge
20h 29m
Merged PRs (30d)
19

Description

## 🐛 Bug

Running `.toJs()` on a PyProxy of a numpy.ndarray containing BigInt throws an exception: "Error: Unrecognized format character 'O'"

### To Reproduce

```html





NumPy BigInt toJs() error

Open your browser console to see Pyodide output

async function main(){
let pyodide = await loadPyodide();
console.log(pyodide.runPython(`
import sys
sys.version
`)); // 3.11.2 (main, Jul 7 2023, 05:19:00) [Clang 17.0.0 (https://github.com/llvm/llvm-project df82394e7a2d06506718cafa347b
await pyodide.loadPackage("numpy");
let ndarray = pyodide.runPython(`import numpy as np

# Creating a numpy array with Python's native integers
bigints = [10**100, 10**200, 10**300]
arr = np.array(bigints, dtype=object)

# Output the array
arr`);
console.log(ndarray.type); // numpy.ndarray
ndarray.toJs(); // PythonError: pyodide.ffi.JsException: Error: Unrecognized format character 'O'.
}
main();

```

```
Uncaught (in promise) PythonError: pyodide.ffi.JsException: Error: Unrecognized format character 'O'.

The above exception was the direct cause of the following exception:

pyodide.ffi.ConversionError: Conversion from python to javascript failed

at new_error (pyodide.asm.js:9:14992)
at pyodide.asm.wasm:0x152d67
at pyodide.asm.wasm:0x152e6c
at Module._pythonexc2js (pyodide.asm.js:9:656029)
at Proxy.toJs (pyodide.asm.js:9:77512)
at main (bigint_pyodide_error_repro.html:24:7)
new_error @ pyodide.asm.js:9
$wrap_exception @ pyodide.asm.wasm:0x152d67
$pythonexc2js @ pyodide.asm.wasm:0x152e6c
Module._pythonexc2js @ pyodide.asm.js:9
toJs @ pyodide.asm.js:9
main @ bigint_pyodide_error_repro.html:24
await in main (async)
(anonymous) @ bigint_pyodide_error_repro.html:26
```

### Expected behavior

According to type conversions, BigInt should convert to Number or int, which is possibly lossy, but should not throw an error. https://pyodide.org/en/stable/usage/type-conversions.html

### Environment

- Pyodide Version: v0.23.4
- Browser version: Chrome 115.0.5790.111 (Official Build) (64-bit)
- Any other relevant information:

### Additional context

N/A

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.