Thanks to visit codestin.com
Credit goes to github.com

Skip to content

UnicodeDecodeError or SystemError when using f-string with lambda and non-ASCII characters #130618

Closed
@gaesa

Description

@gaesa

Bug report

Bug description:

When using an f-string in combination with lambda functions that return non-ASCII characters, either a UnicodeDecodeError or a SystemError is raised, depending on the specific modification made to the code. These errors do not occur consistently, but rather vary based on how the lambdas and string content are altered.

Steps to Reproduce

def test1(foo, bar):
    return ""

def test2():
    return f"{test1(
        foo=lambda: '、、、、、、、、、、、、、、、、、',
        bar=lambda: 'abcdefghijklmnopqrstuvwxyz 123456789 123456789',
    )}"

Run the code with python <file.py>, which triggers the following error:

UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 28-29: unexpected end of data

Expected Behavior

The code should execute without raising any errors.

Actual Behavior

The following behaviors are observed when making the specified modifications. Each of these cases is independent:

  1. Removing one character from the first string (foo): No error.
  2. Removing all characters from the first string: SystemError: Negative size passed to PyUnicode_New.
  3. Removing foo= (i.e., not passing foo by keyword): No error.
  4. Removing lambda: (i.e., making either argument or both into a str type instead of a Callable[[], str]): No error.
  5. Removing any character from the second string (bar): No error.

Other Relevant Information

The bug does not reproduce in Python 3.13.1, but it does reproduce in Python 3.13.2 and 3.12.9.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions