* fix: flush stdout on interpreter shutdown matching CPython behavior
When stdout flush fails during shutdown, report the error via
run_unraisable and exit with code 120 (matching CPython's
Py_FinalizeEx). Skip flushing already-closed or None streams.
Stderr flush errors remain silently ignored per CPython behavior.
Fixes#5521
Signed-off-by: majiayu000 <1835304752@qq.com>
* refactor: replace magic number 120 with named constant EXITCODE_FLUSH_FAILURE
Address review feedback on PR #7503: improve readability by extracting
the CPython-compat exit code into a named constant.
Signed-off-by: majiayu000 <1835304752@qq.com>
---------
Signed-off-by: majiayu000 <1835304752@qq.com>
After you suggestion in https://github.com/python/cpython/issues/116504#issuecomment-1999239012 I went to take a look at `test_cmd_line` in RustPython (it was so long ago I contributed to this amazing project, so may thing had changed!), and I've noticed this.
This is a problem, here' the simplest demo:
```python
import unittest
class TestMe(unittest.TestCase):
@unittest.expectedFailure
def test_me(self):
def run():
raise ValueError
with self.subTest(run=run):
run()
if __name__ == '__main__':
unittest.main()
```
This works as expected:
```
» ./python.exe ex.py
x
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK (expected failures=1)
```
This does not:
```python
import unittest
class TestMe(unittest.TestCase):
def test_me(self):
@unittest.expectedFailure
def run():
raise ValueError
with self.subTest(run=run):
run()
if __name__ == '__main__':
unittest.main()
```
Produces:
```
» ./python.exe ex.py
E
======================================================================
ERROR: test_me (__main__.TestMe.test_me) (run=<function TestMe.test_me.<locals>.run at 0x1057a2150>)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sobolev/Desktop/cpython2/ex.py", line 10, in test_me
run()
~~~^^
File "/Users/sobolev/Desktop/cpython2/ex.py", line 7, in run
raise ValueError
ValueError
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
```
So, I propose to remove these decorators, let's only keep `TODO` comments to indicate separate failures.
The failing test was unsetting `PYTHONPATH`, but neglecting to unset
`RUSTPYTHONPATH`, which obviously was not significant for the original
CPython test. Including `RUSTPYTHONPATH` in the test fixes it.
* added int_max_str_digits to settings struct
* changed vm init to use the value from settings
* added error handling and set the settings default to -1 which sets the value to 4300