Skip to content

Commit cf81ad0

Browse files
committed
[GR-13225] [GR-10752] Implement PEP 553 breakpoint() message
PullRequest: graalpython/391
2 parents 243eaf0 + 0afb8f7 commit cf81ad0

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127

128128
local labsjdk8Mixin = {
129129
downloads +: {
130-
JAVA_HOME: utils.download("labsjdk", "8u172-jvmci-0.48"),
130+
JAVA_HOME: utils.download("labsjdk", "8u192-jvmci-0.54"),
131131
EXTRA_JAVA_HOMES : { pathlist: [utils.download("oraclejdk", "11+20")] },
132132
},
133133
environment +: {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
import static com.oracle.graal.python.nodes.BuiltinNames.ROUND;
5656
import static com.oracle.graal.python.nodes.BuiltinNames.SETATTR;
5757
import static com.oracle.graal.python.nodes.BuiltinNames.SUM;
58-
import static com.oracle.graal.python.nodes.BuiltinNames.__BREAKPOINT__;
58+
import static com.oracle.graal.python.nodes.BuiltinNames.BREAKPOINT;
59+
import static com.oracle.graal.python.nodes.BuiltinNames.BREAKPOINTHOOK;
5960
import static com.oracle.graal.python.nodes.BuiltinNames.__BUILTIN__;
6061
import static com.oracle.graal.python.nodes.BuiltinNames.__DEBUG__;
6162
import static com.oracle.graal.python.nodes.BuiltinNames.__DUMP_TRUFFLE_AST__;
@@ -93,6 +94,7 @@
9394
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
9495
import com.oracle.graal.python.builtins.objects.code.PCode;
9596
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
97+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
9698
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
9799
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
98100
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
@@ -168,6 +170,7 @@
168170
import com.oracle.truffle.api.CompilerDirectives;
169171
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
170172
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
173+
import com.oracle.truffle.api.debug.Debugger;
171174
import com.oracle.truffle.api.Truffle;
172175
import com.oracle.truffle.api.dsl.Cached;
173176
import com.oracle.truffle.api.dsl.Fallback;
@@ -1501,12 +1504,33 @@ public Object setAttr(Object object, Object key, Object value,
15011504
}
15021505
}
15031506

1504-
@Builtin(name = __BREAKPOINT__, fixedNumOfPositionalArgs = 0)
1507+
@Builtin(name = BREAKPOINT, takesVarArgs = true, takesVarKeywordArgs = true)
15051508
@GenerateNodeFactory
15061509
public abstract static class BreakPointNode extends PythonBuiltinNode {
1510+
@Child HashingStorageNodes.GetItemNode getSysModuleNode;
1511+
@Child ReadAttributeFromObjectNode getBreakpointhookNode;
1512+
@Child CallNode callNode;
1513+
15071514
@Specialization
1508-
public Object doIt() {
1509-
return PNone.NONE;
1515+
public Object doIt(VirtualFrame frame, Object[] args, PKeyword[] kwargs) {
1516+
if (Debugger.find(getContext().getEnv()).getSessionCount() > 0) {
1517+
// we already have a Truffle debugger attached, it'll stop here
1518+
return PNone.NONE;
1519+
} else {
1520+
if (getSysModuleNode == null) {
1521+
CompilerDirectives.transferToInterpreterAndInvalidate();
1522+
getSysModuleNode = insert(HashingStorageNodes.GetItemNode.create());
1523+
getBreakpointhookNode = insert(ReadAttributeFromObjectNode.create());
1524+
callNode = insert(CallNode.create());
1525+
}
1526+
PDict sysModules = getContext().getSysModules();
1527+
Object sysModule = getSysModuleNode.execute(sysModules.getDictStorage(), "sys");
1528+
Object breakpointhook = getBreakpointhookNode.execute(sysModule, BREAKPOINTHOOK);
1529+
if (breakpointhook == PNone.NO_VALUE) {
1530+
throw raise(PythonBuiltinClassType.RuntimeError, "lost sys.breakpointhook");
1531+
}
1532+
return callNode.execute(frame, breakpointhook, args, kwargs);
1533+
}
15101534
}
15111535
}
15121536

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/BuiltinNames.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -45,17 +45,18 @@ public abstract class BuiltinNames {
4545
public static final String SELF = "self";
4646

4747
// graalpython internals
48-
public static final String __BREAKPOINT__ = "__breakpoint__";
4948
public static final String __BUILTINS_PATCHES__ = "__builtins_patches__";
5049

5150
// cpython internals
51+
public static final String BREAKPOINT = "breakpoint";
5252
public static final String MODULE = "module";
5353
public static final String __BUILD_CLASS__ = "__build_class__";
5454
public static final String __MAIN__ = "__main__";
5555
public static final String __BUILTINS__ = "__builtins__";
5656
public static final String __DEBUG__ = "__debug__";
5757

5858
// sys
59+
public static final String BREAKPOINTHOOK = "breakpointhook";
5960
public static final String EXCEPTHOOK = "excepthook";
6061
public static final String LAST_TYPE = "last_type";
6162
public static final String LAST_VALUE = "last_value";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/PythonCallNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,6 @@ private static boolean isCall(Class<?> tag) {
272272
}
273273

274274
private boolean isBreakpoint(Class<?> tag) {
275-
return tag == DebuggerTags.AlwaysHalt.class && calleeName.equals(BuiltinNames.__BREAKPOINT__);
275+
return tag == DebuggerTags.AlwaysHalt.class && calleeName.equals(BuiltinNames.BREAKPOINT);
276276
}
277277
}

graalpython/lib-graalpython/sys.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,32 @@ def __print_traceback__(typ, value, tb):
175175
del make_excepthook
176176

177177

178+
@__builtin__
179+
def breakpointhook(*args, **kws):
180+
import importlib, os, warnings
181+
hookname = os.getenv('PYTHONBREAKPOINT')
182+
if hookname is None or len(hookname) == 0:
183+
warnings.warn('Graal Python cannot run pdb, yet, consider using `--inspect` on the commandline', RuntimeWarning)
184+
hookname = 'pdb.set_trace'
185+
elif hookname == '0':
186+
return None
187+
modname, dot, funcname = hookname.rpartition('.')
188+
if dot == '':
189+
modname = 'builtins'
190+
try:
191+
module = importlib.import_module(modname)
192+
hook = getattr(module, funcname)
193+
except:
194+
warnings.warn(
195+
'Ignoring unimportable $PYTHONBREAKPOINT: {}'.format(
196+
hookname),
197+
RuntimeWarning)
198+
return hook(*args, **kws)
199+
200+
201+
__breakpointhook__ = breakpointhook
202+
203+
178204
@__builtin__
179205
def getrecursionlimit():
180206
return 1000

0 commit comments

Comments
 (0)