[Bug]: `getattr()` on an instance never hashes a `str` subclass name
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 50/100
Piste de recherche
Start in ObjectBuiltins.java at GetAttributeNode and its doIt method, where the name is converted to a TruffleString before the MRO lookup and the instance read. The comment above doIt lists the nodes that must stay in sync, and object.__setattr__ is named too. Run the four expressions in the table against both GraalPy and CPython, since the issue gives the expected results for each. Done when getattr, hasattr and setattr on a str subclass name propagate a __hash__ error the way CPython does, and the getattr(1, ...) row still returns the CPython result.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Describe the bug
getattr(), hasattr() and setattr() on an ordinary instance convert a str subclass attribute name to a plain string before the lookup, so the name's __hash__ is never called. CPython looks the name object itself up in the instance dict, which hashes it and propagates an error from __hash__.
Operating system
macOS
CPU architecture
ARM64
GraalPy version
master 3289197e63 (Python 3.13.14), built with mx python-jvm
JDK version
No response
Context configuration
No response
Steps to reproduce
class RaisingHash(str):
def __hash__(self):
raise KeyError('hash')
class C:
pass
| Expression | GraalPy | CPython |
|---|---|---|
getattr(C(), RaisingHash('x'), None) |
None |
KeyError: 'hash' |
hasattr(C(), RaisingHash('x')) |
False |
KeyError: 'hash' |
setattr(C(), RaisingHash('x'), 1) |
None |
AttributeError: 'C' object has no attribute 'x' |
getattr(1, RaisingHash('real'), None) |
1 |
None |
Expected behavior
The CPython column above (3.12.13 and 3.13.14).
Stack trace
Additional context
Root Cause
ObjectBuiltins.GetAttributeNode converts the name to a TruffleString first (ObjectBuiltins.java#L542) and uses that string for the MRO lookup and for the read from the object (#L545-L560), so the __hash__ of a str subclass is never called. CPython looks the name object itself up in the instance dict (object.c#L1488), which hashes it and propagates the error; its type lookup hashes it too but treats a failure as a miss, which is why the last row finds nothing on an object without a dict.
Fix Suggestion
In GetAttributeNode.doIt, keep the string path for an exact str and, for a str subclass, look the instance storage up with the original name object through the ordinary dict lookup, treating a __hash__ failure during the type lookup as a miss. The nodes that the comment above doIt lists as kept in sync, and object.__setattr__, convert the name the same way.
- Langage dominant
- Python
- Étoiles
- 1.7k
- Forks
- 167
- Merge moyen
- 3 j 11 h
- PR mergées (30 j)
- 68
Préparer son environnement
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de oracle/graalpython
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
oracle/graalpython#1105 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
oracle/graalpython#1102 ·
Les mainteneurs répondent en général sous 1 jour
-
mmap.mmap.move() is missingPeut-être pris @deronnax l’a pris il y a 4 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 62/100
oracle/graalpython#1368 ·
Les mainteneurs répondent en général sous 1 jour
-
mmap extended slicing ignores the step; negative steps raise java.lang.IndexOutOfBoundsExceptionOuverte
Difficulté 3/5 1-2 jours Accessibilité débutants 64/100
oracle/graalpython#1367 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 4/5 3-5 jours Accessibilité débutants 22/100
oracle/graalpython#1366 ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de oracle/graalpython
Issues similaires
-
Difficulté 1/5 1-3 heures Accessibilité débutants 78/100
Bforartists/Manual#1916 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 72/100
JdeRobot/PerceptionMetrics#698 ·
Les mainteneurs répondent en général sous 6 jours
-
good first issue help wanted
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
rootsiemen/seo-agent-tools#70 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
stephrobert/dsoxlab#413 ·
Les mainteneurs répondent en général sous 1 jour
-
format:line_indent keeps the second of two consecutive blank lines, so valid stairs failPeut-être pris @albertlieyingadrian l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 83/100