Skip to content

bpo-45019: Silence a warning in test_ctypes. #28362

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
Show file tree
Hide file tree
Changes from 3 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
7 changes: 4 additions & 3 deletions Lib/ctypes/test/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
A testcase which accesses *values* in a dll.
"""

import imp
import _imp
import importlib.util
import unittest
import sys
from ctypes import *
from test.support import import_helper, captured_stdout
from test.support import import_helper

import _ctypes_test

Expand Down Expand Up @@ -73,6 +73,7 @@ class struct_frozen(Structure):
self.assertTrue([entry.code[i] for i in range(abs(entry.size))])
# Check the module's package-ness.
with import_helper.frozen_modules():
# Hide the message written by the __hello__ module.
Copy link
Member

Choose a reason for hiding this comment

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

You can drop this comment.

spec = importlib.util.find_spec(modname)
if entry.size < 0:
# It's a package.
Expand All @@ -81,7 +82,7 @@ class struct_frozen(Structure):
self.assertIsNone(spec.submodule_search_locations)

with import_helper.frozen_modules():
expected = imp._frozen_module_names()
expected = _imp._frozen_module_names()
self.maxDiff = None
self.assertEqual(modules, expected, "PyImport_FrozenModules example "
"in Doc/library/ctypes.rst may be out of date")
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def test_frozen(self):
if name in sys.modules:
del sys.modules[name]
with import_helper.frozen_modules():
with captured_stdout() as out:
import __hello__
import __hello__
with captured_stdout() as out:
__hello__.main()
self.assertEqual(out.getvalue(), 'Hello world!\n')


Expand Down
35 changes: 21 additions & 14 deletions Lib/test/test_importlib/frozen/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ def deprecated():
def fresh(name, *, oldapi=False):
with util.uncache(name):
with import_helper.frozen_modules():
with captured_stdout() as stdout:
if oldapi:
with deprecated():
yield stdout
else:
yield stdout
if oldapi:
with deprecated():
yield
else:
yield


class ExecModuleTests(abc.LoaderTests):
Expand All @@ -44,8 +43,10 @@ def exec_module(self, name):
module.__spec__ = spec
assert not hasattr(module, 'initialized')

with fresh(name) as stdout:
with fresh(name):
self.machinery.FrozenImporter.exec_module(module)
with captured_stdout() as stdout:
module.main()

self.assertTrue(module.initialized)
self.assertTrue(hasattr(module, '__spec__'))
Expand Down Expand Up @@ -119,8 +120,10 @@ def test_unloadable(self):
class LoaderTests(abc.LoaderTests):

def load_module(self, name):
with fresh(name, oldapi=True) as stdout:
with fresh(name, oldapi=True):
module = self.machinery.FrozenImporter.load_module(name)
with captured_stdout() as stdout:
module.main()
return module, stdout

def test_module(self):
Expand Down Expand Up @@ -165,15 +168,18 @@ def test_lacking_parent(self):
self.assertFalse(hasattr(module, '__file__'))

def test_module_reuse(self):
with fresh('__hello__', oldapi=True) as stdout:
with fresh('__hello__', oldapi=True):
module1 = self.machinery.FrozenImporter.load_module('__hello__')
module2 = self.machinery.FrozenImporter.load_module('__hello__')
with captured_stdout() as stdout:
module1.main()
module2.main()
self.assertIs(module1, module2)
self.assertEqual(stdout.getvalue(),
'Hello world!\nHello world!\n')

def test_module_repr(self):
with fresh('__hello__', oldapi=True) as stdout:
with fresh('__hello__', oldapi=True):
module = self.machinery.FrozenImporter.load_module('__hello__')
repr_str = self.machinery.FrozenImporter.module_repr(module)
self.assertEqual(repr_str,
Expand Down Expand Up @@ -203,11 +209,12 @@ class InspectLoaderTests:
def test_get_code(self):
# Make sure that the code object is good.
name = '__hello__'
with import_helper.frozen_modules():
code = self.machinery.FrozenImporter.get_code(name)
mod = types.ModuleType(name)
exec(code, mod.__dict__)
with captured_stdout() as stdout:
with import_helper.frozen_modules():
code = self.machinery.FrozenImporter.get_code(name)
mod = types.ModuleType(name)
exec(code, mod.__dict__)
mod.main()
self.assertTrue(hasattr(mod, 'initialized'))
self.assertEqual(stdout.getvalue(), 'Hello world!\n')

Expand Down
6 changes: 3 additions & 3 deletions Python/frozen_modules/MANIFEST

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 21 additions & 10 deletions Python/frozen_modules/hello.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Tools/freeze/flag.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
initialized = True
print("Hello world!")

def main():
print("Hello world!")

if __name__ == '__main__':
main()
6 changes: 5 additions & 1 deletion Tools/freeze/hello.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
print('Hello world...')
def main():
print("Hello world!")

if __name__ == '__main__':
main()