|
4 | 4 | Date and Time |
5 | 5 | """ |
6 | 6 |
|
7 | | -import time |
8 | 7 | from datetime import datetime, timedelta |
9 | 8 | import dateutil.parser |
10 | 9 | import re |
| 10 | +import sys |
| 11 | +import time |
11 | 12 |
|
12 | 13 | from mathics.version import __version__ # noqa used in loading to check consistency. |
13 | 14 |
|
@@ -126,62 +127,63 @@ def apply(self, evaluation): |
126 | 127 | return SymbolInfinity |
127 | 128 |
|
128 | 129 |
|
129 | | -class TimeConstrained(Builtin): |
130 | | - """ |
131 | | - <dl> |
132 | | - <dt>'TimeConstrained[$expr$, $t$]' |
133 | | - <dd>'evaluates $expr$, stopping after $t$ seconds.' |
134 | | - <dt>'TimeConstrained[$expr$, $t$, $failexpr$]' |
135 | | - <dd>'returns $failexpr$ if the time constraint is not met.' |
136 | | - </dl> |
137 | | - >> TimeConstrained[Integrate[Sin[x]^1000000,x],1] |
138 | | - = $Aborted |
139 | | -
|
140 | | - >> TimeConstrained[Integrate[Sin[x]^1000000,x], 1, Integrate[Cos[x],x]] |
141 | | - = Sin[x] |
142 | | -
|
143 | | - >> s=TimeConstrained[Integrate[Sin[x] ^ 3, x], a] |
144 | | - : Number of seconds a is not a positive machine-sized number or Infinity. |
145 | | - = TimeConstrained[Integrate[Sin[x] ^ 3, x], a] |
146 | | -
|
147 | | - >> a=1; s |
148 | | - = -Cos[x] + Cos[x] ^ 3 / 3 |
149 | | -
|
150 | | - Possible issues: for certain time-consuming functions (like simplify) |
151 | | - which are based on sympy or other libraries, it is possible that |
152 | | - the evaluation continues after the timeout. However, at the end of the evaluation, the function will return $\\$Aborted$ and the results will not affect |
153 | | - the state of the mathics kernel. |
154 | | -
|
155 | | - """ |
156 | | - |
157 | | - attributes = ('HoldAll',) |
158 | | - messages = { |
159 | | - 'timc': 'Number of seconds `1` is not a positive machine-sized number or Infinity.', |
160 | | - } |
161 | | - |
162 | | - def apply_2(self, expr, t, evaluation): |
163 | | - 'TimeConstrained[expr_, t_]' |
164 | | - return self.apply_3(expr, t, SymbolAborted, evaluation) |
165 | | - |
166 | | - def apply_3(self, expr, t, failexpr, evaluation): |
167 | | - 'TimeConstrained[expr_, t_, failexpr_]' |
168 | | - t = t.evaluate(evaluation) |
169 | | - if not t.is_numeric(): |
170 | | - evaluation.message('TimeConstrained', 'timc', t) |
171 | | - return |
172 | | - try: |
173 | | - t = float(t.to_python()) |
174 | | - evaluation.timeout_queue.append((t, datetime.now().timestamp())) |
175 | | - request = lambda : expr.evaluate(evaluation) |
176 | | - res = run_with_timeout_and_stack(request, t, evaluation) |
177 | | - except TimeoutInterrupt: |
178 | | - evaluation.timeout_queue.pop() |
179 | | - return failexpr.evaluate(evaluation) |
180 | | - except: |
| 130 | +if sys.platform != "win32": |
| 131 | + class TimeConstrained(Builtin): |
| 132 | + """ |
| 133 | + <dl> |
| 134 | + <dt>'TimeConstrained[$expr$, $t$]' |
| 135 | + <dd>'evaluates $expr$, stopping after $t$ seconds.' |
| 136 | + <dt>'TimeConstrained[$expr$, $t$, $failexpr$]' |
| 137 | + <dd>'returns $failexpr$ if the time constraint is not met.' |
| 138 | + </dl> |
| 139 | + >> TimeConstrained[Integrate[Sin[x]^1000000,x],1] |
| 140 | + = $Aborted |
| 141 | +
|
| 142 | + >> TimeConstrained[Integrate[Sin[x]^1000000,x], 1, Integrate[Cos[x],x]] |
| 143 | + = Sin[x] |
| 144 | +
|
| 145 | + >> s=TimeConstrained[Integrate[Sin[x] ^ 3, x], a] |
| 146 | + : Number of seconds a is not a positive machine-sized number or Infinity. |
| 147 | + = TimeConstrained[Integrate[Sin[x] ^ 3, x], a] |
| 148 | +
|
| 149 | + >> a=1; s |
| 150 | + = -Cos[x] + Cos[x] ^ 3 / 3 |
| 151 | +
|
| 152 | + Possible issues: for certain time-consuming functions (like simplify) |
| 153 | + which are based on sympy or other libraries, it is possible that |
| 154 | + the evaluation continues after the timeout. However, at the end of the evaluation, the function will return $\\$Aborted$ and the results will not affect |
| 155 | + the state of the mathics kernel. |
| 156 | +
|
| 157 | + """ |
| 158 | + |
| 159 | + attributes = ('HoldAll',) |
| 160 | + messages = { |
| 161 | + 'timc': 'Number of seconds `1` is not a positive machine-sized number or Infinity.', |
| 162 | + } |
| 163 | + |
| 164 | + def apply_2(self, expr, t, evaluation): |
| 165 | + 'TimeConstrained[expr_, t_]' |
| 166 | + return self.apply_3(expr, t, SymbolAborted, evaluation) |
| 167 | + |
| 168 | + def apply_3(self, expr, t, failexpr, evaluation): |
| 169 | + 'TimeConstrained[expr_, t_, failexpr_]' |
| 170 | + t = t.evaluate(evaluation) |
| 171 | + if not t.is_numeric(): |
| 172 | + evaluation.message('TimeConstrained', 'timc', t) |
| 173 | + return |
| 174 | + try: |
| 175 | + t = float(t.to_python()) |
| 176 | + evaluation.timeout_queue.append((t, datetime.now().timestamp())) |
| 177 | + request = lambda : expr.evaluate(evaluation) |
| 178 | + res = run_with_timeout_and_stack(request, t, evaluation) |
| 179 | + except TimeoutInterrupt: |
| 180 | + evaluation.timeout_queue.pop() |
| 181 | + return failexpr.evaluate(evaluation) |
| 182 | + except: |
| 183 | + evaluation.timeout_queue.pop() |
| 184 | + raise |
181 | 185 | evaluation.timeout_queue.pop() |
182 | | - raise |
183 | | - evaluation.timeout_queue.pop() |
184 | | - return res |
| 186 | + return res |
185 | 187 |
|
186 | 188 |
|
187 | 189 | class Timing(Builtin): |
|
0 commit comments