ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 06 Jul 2021 15:10:22 +0200Catch exception from forked subprocesshttps://ask.sagemath.org/question/57886/catch-exception-from-forked-subprocess/ I use `fork` to give certain methods only a fixed time for execution. These methods might, however, raise an exception and I don't know how to catch these. The code:
@fork
def test() :
raise ValueError("Found a value error")
try :
test()
except ValueError :
print("Value error caught")
does not catch the error. Is there a way to do this?philipp7Tue, 06 Jul 2021 15:10:22 +0200https://ask.sagemath.org/question/57886/How to capture error message from exception in call to integrate?https://ask.sagemath.org/question/57289/how-to-capture-error-message-from-exception-in-call-to-integrate/Sometimes when I call integrate using different algorithms, I get `TypeError` exception. (or any other, but this one seems to be the most common)
I'd like to capture into a variable, the message that the other CAS could have generated also, this way I would know if the exception was due to sagemath interface to the other CAS, or if it is due to the other CAS generating internal error on its own.
An example will make it easy to explain. In the following, I can capture the exception name itself. But not the rest of the message that normally show on the terminal.
var('x')
try:
integrate(log(1+x)/x/(1+(1+x)^(1/2))^(1/2),x, algorithm="fricas")
except Exception as ee:
print("Exception is ",ee)
the_exception = ee
The above prints on the screen the following
Exception is An error occurred when FriCAS evaluated 'integrate(sage2,sage1)':
>> Error detected within library code:
integrate: implementation incomplete (constant residues)
I can capture into a variable, the name of the exception
sage: name_of_exception = the_exception.__class__.__name__
'TypeError'
But how to capture into a variable the rest of error message that shows on the screen? In particular the message
>> Error detected within library code:
integrate: implementation incomplete (constant residues)
This will help me later know if the error was due to interface error, or due to internal error in the other CAS being used to do the integration. I looked at Python documentation for Exception, but so far did not see anything. There might be a sagemath specific way to do this?NasserThu, 27 May 2021 05:39:37 +0200https://ask.sagemath.org/question/57289/A simple sum causes division by zero exceptionhttps://ask.sagemath.org/question/55766/a-simple-sum-causes-division-by-zero-exception/I'm very new to SageMath and did some small experiments yesterday. This is my Code:
x, n, r, i = var('x, n, r, i')
f(x, n, r) = x * sum((1 + r) ^ i, i, 1, n)
f(1, 30, 0.0)
It works well if `r != 0`, but will raise a exception of "division by zero" if equal.
My ipynb file: nbviewer.jupyter.org/gist/7sDream/5db3cfd153269fd1a1cacaf0b60f69bb
It seems some optimizations of sum did not consider the range of variable `r`.
But there is also no change if I added `assume(r >= 0)`before define `f`.
So how can I disable this sum optimizations? Or what is wrong with the way I use it?7sDreamFri, 19 Feb 2021 12:53:02 +0100https://ask.sagemath.org/question/55766/How to doctest an exception ?https://ask.sagemath.org/question/39306/how-to-doctest-an-exception/I'd like to doctest an exception, to show that some call is *expected* to fail (and, latter, to show how to cope with that). Something along the lines of:
sage: foo(x)
ValueError
... #Elided
ValueError: x is not palatable to foo
I haven't found any doctest directive allowing me to tell "This test is *expected* to raise an exception" ; so, when I run the doctest, I can't even compare what I get with the text of the expected traceback. The only solution I found so far is to wrap my test in a ```try:... except: ...``` construct, which is not very demonstrative.
Any idea ?Emmanuel CharpentierSat, 28 Oct 2017 13:24:29 +0200https://ask.sagemath.org/question/39306/Exception does not show which line raised ithttps://ask.sagemath.org/question/32811/exception-does-not-show-which-line-raised-it/Hi there,
I'm new to sage, and made some sage scripts to improve my python scripts' performances. Then I load my script into a sage session, and run a test function.
When I get exception, it shows me the traceback with functions names only, without the instruction that triggered it. For example :
sage: f.runtest()
Generating keys ...
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-3-abdb981a9bdb> in <module>()
----> 1 f.runtest()
<string> in runtest(self)
<string> in KeyGen(self)
/home/hack4u/Download/SageMath/src/sage/rings/rational.pyx in sage.rings.rational.Rational.__mod__ (/home/hack4u/Download/SageMath/src/build/cythonized/sage/rings/rational.c:22716)()
2541 n = rat.numer() % other
2542 d = rat.denom() % other
-> 2543 d = d.inverse_mod(other)
2544 return (n * d) % other
2545
/home/hack4u/Download/SageMath/src/sage/rings/integer.pyx in sage.rings.integer.Integer.inverse_mod (/home/hack4u/Download/SageMath/src/build/cythonized/sage/rings/integer.c:38530)()
6169 sig_off()
6170 if r == 0:
-> 6171 raise ZeroDivisionError, "Inverse does not exist."
6172 return ans
6173
ZeroDivisionError: Inverse does not exist.
And I have no idea which line in `KeyGen` does trigger that `ZeroDivisionError`. Any idea ?
matlinkWed, 16 Mar 2016 10:10:32 +0100https://ask.sagemath.org/question/32811/substitute() raises exception where python does nothttps://ask.sagemath.org/question/32744/substitute-raises-exception-where-python-does-not/ In my usual workflow, I derive symbolic equations and then substitute their arguments by values stored in dictionaries to obtain numerical solutions. Now I found out that the substitute() command raises division by zero exception where standard python does not. This is a bit of a problem, as these exceptions prevent my code from evaluating all data sets once such an exception occurs and I don't want to wrap each line of code in a try-except construct. Does anyone know how the exception could be avoided? Here is an example:
var('y a x')
eq_y = y == a/x
vdict = {}
vdict[a] = 1.
vdict[x] = 0.
vdict[a]/vdict[x]
+infinity
However, if I substitute vdict into eq_y, I get an exception:
eq_y.subs(vdict)
Traceback (click to the left of this block for traceback)
...
ValueError: power::eval(): division by zero
stanWed, 09 Mar 2016 15:23:25 +0100https://ask.sagemath.org/question/32744/scary muli_polynomial_ring warninghttps://ask.sagemath.org/question/10426/scary-muli_polynomial_ring-warning/Thw following warning message looks pretty scary, since it is accompanied by a stack trace. And I have absolutely no idea (yet) as to what it is trying to tell me. Is the result it prints in the end reliable? If so, why the fuss? And if not, what is causing this problem? Is this an indication of a bug?
sage: R1.<cosAlpha, sinAlpha> = AA[]
sage: QR1 = R1.quotient(R1.ideal(cosAlpha^2 + sinAlpha^2 - 1))
sage: QR1(-8*sinAlpha - 4*sinAlpha*cosAlpha + 5*2*(2*sinAlpha*cosAlpha)*(cosAlpha*cosAlpha - sinAlpha*sinAlpha))
verbose 0 (3490: multi_polynomial_ideal.py, groebner_basis) Warning: falling back to very slow toy implementation.
singular_ring_delete(ring*) called with NULL pointer.
File "<stdin>", line 1, in <module>
File "_sage_input_4.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("UVIxKC04KnNpbkFscGhhIC0gNCpzaW5BbHBoYSpjb3NBbHBoYSArIDUqMiooMipzaW5BbHBoYSpjb3NBbHBoYSkqKGNvc0FscGhhKmNvc0FscGhhIC0gc2luQWxwaGEqc2luQWxwaGEpKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single')
File "", line 1, in <module>
File "/tmp/tmpKfaoEi/___code___.py", line 3, in <module>
exec compile(u'QR1(-_sage_const_8 *sinAlpha - _sage_const_4 *sinAlpha*cosAlpha + _sage_const_5 *_sage_const_2 *(_sage_const_2 *sinAlpha*cosAlpha)*(cosAlpha*cosAlpha - sinAlpha*sinAlpha))' + '\n', '', 'single')
File "", line 1, in <module>
File "sage/rings/quotient_ring.py", line 993, in _element_constructor_
return self.element_class(self, x)
File "sage/rings/quotient_ring_element.py", line 99, in __init__
self._reduce_()
File "sage/rings/quotient_ring_element.py", line 118, in _reduce_
self.__rep = I.reduce(self.__rep)
File "sage/rings/polynomial/multi_polynomial_ideal.py", line 4019, in reduce
strat = self._groebner_strategy()
File "sage/rings/polynomial/multi_polynomial_ideal.py", line 910, in _groebner_strategy
return GroebnerStrategy(MPolynomialIdeal(self.ring(), self.groebner_basis()))
Exception KeyError: (The ring pointer 0x0,) in 'sage.libs.singular.ring.singular_ring_delete' ignored
-40*cosAlphabar*sinAlphabar^3 + 16*cosAlphabar*sinAlphabar - 8*sinAlphabarMvGWed, 07 Aug 2013 10:06:35 +0200https://ask.sagemath.org/question/10426/Can I pause a computation rather than aborting it? (Interrupt Command)https://ask.sagemath.org/question/7628/can-i-pause-a-computation-rather-than-aborting-it-interrupt-command/Can I pause a computation rather than aborting it?
Sometimes I don't want to discard work by raising an exception. It would be nice if I could just pause execution ad hoc.ccanoncTue, 24 Aug 2010 17:01:04 +0200https://ask.sagemath.org/question/7628/