diff --git a/nw/core/tree.py b/nw/core/tree.py index b0c0340c..9a6c3050 100644 --- a/nw/core/tree.py +++ b/nw/core/tree.py @@ -77,6 +77,7 @@ class NWTree(): self._treeChanged = False # True if tree structure has changed self._handleSeed = None # Used for generating handles for testing + self._handleCount = 0 # A counter that is added to the handle generator return @@ -515,7 +516,8 @@ class NWTree(): handle requests come faster than the clock resolution. """ if self._handleSeed is None: - newSeed = str(time()) + addSeed + newSeed = "%s_%d_%s" % (str(time()), self._handleCount, addSeed) + self._handleCount += 1 else: # This is used for debugging newSeed = str(self._handleSeed) diff --git a/nw/guimain.py b/nw/guimain.py index 300c00a7..af4ac35b 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -371,9 +371,9 @@ class GuiMain(QMainWindow): logger.info("Creating new project") if self.theProject.newProject(projData): + self.hasProject = True self.rebuildTrees() self.saveProject() - self.hasProject = True self.docEditor.setDictionaries() self.rebuildIndex(beQuiet=True) self.statusBar.setRefTime(self.theProject.projOpened) diff --git a/tests/test_core/test_core_tree.py b/tests/test_core/test_core_tree.py index 803e88c4..c2d15ed7 100644 --- a/tests/test_core/test_core_tree.py +++ b/tests/test_core/test_core_tree.py @@ -24,6 +24,7 @@ import os import pytest from lxml import etree +from hashlib import sha256 from nw.core.project import NWProject, NWItem, NWTree from nw.enum import nwItemClass, nwItemType, nwItemLayout @@ -394,22 +395,33 @@ def testCoreTree_MakeHandles(monkeypatch, dummyGUI): tHandle = theTree._makeHandle() assert tHandle == "73475cb40a568" - # Add the next in line to the project to foprce duplicate + # Add the next in line to the project to force duplicate theTree._projTree["44cb730c42048"] = None tHandle = theTree._makeHandle() assert tHandle == "71ee45a3c0db9" # Fix the time() function and force a handle collission theTree.setSeed(None) + theTree._handleCount = 0 monkeypatch.setattr("nw.core.tree.time", lambda: 123.4) tHandle = theTree._makeHandle() theTree._projTree[tHandle] = None - assert tHandle == "5f466d7afa48b" + newSeed = "123.4_0_" + assert tHandle == sha256(newSeed.encode()).hexdigest()[0:13] tHandle = theTree._makeHandle() theTree._projTree[tHandle] = None - assert tHandle == "a79acf4c634a7" + newSeed = "123.4_1_" + assert tHandle == sha256(newSeed.encode()).hexdigest()[0:13] + + # Reset the count and the handle for 0 and 1 should be duplicates + # which forces the function to add the '!' + theTree._handleCount = 0 + tHandle = theTree._makeHandle() + theTree._projTree[tHandle] = None + newSeed = "123.4_1_!" + assert tHandle == sha256(newSeed.encode()).hexdigest()[0:13] # END Test testCoreTree_MakeHandles