Skip to content

Commit ce83577

Browse files
committed
SysModuleBuiltins add get/setswitchinterval builtins
1 parent f10fb7e commit ce83577

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import static com.oracle.graal.python.nodes.ErrorMessages.ARG_TYPE_MUST_BE;
8585
import static com.oracle.graal.python.nodes.ErrorMessages.LOST_S;
8686
import static com.oracle.graal.python.nodes.ErrorMessages.REC_LIMIT_GREATER_THAN_1;
87+
import static com.oracle.graal.python.nodes.ErrorMessages.SWITCH_INTERVAL_MUST_BE_POSITIVE;
8788
import static com.oracle.graal.python.nodes.ErrorMessages.S_EXPECTED_GOT_P;
8889
import static com.oracle.graal.python.nodes.ErrorMessages.WARN_CANNOT_RUN_PDB_YET;
8990
import static com.oracle.graal.python.nodes.ErrorMessages.WARN_DEPRECTATED_SYS_CHECKINTERVAL;
@@ -103,6 +104,7 @@
103104
import java.util.Map;
104105
import java.util.Set;
105106

107+
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
106108
import com.oracle.graal.python.lib.PyFloatCheckExactNode;
107109
import com.oracle.graal.python.lib.PyLongAsIntNode;
108110
import org.graalvm.nativeimage.ImageInfo;
@@ -1483,4 +1485,50 @@ Object setCheckInterval(VirtualFrame frame, @SuppressWarnings("unused") PythonMo
14831485
return PNone.NONE;
14841486
}
14851487
}
1488+
1489+
@Builtin(name = "getswitchinterval", minNumOfPositionalArgs = 1, declaresExplicitSelf = true, doc = "getswitchinterval($module, /)\n" +
1490+
"--\n" +
1491+
"\n" +
1492+
"Return the current thread switch interval; see sys.setswitchinterval().")
1493+
@GenerateNodeFactory
1494+
abstract static class GetSwitchIntervalNode extends PythonBuiltinNode {
1495+
private final static double FACTOR = 1.e-6;
1496+
1497+
@Specialization
1498+
Object getCheckInterval(VirtualFrame frame, @SuppressWarnings("unused") PythonModule sys) {
1499+
return FACTOR * getContext().getSysModuleState().getSwitchInterval();
1500+
}
1501+
}
1502+
1503+
@Builtin(name = "setswitchinterval", minNumOfPositionalArgs = 2, declaresExplicitSelf = true, doc = "setswitchinterval($module, interval, /)\n" +
1504+
"--\n" +
1505+
"\n" +
1506+
"Set the ideal thread switching delay inside the Python interpreter.\n" +
1507+
"\n" +
1508+
"The actual frequency of switching threads can be lower if the\n" +
1509+
"interpreter executes long sequences of uninterruptible code" +
1510+
"(this is implementation-specific and workload-dependent).\n" +
1511+
"\n" +
1512+
"The parameter must represent the desired switching delay in seconds\n" +
1513+
"A typical value is 0.005 (5 milliseconds).")
1514+
@GenerateNodeFactory
1515+
abstract static class SetSwitchIntervalNode extends PythonBuiltinNode {
1516+
private final static double FACTOR = 1.e6;
1517+
1518+
@Specialization
1519+
Object setCheckInterval(VirtualFrame frame, @SuppressWarnings("unused") PythonModule sys, Object arg,
1520+
@Cached PyFloatAsDoubleNode floatAsDoubleNode,
1521+
@Cached PyFloatCheckExactNode floatCheckExactNode) {
1522+
try {
1523+
double interval = floatAsDoubleNode.execute(frame, arg);
1524+
if (interval <= 0.0) {
1525+
throw raise(ValueError, SWITCH_INTERVAL_MUST_BE_POSITIVE);
1526+
}
1527+
getContext().getSysModuleState().setSwitchInterval(FACTOR * interval);
1528+
} catch (PException ignore) {
1529+
1530+
}
1531+
return PNone.NONE;
1532+
}
1533+
}
14861534
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ public abstract class ErrorMessages {
618618
public static final String THROW_THIRD_ARG_MUST_BE_TRACEBACK = "throw() third argument must be a traceback object";
619619
public static final String TDATAOBJECT_SHOULD_NOT_HAVE_MORE_LINKS = "_tee_dataobject should not have more than %s links";
620620
public static final String TDATAOBJECT_SHOULDNT_HAVE_NEXT = "_tee_dataobject shouldn't have a next if not full";
621+
public static final String SWITCH_INTERVAL_MUST_BE_POSITIVE = "switch interval must be strictly positive";
621622
public static final String TIMED_OUT = "timed out";
622623
public static final String TIMEOUT_VALUE_MUST_BE_POSITIVE = "timeout value must be positive";
623624
public static final String TIMEOUT_VALUE_OUT_OF_RANGE = "Timeout value out of range";

graalpython/lib-graalpython/sys.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,3 @@ def exit(arg=None):
5757
if isinstance(arg, tuple) and len(arg) == 1:
5858
code = arg[0]
5959
raise SystemExit(code)
60-
61-
@__graalpython__.builtin
62-
def getswitchinterval():
63-
return __graalpython__.sys_state.switchinterval
64-
65-
@__graalpython__.builtin
66-
def setswitchinterval(value):
67-
if not isinstance(value, (int, float)):
68-
raise TypeError("must be real number, not str")
69-
if value <= 0:
70-
raise ValueError("switch interval must be strictly positive")
71-
__graalpython__.sys_state.switchinterval = value

0 commit comments

Comments
 (0)