71
71
import static com .oracle .graal .python .nodes .BuiltinNames .DISPLAYHOOK ;
72
72
import static com .oracle .graal .python .nodes .BuiltinNames .EXCEPTHOOK ;
73
73
import static com .oracle .graal .python .nodes .BuiltinNames .EXIT ;
74
+ import static com .oracle .graal .python .nodes .BuiltinNames .MODULES ;
74
75
import static com .oracle .graal .python .nodes .BuiltinNames .PYTHONBREAKPOINT ;
75
76
import static com .oracle .graal .python .nodes .BuiltinNames .STDERR ;
76
77
import static com .oracle .graal .python .nodes .BuiltinNames .STDIN ;
106
107
import java .util .Map ;
107
108
import java .util .Set ;
108
109
109
- import com .oracle .graal .python .lib .PyTraceBackPrintNode ;
110
- import com .oracle .graal .python .nodes .subscript .GetItemNode ;
110
+ import com .oracle .graal .python .nodes .util .CannotCastException ;
111
111
import org .graalvm .nativeimage .ImageInfo ;
112
112
113
113
import com .oracle .graal .python .PythonLanguage ;
148
148
import com .oracle .graal .python .builtins .objects .tuple .TupleBuiltins ;
149
149
import com .oracle .graal .python .lib .PyFloatAsDoubleNode ;
150
150
import com .oracle .graal .python .lib .PyFloatCheckExactNode ;
151
+ import com .oracle .graal .python .lib .PyImportImport ;
151
152
import com .oracle .graal .python .lib .PyLongAsIntNode ;
152
153
import com .oracle .graal .python .lib .PyNumberAsSizeNode ;
153
154
import com .oracle .graal .python .lib .PyObjectCallMethodObjArgs ;
156
157
import com .oracle .graal .python .lib .PyObjectReprAsObjectNode ;
157
158
import com .oracle .graal .python .lib .PyObjectSetAttr ;
158
159
import com .oracle .graal .python .lib .PyObjectStrAsObjectNode ;
160
+ import com .oracle .graal .python .lib .PyTraceBackPrintNode ;
159
161
import com .oracle .graal .python .lib .PyUnicodeAsEncodedString ;
160
162
import com .oracle .graal .python .lib .PyUnicodeFromEncodedObject ;
161
163
import com .oracle .graal .python .nodes .ErrorMessages ;
171
173
import com .oracle .graal .python .nodes .function .builtins .clinic .ArgumentClinicProvider ;
172
174
import com .oracle .graal .python .nodes .object .GetClassNode ;
173
175
import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
174
- import com .oracle .graal .python .nodes .statement .ImportNode ;
175
176
import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
176
177
import com .oracle .graal .python .nodes .util .ExceptionStateNodes .GetCaughtExceptionNode ;
177
178
import com .oracle .graal .python .runtime .PosixSupportLibrary ;
@@ -394,7 +395,7 @@ public void initialize(Python3Core core) {
394
395
builtinConstants .put ("byteorder" , ByteOrder .nativeOrder () == ByteOrder .LITTLE_ENDIAN ? "little" : "big" );
395
396
builtinConstants .put ("copyright" , LICENSE );
396
397
final PythonObjectFactory factory = PythonObjectFactory .getUncached ();
397
- builtinConstants .put ("modules" , factory .createDict ());
398
+ builtinConstants .put (MODULES , factory .createDict ());
398
399
builtinConstants .put ("path" , factory .createList ());
399
400
builtinConstants .put ("builtin_module_names" , factory .createTuple (core .builtinModuleNames ()));
400
401
builtinConstants .put ("maxsize" , MAXSIZE );
@@ -1356,48 +1357,34 @@ abstract static class BreakpointHookNode extends PythonBuiltinNode {
1356
1357
static final String VAL_PDB_SETTRACE = "pdb.set_trace" ;
1357
1358
static final String MOD_OS = "os" ;
1358
1359
static final String ATTR_ENVIRON = "environ" ;
1360
+ static final String METH_GET = "get" ;
1359
1361
1360
- @ Child private ImportNode .ImportExpression importOsNode ;
1361
- @ Child private ImportNode .ImportExpression importNode ;
1362
-
1363
- private ImportNode .ImportExpression ensureImportNode (String name ) {
1364
- if (importNode == null ) {
1365
- CompilerDirectives .transferToInterpreterAndInvalidate ();
1366
- importNode = insert (ImportNode .createAsExpression (name ));
1367
- }
1368
- return importNode ;
1369
- }
1370
-
1371
- private Object importOs (VirtualFrame frame ) {
1372
- if (importOsNode == null ) {
1373
- CompilerDirectives .transferToInterpreterAndInvalidate ();
1374
- importOsNode = insert (ImportNode .createAsExpression (MOD_OS ));
1375
- }
1376
- return importOsNode .execute (frame );
1377
- }
1378
-
1379
- private Object getEnv (VirtualFrame frame , PyObjectGetAttr getAttr , GetItemNode getItemNode , String name ) {
1380
- Object os = importOs (frame );
1362
+ private String getEnvVar (VirtualFrame frame , PyImportImport importNode , PyObjectGetAttr getAttr , PyObjectCallMethodObjArgs callMethodObjArgs ,
1363
+ CastToJavaStringNode castToJavaStringNode ) {
1364
+ Object os = importNode .execute (frame , MOD_OS );
1381
1365
final Object environ = getAttr .execute (frame , os , ATTR_ENVIRON );
1382
- return getItemNode .execute (frame , environ , name );
1366
+ Object var = callMethodObjArgs .execute (frame , environ , METH_GET , PYTHONBREAKPOINT );
1367
+ try {
1368
+ return castToString (var , castToJavaStringNode );
1369
+ } catch (CannotCastException cce ) {
1370
+ return null ;
1371
+ }
1383
1372
}
1384
1373
1385
1374
@ Specialization
1386
1375
Object doHook (VirtualFrame frame , Object [] args , PKeyword [] keywords ,
1387
1376
@ Cached CallNode callNode ,
1388
1377
@ Cached PyObjectGetAttr getAttr ,
1389
- @ Cached GetItemNode getItemNode ,
1390
- @ Cached IsBuiltinClassProfile importErrorProfile ,
1378
+ @ Cached PyImportImport importNode ,
1379
+ @ Cached PyObjectCallMethodObjArgs callMethodObjArgs ,
1391
1380
@ Cached IsBuiltinClassProfile attrErrorProfile ,
1392
1381
@ Cached CastToJavaStringNode castToJavaStringNode ,
1382
+ @ Cached BuiltinFunctions .IsInstanceNode isInstanceNode ,
1393
1383
@ Cached WarningsModuleBuiltins .WarnNode warnNode ) {
1394
- Object v = getEnv (frame , getAttr , getItemNode , PYTHONBREAKPOINT );
1395
- final String hookName ;
1396
- if (v == PNone .NO_VALUE ) {
1384
+ String hookName = getEnvVar (frame , importNode , getAttr , callMethodObjArgs , castToJavaStringNode );
1385
+ if (hookName == null || hookName .length () == 0 ) {
1397
1386
warnNode .warnFormat (frame , RuntimeWarning , WARN_CANNOT_RUN_PDB_YET );
1398
1387
hookName = VAL_PDB_SETTRACE ;
1399
- } else {
1400
- hookName = castToString (v , castToJavaStringNode );
1401
1388
}
1402
1389
1403
1390
if (hookName .length () == 1 && hookName .charAt (0 ) == '0' ) {
@@ -1406,33 +1393,38 @@ Object doHook(VirtualFrame frame, Object[] args, PKeyword[] keywords,
1406
1393
}
1407
1394
1408
1395
final int lastDot = PythonUtils .lastIndexOf (hookName , '.' );
1409
- final String modName ;
1396
+ final String modPath ;
1410
1397
final String attrName ;
1411
1398
if (lastDot == -1 ) {
1412
1399
// The breakpoint is a built-in, e.g. PYTHONBREAKPOINT=int
1413
- modName = BUILTINS ;
1400
+ modPath = BUILTINS ;
1414
1401
attrName = hookName ;
1415
- } else {
1402
+ } else if ( lastDot != 0 ) {
1416
1403
// Split on the last dot
1417
- modName = PythonUtils .substring (hookName , 0 , lastDot );
1404
+ modPath = PythonUtils .substring (hookName , 0 , lastDot );
1418
1405
attrName = PythonUtils .substring (hookName , lastDot + 1 );
1406
+ } else {
1407
+ warnNode .warnFormat (frame , RuntimeWarning , WARN_IGNORE_UNIMPORTABLE_BREAKPOINT_S , hookName );
1408
+ return PNone .NONE ;
1419
1409
}
1420
1410
1421
1411
final Object module ;
1422
1412
try {
1423
- module = ensureImportNode ( modName ) .execute (frame );
1413
+ module = importNode .execute (frame , modPath );
1424
1414
} catch (PException pe ) {
1425
- pe .expect (ImportError , importErrorProfile );
1426
- warnNode .warnFormat (frame , RuntimeWarning , WARN_IGNORE_UNIMPORTABLE_BREAKPOINT_S , hookName );
1415
+ if (isInstanceNode .executeWith (frame , pe .getUnreifiedException (), ImportError )) {
1416
+ warnNode .warnFormat (frame , RuntimeWarning , WARN_IGNORE_UNIMPORTABLE_BREAKPOINT_S , hookName );
1417
+ }
1427
1418
return PNone .NONE ;
1428
1419
}
1429
1420
1430
1421
final Object hook ;
1431
1422
try {
1432
1423
hook = getAttr .execute (frame , module , attrName );
1433
1424
} catch (PException pe ) {
1434
- pe .expect (AttributeError , attrErrorProfile );
1435
- warnNode .warnFormat (frame , RuntimeWarning , WARN_IGNORE_UNIMPORTABLE_BREAKPOINT_S , hookName );
1425
+ if (attrErrorProfile .profileException (pe , AttributeError )) {
1426
+ warnNode .warnFormat (frame , RuntimeWarning , WARN_IGNORE_UNIMPORTABLE_BREAKPOINT_S , hookName );
1427
+ }
1436
1428
return PNone .NONE ;
1437
1429
}
1438
1430
0 commit comments