Ticket #7514: sagenb-7514-rebase-part2.patch

File sagenb-7514-rebase-part2.patch, 3.5 KB (added by was, 11 years ago)

trac 7514 sagenb-part2: proper tracebacks; make source code introspection of functions defined in \ the notebook finally work in the notebook; properly cleanup worksheet files when notebook server is killed.

  • sagenb/notebook/run_notebook.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1262288009 28800
    # Node ID d5c94309582791ac3c3585d5edb5102c1bdd42c4
    # Parent  f19a0ed4113db84008f43930ffac2204cb8d361a
    trac 7514 sagenb-part2: proper tracebacks; make source code introspection of functions defined in the notebook finally work in the notebook; properly cleanup worksheet files when notebook server is killed.
    
    diff --git a/sagenb/notebook/run_notebook.py b/sagenb/notebook/run_notebook.py
    a b  
    185185import signal, sys, random
    186186def save_notebook():
    187187    from twisted.internet.error import ReactorNotRunning
     188    print "Quitting all running worksheets..."
     189    twist.notebook.quit()
    188190    print "Saving notebook..."
    189191    twist.notebook.save()
    190192    try:
  • sagenb/notebook/worksheet.py

    diff --git a/sagenb/notebook/worksheet.py b/sagenb/notebook/worksheet.py
    a b  
    4949whitespace = re.compile('\s')  # Match any whitespace character
    5050non_whitespace = re.compile('\S')
    5151
     52# The file to which the Sage code that will be evaluated is written.
     53CODE_PY = "___code___.py"
     54
    5255# Constants that control the behavior of the worksheet.
    5356INTERRUPT_TRIES = 3    # number of times to send control-c to subprocess before giving up
    5457INITIAL_NUM_CELLS = 1  # number of empty cells in new worksheets
     
    31793182        if C.is_no_output():
    31803183            # Clean up the temp directories associated to C, and do not set any output
    31813184            # text that C might have got.
    3182             shutil.rmtree(self.cell_directory(C), ignore_errors=True)
     3185            d = self.cell_directory(C)
     3186            for X in os.path.listdir(d):
     3187                if os.path.split(X)[-1] != CODE_PY:
     3188                    Y = os.path.join(d, X)
     3189                    if os.path.isfile(Y):
     3190                        try: os.unlink(Y)
     3191                        except: pass
     3192                    else:
     3193                        shutil.rmtree(Y, ignore_errors=True)
    31833194            return 'd', C
    31843195       
    31853196        if not C.introspect():
     
    31903201                if not os.path.exists(cell_dir):
    31913202                    os.makedirs(cell_dir)
    31923203                for X in filenames:
     3204                    if os.path.split(X)[-1] == CODE_PY: continue
    31933205                    target = os.path.join(cell_dir, os.path.split(X)[1])
    31943206                    try:
    31953207                        if os.path.exists(target):
     
    32023214                        else:
    32033215                            shutil.copy(X, target)
    32043216                        set_restrictive_permissions(target)
     3217                        if os.path.isfile(X):
     3218                            try: os.unlink(X)
     3219                            except: pass
     3220                        else:
     3221                            shutil.rmtree(X, ignore_errors=True)
    32053222                    except Exception, msg:
    32063223                        print "Error copying file from worksheet process:", msg
    32073224            # Generate html, etc.
     
    36223639        return support.get_rightmost_identifier(s)
    36233640
    36243641    def preparse(self, s):
    3625         return 'exec _support_.preparse_worksheet_cell(base64.b64decode("%s"),globals())'%base64.b64encode(s)
     3642        return 'open("%s","w").write(_support_.preparse_worksheet_cell(base64.b64decode("%s"),globals())); execfile(os.path.abspath("%s"))'%(CODE_PY, base64.b64encode(s), CODE_PY)
    36263643
    36273644    ##########################################################
    36283645    # Loading and attaching files