summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ungurianu <alex.ungurianu@protonmail.com>2022-10-28 19:55:10 +0100
committerAlex Ungurianu <alex.ungurianu@protonmail.com>2022-11-16 22:37:13 +0000
commite05b50a1d6c04f7e6cf0c82c43e79cfd8520ee2a (patch)
treec57e962bc896c51b0223347485b82254e8e2f0dd
parent4b1313530ed147288ee46366a1100eb26c6fde7c (diff)
downloadkazoo-e05b50a1d6c04f7e6cf0c82c43e79cfd8520ee2a.tar.gz
refactor: Replace usage of six for their py3 implementations
-rw-r--r--kazoo/client.py53
-rw-r--r--kazoo/handlers/threading.py29
-rw-r--r--kazoo/handlers/utils.py22
-rw-r--r--kazoo/hosts.py4
-rw-r--r--kazoo/protocol/connection.py28
-rw-r--r--kazoo/protocol/serialization.py4
-rw-r--r--kazoo/recipe/lock.py18
-rw-r--r--setup.cfg3
8 files changed, 54 insertions, 107 deletions
diff --git a/kazoo/client.py b/kazoo/client.py
index bf1ec8f..27b7c38 100644
--- a/kazoo/client.py
+++ b/kazoo/client.py
@@ -7,8 +7,6 @@ from os.path import split
import re
import warnings
-import six
-
from kazoo.exceptions import (
AuthFailedError,
ConfigurationError,
@@ -66,9 +64,6 @@ from kazoo.recipe.queue import Queue, LockingQueue
from kazoo.recipe.watchers import ChildrenWatch, DataWatch
-string_types = six.string_types
-bytes_types = (six.binary_type,)
-
CLOSED_STATES = (
KeeperState.EXPIRED_SESSION,
KeeperState.AUTH_FAILED,
@@ -415,10 +410,10 @@ class KazooClient(object):
def _reset_watchers(self):
watchers = []
- for child_watchers in six.itervalues(self._child_watchers):
+ for child_watchers in self._child_watchers.values():
watchers.extend(child_watchers)
- for data_watchers in six.itervalues(self._data_watchers):
+ for data_watchers in self._data_watchers.values():
watchers.extend(data_watchers)
self._child_watchers = defaultdict(set)
@@ -821,7 +816,7 @@ class KazooClient(object):
version = _try_fetch()
if _is_valid(version):
return version
- for _i in six.moves.range(0, retries):
+ for _i in range(0, retries):
version = _try_fetch()
if _is_valid(version):
return version
@@ -854,9 +849,9 @@ class KazooClient(object):
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
- if not isinstance(scheme, string_types):
+ if not isinstance(scheme, str):
raise TypeError("Invalid type for 'scheme' (string expected)")
- if not isinstance(credential, string_types):
+ if not isinstance(credential, str):
raise TypeError("Invalid type for 'credential' (string expected)")
# we need this auth data to re-authenticate on reconnect
@@ -1034,7 +1029,7 @@ class KazooClient(object):
if acl is None and self.default_acl:
acl = self.default_acl
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
if acl and (
isinstance(acl, ACL) or not isinstance(acl, (tuple, list))
@@ -1042,7 +1037,7 @@ class KazooClient(object):
raise TypeError(
"Invalid type for 'acl' (acl must be a tuple/list" " of ACL's"
)
- if value is not None and not isinstance(value, bytes_types):
+ if value is not None and not isinstance(value, bytes):
raise TypeError("Invalid type for 'value' (must be a byte string)")
if not isinstance(ephemeral, bool):
raise TypeError("Invalid type for 'ephemeral' (bool expected)")
@@ -1205,7 +1200,7 @@ class KazooClient(object):
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
if watch and not callable(watch):
raise TypeError("Invalid type for 'watch' (must be a callable)")
@@ -1248,7 +1243,7 @@ class KazooClient(object):
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
if watch and not callable(watch):
raise TypeError("Invalid type for 'watch' (must be a callable)")
@@ -1304,7 +1299,7 @@ class KazooClient(object):
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
if watch and not callable(watch):
raise TypeError("Invalid type for 'watch' (must be a callable)")
@@ -1346,7 +1341,7 @@ class KazooClient(object):
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
async_result = self.handler.async_result()
@@ -1389,7 +1384,7 @@ class KazooClient(object):
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
if isinstance(acls, ACL) or not isinstance(acls, (tuple, list)):
raise TypeError(
@@ -1447,9 +1442,9 @@ class KazooClient(object):
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
- if value is not None and not isinstance(value, bytes_types):
+ if value is not None and not isinstance(value, bytes):
raise TypeError("Invalid type for 'value' (must be a byte string)")
if not isinstance(version, int):
raise TypeError("Invalid type for 'version' (int expected)")
@@ -1523,7 +1518,7 @@ class KazooClient(object):
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
if not isinstance(version, int):
raise TypeError("Invalid type for 'version' (int expected)")
@@ -1632,11 +1627,11 @@ class KazooClient(object):
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
- if joining and not isinstance(joining, string_types):
+ if joining and not isinstance(joining, str):
raise TypeError("Invalid type for 'joining' (string expected)")
- if leaving and not isinstance(leaving, string_types):
+ if leaving and not isinstance(leaving, str):
raise TypeError("Invalid type for 'leaving' (string expected)")
- if new_members and not isinstance(new_members, string_types):
+ if new_members and not isinstance(new_members, str):
raise TypeError(
"Invalid type for 'new_members' (string " "expected)"
)
@@ -1690,13 +1685,13 @@ class TransactionRequest(object):
if acl is None and self.client.default_acl:
acl = self.client.default_acl
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
if acl and not isinstance(acl, (tuple, list)):
raise TypeError(
"Invalid type for 'acl' (acl must be a tuple/list" " of ACL's"
)
- if not isinstance(value, bytes_types):
+ if not isinstance(value, bytes):
raise TypeError("Invalid type for 'value' (must be a byte string)")
if not isinstance(ephemeral, bool):
raise TypeError("Invalid type for 'ephemeral' (bool expected)")
@@ -1722,7 +1717,7 @@ class TransactionRequest(object):
`recursive`.
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
if not isinstance(version, int):
raise TypeError("Invalid type for 'version' (int expected)")
@@ -1733,9 +1728,9 @@ class TransactionRequest(object):
arguments as :meth:`KazooClient.set`.
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
- if not isinstance(value, bytes_types):
+ if not isinstance(value, bytes):
raise TypeError("Invalid type for 'value' (must be a byte string)")
if not isinstance(version, int):
raise TypeError("Invalid type for 'version' (int expected)")
@@ -1750,7 +1745,7 @@ class TransactionRequest(object):
does not match the specified version.
"""
- if not isinstance(path, string_types):
+ if not isinstance(path, str):
raise TypeError("Invalid type for 'path' (string expected)")
if not isinstance(version, int):
raise TypeError("Invalid type for 'version' (int expected)")
diff --git a/kazoo/handlers/threading.py b/kazoo/handlers/threading.py
index 210c31e..b28bae8 100644
--- a/kazoo/handlers/threading.py
+++ b/kazoo/handlers/threading.py
@@ -13,20 +13,15 @@ environments that use threads.
from __future__ import absolute_import
import logging
+import queue
import socket
import threading
import time
-import six
-
import kazoo.python2atexit as python2atexit
from kazoo.handlers import utils
from kazoo.handlers.utils import selector_select
-try:
- import Queue
-except ImportError: # pragma: nocover
- import queue as Queue
# sentinel objects
_STOP = object()
@@ -35,11 +30,11 @@ log = logging.getLogger(__name__)
def _to_fileno(obj):
- if isinstance(obj, six.integer_types):
+ if isinstance(obj, int):
fd = int(obj)
elif hasattr(obj, "fileno"):
fd = obj.fileno()
- if not isinstance(fd, six.integer_types):
+ if not isinstance(fd, int):
raise TypeError("fileno() returned a non-integer")
fd = int(fd)
else:
@@ -98,8 +93,8 @@ class SequentialThreadingHandler(object):
name = "sequential_threading_handler"
timeout_exception = KazooTimeoutError
sleep_func = staticmethod(time.sleep)
- queue_impl = Queue.Queue
- queue_empty = Queue.Empty
+ queue_impl = queue.Queue
+ queue_empty = queue.Empty
def __init__(self):
"""Create a :class:`SequentialThreadingHandler` instance"""
@@ -113,11 +108,11 @@ class SequentialThreadingHandler(object):
def running(self):
return self._running
- def _create_thread_worker(self, queue):
+ def _create_thread_worker(self, work_queue):
def _thread_worker(): # pragma: nocover
while True:
try:
- func = queue.get()
+ func = work_queue.get()
try:
if func is _STOP:
break
@@ -125,7 +120,7 @@ class SequentialThreadingHandler(object):
except Exception:
log.exception("Exception in worker queue thread")
finally:
- queue.task_done()
+ work_queue.task_done()
del func # release before possible idle
except self.queue_empty:
continue
@@ -142,8 +137,8 @@ class SequentialThreadingHandler(object):
# Spawn our worker threads, we have
# - A callback worker for watch events to be called
# - A completion worker for completion events to be called
- for queue in (self.completion_queue, self.callback_queue):
- w = self._create_thread_worker(queue)
+ for work_queue in (self.completion_queue, self.callback_queue):
+ w = self._create_thread_worker(work_queue)
self._workers.append(w)
self._running = True
python2atexit.register(self.stop)
@@ -156,8 +151,8 @@ class SequentialThreadingHandler(object):
self._running = False
- for queue in (self.completion_queue, self.callback_queue):
- queue.put(_STOP)
+ for work_queue in (self.completion_queue, self.callback_queue):
+ work_queue.put(_STOP)
self._workers.reverse()
while self._workers:
diff --git a/kazoo/handlers/utils.py b/kazoo/handlers/utils.py
index 2717059..6b60e75 100644
--- a/kazoo/handlers/utils.py
+++ b/kazoo/handlers/utils.py
@@ -1,22 +1,15 @@
"""Kazoo handler helpers"""
+from collections import defaultdict
import errno
import functools
import os
import select
+import selectors
import ssl
import socket
import time
-from collections import defaultdict
-
-import six
-
-if six.PY34:
- import selectors
-else:
- import selectors2 as selectors
-
HAS_FNCTL = True
try:
import fcntl
@@ -363,14 +356,9 @@ def selector_select(
):
"""Selector-based drop-in replacement for select to overcome select
limitation on a maximum filehandle value.
-
- Need backport selectors2 package in python 2.
"""
if timeout is not None:
- if not (
- isinstance(timeout, six.integer_types)
- or isinstance(timeout, float)
- ):
+ if not isinstance(timeout, (int, float)):
raise TypeError("timeout must be a number")
if timeout < 0:
raise ValueError("timeout must be non-negative")
@@ -400,9 +388,9 @@ def selector_select(
for info in ready:
k, events = info
- if events & selectors.EVENT_READ:
+ if events & selectors_module.EVENT_READ:
revents.extend(fd_fileobjs[k.fd])
- elif events & selectors.EVENT_WRITE:
+ elif events & selectors_module.EVENT_WRITE:
wevents.extend(fd_fileobjs[k.fd])
return revents, wevents, xevents
diff --git a/kazoo/hosts.py b/kazoo/hosts.py
index aa18805..3ece931 100644
--- a/kazoo/hosts.py
+++ b/kazoo/hosts.py
@@ -1,4 +1,4 @@
-from six.moves import urllib_parse
+import urllib.parse
def collect_hosts(hosts):
@@ -20,7 +20,7 @@ def collect_hosts(hosts):
for host_port in host_ports:
# put all complexity of dealing with
# IPv4 & IPv6 address:port on the urlsplit
- res = urllib_parse.urlsplit("xxx://" + host_port)
+ res = urllib.parse.urlsplit("xxx://" + host_port)
host = res.hostname
if host is None:
raise ValueError("bad hostname")
diff --git a/kazoo/protocol/connection.py b/kazoo/protocol/connection.py
index d7d84d1..d672497 100644
--- a/kazoo/protocol/connection.py
+++ b/kazoo/protocol/connection.py
@@ -10,8 +10,6 @@ import ssl
import sys
import time
-import six
-
from kazoo.exceptions import (
AuthFailedError,
ConnectionDropped,
@@ -808,23 +806,11 @@ class ConnectionHandler(object):
try:
response = sasl_cli.process(challenge=challenge)
except puresasl.SASLError as err:
- six.reraise(
- SASLException,
- SASLException("library error: %s" % err),
- sys.exc_info()[2],
- )
+ raise SASLException("library error: %s" % err) from err
except puresasl.SASLProtocolException as err:
- six.reraise(
- AuthFailedError,
- AuthFailedError("protocol error: %s" % err),
- sys.exc_info()[2],
- )
+ raise AuthFailedError("protocol error: %s" % err) from err
except Exception as err:
- six.reraise(
- AuthFailedError,
- AuthFailedError("Unknown error: %s" % err),
- sys.exc_info()[2],
- )
+ raise AuthFailedError("Unknown error: %s" % err) from err
if sasl_cli.complete and not response:
break
@@ -838,13 +824,9 @@ class ConnectionHandler(object):
try:
header, buffer, offset = self._read_header(timeout)
- except ConnectionDropped:
+ except ConnectionDropped as ex:
# Zookeeper simply drops connections with failed authentication
- six.reraise(
- AuthFailedError,
- AuthFailedError("Connection dropped in SASL"),
- sys.exc_info()[2],
- )
+ raise AuthFailedError("Connection dropped in SASL") from ex
if header.xid != xid:
raise RuntimeError(
diff --git a/kazoo/protocol/serialization.py b/kazoo/protocol/serialization.py
index c702318..40e6360 100644
--- a/kazoo/protocol/serialization.py
+++ b/kazoo/protocol/serialization.py
@@ -2,8 +2,6 @@
from collections import namedtuple
import struct
-import six
-
from kazoo.exceptions import EXCEPTIONS
from kazoo.protocol.states import ZnodeStat
from kazoo.security import ACL
@@ -367,7 +365,7 @@ class Transaction(namedtuple("Transaction", "operations")):
def unchroot(client, response):
resp = []
for result in response:
- if isinstance(result, six.string_types):
+ if isinstance(result, str):
resp.append(client.unchroot(result))
else:
resp.append(result)
diff --git a/kazoo/recipe/lock.py b/kazoo/recipe/lock.py
index 8959964..5eb66d4 100644
--- a/kazoo/recipe/lock.py
+++ b/kazoo/recipe/lock.py
@@ -15,16 +15,9 @@ and/or the lease has been lost.
"""
import re
-import sys
-
-try:
- from time import monotonic as now
-except ImportError:
- from time import time as now
+import time
import uuid
-import six
-
from kazoo.exceptions import (
CancelledError,
KazooException,
@@ -45,13 +38,13 @@ class _Watch(object):
self.started_at = None
def start(self):
- self.started_at = now()
+ self.started_at = time.monotonic()
def leftover(self):
if self.duration is None:
return None
else:
- elapsed = now() - self.started_at
+ elapsed = time.monotonic() - self.started_at
return max(0, self.duration - elapsed)
@@ -197,13 +190,12 @@ class Lock(object):
)
except RetryFailedError:
pass
- except KazooException:
+ except KazooException as ex:
# if we did ultimately fail, attempt to clean up
- exc_info = sys.exc_info()
if not already_acquired:
self._best_effort_cleanup()
self.cancelled = False
- six.reraise(exc_info[0], exc_info[1], exc_info[2])
+ raise ex
if gotten:
self.is_acquired = gotten
if not gotten and not already_acquired:
diff --git a/setup.cfg b/setup.cfg
index 12421b7..562e3a7 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -39,9 +39,6 @@ project_urls =
zip_safe = false
include_package_data = true
packages = find:
-install_requires =
- six
- selectors2>=2.0.2 ; python_version < "3.4.0"
[aliases]
release = sdist bdist_wheel