Skip to content

Commit

Permalink
fix: import decimal module
Browse files Browse the repository at this point in the history
- import it in base interpreter
- stops the issue of crashing when imported
  • Loading branch information
brownben committed Nov 8, 2024
1 parent 3e7514b commit 99c315b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/import_decimal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Import decimal module.
Causes problems if the module is not first imported in the main interpreter.
"""

from decimal import Decimal


def test_import_first():
import decimal

assert Decimal(0) == decimal.Decimal(0)


def test_import_again():
import decimal

assert Decimal(0) == decimal.Decimal(0)
5 changes: 5 additions & 0 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ impl Interpreter {
ffi::Py_InitializeFromConfig(ptr::from_mut(config.assume_init_mut()));
}

// The decimal module crashes the interpreter if it is initialised multiple times
// If not initialised in the base interpreter, if a subinterpreter imports it it will crash
// TODO: when decimal works properly remove this hack
execute_string(c"import decimal").unwrap();

let main_thread_state = unsafe { ffi::PyThreadState_Swap(ptr::null_mut()) };

Self {
Expand Down
8 changes: 8 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,11 @@ fn captures_stdout() {
"hello world\ninto stderr"
);
}

#[test]
fn import_decimal_module() {
let results = run_tests!("./examples/import_decimal.py");

assert_eq!(results.len(), 2);
assert!(results.iter().all(|x| x.outcome == Outcome::Pass));
}

0 comments on commit 99c315b

Please sign in to comment.