Skip to content

Commit 7072b9f

Browse files
committed
Guard short attribute names in fast lookup
Fixes #971
1 parent 476fdcb commit 7072b9f

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ def set_dict_attr():
8787
assert_raises(AttributeError, set_dict_attr)
8888

8989

90+
def test_lookup_single_underscore_attr():
91+
class MyClass:
92+
pass
93+
94+
assert_raises(AttributeError, lambda: MyClass()._)
95+
96+
9097
def test_set_dict_attr():
9198
class MyClass(object):
9299
def __init__(self):

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectLookupAttr.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ static Object readAttributeQuickly(Object type, TpSlots slots, Object receiver,
335335
PythonAbstractClass base = bases[0];
336336
if (base instanceof PythonBuiltinClass &&
337337
((PythonBuiltinClass) base).getType() == PythonBuiltinClassType.PythonObject) {
338-
if (!(codePointAtIndexNode.execute(stringName, 0) == '_' && codePointAtIndexNode.execute(stringName, 1) == '_')) {
338+
int length = codePointLengthNode.execute(stringName, TS_ENCODING);
339+
if (!(length >= 2 && codePointAtIndexNode.execute(stringName, 0) == '_' && codePointAtIndexNode.execute(stringName, 1) == '_')) {
339340
// not a special name, so this attribute cannot be inherited, and can
340341
// only be on the type or the object. If it's on the type, return to
341342
// the generic code.

0 commit comments

Comments
 (0)