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

I’m on mac OSX 13.6/15.7 and currently using TeXmacs 2.1.2 (as of 2022/05/05). I’ve recently upgraded from SageMath-9-4 (which worked perfectly fine on these systems) to SageMath-9-7.

When I want to launch a Sage session in TeXmacs now, I get the following error message:

Traceback (most recent call last):
File
“/Applications/TeXmacs.app/Contents/Resources/share/TeXmacs/plugins/tmpy/session/tm_sage.py”, line 16, in
tmpy_home_path = os.environ.get(“TEXMACS_HOME_PATH”) + “/plugins/tmpy”
TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’

and the session freezes. This problem did not occur with SageMath-9-4.

I wonder if anyone has an idea how to fix this. Any help appreciated.

Cheers
Tilda

It seems to me that if the type of os.environ.get(“TEXMACS_HOME_PATH”) is ‘NoneType’ it means that os.environ.get(“TEXMACS_HOME_PATH”) is returning None, id est it is not defined, so Python is running outside the shell which called TeXmacs, and I do not know why. Let me investigate a bit (or maybe someone else will be quicker).

Ubuntu (which I am using) has SageMath-9-5, and that works, so I cannot easily test with 9-7
Hopefully someone else can help.

Dear @pireddag, thanks for your efforts. I have no idea why SageMath-9-7 (i.e. the 3-manifolds binary release of SageMath-9.7 for macOS 10.12 and newer) behaves in this way. Like you, with SageMath-9.5 on Ubuntu, I did not have any problems launching the 3-manifolds version 9.4 on OS X 13.6/15.7. The issues seems to have started with their version 9.6, which I also couldn’t launch in a TeXmacs Sage session.

Maybe @darcy has an idea how to fix this os.environ.get(“TEXMACS_HOME_PATH”) + “/plugins/tmpy” TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’ issue.

Cheers
Tilda

I was having an exchange on this issue with Marc Culler, the maintainer of the 3-manifolds binary release of SageMath-9.7 for macOS. (FYI: https://github.com/3-manifolds/Sage_macOS/issues/48)

He confirmed that there has been some reorganization in the way environment variables are set so that the SageMath app launches with a minimal environment. But this should in principle not impair launching a sage session.

The problem rather seems to be that TeXmacs, i.e. tm_sage.py is not correctly initializing its environment. This is made more complicated by the fact that TeXmacs is using python 2.7 internally, even though the plugins only support python 3 sessions. (tm_sage.py 's shebang is: #! /usr/bin/env python which gives python 2.7 on macOS.)

This is very all very confusing and seems to be a serious issue that needs to be addressed. @pireddag, @jeroen, @darcy, do you think I should file a bug report?

As a work-around which sets the two environment variables to their expected values Marc emailed a patch to tm_sage.py:

--- tm_sage.py.orig	2022-11-20 17:49:33
+++ tm_sage.py	2022-11-21 08:04:38
@@ -13,6 +13,8 @@
 import os
 import sys
 from os.path import exists
+os.environ["TEXMACS_HOME_PATH"] = os.path.join(os.environ["HOME"], ".TeXmacs")
+os.environ["TEXMACS_PATH"] = "/Applications/TeXmacs.app/Contents/Resources/share/TeXmacs"
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/")

He is fully aware of its limitations, but at least SageMath-9-7 is launching in a TeXmacs session now.

Cheers
Tilda

Hi Tilda,

The Sage script in TeXmacs is run with the command sage -python, so I believe the shebang is irrelevant here. It should use exactly the same version of Python as the one Sage does.

You could try this from a Scheme session to check:

(eval-system "sage -python -c \"import sys; print(sys.version)\"")

Running that command yields this output:

“3.10.5 (main, Aug 3 2022, 11:00:27) [Clang 13.0.0 (clang-1300.0.29.30)]”

which is the Python version SageMath-9-7 uses. So, I guess you’re right that the shebang in tm_sage.py is irrelevant in this context.

Even though, the unmodified tm_sage.py that ships with TeXmacs 2.1.2 does not open a sage session on macOS. Marc Culler’s patched version of it however does so, admittedly with a rather interesting warning note:

WARNING: You are under develop mode using
/Users/tilda/.TeXmacs/plugins/tmpy

SageMath version 9.7, Release Date: 2022-09-19
Python 3.10.5 (main, Aug 3 2022, 11:00:27)
[Clang 13.0.0 (clang-1300.0.29.30)]
SageMath plugin for TeXmacs.
Please see the documentation in Help -> Plugins -> Sage

Why am I all of a sudden in develop mode?

Cheers
Tilda

The reason for the unmodified script not working is that Sage is wiping the environment. This seems quite a drastic solution to their problem. It would be useful if they could provide a method to preserve at least some variables, such as ours.

About development mode, please check if you have a directory tmpy in your .TeXmacs/plugins.

Hi Tilda,

I had the same issue on my macbook (air 2020, macos 13.0.1). Marc Culler’s patch I found in this thread solved the issue for me as well (thanks for having started the discussion!). I don’t get the dev mode warning message though. The difference is I patched the global tm_sage.py, which is

/Applications/TeXmacs.app/Contents/Resources/share/TeXmacs/plugins/tmpy/session/tm_sage.py

on my box while you seem to have it in your user space (i.e. in yours .TeXmacs).

I don’t know why it is so, yet it does the trick. Hope this helps,

Chris

1 Like

The patch is useful as a workaround, but unfortunately it can’t be the final solution. The TEXMACS_HOME_PATH is not fixed to $HOME/.TeXmacs and the TEXMACS_PATH in it is MacOS-specific.

There needs to be a mechanism to pass this dynamic information to our script, for which environment variables are the most convenient method, but Sage is wiping them. Perhaps it will need to be passed as arguments to the script then.

Is this information that I should forward to Marc Culler?

Which of this is SageMath-9-7 related, which of this needs to be addressed in TeXmacs and the sage released plugins? Which relate to both?

Yes, I have.

Cheers Tilda

Hmm, I’ve just done a test with Sage 9.7 to check the statements in the github issue that the environment is reset:

export TEXMACS_HOME_PATH="abc"
sage --python -c "import os; print(os.environ['TEXMACS_HOME_PATH'])"

With Sage 9.7 on Linux this nicely outputs abc for me, indicating the variable was not reset. Could you please try this on MacOS?

Dear jeroen,

I ran these commands, both from within a TeXmacs shell session and on a terminal-shell. the output in both cases was:

Traceback (most recent call last):
File"<string>", line 1, in <module>
File “/private/var/tmp/sage-9.7-current/local/var/lib/sage/venv-python3.10.5/lib/python3.10/os.py”, line 679, in getitem
raise KeyError(key) from None
KeyError: ‘TEXMACS_HOME_PATH’

Okay, that’s really annoying. Judging from the issue comments, it seems Sage are looking into fixing this. I think we can hack around it by looking at os.path.realpath(__file__). I’ll give it a try.

1 Like

Something like this could work. The file postscript.py would still need fixing, so plotting won’t work.

Patch
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:
2 Likes

Thanks for the patch. How do I apply it? I’m a bit confused by

/a/plugins … /b/plugins …

Cheers Tilda

It depends where you want to apply the patch, your own copy of the plugin (in TEXMACS_HOME_PATH) or TeXmacs’ copy. Note that the copy installed by TeXmacs would be overwritten on updating.

In any case, go to the parent directory of the plugins directory, save the patch somewhere, and issue patch -p1 < my-patch-file.diff.

I decided to maintain the python powered plugin in https://github.com/texmacs/plugins-in-python

Wait me to spare time to migrate the SageMath plugin to the new github repo.

Your patch seems to be reasonable. Later, plugins in python will be independent of the GNU TeXmacs release cycle.

Thanks for the information, dear jeroen. I applied your patch. On launching a sage session using the patched tm_sage.py I got the following error message:

Traceback (most recent call last):
File “/Users/tilda/.TeXmacs/plugins/tmpy/session/tm_sage.py”, line 29, in <module>
from tmpy.postscript import ps_out, PSOutDummy, pdf_out, FileOutDummy
File “/Users/tilda/.TeXmacs/plugins/tmpy/postscript.py”, line 103, in <module>
pdf_out_tmp_file = os.getenv(“TEXMACS_HOME_PATH”) + “/system/tmp/” + pdf_out_tmp_file
TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’

Seems as if macOS requires a fixed postscript.py to get the session started in the first place.

Cheers Tilda

It is correct that the macOS SageMath launches Sage with a controlled minimal environment, and that this was a change from 9.4. This was done mainly to prevent crashes that were being caused by environments that were linked to old Sage installations. A planned enhancement to the SageMath app is to allow users to specify environment variables to be added to this minimal environment via an app preferences setting. That will probably help with this problem, although I did not realize that this would need to apply to sage -python as well as to sage itself. I will make sure that happens when I write the enhancement.

Nonetheless, it seems to me that TeXmacs would do well to have a fallback environment that gets used if the required variables turn out to be undefined. Reverting to a usable default seems much better than crashing.

1 Like