Skip to content

Incorrect argument type for str() crashes the interpreter #122888

Closed
@pschanely

Description

@pschanely

Crash report

What happened?

Using a byte string for the encoding parameter of str() results in a crash (instead of the expected TypeError):

str(b"hello", b"ascii")

Here is the output I get on macos, 3.13.0b4:

% python Python 3.13.0b4 (main, Jul 24 2024, 12:21:52) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> str(b"hello", b"ascii") Fatal Python error: _Py_CheckFunctionResult: a function returned a result with an exception set Python runtime state: initialized TypeError: str() argument 'encoding' must be str, not bytes The above exception was the direct cause of the following exception: SystemError: <class 'str'> returned a result with an exception set Current thread 0x00000002064b0c00 (most recent call first): File "<python-input-0>", line 1 in <module> File "/Users/phillipschanely/.pyenv/versions/3.13.0b4-debug/lib/python3.13/code.py", line 91 in runcode File "/Users/phillipschanely/.pyenv/versions/3.13.0b4-debug/lib/python3.13/_pyrepl/console.py", line 200 in runsource File "/Users/phillipschanely/.pyenv/versions/3.13.0b4-debug/lib/python3.13/code.py", line 303 in push File "/Users/phillipschanely/.pyenv/versions/3.13.0b4-debug/lib/python3.13/_pyrepl/simple_interact.py", line 156 in run_multiline_interactive_console File "/Users/phillipschanely/.pyenv/versions/3.13.0b4-debug/lib/python3.13/_pyrepl/main.py", line 57 in interactive_console File "/Users/phillipschanely/.pyenv/versions/3.13.0b4-debug/lib/python3.13/_pyrepl/__main__.py", line 6 in <module> File "/Users/phillipschanely/.pyenv/versions/3.13.0b4-debug/lib/python3.13/runpy.py", line 88 in _run_code File "/Users/phillipschanely/.pyenv/versions/3.13.0b4-debug/lib/python3.13/runpy.py", line 198 in _run_module_as_main zsh: abort python

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Output from running 'python -VV' on the command line:

Python 3.13.0b4 (main, Jul 24 2024, 12:21:52) [Clang 15.0.0 (clang-1500.3.9.4)]

Linked PRs

Metadata

Metadata

Assignees

Labels

release-blockertype-crashA hard crash of the interpreter, possibly with a core dump

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions