260
260
import com .oracle .truffle .api .profiles .BranchProfile ;
261
261
import com .oracle .truffle .api .profiles .ConditionProfile ;
262
262
import com .oracle .truffle .api .profiles .ValueProfile ;
263
+ import com .oracle .graal .python .builtins .objects .str .StringBuiltins .IsIdentifierNode ;
263
264
264
265
@ CoreFunctions (defineModule = BuiltinNames .BUILTINS )
265
266
public final class BuiltinConstructors extends PythonBuiltins {
@@ -2214,6 +2215,7 @@ Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict
2214
2215
@ Cached GetItemsizeNode getItemSize ,
2215
2216
@ Cached WriteAttributeToObjectNode writeItemSize ,
2216
2217
@ Cached GetBestBaseClassNode getBestBaseNode ,
2218
+ @ Cached IsIdentifierNode isIdentifier ,
2217
2219
@ Cached DictBuiltins .CopyNode copyDict ) {
2218
2220
// Determine the proper metatype to deal with this
2219
2221
String name = castStr .execute (wName );
@@ -2230,7 +2232,8 @@ Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict
2230
2232
2231
2233
try {
2232
2234
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 );
2234
2237
2235
2238
for (DictEntry entry : hashingStoragelib .entries (namespace .getDictStorage ())) {
2236
2239
Object setName = getSetNameNode .execute (entry .value );
@@ -2326,7 +2329,8 @@ private String getModuleNameFromGlobals(PythonObject globals, HashingStorageLibr
2326
2329
2327
2330
private PythonClass typeMetaclass (VirtualFrame frame , String name , PTuple bases , PDict namespace , Object metaclass ,
2328
2331
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 ) {
2330
2334
Object [] array = ensureGetObjectArrayNode ().execute (bases );
2331
2335
2332
2336
PythonAbstractClass [] basesArray ;
@@ -2422,6 +2426,9 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
2422
2426
// Check valid slot name
2423
2427
if (element instanceof String ) {
2424
2428
slotName = (String ) element ;
2429
+ if (!(boolean ) isIdentifier .call (frame , slotName )) {
2430
+ throw raise (TypeError , ErrorMessages .SLOTS_MUST_BE_IDENTIFIERS );
2431
+ }
2425
2432
} else {
2426
2433
throw raise (TypeError , ErrorMessages .MUST_BE_STRINGS_NOT_P , "__slots__ items" , element );
2427
2434
}
@@ -2638,7 +2645,7 @@ private String mangle(String privateobj, String ident) {
2638
2645
// Name mangling: __private becomes _classname__private. This is independent from how
2639
2646
// the name is used.
2640
2647
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 ) != '_' ) {
2642
2649
return ident ;
2643
2650
}
2644
2651
nlen = ident .length ();
0 commit comments