Skip to content

Commit

Permalink
Trac #33256: buffer.py: make files readonly only when possible
Browse files Browse the repository at this point in the history
After #31306 we have doctest failure on sage-on-distros because files
have been moved out of `SAGE_EXTCODE`. In
`sage/repl/rich_output/buffer.py` files are made readonly with `chmod`
so they are immutable during the process. Unfortunately `chmod` fails
when trying to act on system installed files as a regular user. There is
a safety for files in `SAGE_EXTCODE`, this is a useless complication and
should be replaced by a `try` block.

URL: https://trac.sagemath.org/33256
Reported by: fbissey
Ticket author(s): François Bissey
Reviewer(s): Antonio Rojas
  • Loading branch information
Release Manager committed Feb 13, 2022
2 parents 7f8b268 + 2c3a144 commit 8326dee
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/sage/repl/rich_output/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,20 @@ def _chmod_readonly(cls, filename):
sage: stat.S_IMODE(os.stat(tmp).st_mode) & (stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
0
"""
from sage.env import SAGE_EXTCODE
from sage.env import SAGE_SRC
filename = os.path.abspath(filename)
if filename.startswith(os.path.abspath(SAGE_EXTCODE)):
if filename.startswith(os.path.abspath(SAGE_SRC)):
# Do not change permissions on the sample rich output
# files, as it will cause trouble when upgrading Sage
return
import stat
mode = os.stat(filename).st_mode
mode = stat.S_IMODE(mode) & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
os.chmod(filename, mode)
# The file may already be read only for that user
try:
os.chmod(filename, mode)
except PermissionError:
pass

def _repr_(self):
"""
Expand Down

0 comments on commit 8326dee

Please sign in to comment.