Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #33213: move sage.interfaces.cleaner.cleaner() to quit interface.
Browse files Browse the repository at this point in the history
With sage-cleaner on the chopping block, its interface is going to
need to be removed eventually. Part of it is still used: our expect
interface allows instances to register themselves to be killed with
the expect_quitall() function in sage.interfaces.quit. To keep that
working, we move/rename the function that registers a a process to
register_spawned_process() in the module sage.interfaces.quit.
  • Loading branch information
orlitzky authored and dimpase committed Jun 13, 2022
1 parent c8c9d40 commit ec1a3cf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
16 changes: 0 additions & 16 deletions src/sage/interfaces/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@
SAGE_SPAWNED_PROCESS_FILE = os.path.join(_spd.name, "spawned_processes")
atexit.register(lambda: _spd.cleanup())

def cleaner(pid, cmd=''):
"""
Write a line to the ``spawned_processes`` file with the given
``pid`` and ``cmd``.
"""
if cmd != '':
cmd = cmd.strip().split()[0]
# This is safe, since only this process writes to this file.
try:
with open(SAGE_SPAWNED_PROCESS_FILE, 'a') as o:
o.write('%s %s\n'%(pid, cmd))
except IOError:
pass
else:
start_cleaner()


def start_cleaner():
"""
Expand Down
3 changes: 1 addition & 2 deletions src/sage/interfaces/expect.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import time
import gc
from . import quit
from . import cleaner
from random import randrange

import pexpect
Expand Down Expand Up @@ -518,7 +517,7 @@ def _start(self, alt_message=None, block_during_init=True):
os.chdir(currentdir)

if self._do_cleaner():
cleaner.cleaner(self._expect.pid, cmd)
quit.register_spawned_process(self._expect.pid, cmd)

try:
self._expect.expect(self._prompt)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/interfaces/qsieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def data_to_list(out, n, time):

from sage.interfaces.sagespawn import SageSpawn
import pexpect
from . import cleaner
from . import quit
class qsieve_nonblock:
"""
A non-blocking version of Hart's quadratic sieve.
Expand Down Expand Up @@ -165,7 +165,7 @@ def __init__(self, n, time):
env = os.environ.copy()
env['TMPDIR'] = tmp_dir('qsieve')
self._p = SageSpawn(cmd, env=env)
cleaner.cleaner(self._p.pid, 'QuadraticSieve')
quit.register_spawned_process(self._p.pid, 'QuadraticSieve')
self._p.sendline(str(self._n)+'\n\n\n')
self._done = False
self._out = ''
Expand Down
29 changes: 26 additions & 3 deletions src/sage/interfaces/quit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,33 @@

import os

expect_objects = []
import atexit, tempfile
_spd = tempfile.TemporaryDirectory()
SAGE_SPAWNED_PROCESS_FILE = os.path.join(_spd.name, "spawned_processes")
atexit.register(lambda: _spd.cleanup())

def register_spawned_process(pid, cmd=''):
"""
Write a line to the ``spawned_processes`` file with the given
``pid`` and ``cmd``.
"""
if cmd != '':
cmd = cmd.strip().split()[0]
# This is safe, since only this process writes to this file.
try:
with open(SAGE_SPAWNED_PROCESS_FILE, 'a') as o:
o.write('%s %s\n'%(pid, cmd))
except IOError:
pass
else:
# If sage is being used as a python library, we need to launch
# the cleaner ourselves upon being told that there will be
# something to clean.
from sage.interfaces.cleaner import start_cleaner
start_cleaner()


expect_objects = []

def expect_quitall(verbose=False):
"""
Expand Down Expand Up @@ -63,8 +88,6 @@ def kill_spawned_jobs(verbose=False):
sage: sage.interfaces.quit.expect_quitall()
"""
from sage.interfaces.cleaner import SAGE_SPAWNED_PROCESS_FILE

if not os.path.exists(SAGE_SPAWNED_PROCESS_FILE):
return
with open(SAGE_SPAWNED_PROCESS_FILE) as f:
Expand Down
4 changes: 2 additions & 2 deletions src/sage/interfaces/rubik.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import time
import shlex

from . import cleaner
from . import quit

from sage.cpython.string import bytes_to_str
from sage.groups.perm_gps.cubegroup import index2singmaster
Expand Down Expand Up @@ -109,7 +109,7 @@ def __init__(self, verbose=False, wait=True):
def start(self):
cmd = shlex.quote(sage.features.rubiks.optimal().absolute_filename())
child = pexpect.spawn(cmd)
cleaner.cleaner(child.pid, cmd)
quit.register_spawned_process(child.pid, cmd)
child.timeout = None
self.child = child
self._ready = False
Expand Down

0 comments on commit ec1a3cf

Please sign in to comment.