Skip to content

Commit 06c2eb0

Browse files
author
Franziska Geiger
committed
[GR-17332] Dict contains returns false in case of wrapped PString types
PullRequest: graalpython/595
2 parents 33d2de5 + bb91b89 commit 06c2eb0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,39 @@ def test_unhashable_key():
457457
assert_raises(TypeError, lambda: d[key_list])
458458
key_tuple_list = (key_list, 2)
459459
assert_raises(TypeError, lambda: d[key_tuple_list])
460+
461+
class EncodedString(str):
462+
# unicode string subclass to keep track of the original encoding.
463+
# 'encoding' is None for unicode strings and the source encoding
464+
# otherwise
465+
encoding = None
466+
467+
def __deepcopy__(self, memo):
468+
return self
469+
470+
def byteencode(self):
471+
assert self.encoding is not None
472+
return self.encode(self.encoding)
473+
474+
def utf8encode(self):
475+
assert self.encoding is None
476+
return self.encode("UTF-8")
477+
478+
@property
479+
def is_unicode(self):
480+
return self.encoding is None
481+
482+
def contains_surrogates(self):
483+
return string_contains_surrogates(self)
484+
485+
def as_utf8_string(self):
486+
return bytes_literal(self.utf8encode(), 'utf8')
487+
488+
489+
def test_wrapped_string_contains():
490+
testString = EncodedString('something')
491+
dict = {'something': (1, 0), 'nothing': (2, 0)}
492+
reached = False
493+
if testString in dict:
494+
reached = True
495+
assert reached

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/HashingStorageNodes.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import com.oracle.graal.python.nodes.PNodeWithGlobalState.DefaultContextManager;
8686
import com.oracle.graal.python.nodes.PRaiseNode;
8787
import com.oracle.graal.python.nodes.SpecialMethodNames;
88+
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
8889
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
8990
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromDynamicObjectNode;
9091
import com.oracle.graal.python.nodes.builtins.ListNodes.FastConstructListNode;
@@ -591,6 +592,19 @@ protected boolean readUncached(VirtualFrame frame, PythonObjectHybridDictStorage
591592
}
592593

593594
@Specialization(guards = "!isJavaString(name)")
595+
protected boolean readUncached(VirtualFrame frame, DynamicObjectStorage storage, PString name,
596+
@Cached LookupInheritedAttributeNode.Dynamic lookupHash,
597+
@Cached LookupAttributeInMRONode.Dynamic lookupStringHash,
598+
@Cached ContainsKeyNode recursiveNode) {
599+
if (lookupHash.execute(name, __HASH__) == lookupStringHash.execute(PythonBuiltinClassType.PString, __HASH__)) {
600+
return recursiveNode.execute(frame, storage, name.getValue());
601+
}
602+
CompilerDirectives.transferToInterpreter();
603+
// see GR-17389
604+
throw new RuntimeException("String subclasses with custom hash in dict not implemented.");
605+
}
606+
607+
@Specialization(guards = {"!isJavaString(name)", "!isPString(name)"})
594608
@SuppressWarnings("unused")
595609
protected boolean readUncached(DynamicObjectStorage storage, Object name) {
596610
return false;

0 commit comments

Comments
 (0)