Skip to content

SyntaxError when string inside fstring shares quotes.  #5441

Open
@key262yek

Description

@key262yek

Feature

When using f-strings, a SyntaxError occurs if a string inside the curly braces is enclosed with the same type of quotes as the outermost quotes.

In RustPython

>>>>> f'hello {'__friend__'}'
  File "<stdin>", line 1
    f'hello {'__friend__'}'
              ^
SyntaxError: invalid syntax. Got unexpected token '__friend__'
>>>>> f'hello {"__friend__"}'
'hello __friend__'
>>>>> f"hello {'__friend__'}"
'hello __friend__'

Python Documentation or reference to CPython source code

In CPython v3.12.3

Python 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:38:13) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f'hello {'__friend__'}'
'hello __friend__'

During update traceback.py from cpython v3.12.3
Following code are in traceback.py

# Lib/traceback.py

# line 170 - 174
def _safe_string(value, what, func=str):
    try:
        return func(value)
    except:
        return f'<{what} {func.__name__}() failed>'

# line 740 - 745
        self._str = _safe_string(exc_value, 'exception')
        try:
            self.__notes__ = getattr(exc_value, '__notes__', None)
        except Exception as e:
            self.__notes__ = [
                f'Ignored error getting __notes__: {_safe_string(e, '__notes__', repr)}']

which make SyntaxError in RustPython

(base) ➜  RustPython git:(updaate_traceback_from_v3.12.3) ✗ cargo run Lib/test/test_traceback.py
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
     Running `target/debug/rustpython Lib/test/test_traceback.py`
Traceback (most recent call last):
  File "Lib/test/test_traceback.py", line 10, in <module>
    import unittest
  File "/home/mschoi/RustPython/pylib/Lib/unittest/__init__.py", line 60, in <module>
    from .result import TestResult
  File "/home/mschoi/RustPython/pylib/Lib/unittest/result.py", line 5, in <module>
    import traceback
  File "/home/mschoi/RustPython/pylib/Lib/traceback.py", line 745
    f'Ignored error getting __notes__: {_safe_string(e, '__notes__', repr)}']
                                                         ^
SyntaxError: invalid syntax. Got unexpected token '__notes__'

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-compatA discrepancy between RustPython and CPython

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions