This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: importlib: Remove libregrtest hack: "We import importlib *ASAP* in order to test #15386"
Type: Stage: resolved
Components: Tests Versions: Python 3.9
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, ncoghlan, vstinner
Priority: normal Keywords: patch

Created on 2019-07-01 15:24 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 14661 merged vstinner, 2019-07-09 09:55
Messages (3)
msg347032 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-07-01 15:24
Lib/test/ starts with:

# We import importlib *ASAP* in order to test #15386
import importlib

If there is a bug to test, would it be possible to write a dedicated test, rather than putting the test... in the code of the test runner?

Code added by bpo-15386:

commit be7e49fd820318509cd8b4dbde479c552f74ef62
Author: Nick Coghlan <>
Date:   Fri Jul 20 23:40:09 2012 +1000

    Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap


diff --git a/Lib/ b/Lib/
index d612727c6f..39c0e9f7dd 100644
--- a/Lib/
+++ b/Lib/
@@ -12,8 +12,8 @@ importers when locating support scripts as well as when importing modules.
 import os
 import sys
+import importlib.machinery # importlib first so we can test #15386 via -m
 import imp
-import importlib.machinery
 from pkgutil import read_code, get_loader, get_importer
 __all__ = [
diff --git a/Lib/test/ b/Lib/test/
index 28655f0e65..3c8359aca1 100755
--- a/Lib/test/
+++ b/Lib/test/
@@ -165,6 +165,9 @@ example, to run all the tests except for the gui tests, give the
 option '-uall,-gui'.
+# We import importlib *ASAP* in order to test #15386
+import importlib
 import builtins
 import faulthandler
 import getopt
diff --git a/Lib/test/ b/Lib/test/
index 89ec8dcedc..51b52c7e0b 100644
--- a/Lib/test/
+++ b/Lib/test/
@@ -1,8 +1,9 @@
+# We import importlib *ASAP* in order to test #15386
+import importlib
 import builtins
 import imp
 from importlib.test.import_ import test_suite as importlib_import_test_suite
 from importlib.test.import_ import util as importlib_util
-import importlib
 import marshal
 import os
 import platform
msg347927 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-07-14 17:31
New changeset 8b7db5a1114e2113a756bdf8877fbe366055c69a by Victor Stinner in branch 'master':
bpo-37473: Don't import importlib ASAP in tests (GH-14661)
msg347929 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-07-14 17:32
I merged your PR. If someone considers than test_there_can_be_only_one() is not enough, please add a new test.
Date User Action Args
2022-04-11 14:59:17adminsetgithub: 81654
2019-07-14 17:32:22vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg347929

stage: patch review -> resolved
2019-07-14 17:31:24vstinnersetmessages: + msg347927
2019-07-09 09:55:31vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request14468
2019-07-01 15:24:52vstinnercreate