cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-102567: Add -X importtime=2 for logging an importtime message for already-loaded modules

Open noahbkim opened this issue 1 year ago • 5 comments
trafficstars

As mentioned in the issue:

While -X importtime is incredibly useful for analyzing module import times, by design, it doesn't log anything if an imported module has already been loaded. -X importtime=2 would provide additional output for every module that's already been loaded:

The updated example:

>>> import uuid
import time: cached    | cached     | builtins
import time: cached    | cached     | linecache
import time: cached    | cached     |   _io
import time: cached    | cached     |   os
import time: cached    | cached     |   sys
import time: cached    | cached     |   enum
import time:       594 |        594 |   _uuid
import time:      2428 |       3022 | uuid

Discussion: https://discuss.python.org/t/x-importtrace-to-supplement-x-importtime-for-loaded-modules/23882/5 Prior email chain: https://mail.python.org/archives/list/[email protected]/thread/GEISYQ5BXWGKT33RWF77EOSOMMMFUBUS/

  • Issue: gh-102567

noahbkim avatar May 06 '24 15:05 noahbkim

Hello! This PR needs to add:

  1. A NEWS entry
  2. A whatsnew entry
  3. A new paragraph in the Doc/cmdline for the -X option.
  4. New test case in the Lib/test/test_cmd_line

Eclips4 avatar May 06 '24 17:05 Eclips4

Hello! This PR needs to add:

  1. A NEWS entry
  2. A whatsnew entry
  3. A new paragraph in the Doc/cmdline for the -X option.
  4. New test case in the Lib/test/test_cmd_line

Thanks for the quick feedback. I've committed a first pass of 1-3. As for tests: there appear to be no importtime tests (likely due to the fact that any simple implementation would be super noisy as other parts of the interpreter change). Would it be sufficient to do some rudimentary check i.e. import foo then ensure a corresponding foo row appears in the output?

Thanks again!

noahbkim avatar May 06 '24 18:05 noahbkim

Hello! This PR needs to add:

  1. A NEWS entry
  2. A whatsnew entry
  3. A new paragraph in the Doc/cmdline for the -X option.
  4. New test case in the Lib/test/test_cmd_line

Thanks for the quick feedback. I've committed a first pass of 1-3. As for tests: there appear to be no importtime tests (likely due to the fact that any simple implementation would be super noisy as other parts of the interpreter change). Would it be sufficient to do some rudimentary check i.e. import foo then ensure a corresponding foo row appears in the output?

Thanks again!

Yes, it would be sufficient :)

Eclips4 avatar May 06 '24 19:05 Eclips4

Also, there's a merge conflict. Can you resolve it? (I can do it myself, but it seems like you have turned off this option)

Eclips4 avatar May 06 '24 19:05 Eclips4

Also, there's a merge conflict. Can you resolve it? (I can do it myself, but it seems like you have turned off this option)

Done, just added tests as well

noahbkim avatar May 07 '24 17:05 noahbkim

@Eclips4 who should I ping to get final review? Is it alright if I @ the blamed reviewers?

noahbkim avatar May 28 '24 18:05 noahbkim

@Eclips4 who should I ping to get final review? Is it alright if I @ the blamed reviewers?

I guess @vstinner is the right person to review this 😄 However, you should move your changes from whatsnew/3.13.rst to whatsnew/3.14.rst since 3.13 branch is in feature-freeze mode, and this feature will appear only in the 3.14 version.

Eclips4 avatar May 28 '24 18:05 Eclips4

However, you should move your changes from whatsnew/3.13.rst to whatsnew/3.14.rst since 3.13 branch is in feature-freeze mode, and this feature will appear only in the 3.14 version.

The same applies for the versionchanged directives.

Eclips4 avatar May 28 '24 20:05 Eclips4

The following commit authors need to sign the Contributor License Agreement:

Click the button to sign:
CLA not signed

cpython-cla-bot[bot] avatar Jul 11 '24 16:07 cpython-cla-bot[bot]

Ah, we may have to put this on hold. Something's been added that runs (and imports posix) every time you type in the repl, causing it to become nearly unusable.

noahbkim avatar Jul 11 '24 16:07 noahbkim

Apologies for the undue pings, I must have screwed something up with the force push.

noahbkim avatar Jul 11 '24 16:07 noahbkim

Ah, we may have to put this on hold. Something's been added that runs (and imports posix) every time you type in the repl, causing it to become nearly unusable.

Here's how it can be resolved:

diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py
index c4dedd97d1..db35ee5fb4 100644
--- a/Lib/_pyrepl/unix_console.py
+++ b/Lib/_pyrepl/unix_console.py
@@ -38,6 +38,10 @@
 from .unix_eventqueue import EventQueue
 from .utils import wlen

+try:
+    import posix
+except ImportError:
+    posix = None

 TYPE_CHECKING = False

@@ -545,11 +549,7 @@ def clear(self):

     @property
     def input_hook(self):
-        try:
-            import posix
-        except ImportError:
-            return None
-        if posix._is_inputhook_installed():
+        if posix is not None and posix._is_inputhook_installed():
             return posix._inputhook

     def __enable_bracketed_paste(self) -> None:

Eclips4 avatar Jul 13 '24 07:07 Eclips4

Feedback has been implemented, bumping for rearview!

noahbkim avatar Sep 03 '24 21:09 noahbkim

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

bedevere-app[bot] avatar Sep 04 '24 05:09 bedevere-app[bot]

All commit authors signed the Contributor License Agreement.
CLA signed

cpython-cla-bot[bot] avatar Sep 30 '24 22:09 cpython-cla-bot[bot]

Apologies for the noise, I had to amend some of the commits with the correct email to pick up the right CLA.

noahbkim avatar Sep 30 '24 22:09 noahbkim