From 8feaf0856b7c059c35b6f529b2575c7a7b256895 Mon Sep 17 00:00:00 2001 From: haoping Date: Wed, 4 Aug 2021 18:25:46 -0500 Subject: [PATCH 1/2] fix prefix_range_end --- etcd3/exceptions.py | 4 ++++ etcd3/utils.py | 7 +++++-- tests/test_etcd3.py | 6 ++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/etcd3/exceptions.py b/etcd3/exceptions.py index ba95ed20..952fa312 100644 --- a/etcd3/exceptions.py +++ b/etcd3/exceptions.py @@ -32,3 +32,7 @@ def __init__(self, compacted_revision): class NoServerAvailableError(Etcd3Exception): pass + + +class NoPrefixEndError(Etcd3Exception): + pass diff --git a/etcd3/utils.py b/etcd3/utils.py index 5b9068eb..0c40d022 100644 --- a/etcd3/utils.py +++ b/etcd3/utils.py @@ -1,11 +1,14 @@ +import etcd3.exceptions as exceptions + + def prefix_range_end(prefix): """Create a bytestring that can be used as a range_end for a prefix.""" s = bytearray(prefix) for i in reversed(range(len(s))): if s[i] < 0xff: s[i] = s[i] + 1 - break - return bytes(s) + return s[:i + 1] + raise exceptions.NoPrefixEndError() def to_bytes(maybe_bytestring): diff --git a/tests/test_etcd3.py b/tests/test_etcd3.py index 1f5e5040..6f7dfe6a 100644 --- a/tests/test_etcd3.py +++ b/tests/test_etcd3.py @@ -1119,9 +1119,11 @@ def test_disarm_alarm(self, etcd): class TestUtils(object): def test_prefix_range_end(self): assert etcd3.utils.prefix_range_end(b'foo') == b'fop' - assert etcd3.utils.prefix_range_end(b'ab\xff') == b'ac\xff' + assert etcd3.utils.prefix_range_end(b'ab\xff') == b'ac' assert (etcd3.utils.prefix_range_end(b'a\xff\xff\xff\xff\xff') - == b'b\xff\xff\xff\xff\xff') + == b'b') + with pytest.raises(expected_exception=etcd3.exceptions.NoPrefixEndError) : + etcd3.utils.prefix_range_end(b'\xff\xff\xff\xff\xff') def test_to_bytes(self): assert isinstance(etcd3.utils.to_bytes(b'doot'), bytes) is True From b694fd8a465787dfec191a6eaf69919019b5eb89 Mon Sep 17 00:00:00 2001 From: haoping Date: Sat, 7 Aug 2021 08:04:14 -0500 Subject: [PATCH 2/2] prefix end of all 0xff set to b'\0' --- etcd3/exceptions.py | 4 ---- etcd3/utils.py | 5 +---- tests/test_etcd3.py | 3 +-- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/etcd3/exceptions.py b/etcd3/exceptions.py index 952fa312..ba95ed20 100644 --- a/etcd3/exceptions.py +++ b/etcd3/exceptions.py @@ -32,7 +32,3 @@ def __init__(self, compacted_revision): class NoServerAvailableError(Etcd3Exception): pass - - -class NoPrefixEndError(Etcd3Exception): - pass diff --git a/etcd3/utils.py b/etcd3/utils.py index 0c40d022..a33b658c 100644 --- a/etcd3/utils.py +++ b/etcd3/utils.py @@ -1,6 +1,3 @@ -import etcd3.exceptions as exceptions - - def prefix_range_end(prefix): """Create a bytestring that can be used as a range_end for a prefix.""" s = bytearray(prefix) @@ -8,7 +5,7 @@ def prefix_range_end(prefix): if s[i] < 0xff: s[i] = s[i] + 1 return s[:i + 1] - raise exceptions.NoPrefixEndError() + return b'\0' def to_bytes(maybe_bytestring): diff --git a/tests/test_etcd3.py b/tests/test_etcd3.py index 6f7dfe6a..0c3e7a12 100644 --- a/tests/test_etcd3.py +++ b/tests/test_etcd3.py @@ -1122,8 +1122,7 @@ def test_prefix_range_end(self): assert etcd3.utils.prefix_range_end(b'ab\xff') == b'ac' assert (etcd3.utils.prefix_range_end(b'a\xff\xff\xff\xff\xff') == b'b') - with pytest.raises(expected_exception=etcd3.exceptions.NoPrefixEndError) : - etcd3.utils.prefix_range_end(b'\xff\xff\xff\xff\xff') + assert b'\0' == etcd3.utils.prefix_range_end(b'\xff\xff\xff\xff\xff') def test_to_bytes(self): assert isinstance(etcd3.utils.to_bytes(b'doot'), bytes) is True