Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-34022: Unbreak importlib tests when SOURCE_DATE_EPOCH is set #9484

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 30 Lib/test/test_importlib/source/test_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def test_unloadable(self):
loader.load_module('bad name')

@util.writes_bytecode_files
@util.without_source_date_epoch
def test_checked_hash_based_pyc(self):
with util.create_modules('_temp') as mapping:
source = mapping['_temp']
Expand Down Expand Up @@ -267,6 +268,7 @@ def test_checked_hash_based_pyc(self):
)

@util.writes_bytecode_files
@util.without_source_date_epoch
def test_overridden_checked_hash_based_pyc(self):
with util.create_modules('_temp') as mapping, \
unittest.mock.patch('_imp.check_hash_based_pycs', 'never'):
Expand All @@ -292,6 +294,7 @@ def test_overridden_checked_hash_based_pyc(self):
self.assertEqual(mod.state, 'old')

@util.writes_bytecode_files
@util.without_source_date_epoch
def test_unchecked_hash_based_pyc(self):
with util.create_modules('_temp') as mapping:
source = mapping['_temp']
Expand Down Expand Up @@ -322,6 +325,7 @@ def test_unchecked_hash_based_pyc(self):
)

@util.writes_bytecode_files
@util.without_source_date_epoch
def test_overiden_unchecked_hash_based_pyc(self):
with util.create_modules('_temp') as mapping, \
unittest.mock.patch('_imp.check_hash_based_pycs', 'always'):
Expand Down Expand Up @@ -359,6 +363,25 @@ def test_overiden_unchecked_hash_based_pyc(self):
abc=importlib_abc, util=importlib_util)


# Run tests with SOURCE_DATE_EPOCH set explicitly.
class SourceDateEpochTestMeta(type(Source_SimpleTest)):
def __new__(mcls, name, bases, dct):
cls = super().__new__(mcls, name, bases, dct)

for attr in dir(cls):
if attr.startswith('test_'):
meth = getattr(cls, attr)
wrapper = util.with_source_date_epoch(meth)
setattr(cls, attr, wrapper)

return cls


class SourceDateEpoch_SimpleTest(Source_SimpleTest,
metaclass=SourceDateEpochTestMeta):
pass


class BadBytecodeTest:

def import_(self, file, module_name):
Expand Down Expand Up @@ -617,6 +640,7 @@ def test_bad_marshal(self):

# [bad timestamp]
@util.writes_bytecode_files
@util.without_source_date_epoch
def test_old_timestamp(self):
# When the timestamp is older than the source, bytecode should be
# regenerated.
Expand Down Expand Up @@ -668,6 +692,12 @@ class SourceLoaderBadBytecodeTestPEP451(
util=importlib_util)


class SourceDateEpoch_SourceBadBytecodePEP451(
Source_SourceBadBytecodePEP451,
metaclass=SourceDateEpochTestMeta):
pass


class SourceLoaderBadBytecodeTestPEP302(
SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302):
pass
Expand Down
20 changes: 20 additions & 0 deletions 20 Lib/test/test_importlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,26 @@ def wrapper(*args, **kwargs):
return wrapper


def without_source_date_epoch(fxn):
"""Runs function with SOURCE_DATE_EPOCH unset."""
@functools.wraps(fxn)
def wrapper(*args, **kwargs):
with support.EnvironmentVarGuard() as env:
env.unset('SOURCE_DATE_EPOCH')
return fxn(*args, **kwargs)
return wrapper


def with_source_date_epoch(fxn):
"""Runs function with SOURCE_DATE_EPOCH set."""
@functools.wraps(fxn)
def wrapper(*args, **kwargs):
with support.EnvironmentVarGuard() as env:
env['SOURCE_DATE_EPOCH'] = '123456789'
return fxn(*args, **kwargs)
return wrapper


def ensure_bytecode_path(bytecode_path):
"""Ensure that the __pycache__ directory for PEP 3147 pyc file exists.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unbreak importlib tests when :envvar:`SOURCE_DATE_EPOCH` is set.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.