sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.45k stars 482 forks source link

Improve parsing in interact mode of interfaces #28439

Closed mwageringel closed 5 years ago

mwageringel commented 5 years ago

The function Interface.interact() gives interactive access to the interpreter session of a pexpect interface that is running in the background. This ticket fixes the following issues with the output:


The whitespace issues are particularly frequent with interfaces that use ASCII art for printing like Mathematica and Macaulay2:

sage: macaulay2.interact()

  --> Switching to Macaulay2 <--

macaulay2: M = ZZ^2
2
ZZ

ZZ-module, free

An artificial example using a standard interface which shows whitespace and escaping issue:

sage: gap.interact()

  --> Switching to Gap <--

gap: Print("  \\\\")
\

With the usual gap.eval() the escaping is done correctly, but in the case of Gap, whitespace is still stripped from the front.

sage: gap.eval(r'Print("  \\\\")')
\\

I changed the Gap interface to preserve whitespace at the front in order to be able to add a doctest for interact using a standard interface.

As for the nested parentheses, the following example now works. The expressions sage(...) are evaluated in Sage and the results are converted to Gap. Previously, this only worked for very simple expressions not containing any parenthesis.

sage: %gap

  --> Switching to Gap <--

gap: 2 + sage((1+2)*gap(-(5-3)^2).sage()) - sage(1+(2-1))
-12

Component: interfaces

Keywords: macaulay2, gap

Author: Markus Wageringel

Branch/Commit: dc0d969

Reviewer: Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/28439

mwageringel commented 5 years ago

Commit: 51dadd7

mwageringel commented 5 years ago

New commits:

2e3ffa328439: fix whitespace in interact mode of interfaces
51dadd728439: fix pyflakes issues
mwageringel commented 5 years ago

Author: Markus Wageringel

mwageringel commented 5 years ago

Branch: u/gh-mwageringel/28439

mwageringel commented 5 years ago
comment:2

I suggest testing this in the terminal, as in the Jupyter notebook there does not seem to be a way to exit the subshells.

The remaining pyflakes warning is a false positive.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 5 years ago

Changed commit from 51dadd7 to 760de84

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 5 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

760de8428439: handle parsing of nested parentheses
mwageringel commented 5 years ago
comment:5

I have fixed another problem, see the description.

mwageringel commented 5 years ago

Description changed:

--- 
+++ 
@@ -2,6 +2,7 @@

 - whitespace is incorrectly stripped from the front of the output string
 - special characters are not properly escaped
+- the syntax `sage(...)` for evaluating expressions in Sage does not support nested parentheses

 The whitespace issues are particular frequent with interfaces that use ASCII art for printing like Mathematica and Macaulay2:

@@ -36,3 +37,14 @@

I changed the Gap interface to preserve whitespace at the front in order to be able to add a doctest for interact using a standard interface. + +As for the nested parentheses, the following example now works. The expressions sage(...) are evaluated in Sage and converted to Gap. + +``` +sage: %gap +

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 5 years ago

Changed commit from 760de84 to dc0d969

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 5 years ago

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

5547fe728439: fix whitespace in interact mode of interfaces
8e39d6d28439: fix pyflakes issues
dc0d96928439: handle parsing of nested parentheses
mwageringel commented 5 years ago
comment:7

Rebased to fix an incorrect trac link in the first commit spotted by the patchbot.

mwageringel commented 5 years ago

Description changed:

--- 
+++ 
@@ -4,7 +4,9 @@
 - special characters are not properly escaped
 - the syntax `sage(...)` for evaluating expressions in Sage does not support nested parentheses

-The whitespace issues are particular frequent with interfaces that use ASCII art for printing like Mathematica and Macaulay2:
+---
+
+The whitespace issues are particularly frequent with interfaces that use ASCII art for printing like Mathematica and Macaulay2:

sage: macaulay2.interact() @@ -38,7 +40,7 @@

I changed the Gap interface to preserve whitespace at the front in order to be able to add a doctest for interact using a standard interface.

-As for the nested parentheses, the following example now works. The expressions sage(...) are evaluated in Sage and converted to Gap. +As for the nested parentheses, the following example now works. The expressions sage(...) are evaluated in Sage and the results are converted to Gap. Previously, this only worked for very simple expressions not containing any parenthesis.

 sage: %gap
tscrim commented 5 years ago

Reviewer: Travis Scrimshaw

tscrim commented 5 years ago
comment:8

All of these changes make sense and look good.

mwageringel commented 5 years ago
comment:9

Thanks.

slel commented 5 years ago
comment:10

I kept noticing incorrect stripping of initial whitespace without ever doing anything about it.

Finally initial whitespace will be preserved. Wholehearted thanks for that!

vbraun commented 5 years ago

Changed branch from u/gh-mwageringel/28439 to dc0d969