From 8b2cbbc5830c425c52e29f8ba0b5519337cb8894 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 5 Oct 2019 22:29:00 +0200 Subject: [PATCH] Use a monotonic time for registry cache. So an old cache does not take precedence over a newly set value. I noticed this bug because of the time going backward in tests because of the time.time mock introduced in dcf55cf6deb90f2498e53068175562aa36d315cc, but this may happen in production systems too. Also adds another deprecation warning for python < 3.3. --- setup.py | 5 +++++ src/registry.py | 12 +++++++++--- src/test.py | 1 - 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 02e9064e2..b4ff8ebc3 100644 --- a/setup.py +++ b/setup.py @@ -267,5 +267,10 @@ if sys.version_info < (3, 0): 'bug at and we\'ll ' 'see together what we can do about it.', DeprecationWarning) +if sys.version_info < (3, 3): + warnings.warn('You are installing Limnoria for Python < 3.3, which is ' + 'only partially support. Limnoria requires Python 3.4 or later ' + 'to be fully stable.', + DeprecationWarning) # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/src/registry.py b/src/registry.py index c37bf0312..1208ef2e1 100644 --- a/src/registry.py +++ b/src/registry.py @@ -70,6 +70,12 @@ ENCODING = 'string_escape' if minisix.PY2 else 'unicode_escape' decoder = codecs.getdecoder(ENCODING) encoder = codecs.getencoder(ENCODING) +if hasattr(time, 'monotonic'): + monotonic_time = time.monotonic +else: + # fallback for python < 3.3 + monotonic_time = time.time + _cache = utils.InsensitivePreservingDict() _lastModified = 0 def open_registry(filename, clear=False): @@ -105,7 +111,7 @@ def open_registry(filename, clear=False): except ValueError: raise InvalidRegistryFile('Error unpacking line %r' % acc) _cache[key] = value - _lastModified = time.time() + _lastModified = monotonic_time() _fd.close() CONF_FILE_HEADER = """ @@ -435,7 +441,7 @@ class Value(Group): if self._name == 'unset': self._lastModified = 0 self.__parent.setName(*args) - self._lastModified = time.time() + self._lastModified = monotonic_time() def set(self, s): """Override this with a function to convert a string to whatever type @@ -456,7 +462,7 @@ class Value(Group): inherited=True means the value is inherited from the parent, so if the parent gets a new value, this group will get the new value as well.""" - self._lastModified = time.time() + self._lastModified = monotonic_time() self.value = v if self._supplyDefault: for (name, child) in list(self._children.items()): diff --git a/src/test.py b/src/test.py index 820ae978b..1127e71ac 100644 --- a/src/test.py +++ b/src/test.py @@ -87,7 +87,6 @@ def timeFastForward(extra_offset): mock_time_offset += extra_offset def setupMockTime(): - mock_time_offset = 0 time.time = mockTime def teardownMockTime():