Skip to content

Commit 521edd5

Browse files
Subbarao Garlapatimeta-codesync[bot]
authored andcommitted
Add OSS-buildbot-style lazy imports test coverage
Summary: Add OSS-buildbot-style lazy imports test coverage. Itamar [tried](https://www.internalfb.com/diff/D84710473) to do this earlier, but there were some lazy imports test failures. Almost all of these were test file incompatibilities (e.g. needing to eagerly import inside a test file in order to trigger an `ImportError` that would happen with & without Lazy Imports) and not Lazy Imports issues. However, one task (T245974066) has been filed to fix a Lazy Imports refleak in the niche case that happens when the subinterpreter creates a lazy object from a Python module (C extensions are fine though), then imports & loads `_testsinglephase` and then loads the lazy object Reviewed By: itamaro Differential Revision: D86225881 fbshipit-source-id: 99994f1cffca986d4c3ff75a95b92e68c907a074
1 parent 9875ce1 commit 521edd5

6 files changed

Lines changed: 43 additions & 6 deletions

File tree

Lib/bz2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import os
1616

1717
from _bz2 import BZ2Compressor, BZ2Decompressor
18-
18+
# Meta Lazy Imports compatibility: Force _bz2 to be imported
19+
BZ2Compressor
1920

2021
# Value 0 no longer used
2122
_MODE_READ = 1

Lib/dbm/sqlite3.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import os
2-
import sqlite3
32
from pathlib import Path
43
from contextlib import suppress, closing
54
from collections.abc import MutableMapping
65

6+
# Meta Lazy Imports compatibility: Eagerly import `sqlite3` so the ImportError
7+
# is raised right away
8+
try:
9+
import sqlite3
10+
except ImportError:
11+
raise ImportError("No module named '_sqlite3'")
12+
713
BUILD_TABLE = """
814
CREATE TABLE IF NOT EXISTS Dict (
915
key BLOB UNIQUE NOT NULL,

Lib/test/support/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,13 @@ def missing_compiler_executable(cmd_names=[]):
19921992
missing.
19931993
19941994
"""
1995+
try:
1996+
# FIXME: Eager import `_distutils_hack.override` here, if it exists,
1997+
# otherwise we can get spurious errors about `No module named 'distutils'`
1998+
# when importing setuptools.
1999+
import _distutils_hack.override
2000+
except ModuleNotFoundError:
2001+
pass
19952002
from setuptools._distutils import ccompiler, sysconfig
19962003
from setuptools import errors
19972004
import shutil

Lib/test/test_collections.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
"""Unit tests for collections.py."""
2-
3-
import array
2+
# Meta Lazy Imports compatibility: Eagerly import `array` so `array.array` is
3+
# registered with `_collections_abc.Mapping` before refleak tests save ABC state.
4+
# This is necessary since `dash_R_cleanup()` clears and restores ABC registries.
5+
try:
6+
import array
7+
except Exception:
8+
pass
49
import collections
510
import copy
611
import doctest

Lib/test/test_context.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import sys
22
import collections.abc
33
import concurrent.futures
4-
import contextvars
4+
# Meta Lazy Imports compatibility: Eagerly import contextvars so Context is registered
5+
# with `_collections_abc.Mapping` before refleak tests save ABC state.
6+
# This is necessary since `dash_R_cleanup()` clears and restores ABC registries.
7+
try:
8+
import contextvars
9+
except Exception:
10+
pass
511
import functools
612
import gc
713
import random

Lib/test/test_import/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from unittest import mock
2929
import _imp
3030

31-
from test.support import os_helper
31+
from test.support import os_helper, refleak_helper
3232
from test.support import (
3333
STDLIB_DIR,
3434
swap_attr,
@@ -3168,6 +3168,12 @@ def test_check_state_first(self):
31683168

31693169
@requires_subinterpreters
31703170
def test_basic_multiple_interpreters_main_no_reset(self):
3171+
# Skip during refleak testing with lazy imports
3172+
if refleak_helper.hunting_for_refleaks() and importlib.is_lazy_imports_enabled():
3173+
raise unittest.SkipTest(
3174+
"TODO(T245974066): Fix Lazy Imports refleak with Single-Phase Initialization Modules"
3175+
)
3176+
31713177
# without resetting; already loaded in main interpreter
31723178

31733179
# At this point:
@@ -3312,6 +3318,12 @@ def test_basic_multiple_interpreters_deleted_no_reset(self):
33123318

33133319
@requires_subinterpreters
33143320
def test_basic_multiple_interpreters_reset_each(self):
3321+
# Skip during refleak testing with lazy imports
3322+
if refleak_helper.hunting_for_refleaks() and importlib.is_lazy_imports_enabled():
3323+
raise unittest.SkipTest(
3324+
"TODO(T245974066): Fix Lazy Imports refleak with Single-Phase Initialization Modules"
3325+
)
3326+
33153327
# resetting between each interpreter
33163328

33173329
# At this point:

0 commit comments

Comments
 (0)