Skip to content

Align code introspection and monitoring with Python 3.15 - #9178

Merged
youknowone merged 4 commits into
RustPython:mainfrom
youknowdot:fix/code-introspection-monitoring-315
Oct 18, 2026
Merged

youknowone merged 4 commits into
RustPython:mainfrom
youknowdot:fix/code-introspection-monitoring-315

Conversation

@youknowdot

Copy link
Copy Markdown
Contributor

Summary

Extract a coherent code-object metadata and monitoring compatibility batch from #8954, based directly on main at fc9f4a9.

  • Preserve co_qualname object semantics: exact strings are interned, string subclasses retain identity, and marshal round trips preserve WTF8 qualified names. Marshal keeps CPython's exact-string policy.
  • Apply no_debug_ranges consistently and normalize valid signed location-table deltas without changing bytecode offsets. Unconvertible tables retain existing bytes; this is not a general malformed-table reader hardening change.
  • Track monitoring DISABLE by event and physical instruction site, preserve CALL/C_RETURN/C_RAISE coupling, and handle local non-instrumented subscriptions.
  • Match observed legacy branch behavior through LINE/INSTRUCTION wrappers, including dispatch source offsets and the one-invocation LINE removal transition.
  • Release replaced/cleared monitoring callbacks outside the monitoring lock, retaining CPython's tool-name visibility and nested-free ordering. Restore tracing state before interpreting callback results.
  • Import the canonical monitoring shutdown helper through scripts/update_lib and retire only its genuinely executed, now-passing expectedFailure marker.

Canonical source: CPython 3.15.0 at d220409a13b77de7a6c30abe16ea90db01528edc. Existing related canonical files were already present; no artificial Lib updates, duplicate snippets, or Python-behavior Rust tests are added. Merged function/type lifetime fixes remain intact.

Validation

Current source: 675826ba7a.

  • Fresh default-feature release build (filesystem stdlib) and normal commit hooks passed.
  • Five bounded oracle/candidate observations match all JSON values, including exact-string interning, subclass identity/rejection, surrogate marshal values, valid signed location entries, no-debug ranges, legacy/direct branch disabling, chronological wrapper offsets, and callback-finalizer reentry/tool reuse.
  • Pinned CPython test_code: 42 total, 41 passed and 1 skipped. Candidate repository test_code: 37 total, 21 passed, 15 skipped, 1 existing expected failure.
  • Pinned CPython test_monitoring: 98 passed. Candidate repository test_monitoring: 98 total, 91 passed, 5 skipped, 2 existing expected failures. The shutdown helper ran successfully in a child interpreter before its marker was retired.
  • Workspace native: 1,369 passed. C-API native: 123 passed, 4 existing ignored tests.
  • Strict workspace/C-API Clippy and compiler-only nonthreading check passed. Workspace docs: 24 passed, 18 ignored; C-API docs: no test cases.
  • test_marshal: 75 total, 59 passed, 15 skipped, 1 existing expected failure.
  • test_sys_settrace: 448 total, 371 passed, 77 skipped.
  • test_dis: 145 total, 119 passed, 26 skipped.
  • test_types: 136 total, 132 passed, 2 skipped, 2 existing expected failures.

Local gates bind source 675826ba7a to base fc9f4a9. Main advanced independently during validation; hosted merge-ref CI must establish integration with those landed changes before merge readiness. Local old-base test counts are not claimed as merged-tree results.

The repository suites retain genuine unsupported C-extension/resource guards. C-API native coverage does not imply Python _testcapi availability. No CPU/network/bigmem resources were enabled.

Scope limits

This does not claim complete monitoring parity for callback-time instrumentation replacement, cross-tool callback mutation/order, orphan/loop predecessor mapping, or reserved tracing/profiling instrumentation. Those inherited paths are not redesigned here. It also does not claim safe interpretation of arbitrary malformed location tables or lossless unrelated compiler-only string fields.

Baseline mismatches were retained and corrected against pinned source and oracle observations; no new test exclusions or altered assertions were used.

Assisted-by: OpenAI Codex:model version unavailable

Preserve canonical qualified-name interning and subclass values, normalize valid debug-range tables, and correct event-specific monitoring disable and callback ownership boundaries.

Assisted-by: OpenAI Codex:model version unavailable
Copy the missing test_monitoring dependency from pinned CPython 3.15.0 through scripts/update_lib, retaining existing suite guards.

Assisted-by: OpenAI Codex:model version unavailable
Model active source instrumentation across all subscribed tools instead of unconditional legacy edge pairing. Preserve tool names during unlocked callback finalization before releasing them.

Assisted-by: OpenAI Codex:model version unavailable
Retain decoded WTF8 qualnames, use exact-str marshal dispatch, and model wrapped branch dispatch with physical DISABLE sites. Retire the proven monitoring shutdown guard through update_lib.

Assisted-by: OpenAI Codex:model version unavailable
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b02f77fd-7476-4676-a0eb-9c61f21c3ab7
📥 Commits

Reviewing files that changed from the base of the PR and between 65bbaca and 675826b.

⛔ Files ignored due to path filters: 2
  • Lib/test/_test_monitoring_shutdown.py is excluded by !Lib/**
  • Lib/test/test_monitoring.py is excluded by !Lib/**
📒 Files selected for processing: 7
  • crates/compiler-core/src/marshal.rs
  • crates/vm/src/builtins/code.rs
  • crates/vm/src/builtins/code/const_cache.rs
  • crates/vm/src/builtins/function.rs
  • crates/vm/src/frame.rs
  • crates/vm/src/stdlib/marshal.rs
  • crates/vm/src/stdlib/sys/monitoring.rs
+--------------------------------------------------------------------------+
| This function returns three different shapes. It's basically modern art. |
+--------------------------------------------------------------------------+
  \
   \     /)/)
        ( '')
        /  |
      o(___)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

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

:4: SyntaxWarning: invalid decimal literal
:7: SyntaxWarning: "\z" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\z"? A raw string is also an option.
:10: SyntaxWarning: "\400" is an invalid octal escape sequence. Such sequences will not work in the future. Did you mean "\400"? A raw string is also an option.
:4: SyntaxWarning: invalid decimal literal
:7: SyntaxWarning: "\z" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\z"? A raw string is also an option.
:10: SyntaxWarning: "\400" is an invalid octal escape sequence. Such sequences will not work in the future. Did you mean "\400"? A raw string is also an option.
:4: SyntaxWarning: invalid decimal literal
:7: SyntaxWarning: "\z" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\z"? A raw string is also an option.
:10: SyntaxWarning: "\400" is an invalid octal escape sequence. Such sequences will not work in the future. Did you mean "\400"? A raw string is also an option.
[ ] lib: cpython/Lib/io.py
[x] lib: cpython/Lib/_pyio.py
[ ] test: cpython/Lib/test/test_io (TODO: 10)

dependencies:

  • io (native: _io, _thread, errno, msvcrt, sys)
    • _pyio
    • _collections_abc (native: sys)
    • abc, codecs, locale, os, stat, warnings

dependent tests: (108 tests)

  • io: test__colorize test_android test_argparse test_ast test_asyncio test_base64 test_buffer test_bufio test_builtin test_bz2 test_calendar test_cmd test_cmd_line_script test_codecs test_compile test_compileall test_compiler_assemble test_concurrent_futures test_configparser test_contextlib test_csv test_dbm_dumb test_descr test_dis test_email test_enum test_file test_fileinput test_fileio test_ftplib test_generated_cases test_getpass test_gzip test_hashlib test_http_cookiejar test_httplib test_httpservers test_importlib test_inspect test_io test_json test_largefile test_logging test_lzma test_mailbox test_marshal test_memoryio test_memoryview test_mimetypes test_minidom test_multibytecodec test_optparse test_pathlib test_pdb test_peg_generator test_pickle test_pickletools test_platform test_plistlib test_pprint test_print test_profile test_pstats test_pty test_pulldom test_pydoc test_pyexpat test_pyrepl test_quopri test_regrtest test_robotparser test_sax test_shlex test_shutil test_site test_smtplib test_socket test_socketserver test_subprocess test_support test_sys test_tarfile test_tempfile test_threadedtempfile test_timeit test_tokenize test_traceback test_types test_typing test_unittest test_univnewlines test_urllib test_urllib2 test_uuid test_wave test_webbrowser test_winconsoleio test_wsgiref test_xml_dom_xmlbuilder test_xml_etree test_xml_etree_c test_xmlrpc test_xpickle test_zipapp test_zipfile test_zipimport test_zoneinfo test_zstd

[x] test: cpython/Lib/test/test_monitoring.py (TODO: 3)

dependencies:

dependent tests: (no tests depend on monitoring)

[ ] test: cpython/Lib/test/test_finalization.py

dependencies:

dependent tests: (no tests depend on finalization)

(module 'bufio' not found)

(module 'largefile' not found)

[ ] lib: cpython/Lib/weakref.py
[x] lib: cpython/Lib/_weakrefset.py
[ ] test: cpython/Lib/test/test_weakref.py (TODO: 9)
[x] test: cpython/Lib/test/test_weakset.py

dependencies:

  • weakref (native: _weakref, atexit, gc, itertools, sys)
    • _weakrefset
    • _collections_abc
    • _weakrefset, copy, types

dependent tests: (200 tests)

  • weakref: test_array test_ast test_asyncio test_builtin test_code test_concurrent_futures test_context test_contextlib test_copy test_ctypes test_deque test_descr test_dict test_enum test_exceptions test_file test_fileio test_finalization test_frame test_functools test_gc test_generators test_genericalias test_importlib test_inspect test_io test_ipaddress test_itertools test_logging test_memoryio test_memoryview test_mmap test_ordered_dict test_pickle test_picklebuffer test_queue test_re test_scope test_set test_slice test_socket test_sqlite3 test_ssl test_struct test_sys test_tempfile test_thread test_threading test_threading_local test_type_params test_types test_typing test_unicodedata test_unittest test_uuid test_weakref test_weakset test_xml_etree
    • asyncio: test_asyncio test_concurrent_futures test_external_inspection test_os test_pdb test_unittest
    • bdb: test_bdb
    • concurrent: test_compileall test_concurrent_futures test_wmi
    • copy: test_bytes test_codecs test_collections test_copyreg test_coroutines test_csv test_decimal test_defaultdict test_dictviews test_email test_fractions test_genericpath test_http_cookies test_minidom test_opcache test_optparse test_os test_platform test_plistlib test_site test_statistics test_structseq test_super test_sysconfig test_tomllib test_urllib2 test_xml_dom_minicompat test_zlib
      • argparse: test_argparse
      • collections: test_annotationlib test_bisect test_c_locale_coercion test_call test_configparser test_contains test_ctypes test_embed test_exception_group test_fileinput test_funcattrs test_hash test_httpservers test_iter test_iterlen test_json test_math test_monitoring test_pathlib test_patma test_pprint test_pydoc test_random test_reprlib test_richcmp test_shelve test_sqlite3 test_string test_traceback test_tuple test_urllib test_userdict test_userlist test_userstring test_with
      • dataclasses: test__colorize test_ctypes test_regrtest test_zoneinfo
      • email.generator: test_email
      • gettext: test_gettext test_tools
      • http.cookiejar: test_http_cookiejar
      • http.server: test_robotparser test_urllib2_localnet test_xmlrpc
      • logging.handlers: test_pkgutil
      • mailbox: test_mailbox
      • smtplib: test_smtplib test_smtpnet
      • tarfile: test_shutil test_tarfile
      • webbrowser: test_webbrowser
    • logging: test_hashlib test_support test_urllib2net
      • venv: test_venv
    • multiprocessing: test_fcntl test_importlib test_multiprocessing_main_handling
    • symtable: test_symtable
    • tempfile: test_bz2 test_cmd_line test_compile test_cprofile test_ctypes test_dis test_doctest test_ensurepip test_faulthandler test_filecmp test_generated_cases test_importlib test_launcher test_linecache test_modulefinder test_ntpath test_peg_generator test_pkg test_pstats test_py_compile test_pyrepl test_runpy test_selectors test_shlex test_string_literals test_subprocess test_sys_settrace test_tabnanny test_termios test_threadedtempfile test_tokenize test_turtle test_urllib_response test_wave test_winconsoleio test_zipapp test_zipfile test_zipfile64 test_zstd
      • ctypes.util: test_ctypes
      • urllib.request: test_sax test_urllibnet

[ ] test: cpython/Lib/test/test_io (TODO: 10)

dependencies:

dependent tests: (no tests depend on file)

(module 'fileio' not found)

[ ] test: cpython/Lib/test/test_descr.py (TODO: 2)
[ ] test: cpython/Lib/test/test_descrtut.py (TODO: 2)

dependencies:

dependent tests: (no tests depend on descr)

(module 'memoryio' not found)

[ ] test: cpython/Lib/test/test_gc.py (TODO: 4)

dependencies:

dependent tests: (174 tests)

  • gc: test_array test_asyncio test_baseexception test_builtin test_call test_class test_context test_csv test_ctypes test_deque test_descr test_dict test_enum test_enumerate test_finalization test_functools test_gc test_generators test_gzip test_inspect test_io test_itertools test_logging test_memoryio test_memoryview test_ordered_dict test_pdb test_peepholer test_pickle test_picklebuffer test_raise test_set test_socket test_ssl test_struct test_structseq test_subprocess test_sys test_sys_setprofile test_sys_settrace test_tempfile test_tuple test_types test_typing test_unittest test_weakref test_weakset test_winreg test_zoneinfo test_zstd
    • timeit: test_timeit
    • trace: test_trace
    • weakref: test_ast test_asyncio test_code test_concurrent_futures test_contextlib test_copy test_exceptions test_file test_fileio test_frame test_genericalias test_importlib test_io test_ipaddress test_mmap test_queue test_re test_scope test_slice test_sqlite3 test_thread test_threading test_threading_local test_type_params test_unicodedata test_unittest test_uuid test_xml_etree
      • asyncio: test_asyncio test_concurrent_futures test_external_inspection test_os test_unittest
      • bdb: test_bdb
      • concurrent: test_compileall test_concurrent_futures test_wmi
      • copy: test_bytes test_codecs test_collections test_copyreg test_coroutines test_decimal test_defaultdict test_dictviews test_email test_fractions test_genericpath test_http_cookies test_minidom test_opcache test_optparse test_os test_platform test_plistlib test_site test_statistics test_super test_sysconfig test_tomllib test_urllib2 test_xml_dom_minicompat test_zlib
      • gzip: test_fileinput test_tarfile test_xmlrpc
      • logging: test_hashlib test_pkgutil test_support test_urllib2net
      • multiprocessing: test_fcntl test_importlib test_multiprocessing_main_handling
      • symtable: test_symtable
      • tempfile: test_argparse test_bz2 test_cmd_line test_compile test_cprofile test_ctypes test_dis test_doctest test_embed test_ensurepip test_faulthandler test_filecmp test_generated_cases test_httpservers test_importlib test_launcher test_linecache test_mailbox test_modulefinder test_ntpath test_pathlib test_peg_generator test_pkg test_pstats test_py_compile test_pydoc test_pyrepl test_regrtest test_runpy test_selectors test_shlex test_shutil test_string_literals test_tabnanny test_termios test_threadedtempfile test_tokenize test_traceback test_turtle test_urllib test_urllib_response test_venv test_wave test_winconsoleio test_zipapp test_zipfile test_zipfile64
      • xml.sax.expatreader: test_sax

(module 'univnewlines' not found)

Legend:

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

@youknowone
youknowone marked this pull request as ready for review October 18, 2026 01:57
@youknowone
youknowone merged commit efa8c26 into RustPython:main Oct 18, 2026
22 checks passed
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T02:05:38.560757Z 675826b Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 675826ba7a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

callbacks
};
// Callback finalizers may reenter monitoring; release owners after unlocking.
drop(callbacks);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Re-clear reentrant event registrations after callback teardown

When a callback is kept alive only by the monitoring table and its __del__ calls set_events or set_local_events (and may re-register itself), this drop runs after clear_tool_id/free_tool_id have already cleared the tool's masks, so the destructor's new masks survive; free_tool_id can then leave a nameless tool with live callbacks/events, and immediate reuse inherits them. Re-clear the tool's masks after finalizers run while still dropping callback owners outside the lock.

Useful? React with 👍 / 👎.


import sys

class CallableCycle:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep executable fixtures out of restricted test changes

Adding this class creates new executable test logic in Lib/test, but the repository's root instructions permit test-file changes only to add or remove @unittest.expectedFailure markers and their associated TODO comments. Restore the shutdown test's marker rather than adding this helper so the change stays within the allowed test-update policy.

AGENTS.md reference: AGENTS.md:L274-L282

Useful? React with 👍 / 👎.

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.

2 participants