The sage plugin now starts a session for SageMath-9-7

Dear Marc, welcome to the Forum! I think your participation here will be much appreciated and beneficial to both projects — TeXmacs and SageMath-9-x.

Cheers
Tilda

I have decided that it was a mistake to modify the user’s environment when running Sage from the command line, even though doing that does prevent crashes for people who are not aware of their outdated sage environment setups. I am going to remove that feature. This does not involve any changes to the app itself. Everything happens in the bash script /usr/local/bin/sage, which is installed by the recommended_extras package. That script sets up a minimal environment and then uses /usr/bin/env to run sage in that environment.

2 Likes

@Tilda
Perhaps this will make postscript.py work:

Patch 2
diff --git a/plugins/tmpy/postscript.py b/plugins/tmpy/postscript.py
index 2d83ac7e0..b2c30d465 100644
--- a/plugins/tmpy/postscript.py
+++ b/plugins/tmpy/postscript.py
@@ -11,6 +11,7 @@
 
 import os
 import platform
+import tempfile
 from io import BytesIO
 from io import open
 from .protocol import *
@@ -35,8 +36,9 @@ class PSOutDummy:
 class FileOutDummy:
     """ Python object to feed a file back to TeXmacs. """
 
-    def __init__(self, data):
+    def __init__(self, data, tmp=False):
         self.content = data
+        self.tempfile = tmp
 
     def __str__(self):
         """Return an empty string for compose_output()"""
@@ -96,13 +98,6 @@ def ps_out (out):
 
     return PSOutDummy(texmacs_escape(data).decode())
 
-pdf_out_tmp_file = "pdf_out_" + str(os.getpid()) + ".pdf"
-if (platform.system() == "Windows"):
-    pdf_out_tmp_file = os.getenv("TEXMACS_HOME_PATH") + "\\system\\tmp\\" + pdf_out_tmp_file
-else:
-    pdf_out_tmp_file = os.getenv("TEXMACS_HOME_PATH") + "/system/tmp/" +  pdf_out_tmp_file
-
-
 def pdf_out (out):
     """Outputs PDF within TeXmacs.
 
@@ -127,9 +122,12 @@ def pdf_out (out):
     Adapted from ps_out().
     """
 
+    pdf_out_tmp_file = tempfile.mkstemp(suffix='.pdf')[1]
+    
     if 'savefig' in dir(out):
         out.savefig(pdf_out_tmp_file, format='pdf')
         name = pdf_out_tmp_file
+        tmp = True
     elif isinstance(out, str):
         if out.find('\n') > 0:
             name = None
@@ -141,11 +139,13 @@ def pdf_out (out):
                 break
             else:
                 raise IOError('File "%s%s" not found.' % (out, str(ext_list)))
+        tmp = False
     elif 'read' in dir(out):
         data = out.read()
         fd = open(pdf_out_tmp_file, 'wb')
         fd.write(data)
         fd.close()
         name = pdf_out_tmp_file
+        tmp = True
 
-    return FileOutDummy(name)
+    return FileOutDummy(name,tmp)
diff --git a/plugins/tmpy/session/tm_sage.py b/plugins/tmpy/session/tm_sage.py
index ab11dd42c..45cffd80f 100644
--- a/plugins/tmpy/session/tm_sage.py
+++ b/plugins/tmpy/session/tm_sage.py
@@ -13,12 +13,9 @@
 import os
 import sys
 from os.path import exists
-tmpy_home_path = os.environ.get("TEXMACS_HOME_PATH") + "/plugins/tmpy"
-if (exists (tmpy_home_path)):
-    sys.path.append(os.environ.get("TEXMACS_HOME_PATH") + "/plugins/")
-else:
-    sys.path.append(os.environ.get("TEXMACS_PATH") + "/plugins/")
-
+file_path = os.path.realpath(__file__)
+plugins_path = os.path.abspath(os.path.join(os.path.dirname(file_path),os.pardir,os.pardir))
+sys.path.append(plugins_path)
 
 import tempfile
 import traceback
@@ -138,12 +135,10 @@ eval(co, my_globals)
 ###############################################################################
 # Session start
 ###############################################################################
-if (os.path.exists (tmpy_home_path)):
-    flush_verbatim ("WARNING: You are under develop mode using " + tmpy_home_path)
-    flush_newline (2)
 flush_verbatim (sage.misc.banner.version() + "\n" +
 		"Python " + sys.version + "\n" +
                "SageMath plugin for TeXmacs.\n" +
+               "Loaded from " + plugins_path + "\n" +
                "Please see the documentation in Help -> Plugins -> Sage")
 flush_prompt (">>> ")
 while True:
1 Like

@culler Welcome to the forum and many thanks for your comments!

In my patch I’ve removed the need for the environment variable. In one location it was used to load extra code and in another to create a temporary file. The former could be done using a relative path to the script file and for the latter I’ve used the tempfile module instead.

It does! No more error messages, no warnings, and pretty fast session launch. Thanks again, dear jeroen.

Will these changes also become part of the official tmpy-plugin?

Cheers
Tilda

I have also released a new version 1.5.3 of SageMath-9.7 which passes all of the current environment to Sage. (Only the extra installed script /usr/local/bin/sage was changed.) So we now have belt and suspenders on this one.

2 Likes

Great!! SageMath-9-7 now smoothly runs in a TeXmacs session :relaxed::pray::relaxed:

2 Likes

I’m realising that this way of running plugins is very prone to problems due to the fact that relies on global variables and state passing. A more functional approach would be that TeXmacs produce an adapted running script which contains all the data needed as values opposed at environment variables, that is instead of using a Python script which then has to read variables from the environment, TeXmacs could insatiate a script template with the right values and then run this. This leveage the interpreted nature of Python by programmatically create the script which has to be run, mostly like a macro would do. This avoid state passing and it is more “functional”, which is a general philosophy to make local all the dependencies in order to avoid problems like those affecting these scripts.

1 Like

I just figured out that version 1.5.3 of SageMath-9.7, i.e. Marc’s new shell script copied to /usr/local/bin/sage does open a sage session using the unpatched tm_sage.py that ships with TeXmacs 2.1.2.

1 Like