Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_pkgimport.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | bc18b5d4920e |
| children |
| rev | line source |
|---|---|
| 60648 | 1 import os |
| 2 import sys | |
| 3 import shutil | |
| 4 import string | |
| 5 import random | |
| 6 import tempfile | |
| 7 import unittest | |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
84154
bc18b5d4920e
Issue #17177: Stop using imp in a bunch of tests
Brett Cannon <brett@python.org>
parents:
71103
diff
changeset
|
9 from importlib.util import cache_from_source |
|
71103
0c49260e85a0
Issue #12451: Add support.create_empty_file()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
10 from test.support import run_unittest, create_empty_file |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 class TestImport(unittest.TestCase): |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 def __init__(self, *args, **kw): |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 self.package_name = 'PACKAGE_' |
|
39575
63237fcefd99
Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents:
32885
diff
changeset
|
16 while self.package_name in sys.modules: |
|
42585
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
39575
diff
changeset
|
17 self.package_name += random.choose(string.ascii_letters) |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 self.module_name = self.package_name + '.foo' |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 unittest.TestCase.__init__(self, *args, **kw) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 def remove_modules(self): |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 for module_name in (self.package_name, self.module_name): |
|
39575
63237fcefd99
Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents:
32885
diff
changeset
|
23 if module_name in sys.modules: |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 del sys.modules[module_name] |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 def setUp(self): |
|
24710
11c701038805
Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
27 self.test_dir = tempfile.mkdtemp() |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 sys.path.append(self.test_dir) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 self.package_dir = os.path.join(self.test_dir, |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 self.package_name) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 os.mkdir(self.package_dir) |
|
71103
0c49260e85a0
Issue #12451: Add support.create_empty_file()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
32 create_empty_file(os.path.join(self.package_dir, '__init__.py')) |
| 42622 | 33 self.module_path = os.path.join(self.package_dir, 'foo.py') |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 def tearDown(self): |
| 60648 | 36 shutil.rmtree(self.test_dir) |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 self.assertNotEqual(sys.path.count(self.test_dir), 0) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 sys.path.remove(self.test_dir) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 self.remove_modules() |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 def rewrite_file(self, contents): |
| 60648 | 42 compiled_path = cache_from_source(self.module_path) |
| 43 if os.path.exists(compiled_path): | |
| 44 os.remove(compiled_path) | |
| 45 with open(self.module_path, 'w') as f: | |
| 46 f.write(contents) | |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18943
diff
changeset
|
47 |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 def test_package_import__semantics(self): |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 # Generate a couple of broken modules to try importing. |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 # ...try loading the module when there's a SyntaxError |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 self.rewrite_file('for') |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 try: __import__(self.module_name) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 except SyntaxError: pass |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
56 else: raise RuntimeError('Failed to induce SyntaxError') # self.fail()? |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
57 self.assertNotIn(self.module_name, sys.modules) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
58 self.assertFalse(hasattr(sys.modules[self.package_name], 'foo')) |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 # ...make up a variable name that isn't bound in __builtins__ |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 var = 'a' |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 while var in dir(__builtins__): |
|
42585
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
39575
diff
changeset
|
63 var += random.choose(string.ascii_letters) |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 # ...make a module that just contains that |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 self.rewrite_file(var) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 try: __import__(self.module_name) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 except NameError: pass |
|
43014
9834301a2fd3
Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
70 else: raise RuntimeError('Failed to induce NameError.') |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 # ...now change the module so that the NameError doesn't |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 # happen |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 self.rewrite_file('%s = 1' % var) |
|
32885
2aa7aef4e403
Removed reliance on damaged module object appearing in sys.modules
Tim Peters <tim.peters@gmail.com>
parents:
24710
diff
changeset
|
75 module = __import__(self.module_name).foo |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 self.assertEqual(getattr(module, var), 1) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
78 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
79 def test_main(): |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
80 run_unittest(TestImport) |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
81 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
82 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
83 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
84 test_main() |
