Skip to content

Commit a5d2f45

Browse files
committed
fix remaining importlib.frozen tests
1 parent 94e35d0 commit a5d2f45

File tree

6 files changed

+28
-29
lines changed

6 files changed

+28
-29
lines changed

graalpython/com.oracle.graal.python.frozen/freeze_modules.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
'__hello__ : __hello_alias__',
8080
'__hello__ : <__phello_alias__>',
8181
'__hello__ : __phello_alias__.spam',
82+
'__hello__ : <__phello__>',
83+
'__hello__ : __phello__.spam',
8284
# '<__phello__.**.*>',
8385
f'frozen_only : __hello_only__ = {FROZEN_ONLY}',
8486
],
@@ -533,7 +535,10 @@ def write_frozen_lookup(out_file, modules):
533535
out_file.write(" public static final PythonFrozenModule lookup(String name) {\n")
534536
out_file.write(" switch (name) {\n")
535537
for module in modules:
536-
out_file.write(f' case "{module.name}": return Map.{module.symbol};\n')
538+
if module.source and (module.source.ispkg != module.ispkg):
539+
out_file.write(f' case "{module.name}": return Map.{module.symbol}.asPackage({"true" if module.ispkg else "false"});\n')
540+
else:
541+
out_file.write(f' case "{module.name}": return Map.{module.symbol};\n')
537542
out_file.write(" default: return null;\n")
538543
out_file.write(" }\n")
539544
out_file.write(" }\n")
@@ -560,6 +565,8 @@ def write_frozen_module_file(file, modules):
560565
# set mtime to the old one, if we didn't change anything
561566
print(f"{file} not modified")
562567
os.utime(file, (atime, mtime))
568+
else:
569+
print(f"{file} modified, rebuild needed!")
563570

564571
def add_tabs(str, number):
565572
lines = str.splitlines()

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/FrozenModules.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public static final PythonFrozenModule lookup(String name) {
203203
case "abc": return Map.ABC;
204204
case "codecs": return Map.CODECS;
205205
case "encodings": return Map.ENCODINGS;
206-
case "encodings.__init__": return Map.ENCODINGS;
206+
case "encodings.__init__": return Map.ENCODINGS.asPackage(false);
207207
case "encodings.aliases": return Map.ENCODINGS_ALIASES;
208208
case "encodings.ascii": return Map.ENCODINGS_ASCII;
209209
case "encodings.base64_codec": return Map.ENCODINGS_BASE64_CODEC;
@@ -339,7 +339,7 @@ public static final PythonFrozenModule lookup(String name) {
339339
case "heapq": return Map.HEAPQ;
340340
case "reprlib": return Map.REPRLIB;
341341
case "collections": return Map.COLLECTIONS;
342-
case "collections.__init__": return Map.COLLECTIONS;
342+
case "collections.__init__": return Map.COLLECTIONS.asPackage(false);
343343
case "collections.abc": return Map.COLLECTIONS_ABC;
344344
case "functools": return Map.FUNCTOOLS;
345345
case "copyreg": return Map.COPYREG;
@@ -356,8 +356,10 @@ public static final PythonFrozenModule lookup(String name) {
356356
case "stat": return Map.STAT;
357357
case "__hello__": return Map.__HELLO__;
358358
case "__hello_alias__": return Map.__HELLO__;
359-
case "__phello_alias__": return Map.__HELLO__;
359+
case "__phello_alias__": return Map.__HELLO__.asPackage(true);
360360
case "__phello_alias__.spam": return Map.__HELLO__;
361+
case "__phello__": return Map.__HELLO__.asPackage(true);
362+
case "__phello__.spam": return Map.__HELLO__;
361363
case "__hello_only__": return Map.FROZEN_ONLY;
362364
default: return null;
363365
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/PythonFrozenModule.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,23 @@ private static byte[] getByteCode(String symbol) {
6161
}
6262

6363
public PythonFrozenModule(String symbol, String name, boolean isPackage) {
64+
this(name, getByteCode(symbol), isPackage);
65+
}
66+
67+
private PythonFrozenModule(String name, byte[] code, boolean isPackage) {
6468
this.name = name;
65-
this.code = getByteCode(symbol);
69+
this.code = code;
6670
this.isPackage = isPackage;
6771
}
6872

73+
public PythonFrozenModule asPackage(boolean flag) {
74+
if (flag == isPackage) {
75+
return this;
76+
} else {
77+
return new PythonFrozenModule(name, code, flag);
78+
}
79+
}
80+
6981
public String getName() {
7082
return name;
7183
}

graalpython/lib-python/3/__hello__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
print('Hello world!')
1+
initialized = True
2+
print('Hello world!')

graalpython/lib-python/3/test/test_importlib/frozen/test_finder.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@ def find(self, name, path=None):
1515
finder = self.machinery.FrozenImporter
1616
return finder.find_spec(name, path)
1717

18-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
1918
def test_module(self):
2019
name = '__hello__'
2120
spec = self.find(name)
2221
self.assertEqual(spec.origin, 'frozen')
2322

24-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
2523
def test_package(self):
2624
spec = self.find('__phello__')
2725
self.assertIsNotNone(spec)
2826

29-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
3027
def test_module_in_package(self):
3128
spec = self.find('__phello__.spam', ['__phello__'])
3229
self.assertIsNotNone(spec)
@@ -55,18 +52,15 @@ def find(self, name, path=None):
5552
finder = self.machinery.FrozenImporter
5653
return finder.find_module(name, path)
5754

58-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
5955
def test_module(self):
6056
name = '__hello__'
6157
loader = self.find(name)
6258
self.assertTrue(hasattr(loader, 'load_module'))
6359

64-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
6560
def test_package(self):
6661
loader = self.find('__phello__')
6762
self.assertTrue(hasattr(loader, 'load_module'))
6863

69-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
7064
def test_module_in_package(self):
7165
loader = self.find('__phello__.spam', ['__phello__'])
7266
self.assertTrue(hasattr(loader, 'load_module'))

graalpython/lib-python/3/test/test_importlib/frozen/test_loader.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def exec_module(self, name):
2525
self.assertEqual(module.__spec__.origin, 'frozen')
2626
return module, stdout.getvalue()
2727

28-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
2928
def test_module(self):
3029
name = '__hello__'
3130
module, output = self.exec_module(name)
@@ -35,7 +34,6 @@ def test_module(self):
3534
self.assertEqual(output, 'Hello world!\n')
3635
self.assertTrue(hasattr(module, '__spec__'))
3736

38-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
3937
def test_package(self):
4038
name = '__phello__'
4139
module, output = self.exec_module(name)
@@ -48,7 +46,6 @@ def test_package(self):
4846
expected=value))
4947
self.assertEqual(output, 'Hello world!\n')
5048

51-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
5249
def test_lacking_parent(self):
5350
name = '__phello__.spam'
5451
with util.uncache('__phello__'):
@@ -62,7 +59,6 @@ def test_lacking_parent(self):
6259
expected=value))
6360
self.assertEqual(output, 'Hello world!\n')
6461

65-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
6662
def test_module_repr(self):
6763
name = '__hello__'
6864
module, output = self.exec_module(name)
@@ -72,7 +68,6 @@ def test_module_repr(self):
7268
self.assertEqual(repr_str,
7369
"<module '__hello__' (frozen)>")
7470

75-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
7671
def test_module_repr_indirect(self):
7772
name = '__hello__'
7873
module, output = self.exec_module(name)
@@ -82,7 +77,6 @@ def test_module_repr_indirect(self):
8277
# No way to trigger an error in a frozen module.
8378
test_state_after_failure = None
8479

85-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
8680
def test_unloadable(self):
8781
assert self.machinery.FrozenImporter.find_module('_not_real') is None
8882
with self.assertRaises(ImportError) as cm:
@@ -97,7 +91,6 @@ def test_unloadable(self):
9791

9892
class LoaderTests(abc.LoaderTests):
9993

100-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
10194
def test_module(self):
10295
with util.uncache('__hello__'), captured_stdout() as stdout:
10396
with warnings.catch_warnings():
@@ -112,7 +105,6 @@ def test_module(self):
112105
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
113106
self.assertFalse(hasattr(module, '__file__'))
114107

115-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
116108
def test_package(self):
117109
with util.uncache('__phello__'), captured_stdout() as stdout:
118110
with warnings.catch_warnings():
@@ -131,7 +123,6 @@ def test_package(self):
131123
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
132124
self.assertFalse(hasattr(module, '__file__'))
133125

134-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
135126
def test_lacking_parent(self):
136127
with util.uncache('__phello__', '__phello__.spam'), \
137128
captured_stdout() as stdout:
@@ -150,7 +141,6 @@ def test_lacking_parent(self):
150141
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
151142
self.assertFalse(hasattr(module, '__file__'))
152143

153-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
154144
def test_module_reuse(self):
155145
with util.uncache('__hello__'), captured_stdout() as stdout:
156146
with warnings.catch_warnings():
@@ -161,7 +151,6 @@ def test_module_reuse(self):
161151
self.assertEqual(stdout.getvalue(),
162152
'Hello world!\nHello world!\n')
163153

164-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
165154
def test_module_repr(self):
166155
with util.uncache('__hello__'), captured_stdout():
167156
with warnings.catch_warnings():
@@ -171,7 +160,6 @@ def test_module_repr(self):
171160
self.assertEqual(repr_str,
172161
"<module '__hello__' (frozen)>")
173162

174-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
175163
def test_module_repr_indirect(self):
176164
with util.uncache('__hello__'), captured_stdout():
177165
module = self.machinery.FrozenImporter.load_module('__hello__')
@@ -181,7 +169,6 @@ def test_module_repr_indirect(self):
181169
# No way to trigger an error in a frozen module.
182170
test_state_after_failure = None
183171

184-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
185172
def test_unloadable(self):
186173
assert self.machinery.FrozenImporter.find_module('_not_real') is None
187174
with self.assertRaises(ImportError) as cm:
@@ -198,7 +185,6 @@ class InspectLoaderTests:
198185

199186
"""Tests for the InspectLoader methods for FrozenImporter."""
200187

201-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
202188
def test_get_code(self):
203189
# Make sure that the code object is good.
204190
name = '__hello__'
@@ -209,13 +195,11 @@ def test_get_code(self):
209195
self.assertTrue(hasattr(mod, 'initialized'))
210196
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
211197

212-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
213198
def test_get_source(self):
214199
# Should always return None.
215200
result = self.machinery.FrozenImporter.get_source('__hello__')
216201
self.assertIsNone(result)
217202

218-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
219203
def test_is_package(self):
220204
# Should be able to tell what is a package.
221205
test_for = (('__hello__', False), ('__phello__', True),
@@ -224,7 +208,6 @@ def test_is_package(self):
224208
result = self.machinery.FrozenImporter.is_package(name)
225209
self.assertEqual(bool(result), is_package)
226210

227-
@impl_detail("GR-26392: add support for frozen modules", graalvm=False)
228211
def test_failure(self):
229212
# Raise ImportError for modules that are not frozen.
230213
for meth_name in ('get_code', 'get_source', 'is_package'):

0 commit comments

Comments
 (0)