Skip to content

Commit f71da5f

Browse files
authored
Merge pull request #678 from a-ungurianu/devx/bye_bye_six
refactor: Remove vestiges of python2 support
2 parents 4b13135 + 74ecc0f commit f71da5f

File tree

13 files changed

+71
-197
lines changed

13 files changed

+71
-197
lines changed

kazoo/client.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import re
88
import warnings
99

10-
import six
11-
1210
from kazoo.exceptions import (
1311
AuthFailedError,
1412
ConfigurationError,
@@ -66,9 +64,6 @@
6664
from kazoo.recipe.watchers import ChildrenWatch, DataWatch
6765

6866

69-
string_types = six.string_types
70-
bytes_types = (six.binary_type,)
71-
7267
CLOSED_STATES = (
7368
KeeperState.EXPIRED_SESSION,
7469
KeeperState.AUTH_FAILED,
@@ -415,10 +410,10 @@ def _reset(self):
415410

416411
def _reset_watchers(self):
417412
watchers = []
418-
for child_watchers in six.itervalues(self._child_watchers):
413+
for child_watchers in self._child_watchers.values():
419414
watchers.extend(child_watchers)
420415

421-
for data_watchers in six.itervalues(self._data_watchers):
416+
for data_watchers in self._data_watchers.values():
422417
watchers.extend(data_watchers)
423418

424419
self._child_watchers = defaultdict(set)
@@ -821,7 +816,7 @@ def _is_valid(version):
821816
version = _try_fetch()
822817
if _is_valid(version):
823818
return version
824-
for _i in six.moves.range(0, retries):
819+
for _i in range(0, retries):
825820
version = _try_fetch()
826821
if _is_valid(version):
827822
return version
@@ -854,9 +849,9 @@ def add_auth_async(self, scheme, credential):
854849
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
855850
856851
"""
857-
if not isinstance(scheme, string_types):
852+
if not isinstance(scheme, str):
858853
raise TypeError("Invalid type for 'scheme' (string expected)")
859-
if not isinstance(credential, string_types):
854+
if not isinstance(credential, str):
860855
raise TypeError("Invalid type for 'credential' (string expected)")
861856

862857
# we need this auth data to re-authenticate on reconnect
@@ -1034,15 +1029,15 @@ def create_async(
10341029
if acl is None and self.default_acl:
10351030
acl = self.default_acl
10361031

1037-
if not isinstance(path, string_types):
1032+
if not isinstance(path, str):
10381033
raise TypeError("Invalid type for 'path' (string expected)")
10391034
if acl and (
10401035
isinstance(acl, ACL) or not isinstance(acl, (tuple, list))
10411036
):
10421037
raise TypeError(
10431038
"Invalid type for 'acl' (acl must be a tuple/list" " of ACL's"
10441039
)
1045-
if value is not None and not isinstance(value, bytes_types):
1040+
if value is not None and not isinstance(value, bytes):
10461041
raise TypeError("Invalid type for 'value' (must be a byte string)")
10471042
if not isinstance(ephemeral, bool):
10481043
raise TypeError("Invalid type for 'ephemeral' (bool expected)")
@@ -1205,7 +1200,7 @@ def exists_async(self, path, watch=None):
12051200
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
12061201
12071202
"""
1208-
if not isinstance(path, string_types):
1203+
if not isinstance(path, str):
12091204
raise TypeError("Invalid type for 'path' (string expected)")
12101205
if watch and not callable(watch):
12111206
raise TypeError("Invalid type for 'watch' (must be a callable)")
@@ -1248,7 +1243,7 @@ def get_async(self, path, watch=None):
12481243
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
12491244
12501245
"""
1251-
if not isinstance(path, string_types):
1246+
if not isinstance(path, str):
12521247
raise TypeError("Invalid type for 'path' (string expected)")
12531248
if watch and not callable(watch):
12541249
raise TypeError("Invalid type for 'watch' (must be a callable)")
@@ -1304,7 +1299,7 @@ def get_children_async(self, path, watch=None, include_data=False):
13041299
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
13051300
13061301
"""
1307-
if not isinstance(path, string_types):
1302+
if not isinstance(path, str):
13081303
raise TypeError("Invalid type for 'path' (string expected)")
13091304
if watch and not callable(watch):
13101305
raise TypeError("Invalid type for 'watch' (must be a callable)")
@@ -1346,7 +1341,7 @@ def get_acls_async(self, path):
13461341
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
13471342
13481343
"""
1349-
if not isinstance(path, string_types):
1344+
if not isinstance(path, str):
13501345
raise TypeError("Invalid type for 'path' (string expected)")
13511346

13521347
async_result = self.handler.async_result()
@@ -1389,7 +1384,7 @@ def set_acls_async(self, path, acls, version=-1):
13891384
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
13901385
13911386
"""
1392-
if not isinstance(path, string_types):
1387+
if not isinstance(path, str):
13931388
raise TypeError("Invalid type for 'path' (string expected)")
13941389
if isinstance(acls, ACL) or not isinstance(acls, (tuple, list)):
13951390
raise TypeError(
@@ -1447,9 +1442,9 @@ def set_async(self, path, value, version=-1):
14471442
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
14481443
14491444
"""
1450-
if not isinstance(path, string_types):
1445+
if not isinstance(path, str):
14511446
raise TypeError("Invalid type for 'path' (string expected)")
1452-
if value is not None and not isinstance(value, bytes_types):
1447+
if value is not None and not isinstance(value, bytes):
14531448
raise TypeError("Invalid type for 'value' (must be a byte string)")
14541449
if not isinstance(version, int):
14551450
raise TypeError("Invalid type for 'version' (int expected)")
@@ -1523,7 +1518,7 @@ def delete_async(self, path, version=-1):
15231518
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
15241519
15251520
"""
1526-
if not isinstance(path, string_types):
1521+
if not isinstance(path, str):
15271522
raise TypeError("Invalid type for 'path' (string expected)")
15281523
if not isinstance(version, int):
15291524
raise TypeError("Invalid type for 'version' (int expected)")
@@ -1632,11 +1627,11 @@ def reconfig_async(self, joining, leaving, new_members, from_config):
16321627
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
16331628
16341629
"""
1635-
if joining and not isinstance(joining, string_types):
1630+
if joining and not isinstance(joining, str):
16361631
raise TypeError("Invalid type for 'joining' (string expected)")
1637-
if leaving and not isinstance(leaving, string_types):
1632+
if leaving and not isinstance(leaving, str):
16381633
raise TypeError("Invalid type for 'leaving' (string expected)")
1639-
if new_members and not isinstance(new_members, string_types):
1634+
if new_members and not isinstance(new_members, str):
16401635
raise TypeError(
16411636
"Invalid type for 'new_members' (string " "expected)"
16421637
)
@@ -1690,13 +1685,13 @@ def create(
16901685
if acl is None and self.client.default_acl:
16911686
acl = self.client.default_acl
16921687

1693-
if not isinstance(path, string_types):
1688+
if not isinstance(path, str):
16941689
raise TypeError("Invalid type for 'path' (string expected)")
16951690
if acl and not isinstance(acl, (tuple, list)):
16961691
raise TypeError(
16971692
"Invalid type for 'acl' (acl must be a tuple/list" " of ACL's"
16981693
)
1699-
if not isinstance(value, bytes_types):
1694+
if not isinstance(value, bytes):
17001695
raise TypeError("Invalid type for 'value' (must be a byte string)")
17011696
if not isinstance(ephemeral, bool):
17021697
raise TypeError("Invalid type for 'ephemeral' (bool expected)")
@@ -1722,7 +1717,7 @@ def delete(self, path, version=-1):
17221717
`recursive`.
17231718
17241719
"""
1725-
if not isinstance(path, string_types):
1720+
if not isinstance(path, str):
17261721
raise TypeError("Invalid type for 'path' (string expected)")
17271722
if not isinstance(version, int):
17281723
raise TypeError("Invalid type for 'version' (int expected)")
@@ -1733,9 +1728,9 @@ def set_data(self, path, value, version=-1):
17331728
arguments as :meth:`KazooClient.set`.
17341729
17351730
"""
1736-
if not isinstance(path, string_types):
1731+
if not isinstance(path, str):
17371732
raise TypeError("Invalid type for 'path' (string expected)")
1738-
if not isinstance(value, bytes_types):
1733+
if not isinstance(value, bytes):
17391734
raise TypeError("Invalid type for 'value' (must be a byte string)")
17401735
if not isinstance(version, int):
17411736
raise TypeError("Invalid type for 'version' (int expected)")
@@ -1750,7 +1745,7 @@ def check(self, path, version):
17501745
does not match the specified version.
17511746
17521747
"""
1753-
if not isinstance(path, string_types):
1748+
if not isinstance(path, str):
17541749
raise TypeError("Invalid type for 'path' (string expected)")
17551750
if not isinstance(version, int):
17561751
raise TypeError("Invalid type for 'version' (int expected)")

kazoo/handlers/eventlet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""A eventlet based handler."""
22
from __future__ import absolute_import
33

4+
import atexit
45
import contextlib
56
import logging
67

@@ -12,7 +13,6 @@
1213
from eventlet import queue as green_queue
1314

1415
from kazoo.handlers import utils
15-
import kazoo.python2atexit as python2atexit
1616
from kazoo.handlers.utils import selector_select
1717

1818
LOG = logging.getLogger(__name__)
@@ -140,15 +140,15 @@ def start(self):
140140
w = eventlet.spawn(self._process_callback_queue)
141141
self._workers.append((w, self.callback_queue))
142142
self._started = True
143-
python2atexit.register(self.stop)
143+
atexit.register(self.stop)
144144

145145
def stop(self):
146146
while self._workers:
147147
w, q = self._workers.pop()
148148
q.put(_STOP)
149149
w.wait()
150150
self._started = False
151-
python2atexit.unregister(self.stop)
151+
atexit.unregister(self.stop)
152152

153153
def socket(self, *args, **kwargs):
154154
return utils.create_tcp_socket(green_socket)

kazoo/handlers/gevent.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""A gevent based handler."""
22
from __future__ import absolute_import
33

4+
import atexit
45
import logging
56

67
import gevent
@@ -13,13 +14,9 @@
1314

1415
from kazoo.handlers.utils import selector_select
1516

16-
try:
17-
from gevent.lock import Semaphore, RLock
18-
except ImportError:
19-
from gevent.coros import Semaphore, RLock
17+
from gevent.lock import Semaphore, RLock
2018

2119
from kazoo.handlers import utils
22-
from kazoo import python2atexit
2320

2421
_using_libevent = gevent.__version__.startswith("0.")
2522

@@ -104,7 +101,7 @@ def start(self):
104101
for queue in (self.callback_queue,):
105102
w = self._create_greenlet_worker(queue)
106103
self._workers.append(w)
107-
python2atexit.register(self.stop)
104+
atexit.register(self.stop)
108105

109106
def stop(self):
110107
"""Stop the greenlet workers and empty all queues."""
@@ -124,7 +121,7 @@ def stop(self):
124121
# Clear the queues
125122
self.callback_queue = self.queue_impl() # pragma: nocover
126123

127-
python2atexit.unregister(self.stop)
124+
atexit.unregister(self.stop)
128125

129126
def select(self, *args, **kwargs):
130127
return selector_select(

kazoo/handlers/threading.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,16 @@
1212
"""
1313
from __future__ import absolute_import
1414

15+
import atexit
1516
import logging
17+
import queue
1618
import socket
1719
import threading
1820
import time
1921

20-
import six
21-
22-
import kazoo.python2atexit as python2atexit
2322
from kazoo.handlers import utils
2423
from kazoo.handlers.utils import selector_select
2524

26-
try:
27-
import Queue
28-
except ImportError: # pragma: nocover
29-
import queue as Queue
3025

3126
# sentinel objects
3227
_STOP = object()
@@ -35,11 +30,11 @@
3530

3631

3732
def _to_fileno(obj):
38-
if isinstance(obj, six.integer_types):
33+
if isinstance(obj, int):
3934
fd = int(obj)
4035
elif hasattr(obj, "fileno"):
4136
fd = obj.fileno()
42-
if not isinstance(fd, six.integer_types):
37+
if not isinstance(fd, int):
4338
raise TypeError("fileno() returned a non-integer")
4439
fd = int(fd)
4540
else:
@@ -98,8 +93,8 @@ class SequentialThreadingHandler(object):
9893
name = "sequential_threading_handler"
9994
timeout_exception = KazooTimeoutError
10095
sleep_func = staticmethod(time.sleep)
101-
queue_impl = Queue.Queue
102-
queue_empty = Queue.Empty
96+
queue_impl = queue.Queue
97+
queue_empty = queue.Empty
10398

10499
def __init__(self):
105100
"""Create a :class:`SequentialThreadingHandler` instance"""
@@ -113,19 +108,19 @@ def __init__(self):
113108
def running(self):
114109
return self._running
115110

116-
def _create_thread_worker(self, queue):
111+
def _create_thread_worker(self, work_queue):
117112
def _thread_worker(): # pragma: nocover
118113
while True:
119114
try:
120-
func = queue.get()
115+
func = work_queue.get()
121116
try:
122117
if func is _STOP:
123118
break
124119
func()
125120
except Exception:
126121
log.exception("Exception in worker queue thread")
127122
finally:
128-
queue.task_done()
123+
work_queue.task_done()
129124
del func # release before possible idle
130125
except self.queue_empty:
131126
continue
@@ -142,11 +137,11 @@ def start(self):
142137
# Spawn our worker threads, we have
143138
# - A callback worker for watch events to be called
144139
# - A completion worker for completion events to be called
145-
for queue in (self.completion_queue, self.callback_queue):
146-
w = self._create_thread_worker(queue)
140+
for work_queue in (self.completion_queue, self.callback_queue):
141+
w = self._create_thread_worker(work_queue)
147142
self._workers.append(w)
148143
self._running = True
149-
python2atexit.register(self.stop)
144+
atexit.register(self.stop)
150145

151146
def stop(self):
152147
"""Stop the worker threads and empty all queues."""
@@ -156,8 +151,8 @@ def stop(self):
156151

157152
self._running = False
158153

159-
for queue in (self.completion_queue, self.callback_queue):
160-
queue.put(_STOP)
154+
for work_queue in (self.completion_queue, self.callback_queue):
155+
work_queue.put(_STOP)
161156

162157
self._workers.reverse()
163158
while self._workers:
@@ -167,7 +162,7 @@ def stop(self):
167162
# Clear the queues
168163
self.callback_queue = self.queue_impl()
169164
self.completion_queue = self.queue_impl()
170-
python2atexit.unregister(self.stop)
165+
atexit.unregister(self.stop)
171166

172167
def select(self, *args, **kwargs):
173168
return selector_select(*args, **kwargs)

0 commit comments

Comments
 (0)