Skip to content

Align symtable filename handling and global error locations with CPython#7142

Draft
moreal wants to merge 1 commit intoRustPython:mainfrom
moreal:fix/fail-symtable-test_filename_correct
Draft

Align symtable filename handling and global error locations with CPython#7142
moreal wants to merge 1 commit intoRustPython:mainfrom
moreal:fix/fail-symtable-test_filename_correct

Conversation

@moreal
Copy link
Contributor

@moreal moreal commented Feb 14, 2026

Summary

This PR removes one @unittest.expectedFailure in test_symtable by aligning RustPython's _symtable.symtable behavior with CPython for filename handling and symbol-table-time error location reporting.

Fixed Test

  • test.test_symtable.SymtableTest.test_filename_correct

Compatibility gap before this PR

  • For symbol-table-time syntax errors such as def f(x): global x, RustPython reported SyntaxError.offset using the identifier location (x) instead of the directive statement location (global ...).
  • _symtable.symtable in RustPython accepted filename as str only, while CPython accepts filesystem-decoded path inputs.

How CPython handles it

  • In cpython/Python/symtable.c, Global_kind and Nonlocal_kind use statement location data via LOCATION(s) for both directive recording and syntax error location (SET_ERROR_LOCATION(st->st_filename, LOCATION(s));), so offsets point to the directive statement start.
  • In cpython/Modules/symtablemodule.c, _symtable.symtable declares filename: unicode_fs_decoded, which accepts str | bytes | os.PathLike.

How RustPython now handles it

  • In crates/codegen/src/symboltable.rs, Stmt::Global and Stmt::Nonlocal now pass statement.range() to symbol registration so symtable-build-time SyntaxError.offset matches CPython's statement-based location semantics.
  • In crates/vm/src/stdlib/symtable.rs, _symtable.symtable now takes filename: OsPath and uses to_string_lossy() before compilation.
  • OsPath uses TryFromObject and delegates to PathConverter::try_path, which accepts str and bytes (and path-like objects through __fspath__), so existing PyStrRef-style str behavior is preserved while adding CPython-compatible filename inputs.

Behavioral outcome

  • SyntaxError.filename, SyntaxError.lineno, and SyntaxError.offset now match CPython for both parse-time and symtable-build-time failures in test_filename_correct.
  • symtable.symtable("pass", b"spam", "exec") is now accepted, while invalid containers such as bytearray, memoryview, and list still raise TypeError.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 14, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@moreal moreal self-assigned this Feb 14, 2026
@github-actions
Copy link
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[ ] lib: cpython/Lib/collections
[x] lib: cpython/Lib/_collections_abc.py
[ ] test: cpython/Lib/test/test_collections.py (TODO: 3)
[x] test: cpython/Lib/test/test_deque.py (TODO: 3)
[x] test: cpython/Lib/test/test_defaultdict.py (TODO: 1)
[ ] test: cpython/Lib/test/test_ordered_dict.py (TODO: 4)

dependencies:

  • collections (native: _weakref, itertools, sys)
    • _collections_abc
    • _collections_abc, abc, keyword, operator, reprlib

dependent tests: (193 tests)

  • collections: test_annotationlib test_array test_asyncio test_bisect test_builtin test_c_locale_coercion test_call test_collections test_configparser test_contains test_csv test_ctypes test_defaultdict test_deque test_dict test_dictviews test_enum test_exception_group test_file test_fileinput test_fileio test_functools test_genericalias test_hash test_httpservers test_inspect test_io test_ipaddress test_iter test_iterlen test_json test_ordered_dict test_pathlib test_patma test_pickle test_plistlib test_pprint test_pydoc test_random test_set test_shelve test_sqlite3 test_statistics test_string test_struct test_traceback test_types test_typing test_unittest test_urllib test_userdict test_userlist test_userstring test_weakref test_weakset test_with
    • asyncio: test_asyncio test_contextlib_async test_logging test_os test_sys_settrace test_unittest
    • concurrent.futures._base: test_concurrent_futures
    • dbm.dumb: test_dbm_dumb
    • dbm.sqlite3: test_dbm_sqlite3
    • difflib: test_difflib
    • dis: test__opcode test_ast test_code test_compile test_compiler_assemble test_dis test_dtrace test_fstring test_opcache test_peepholer test_positional_only_arg
      • inspect: test_abc test_argparse test_asyncgen test_buffer test_coroutines test_decimal test_generators test_grammar test_ntpath test_operator test_posixpath test_signal test_type_annotations test_yield_from test_zipimport
      • trace: test_trace
    • http.client: test_docxmlrpc test_hashlib test_ssl test_ucn test_unicodedata test_urllib2 test_wsgiref test_xmlrpc
      • urllib.request: test_http_cookiejar test_site test_urllib2_localnet test_urllib2net test_urllibnet
    • importlib.metadata: test_importlib test_zoneinfo
    • inspect:
      • bdb: test_bdb
      • dataclasses: test__colorize test_regrtest
      • rlcompleter: test_rlcompleter
    • multiprocessing: test_concurrent_futures test_fcntl test_multiprocessing_main_handling
    • pkgutil: test_pkgutil
    • platform: test__locale test__osx_support test_android test_baseexception test_cmath test_ctypes test_math test_mimetypes test_platform test_posix test_socket test_sysconfig test_time test_winreg
    • pprint: test_htmlparser test_sys_setprofile
    • queue: test_concurrent_futures test_dummy_thread test_sched
    • selectors: test_selectors test_subprocess
      • socketserver: test_imaplib test_socketserver
    • shutil: test_bz2 test_compileall test_ctypes test_filecmp test_glob test_importlib test_largefile test_py_compile test_reprlib test_shutil test_string_literals test_support test_tempfile test_venv
      • ctypes.util: test_ctypes
      • ensurepip: test_ensurepip
      • tarfile: test_tarfile
      • tempfile: test_bytes test_cmd_line test_contextlib test_doctest test_faulthandler test_importlib test_linecache test_mailbox test_pkg test_runpy test_tabnanny test_termios test_threadedtempfile test_tomllib test_urllib_response test_zipapp test_zipfile test_zipfile64 test_zstd
      • webbrowser: test_webbrowser
      • zipfile: test_zipfile
    • tokenize: test_tokenize test_unparse
      • traceback: test_code_module test_dictcomps test_importlib test_listcomps test_pyexpat test_setcomps test_threading test_unittest
    • traceback:
      • py_compile: test_cmd_line_script test_importlib
    • urllib.parse: test_sqlite3 test_urlparse
    • urllib.robotparser: test_robotparser
    • wave: test_wave

[ ] lib: cpython/Lib/hashlib.py
[ ] test: cpython/Lib/test/test_hashlib.py (TODO: 9)

dependencies:

  • hashlib

dependent tests: (15 tests)

  • hashlib: test_hashlib test_hmac test_smtplib test_tarfile test_unicodedata test_urllib2_localnet
    • urllib.request: test_http_cookiejar test_pathlib test_pydoc test_site test_ssl test_urllib test_urllib2 test_urllib2net test_urllibnet

[ ] lib: cpython/Lib/hmac.py
[ ] test: cpython/Lib/test/test_hmac.py (TODO: 27)

dependencies:

  • hmac

dependent tests: (4 tests)

  • hmac: test_hmac test_smtplib
    • secrets: test_secrets
    • smtplib: test_smtpnet

[x] lib: cpython/Lib/inspect.py
[ ] test: cpython/Lib/test/test_inspect (TODO: 49)

dependencies:

  • inspect

dependent tests: (44 tests)

  • inspect: test_abc test_argparse test_asyncgen test_buffer test_builtin test_code test_collections test_coroutines test_decimal test_enum test_functools test_generators test_grammar test_inspect test_ntpath test_operator test_patma test_posixpath test_pydoc test_signal test_traceback test_type_annotations test_types test_typing test_unittest test_yield_from test_zipimport
    • asyncio: test_asyncio test_contextlib_async test_logging test_os test_sys_settrace test_unittest
    • bdb: test_bdb
    • dataclasses: test__colorize test_genericalias test_pprint test_regrtest test_zoneinfo
    • importlib.metadata: test_importlib
    • pydoc:
      • xmlrpc.server: test_docxmlrpc test_xmlrpc
    • rlcompleter: test_rlcompleter
    • trace: test_trace

[x] test: cpython/Lib/test/test_keywordonlyarg.py (TODO: 1)

dependencies:

dependent tests: (no tests depend on keywordonlyarg)

[x] test: cpython/Lib/test/test_positional_only_arg.py (TODO: 5)

dependencies:

dependent tests: (no tests depend on positional_only_arg)

[ ] lib: cpython/Lib/smtplib.py
[x] test: cpython/Lib/test/test_smtplib.py (TODO: 2)
[x] test: cpython/Lib/test/test_smtpnet.py

dependencies:

  • smtplib (native: email.base64mime, email.generator, email.message, email.utils, sys)
    • datetime (native: _thread, math, sys, time)
    • hmac
    • base64, copy, io, re, socket

dependent tests: (2 tests)

  • smtplib: test_smtplib test_smtpnet

[x] lib: cpython/Lib/symtable.py
[ ] test: cpython/Lib/test/test_symtable.py (TODO: 17)

dependencies:

  • symtable

dependent tests: (1 tests)

  • symtable: test_symtable

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant