SageMath:为什么 sagemath 在出现 TypeErrors 时不给出行号?有没有办法追踪实际的行号?
SageMath: Why doesn't sagemath give line number in case of TypeErrors? Is there a way to trace the actual line number?
在 Windows 10
上使用 Sagemath 9.2
a.sage
i = 10
print("hello " + i)
圣人:加载(“a.sage”)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last)
in
----> 1 load("a.sage")
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/misc/persist.pyx
in sage.misc.persist.load
(build/cythonized/sage/misc/persist.c:2558)()
141
142 if sage.repl.load.is_loadable_filename(filename):
--> 143 sage.repl.load.load(filename, globals())
144 return
145
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/repl/load.py
in load(filename, globals, attach)
270 add_attached_file(fpath)
271 with open(fpath) as f:
--> 272 exec(preparse_file(f.read()) + "\n", globals)
273 elif ext == '.spyx' or ext == '.pyx':
274 if attach:
in
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/rings/integer.pyx
in sage.rings.integer.Integer.add
(build/cythonized/sage/rings/integer.c:12447)() 1785
return y 1786
-> 1787 return coercion_model.bin_op(left, right, operator.add) 1788 1789 cpdef add(self, right):
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/structure/coerce.pyx
in sage.structure.coerce.CoercionModel.bin_op
(build/cythonized/sage/structure/coerce.c:11304)() 1246 #
We should really include the underlying error. 1247 # This
causes so much headache.
-> 1248 raise bin_op_exception(op, x, y) 1249 1250 cpdef canonical_coercion(self, x, y):
TypeError: unsupported operand parent(s) for +: '<class 'str'>' and
'Integer Ring'
在许多其他类型的错误中,sage math 会给出错误发生的行号,但通常在 TypeErrors 中,我看不到这种情况发生
所以,
这在较长的程序中是个大问题,尤其是在更复杂的数据类型中。很难追踪出问题的线路。
发生这种情况的错误类型有哪些?
有没有简单的方法可以跟踪行号(我用的比较长)
如果您改用 %attach a.sage
,它将打印行号。行号用于文件的预解析版本,但您或许可以从中提取足够的信息。这是我看到的:
sage: %attach /Users/palmieri/Desktop/a.sage
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-a6e4524362f6> in <module>
----> 1 get_ipython().run_line_magic('attach', '/Users/palmieri/Desktop/a.sage')
[snip]
~/.sage/temp/John-iMac-2017.local/34847/a.sage5dnlgxa9.py in <module>
5 _sage_const_10 = Integer(10)
6 i = _sage_const_10
----> 7 print("hello " + i)
[snip]
TypeError: unsupported operand parent(s) for +: '<class 'str'>' and 'Integer Ring'
%attach
还具有每当文件更改时自动重新加载的功能。
在 Windows 10
上使用 Sagemath 9.2a.sage
i = 10
print("hello " + i)
圣人:加载(“a.sage”)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in ----> 1 load("a.sage")
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/misc/persist.pyx in sage.misc.persist.load (build/cythonized/sage/misc/persist.c:2558)() 141 142 if sage.repl.load.is_loadable_filename(filename): --> 143 sage.repl.load.load(filename, globals()) 144 return 145
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/repl/load.py in load(filename, globals, attach) 270 add_attached_file(fpath) 271 with open(fpath) as f: --> 272 exec(preparse_file(f.read()) + "\n", globals) 273 elif ext == '.spyx' or ext == '.pyx': 274 if attach:
in
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/rings/integer.pyx in sage.rings.integer.Integer.add (build/cythonized/sage/rings/integer.c:12447)() 1785
return y 1786 -> 1787 return coercion_model.bin_op(left, right, operator.add) 1788 1789 cpdef add(self, right):/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel.bin_op (build/cythonized/sage/structure/coerce.c:11304)() 1246 # We should really include the underlying error. 1247 # This causes so much headache. -> 1248 raise bin_op_exception(op, x, y) 1249 1250 cpdef canonical_coercion(self, x, y):
TypeError: unsupported operand parent(s) for +: '<class 'str'>' and 'Integer Ring'
在许多其他类型的错误中,sage math 会给出错误发生的行号,但通常在 TypeErrors 中,我看不到这种情况发生
所以,
这在较长的程序中是个大问题,尤其是在更复杂的数据类型中。很难追踪出问题的线路。
发生这种情况的错误类型有哪些?
有没有简单的方法可以跟踪行号(我用的比较长)
如果您改用 %attach a.sage
,它将打印行号。行号用于文件的预解析版本,但您或许可以从中提取足够的信息。这是我看到的:
sage: %attach /Users/palmieri/Desktop/a.sage
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-a6e4524362f6> in <module>
----> 1 get_ipython().run_line_magic('attach', '/Users/palmieri/Desktop/a.sage')
[snip]
~/.sage/temp/John-iMac-2017.local/34847/a.sage5dnlgxa9.py in <module>
5 _sage_const_10 = Integer(10)
6 i = _sage_const_10
----> 7 print("hello " + i)
[snip]
TypeError: unsupported operand parent(s) for +: '<class 'str'>' and 'Integer Ring'
%attach
还具有每当文件更改时自动重新加载的功能。