Skip to content

Commit 0e5562c

Browse files
committed
Fix import star with members starting with underscore
1 parent 323d675 commit 0e5562c

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

graalpython/com.oracle.graal.python.test/src/tests/package/moduleA.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@
4040
"""MODULE A DOC"""
4141

4242
print("module A")
43+
44+
publicProperty = 1
45+
_privateProperty = 2

graalpython/com.oracle.graal.python.test/src/tests/package/moduleY.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@
4141
import math
4242
print("cos(100) = "+str(math.cos(100)))
4343
from .subpackage1 import moduleX
44+
45+
__all__ = ['publicProperty', '_privateProperty']
46+
publicProperty = 1
47+
_privateProperty = 2
48+
unlistedProperty = 3

graalpython/com.oracle.graal.python.test/src/tests/test_imports.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ def test_import_star_has_to_be_module():
9393
assert False
9494

9595

96+
def test_import_star_no_all():
97+
g = {}
98+
exec("from package.moduleA import *", g)
99+
assert 'publicProperty' in g
100+
assert '_privateProperty' not in g
101+
102+
103+
def test_import_star_all():
104+
g = {}
105+
exec("from package.moduleY import *", g)
106+
assert 'publicProperty' in g
107+
assert '_privateProperty' in g
108+
assert 'unlistedProperty' not in g
109+
110+
96111
def test_import_as_local():
97112
from sys import path
98113
from sys import path as mypath

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/ImportStarNode.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,11 @@ private void writeAttributeToLocals(VirtualFrame frame, Node inliningTarget, Tru
155155
PyObjectSetItem dictWriteNode, PyObjectSetAttr setAttrNode) {
156156
try {
157157
TruffleString name = castToTruffleStringNode.execute(inliningTarget, attrName);
158-
// skip attributes with leading '__' if there was no '__all__' attribute (see
159-
// 'ceval.c: import_all_from')
160-
if (fromAll || !isDunder(name, cpLenNode, cpAtIndexNode)) {
158+
/*
159+
* skip attributes with leading '_' if there was no '__all__' attribute (see 'ceval.c:
160+
* import_all_from')
161+
*/
162+
if (fromAll || !startsWithUnderscore(name, cpLenNode, cpAtIndexNode)) {
161163
Object moduleAttr = getAttr.execute(frame, inliningTarget, importedModule, name);
162164
writeAttribute(frame, inliningTarget, locals, name, moduleAttr, dictWriteNode, setAttrNode);
163165
}
@@ -167,8 +169,8 @@ private void writeAttributeToLocals(VirtualFrame frame, Node inliningTarget, Tru
167169
}
168170
}
169171

170-
private static boolean isDunder(TruffleString s, TruffleString.CodePointLengthNode cpLenNode, TruffleString.CodePointAtIndexNode cpAtIndexNode) {
171-
return cpLenNode.execute(s, TS_ENCODING) >= 2 && cpAtIndexNode.execute(s, 0, TS_ENCODING) == '_' && cpAtIndexNode.execute(s, 1, TS_ENCODING) == '_';
172+
private static boolean startsWithUnderscore(TruffleString s, TruffleString.CodePointLengthNode cpLenNode, TruffleString.CodePointAtIndexNode cpAtIndexNode) {
173+
return cpLenNode.execute(s, TS_ENCODING) > 0 && cpAtIndexNode.execute(s, 0, TS_ENCODING) == '_';
172174
}
173175

174176
public static ImportStarNode create() {

0 commit comments

Comments
 (0)