Skip to content

Commit

Permalink
remove custom _send_interrupt in singular pexpect interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Nov 28, 2020
1 parent 020bd59 commit 85f65bf
Showing 1 changed file with 24 additions and 63 deletions.
87 changes: 24 additions & 63 deletions src/sage/interfaces/singular.py
Expand Up @@ -273,7 +273,7 @@
functions, it actually lives in Sage. As a consequence, the name
'I' means nothing to Singular. When we called
``I.groebner()``, Sage was able to call the groebner
function on'I' in Singular, since 'I' actually means something to
function on 'I' in Singular, since 'I' actually means something to
Sage.
Long Input
Expand Down Expand Up @@ -324,7 +324,6 @@
import re
import sys
import pexpect
from time import sleep

from .expect import Expect, ExpectElement, FunctionElement, ExpectFunction

Expand Down Expand Up @@ -367,7 +366,7 @@ class Singular(ExtraTabCompletion, Expect):
- David Joyner and William Stein
"""
def __init__(self, maxread=None, script_subdirectory=None,
logfile=None, server=None,server_tmpdir=None,
logfile=None, server=None, server_tmpdir=None,
seed=None):
"""
EXAMPLES::
Expand Down Expand Up @@ -395,7 +394,7 @@ def __init__(self, maxread=None, script_subdirectory=None,
self.__to_clear = [] # list of variable names that need to be cleared.
self._seed = seed

def set_seed(self,seed=None):
def set_seed(self, seed=None):
"""
Set the seed for singular interpreter.
Expand Down Expand Up @@ -487,46 +486,9 @@ def _quit_string(self):
EXAMPLES::
sage: singular._quit_string()
'quit'
'quit;'
"""
return 'quit'

def _send_interrupt(self):
"""
Send an interrupt to Singular. If needed, additional
semi-colons are sent until we get back at the prompt.
TESTS:
The following works without restarting Singular::
sage: a = singular(1)
sage: _ = singular._expect.sendline('1+') # unfinished input
sage: try:
....: alarm(0.5)
....: singular._expect_expr('>') # interrupt this
....: except KeyboardInterrupt:
....: pass
Control-C pressed. Interrupting Singular. Please wait a few seconds...
We can still access a::
sage: 2*a
2
"""
# Work around for Singular bug
# http://www.singular.uni-kl.de:8002/trac/ticket/727
sleep(0.1)

E = self._expect
E.sendline(chr(3))
for i in range(5):
try:
E.expect_upto(self._prompt, timeout=1.0)
return
except Exception:
pass
E.sendline(";")
return 'quit;'

def _read_in_file_command(self, filename):
r"""
Expand All @@ -543,7 +505,7 @@ def _read_in_file_command(self, filename):
sage: singular.get('x')
'2'
"""
return '< "%s";'%filename
return '< "%s";' % filename

def eval(self, x, allow_semicolon=True, strip=True, **kwds):
r"""
Expand All @@ -552,15 +514,13 @@ def eval(self, x, allow_semicolon=True, strip=True, **kwds):
INPUT:
- ``x`` - string (of code)
- ``allow_semicolon`` - default: False; if False then
raise a TypeError if the input line contains a semicolon.
- ``strip`` - ignored
EXAMPLES::
sage: singular.eval('2 > 1')
Expand Down Expand Up @@ -645,7 +605,7 @@ def eval(self, x, allow_semicolon=True, strip=True, **kwds):
x = str(x).rstrip().rstrip(';')
x = x.replace("> ",">\t") #don't send a prompt (added by Martin Albrecht)
if not allow_semicolon and x.find(";") != -1:
raise TypeError("singular input must not contain any semicolons:\n%s"%x)
raise TypeError("singular input must not contain any semicolons:\n%s" % x)
if len(x) == 0 or x[len(x) - 1] != ';':
x += ';'

Expand Down Expand Up @@ -857,7 +817,7 @@ def lib(self, lib, reload=False):
lib += ".lib"
if not reload and lib in self.__libs:
return
self.eval('LIB "%s"'%lib)
self.eval('LIB "%s"' % lib)
self.__libs.append(lib)

LIB = lib
Expand Down Expand Up @@ -906,7 +866,7 @@ def ideal(self, *gens):
return self(gens.name(), 'ideal')

if not isinstance(gens, (list, tuple)):
raise TypeError("gens (=%s) must be a list, tuple, string, or Singular element"%gens)
raise TypeError("gens (=%s) must be a list, tuple, string, or Singular element" % gens)

if len(gens) == 1 and isinstance(gens[0], (list, tuple)):
gens = gens[0]
Expand Down Expand Up @@ -1171,7 +1131,7 @@ def set_ring(self, R):
"""
if not isinstance(R, SingularElement):
raise TypeError("R must be a singular ring")
self.eval("setring %s; short=0"%R.name(), allow_semicolon=True)
self.eval("setring %s; short=0" % R.name(), allow_semicolon=True)

setring = set_ring

Expand Down Expand Up @@ -1321,7 +1281,7 @@ def option(self, cmd=None, val=None):
if not isinstance(val,SingularElement):
raise TypeError("singular.option('set') needs SingularElement as second parameter")
#SingularFunction(self,"option")("\"set\"",val)
self.eval("option(set,%s)"%val.name())
self.eval("option(set,%s)" % val.name())
else:
SingularFunction(self,"option")("\""+str(cmd)+"\"")

Expand Down Expand Up @@ -1501,7 +1461,7 @@ def __setitem__(self, n, value):
value = P(value)
if isinstance(n, tuple):
if len(n) != 2:
raise ValueError("If n (=%s) is a tuple, it must be a 2-tuple"%n)
raise ValueError("If n (=%s) is a tuple, it must be a 2-tuple" % n)
x, y = n
P.eval('%s[%s,%s] = %s'%(self.name(), x, y, value.name()))
else:
Expand Down Expand Up @@ -1821,13 +1781,13 @@ def sage_poly(self, R=None, kcache=None):
out = R(self)
self.parent().eval('short=%s'%is_short)
return out
singular_poly_list = self.parent().eval("string(coef(%s,%s))"%(\
singular_poly_list = self.parent().eval("string(coef(%s,%s))" % (\
self.name(),variable_str)).split(",")
self.parent().eval('short=%s'%is_short)
else:
if isinstance(R, MPolynomialRing_libsingular):
return R(self)
singular_poly_list = self.parent().eval("string(coef(%s,%s))"%(\
singular_poly_list = self.parent().eval("string(coef(%s,%s))" % (\
self.name(),variable_str)).split(",")

# Directly treat constants
Expand Down Expand Up @@ -1899,7 +1859,7 @@ def sage_poly(self, R=None, kcache=None):
return R(sage_repr)

else:
raise TypeError("Cannot coerce %s into %s"%(self,R))
raise TypeError("Cannot coerce %s into %s" % (self, R))

def sage_matrix(self, R, sparse=True):
"""
Expand Down Expand Up @@ -2179,7 +2139,7 @@ def type(self):
"""
# singular reports // $varname $type $stuff
p = re.compile(r"// [\w]+ (\w+) [\w]*")
m = p.match(self.parent().eval("type(%s)"%self.name()))
m = p.match(self.parent().eval("type(%s)" % self.name()))
return m.group(1)

def __iter__(self):
Expand Down Expand Up @@ -2253,9 +2213,9 @@ def attrib(self, name, value=None):
'4'
"""
if value is None:
return int(self.parent().eval('attrib(%s,"%s")'%(self.name(),name)))
return int(self.parent().eval('attrib(%s,"%s")' % (self.name(), name)))
else:
self.parent().eval('attrib(%s,"%s",%d)'%(self.name(),name,value))
self.parent().eval('attrib(%s,"%s",%d)' % (self.name(), name,value))


@instancedoc
Expand Down Expand Up @@ -2283,12 +2243,12 @@ def _instancedoc_(self):
sage: groebner(singular(I))
x+y,
y^2-y
"""%(self._name,)
""" % (self._name,)
prefix2 = \
"""
The Singular documentation for '%s' is given below.
"""%(self._name,)
""" % (self._name,)

try:
return prefix + prefix2 + nodes[node_names[self._name]]
Expand Down Expand Up @@ -2536,7 +2496,7 @@ def write(self, s):
self.sync = int(match.groups()[0])
continue

if self.sync and line == "%d"%(self.sync+1):
if self.sync and line == "%d" % (self.sync + 1):
self.sync = None
continue

Expand Down Expand Up @@ -2589,10 +2549,10 @@ def write(self, s):
print("Maximal degree found: %s" % token)

elif re.match(SingularGBLogPrettyPrinter.num_crit, token) and verbosity >= 1:
print("Leading term degree: %2d. Critical pairs: %s."%(self.curr_deg,token[1:-1]))
print("Leading term degree: %2d. Critical pairs: %s." % (self.curr_deg,token[1:-1]))

elif re.match(SingularGBLogPrettyPrinter.red_num, token) and verbosity >= 3:
print("Performing complete reduction of %s elements."%token[3:-1])
print("Performing complete reduction of %s elements." % token[3:-1])

elif re.match(SingularGBLogPrettyPrinter.deg_lead, token):
if verbosity >= 1:
Expand Down Expand Up @@ -2778,6 +2738,7 @@ def singular_gb_standard_options(func):
does not need to use it manually.
"""
from sage.misc.decorators import sage_wraps

@sage_wraps(func)
def wrapper(*args, **kwds):
with SingularGBDefaultContext():
Expand Down

0 comments on commit 85f65bf

Please sign in to comment.