Skip to content

Capture runtime Python version in capture files - #1055

Open
godlygeek wants to merge 1 commit into
bloomberg:mainfrom
godlygeek:capture_runtime_python_version
Open

godlygeek wants to merge 1 commit into
bloomberg:mainfrom
godlygeek:capture_runtime_python_version

Conversation

@godlygeek

Copy link
Copy Markdown
Contributor

Previously we were capturing the compile time version instead, which is less useful.

@godlygeek godlygeek self-assigned this Oct 16, 2026
@codecov-commenter

codecov-commenter commented Oct 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.31%. Comparing base (060d7de) to head (370e204).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1055   +/-   ##
=======================================
  Coverage   92.31%   92.31%           
=======================================
  Files         103      103           
  Lines       13363    13366    +3     
  Branches      483      483           
=======================================
+ Hits        12336    12339    +3     
  Misses       1027     1027           
Flag Coverage Δ
cpp 92.31% <100.00%> (+<0.01%) ⬆️
python_and_cython 92.31% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@blin blin 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.

It would be useful to have the context from #1048 in the commit message, specifically "record the version of Python that the capture file was generated with, not the version of Python that the Memray module was compiled with".

LGTM, but please see one nit before merging.

Comment thread src/memray/_memray/compat.cpp Outdated
return static_cast<int>(Py_Version);
#else
assert(PyGILState_Check());
PyObject* hexversion = PySys_GetObject("hexversion"); // Borrowed reference

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: sys.hexversion is writeable, there is an edge case where overwritten sys.hexversion causes the wrong branch in parseLinetable to be taken. I'm guessing that this is not a practical concern, but would defer to you on whether this is something worth guarding against / worth mentioning in the commit message as not worth worrying about.

@godlygeek godlygeek Oct 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, not worth worrying about, I think. We've hardened ourselves against malformed line tables, and a well formed line table that's being parsed by the wrong version's parse function would just get treated as malformed. I think that's fine, especially considering this will go away when we drop support for 3.10 in a year or so.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, you know what? Let's just keep returning PY_VERSION_HEX for 3.9 and 3.10. It's less useful for us, but it can't be tampered with, and we won't be supporting those versions much longer anyway.

For Python 3.11 and newer, record the version of Python that the capture
file was generated with rather than the version of Python that the
Memray module was compiled with. The runtime version is more useful.

For Python 3.9 and 3.10, continue recording the version of Python that
Memray was built against, as these versions don't have a convenient way
to get the runtime version in the format we need. `Py_Version` was
introduced in 3.11, and while `sys.hexversion` exists for earlier
versions it's also able to be overwritten by the user, so we choose not
to trust it. Also, accessing it requires holding the GIL.

Signed-off-by: Matt Wozniski <godlygeek@gmail.com>
@godlygeek
godlygeek force-pushed the capture_runtime_python_version branch from 0774f4f to 370e204 Compare October 16, 2026 17:14
@godlygeek

Copy link
Copy Markdown
Contributor Author

Give this another review, please, @blin.

@godlygeek
godlygeek requested a review from blin October 16, 2026 17:15

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants