If we are speaking of "fixing 1437" properly, I have to say that the more I 
think about this bug, the more I am sure that the real problem is not 
inside createFirstTreeNode. This method is doing a quite normal, natural 
thing. 

It should not be an error to have two nodes with the same gnx in two 
separate outlines. The bug 1437 is about exactly this. Commander created 
using leoBridge, has an outline with some freshly created gnx, then it 
replaces the whole outline with the new one read from the file. If that 
outline (read from the file) contains the node with the exactly same gnx, 
it fails to read it correctly.

I think there is no sense in keeping the content of c.fileCommands.gnxDict 
when loading completely new outline. If it is cleared before inside 
openLeoFile, before calling getLeoFile then it won't cause a gnx clash, and 
the outline will be read correctly.

def openLeoFile(self, theFile, fileName, readAtFileNodesFlag=True, silent=
False):
    """Open a Leo file."""
    c, frame = self.c, self.c.frame
    # Set c.openDirectory
    theDir = g.os_path_dirname(fileName)
    if theDir:
        c.openDirectory = c.frame.openDirectory = theDir
    # Get the file.
    *self.gnxDict = {} # clear gnxDict before replacing the whole outline*
    ok, ratio = self.getLeoFile(
        theFile, fileName,
        readAtFileNodesFlag=readAtFileNodesFlag,
        silent=silent,
    )
    if ok:
        frame.resizePanesToRatio(ratio, frame.secondary_ratio)
    return ok

Using the following script to check:
import sys
sys.path.insert(0, '/home/vitalije/programi/leo/bug1437')
import leo.core.leoBridge as leoBridge
import leo.core.leoNodes as leoNodes
import pytest

def bridge():
    return leoBridge.controller(gui='nullGui',
        loadPlugins=False, readSettings=False,
        silent=False, verbose=False, useCaches=False)

def test_1437(fileName):
    # Create, save and close outline with a single node
    br = bridge()
    g = br.g
    c = br.openLeoFile(fileName)
    p = c.rootPosition()
    p1 = p.insertAfter()
    p1.h = "1"
    p.doDelete(p1)
    assert c.rootPosition().h == "1"
    c.save()
    c.close()
    g.app.nodeIndices = leoNodes.NodeIndices(g.app.leoID)
    # Reset the global state so a new bridge will come
    # with fresh nodeIndices. This simulates
    # SegundoBob's launching bridge consecutively in
    # multiple processes
    leoBridge.gBridgeController = None

    # Read the file and verify the headline is "1"
    c = bridge().openLeoFile(fileName)
    assert c.rootPosition().h == "1"
if __name__ == '__main__':
    try:
        for i in range(100):
            test_1437('/tmp/tree1.leo')
    except KeyboardInterrupt:
        sys.exit(0)


Without the line *self.gnxDict = {}*, assertion error happens almost always 
during the loop. With that line it doesn't. So, I guess it solves the 1437.
All unit tests pass.

Vitalije


-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/9e6d9ed0-4857-4e92-aa70-4b13eaf57c03%40googlegroups.com.

Reply via email to