Skip to content

gh-132737: Support profiling modules that import __main___ #132738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 24, 2025

Conversation

aneeshdurg
Copy link
Contributor

@aneeshdurg aneeshdurg commented Apr 19, 2025

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 19, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Apr 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@gaogaotiantian
Copy link
Member

This is not going to work because you introduced extra function calls in cProfile record, which would confuse people. Try a simple print("hello world") and see the difference between the old implementation and the new one.

I can work on this (kind of the responsible person for the module), or if you really want to take a swing at the issue I can give you some direction.

@aneeshdurg
Copy link
Contributor Author

I'd be happy to take a swing at it! Any pointers on what to look into?

@gaogaotiantian
Copy link
Member

So pdb has a very similar problem to solve and it works on your script. The code you run has to be the compiled code from the file (io.open_code actually also sends an audit event which probably matters), not anything else. You should do something like pdb - just update __main__ to make sure your program can import from it. I don't think you need to build a new module.

Also, you need a regression test in this case. You need to write a test that fails with the original implementation, and passes after. Hopefully it won't be this complicated (I know it's not long, but I don't believe dataclass or even pickle is required to reproduce this issue). It's okay to use pickle if that's the easiest way.

@aneeshdurg
Copy link
Contributor Author

I was able to get my repro working by updating main instead! I'll add regression tests soon.

aneeshdurg and others added 2 commits April 20, 2025 11:14
…e-132737.9mW1il.rst

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@gaogaotiantian
Copy link
Member

You can't do it in runctx. This is a function that can be called by the user in their normal program and you are switching their __main__, which would break their code. This can only happen when cProfile is executed from cmdline.

Lib/cProfile.py Outdated
try:
runctx(code, globs, None, options.outfile, options.sort)
runctx(code, __main__.__dict__, None, options.outfile, options.sort)
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need to restore it? You are exiting the program anyway. Also this is not ideal either. This will include all the global variables to the script that is being profiled. We want print(locals()) to be basically the same with or without the profiler.

Copy link
Contributor Author

@aneeshdurg aneeshdurg Apr 20, 2025

Choose a reason for hiding this comment

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

The issue is to juggle the global variables needed by the call to (and implementation of) runctx. It would be a lot easier if I could split cprofile into a module where __main__ only has the main function. Is that something I can do?
if not, it's still possible, just trickier/messier.

Copy link
Member

Choose a reason for hiding this comment

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

No you can't split it into its own module :( that's too much a change. I think the correct way to go is to make the full runctx path independent of any global variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Found a much cleaner fix - just ensure that the "main" function isn't run in the __main__ namespace.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I'm not sure if this is too hacky. I did not find such pattern in other code. It looks like an acceptable solution but I don't know if there will be implications. I want to ask @vstinner about this as he probably knows a lot of interesting usages.

Copy link
Member

Choose a reason for hiding this comment

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

It seems like this hack works as expected. But it's strange and surprising :-)

I tried but failed (test fails) to inject a new __main__ module in sys.modules and leave the cProfile module unchanged:

diff --git a/Lib/cProfile.py b/Lib/cProfile.py
index 6253755a9df..abc03fc61eb 100644
--- a/Lib/cProfile.py
+++ b/Lib/cProfile.py
@@ -166,6 +166,7 @@ def main():
                 'run_module': runpy.run_module,
                 'modname': args[0]
             }
+            modname = args[0]
         else:
             progname = args[0]
             sys.path.insert(0, os.path.dirname(progname))
@@ -181,14 +182,18 @@ def main():
                 '__cached__': None,
                 '__builtins__': __builtins__,
             }
+            modname = spec.name
+
         # cmd has to run in __main__ namespace (or imports from __main__ will
         # break). Clear __main__ and replace with the globals provided.
-        import __main__
-        __main__.__dict__.clear()
-        __main__.__dict__.update(globs)
+        import __main__ as cProfileMain
+        new_main = type(cProfileMain)(modname)
+        new_main.__dict__.clear()
+        new_main.__dict__.update(globs)
+        sys.modules['__main__'] = new_main
 
         try:
-            runctx(code, __main__.__dict__, None, options.outfile, options.sort)
+            runctx(code, new_main.__dict__, None, options.outfile, options.sort)
         except BrokenPipeError as exc:
             # Prevent "Exception ignored" during interpreter shutdown.
             sys.stdout = None

Copy link
Contributor Author

@aneeshdurg aneeshdurg Apr 23, 2025

Choose a reason for hiding this comment

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

Isn't this hack what pdb already does implicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dictionary of the module __main__ is used (see the explanation of

Ah, it's actually explicitly documented

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vstinner @gaogaotiantian I did a bit more poking around and I think I managed to get rid of any of the hacky-ness. cProfile's main remains untouched, and in the case where we execute a file, I create a new module, set it as main, and ensure that the globals dict is the dict of the new module. The tests pass.

Copy link
Member

Choose a reason for hiding this comment

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

I think the current solution looks much less hacky.

…e-132737.9mW1il.rst

Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
@gaogaotiantian
Copy link
Member

I'll merge this in a day or two if no one has objections.

@gaogaotiantian gaogaotiantian merged commit c7a7aa9 into python:main Apr 24, 2025
28 of 29 checks passed
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.

4 participants