260260import com .oracle .truffle .api .profiles .BranchProfile ;
261261import com .oracle .truffle .api .profiles .ConditionProfile ;
262262import com .oracle .truffle .api .profiles .ValueProfile ;
263+ import com .oracle .graal .python .builtins .objects .str .StringBuiltins .IsIdentifierNode ;
263264
264265@ CoreFunctions (defineModule = BuiltinNames .BUILTINS )
265266public final class BuiltinConstructors extends PythonBuiltins {
@@ -2214,6 +2215,7 @@ Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict
22142215 @ Cached GetItemsizeNode getItemSize ,
22152216 @ Cached WriteAttributeToObjectNode writeItemSize ,
22162217 @ Cached GetBestBaseClassNode getBestBaseNode ,
2218+ @ Cached IsIdentifierNode isIdentifier ,
22172219 @ Cached DictBuiltins .CopyNode copyDict ) {
22182220 // Determine the proper metatype to deal with this
22192221 String name = castStr .execute (wName );
@@ -2230,7 +2232,8 @@ Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict
22302232
22312233 try {
22322234 PDict namespace = (PDict ) copyDict .call (frame , namespaceOrig );
2233- PythonClass newType = typeMetaclass (frame , name , bases , namespace , metaclass , lib , hashingStoragelib , getDictAttrNode , getWeakRefAttrNode , getBestBaseNode , getItemSize , writeItemSize );
2235+ PythonClass newType = typeMetaclass (frame , name , bases , namespace , metaclass , lib , hashingStoragelib , getDictAttrNode , getWeakRefAttrNode , getBestBaseNode , getItemSize , writeItemSize ,
2236+ isIdentifier );
22342237
22352238 for (DictEntry entry : hashingStoragelib .entries (namespace .getDictStorage ())) {
22362239 Object setName = getSetNameNode .execute (entry .value );
@@ -2326,7 +2329,8 @@ private String getModuleNameFromGlobals(PythonObject globals, HashingStorageLibr
23262329
23272330 private PythonClass typeMetaclass (VirtualFrame frame , String name , PTuple bases , PDict namespace , Object metaclass ,
23282331 PythonObjectLibrary lib , HashingStorageLibrary hashingStorageLib , LookupAttributeInMRONode getDictAttrNode ,
2329- LookupAttributeInMRONode getWeakRefAttrNode , GetBestBaseClassNode getBestBaseNode , GetItemsizeNode getItemSize , WriteAttributeToObjectNode writeItemSize ) {
2332+ LookupAttributeInMRONode getWeakRefAttrNode , GetBestBaseClassNode getBestBaseNode , GetItemsizeNode getItemSize , WriteAttributeToObjectNode writeItemSize ,
2333+ IsIdentifierNode isIdentifier ) {
23302334 Object [] array = ensureGetObjectArrayNode ().execute (bases );
23312335
23322336 PythonAbstractClass [] basesArray ;
@@ -2422,6 +2426,9 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
24222426 // Check valid slot name
24232427 if (element instanceof String ) {
24242428 slotName = (String ) element ;
2429+ if (!(boolean ) isIdentifier .call (frame , slotName )) {
2430+ throw raise (TypeError , ErrorMessages .SLOTS_MUST_BE_IDENTIFIERS );
2431+ }
24252432 } else {
24262433 throw raise (TypeError , ErrorMessages .MUST_BE_STRINGS_NOT_P , "__slots__ items" , element );
24272434 }
@@ -2638,7 +2645,7 @@ private String mangle(String privateobj, String ident) {
26382645 // Name mangling: __private becomes _classname__private. This is independent from how
26392646 // the name is used.
26402647 int nlen , plen , ipriv ;
2641- if (privateobj == null || ident .charAt (0 ) != '_' || ident .charAt (1 ) != '_' ) {
2648+ if (privateobj == null || ident .equals ( "_" ) || ident . charAt (0 ) != '_' || ident .charAt (1 ) != '_' ) {
26422649 return ident ;
26432650 }
26442651 nlen = ident .length ();
0 commit comments