Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FriCAS is not supported if the user has a ~/.fricas.input init script that adds some unsupported output messages #38890

Open
2 tasks done
gvanuxem opened this issue Oct 31, 2024 · 1 comment · May be fixed by #38894
Open
2 tasks done
Labels

Comments

@gvanuxem
Copy link

Steps To Reproduce

Env: sage-10.4, WSL2 Kali Linux
~/.fricas.input init file with this content:

)set output length 110
)set message time on
)set message storage on

The offending lines are the last two lines since line length is handled in src/sage/interfaces/fricas.py

Expected Behavior

No bug (good parsing of FriCAS output).

Actual Behavior

The output when this init file is present

sage: fricas(7)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~/Tmp/sage-10.4/src/sage/interfaces/expect.py:1519, in ExpectElement.__init__(self, parent, value, is_name, name)
   1518 try:
-> 1519     self._name = parent._create(value, name=name)
   1520 # Convert ValueError and RuntimeError to TypeError for
   1521 # coercion to work properly.

File ~/Tmp/sage-10.4/src/sage/interfaces/interface.py:517, in Interface._create(self, value, name)
    516 name = self._next_var_name() if name is None else name
--> 517 self.set(name, value)
    518 return name

File ~/Tmp/sage-10.4/src/sage/interfaces/fricas.py:663, in FriCAS.set(self, var, value)
    662 output = self.eval(cmd, reformat=False)
--> 663 self._check_errors(value, output)

File ~/Tmp/sage-10.4/src/sage/interfaces/fricas.py:556, in FriCAS._check_errors(self, line, output)
    555         output = output.replace(old, new)
--> 556     raise RuntimeError("An error occurred when FriCAS evaluated '%s':\n%s" % (line, output))
    558 # or even an error

RuntimeError: An error occurred when FriCAS evaluated '7':

                                                                     Time: 0 sec

                                                                Storage: 0 bytes


During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[1], line 1
----> 1 fricas(Integer(7))

File ~/Tmp/sage-10.4/src/sage/misc/lazy_import.pyx:410, in sage.misc.lazy_import.LazyImport.__call__()
    408         True
    409     """
--> 410     return self.get_object()(*args, **kwds)
    411
    412 def __repr__(self):

File ~/Tmp/sage-10.4/src/sage/interfaces/interface.py:306, in Interface.__call__(self, x, name)
    299     return cls(self, x, name=name)
    300 try:
    301     # Special methods do not and should not have an option to
    302     # set the name directly, as the identifier assigned by the
    303     # interface should stay consistent. An identifier with a
    304     # user-assigned name might change its value, so we return a
    305     # new element.
--> 306     result = self._coerce_from_special_method(x)
    307     return result if name is None else result.name(new_name=name)
    308 except TypeError:

File ~/Tmp/sage-10.4/src/sage/interfaces/interface.py:334, in Interface._coerce_from_special_method(self, x)
    332     s = '_gp_'
    333 try:
--> 334     return (x.__getattribute__(s))(self)
    335 except AttributeError:
    336     return self(x._interface_init_())

File ~/Tmp/sage-10.4/src/sage/structure/sage_object.pyx:795, in sage.structure.sage_object.SageObject._fricas_()
    793         import sage.interfaces.fricas
    794         G = sage.interfaces.fricas.fricas
--> 795     return self._interface_(G)
    796
    797 def _fricas_init_(self):

File ~/Tmp/sage-10.4/src/sage/structure/sage_object.pyx:725, in sage.structure.sage_object.SageObject._interface_()
    723     except Exception:
    724         raise NotImplementedError("coercion of object %s to %s not implemented:\n%s\n%s" % (repr(self), I))
--> 725 X = I(s)
    726 if c:
    727     try:

File ~/Tmp/sage-10.4/src/sage/interfaces/interface.py:299, in Interface.__call__(self, x, name)
    296         pass
    298 if isinstance(x, str):
--> 299     return cls(self, x, name=name)
    300 try:
    301     # Special methods do not and should not have an option to
    302     # set the name directly, as the identifier assigned by the
    303     # interface should stay consistent. An identifier with a
    304     # user-assigned name might change its value, so we return a
    305     # new element.
    306     result = self._coerce_from_special_method(x)

File ~/Tmp/sage-10.4/src/sage/interfaces/expect.py:1524, in ExpectElement.__init__(self, parent, value, is_name, name)
   1522 except (RuntimeError, ValueError) as x:
   1523     self._session_number = -1
-> 1524     raise TypeError(*x.args)
   1525 except BaseException:
   1526     self._session_number = -1

TypeError: An error occurred when FriCAS evaluated '7':

                                                                     Time: 0 sec

                                                                Storage: 0 bytes

sage:

Additional Information

Two quick solutions, unless you want to support timing data, storage data or any other things.

One is to add some default settings to revert the ones in ~/.fricas.input:

In src/sage/interfaces/fricas.py, replacing (lines 228-238)

FRICAS_INIT_CODE = (
    ")set functions compile on",
    ")set message autoload off",
    ")set message type off",
    ")set output length " + str(FRICAS_LINE_LENGTH),
    ")lisp (setf |$ioHook|"
    "            (lambda (x &optional args)"
    "              (when (member x '(|startAlgebraOutput| |endOfAlgebraOutput|"
    "                                |startKeyedMsg|      |endOfKeyedMsg|))"
    "               (prin1 x)"
    "               (princ #\\Newline))))")

by

FRICAS_INIT_CODE = (
    ")set functions compile on",
    ")set message any          on",
    ")set message autoload     off",
    ")set message bottomup     off",
    ")set message dropmap      off",
    ")set message expose       off",
    ")set message file         off",
    ")set message frame        off",
    ")set message highlighting off",
    ")set message instant      off",
    ")set message insteach     off",
    ")set message interponly   off",
    ")set message prompt       step",
    ")set message selection    off",
    ")set message set          off",
    ")set message startup      on",
    ")set message storage      off",
    ")set message testing      off",
    ")set message time         off",
    ")set message type         off",
    ")set message void         off",
    ")set output length " + str(FRICAS_LINE_LENGTH),
    ")lisp (setf |$ioHook|"
    "            (lambda (x &optional args)"
    "              (when (member x '(|startAlgebraOutput| |endOfAlgebraOutput|"
    "                                |startKeyedMsg|      |endOfKeyedMsg|))"
    "               (prin1 x)"
    "               (princ #\\Newline))))")

Or, simpler, since os is imported you can export/set the environment variable FRICAS_INITFILE with an empty string using:
os.environ['FRICAS_INITFILE'] = ""
before starting FriCAS. This one is untested, but in a terminal if fricas is in the PATH, issuing

FRICAS_INITFILE='' fricas

will start FriCAS without evaluating ~/.fricas.input

This one is more robust since it starts FriCAS with well-known settings. With this solution, do not modify the actual FRICAS_INIT_CODE python variable.

There are other settings that affect the output of FriCAS for information:

(1) -> )set message
                  Current Values of  messages  Variables

Variable     Description                                Current Value
-----------------------------------------------------------------------------
any          print the internal type of objects of domain Any on
autoload     print file auto-load messages              off
bottomup     display bottom up modemap selection        off
dropmap      display old map defn when replaced         off
expose       warning for unexposed functions            off
file         print msgs also to SPADMSG LISTING         off
frame        display messages about frames              off
highlighting use highlighting in system messages        off
instant      present instantiation summary              off
insteach     present instantiation info                 off
interponly   say when function code is interpreted      on
prompt       set type of input prompt to display        step
selection    display function selection msgs            off
set          show )set setting after assignment         off
startup      display messages on start-up               on
storage      print memory usage after computation       off
testing      print system testing header                off
time         print timings after computation            off
type         print type after computation               on
void         print Void value when it occurs            off

(1) -> )set output
                   Current Values of  output  Variables

Variable     Description                                Current Value
-----------------------------------------------------------------------------
abbreviate   abbreviate type names                      off
algebra      display output in algebraic form           On:CONSOLE
characters   choose special output character set        plain
fortran      create output in FORTRAN format            Off:CONSOLE
fraction     how fractions are formatted                vertical
length       line length of output displays             77
openmath     create output in OpenMath style            Off:CONSOLE
scripts      show subscripts,... linearly               off
showeditor   view output of )show in editor             off
tex          create output in TeX style                 Off:CONSOLE
mathml       create output in MathML style              Off:CONSOLE
texmacs      create output in Texmacs style             Off:CONSOLE
html         create output in HTML style                Off:CONSOLE
formatted    create output in formatted style           Off:CONSOLE

(1) ->

PS: This should also be applicable to axiom.py

Environment

  • WSL2 Kali Linux (kali-rolling) - Windows 11 Professional:
  • Sage 10.4:

Checklist

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
  • I have read the documentation and troubleshoot guide
@mantepse mantepse linked a pull request Oct 31, 2024 that will close this issue
@mantepse
Copy link
Collaborator

Thank you! I like the first solution, it looks robust enough. Are you willing to review the fix (#38894)? (You probably don't have the rights yet, but that can be fixed.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants