From 5797b8655dc638cdb25796e96a7244865806c085 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 6 May 2021 21:58:08 +0200 Subject: [PATCH 1/3] {#769) Fix problem with handle collisions when generated rapidly in Windows --- nw/core/tree.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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) From cc8fc3873429584afc067e50c90cb9dae44847ac Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 6 May 2021 21:58:35 +0200 Subject: [PATCH 2/3] Update the handle generator test --- tests/test_core/test_core_tree.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) 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 From f605f1fc75a2202540a4e041215a7e2c4ca74b0a Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 6 May 2021 22:02:59 +0200 Subject: [PATCH 3/3] Solve an unwante error message when creating a new project --- nw/guimain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)