summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ungurianu <alex.ungurianu@protonmail.com>2022-11-29 11:59:05 +0000
committerGitHub <noreply@github.com>2022-11-29 11:59:05 +0000
commitf71da5f943e1a4b14174ddce9aa5daa6b3739449 (patch)
tree2a1331bdf5d7c657df9fec422d36cbc1af3d2091
parent4b1313530ed147288ee46366a1100eb26c6fde7c (diff)
parent74ecc0f2fd9d17e942de3c1f1cdcbc2c48b2bb1f (diff)
downloadkazoo-f71da5f943e1a4b14174ddce9aa5daa6b3739449.tar.gz
Merge pull request #678 from a-ungurianu/devx/bye_bye_six
refactor: Remove vestiges of python2 support
-rw-r--r--kazoo/client.py53
-rw-r--r--kazoo/handlers/eventlet.py6
-rw-r--r--kazoo/handlers/gevent.py11
-rw-r--r--kazoo/handlers/threading.py35
-rw-r--r--kazoo/handlers/utils.py22
-rw-r--r--kazoo/hosts.py4
-rw-r--r--kazoo/protocol/connection.py32
-rw-r--r--kazoo/protocol/serialization.py4
-rw-r--r--kazoo/python2atexit.py71
-rw-r--r--kazoo/recipe/lock.py16
-rw-r--r--kazoo/testing/harness.py2
-rw-r--r--kazoo/tests/conftest.py9
-rw-r--r--setup.cfg3
13 files changed, 71 insertions, 197 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/eventlet.py b/kazoo/handlers/eventlet.py
index ebf9898..8869cc5 100644
--- a/kazoo/handlers/eventlet.py
+++ b/kazoo/handlers/eventlet.py
@@ -1,6 +1,7 @@
"""A eventlet based handler."""
from __future__ import absolute_import
+import atexit
import contextlib
import logging
@@ -12,7 +13,6 @@ from eventlet.green import selectors as green_selectors
from eventlet import queue as green_queue
from kazoo.handlers import utils
-import kazoo.python2atexit as python2atexit
from kazoo.handlers.utils import selector_select
LOG = logging.getLogger(__name__)
@@ -140,7 +140,7 @@ class SequentialEventletHandler(object):
w = eventlet.spawn(self._process_callback_queue)
self._workers.append((w, self.callback_queue))
self._started = True
- python2atexit.register(self.stop)
+ atexit.register(self.stop)
def stop(self):
while self._workers:
@@ -148,7 +148,7 @@ class SequentialEventletHandler(object):
q.put(_STOP)
w.wait()
self._started = False
- python2atexit.unregister(self.stop)
+ atexit.unregister(self.stop)
def socket(self, *args, **kwargs):
return utils.create_tcp_socket(green_socket)
diff --git a/kazoo/handlers/gevent.py b/kazoo/handlers/gevent.py
index 1c141bc..f36389a 100644
--- a/kazoo/handlers/gevent.py
+++ b/kazoo/handlers/gevent.py
@@ -1,6 +1,7 @@
"""A gevent based handler."""
from __future__ import absolute_import
+import atexit
import logging
import gevent
@@ -13,13 +14,9 @@ import gevent.selectors
from kazoo.handlers.utils import selector_select
-try:
- from gevent.lock import Semaphore, RLock
-except ImportError:
- from gevent.coros import Semaphore, RLock
+from gevent.lock import Semaphore, RLock
from kazoo.handlers import utils
-from kazoo import python2atexit
_using_libevent = gevent.__version__.startswith("0.")
@@ -104,7 +101,7 @@ class SequentialGeventHandler(object):
for queue in (self.callback_queue,):
w = self._create_greenlet_worker(queue)
self._workers.append(w)
- python2atexit.register(self.stop)
+ atexit.register(self.stop)
def stop(self):
"""Stop the greenlet workers and empty all queues."""
@@ -124,7 +121,7 @@ class SequentialGeventHandler(object):
# Clear the queues
self.callback_queue = self.queue_impl() # pragma: nocover
- python2atexit.unregister(self.stop)
+ atexit.unregister(self.stop)
def select(self, *args, **kwargs):
return selector_select(
diff --git a/kazoo/handlers/threading.py b/kazoo/handlers/threading.py
index 210c31e..b9acd87 100644
--- a/kazoo/handlers/threading.py
+++ b/kazoo/handlers/threading.py
@@ -12,21 +12,16 @@ environments that use threads.
"""
from __future__ import absolute_import
+import atexit
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,11 +137,11 @@ 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)
+ atexit.register(self.stop)
def stop(self):
"""Stop the worker threads and empty all queues."""
@@ -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:
@@ -167,7 +162,7 @@ class SequentialThreadingHandler(object):
# Clear the queues
self.callback_queue = self.queue_impl()
self.completion_queue = self.queue_impl()
- python2atexit.unregister(self.stop)
+ atexit.unregister(self.stop)
def select(self, *args, **kwargs):
return selector_select(*args, **kwargs)
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..6ed5cde 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],
- )
- except puresasl.SASLProtocolException as err:
- six.reraise(
- AuthFailedError,
- AuthFailedError("protocol error: %s" % err),
- sys.exc_info()[2],
- )
- except Exception as err:
- six.reraise(
- AuthFailedError,
- AuthFailedError("Unknown error: %s" % err),
- sys.exc_info()[2],
- )
+ raise SASLException("library error") from err
+ except puresasl.SASLProtocolException as exc:
+ raise AuthFailedError("protocol error") from exc
+ except Exception as exc:
+ raise AuthFailedError("Unknown error") from exc
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 exc:
# 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 exc
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/python2atexit.py b/kazoo/python2atexit.py
deleted file mode 100644
index fdf78d2..0000000
--- a/kazoo/python2atexit.py
+++ /dev/null
@@ -1,71 +0,0 @@
-"""Uses the old atexit with added unregister for python 2.x
-and the new atexit for python 3.x
-"""
-import atexit
-import sys
-
-
-__all__ = ["register", "unregister"]
-
-
-_exithandlers = []
-
-
-def _run_exitfuncs():
- """run any registered exit functions
-
- _exithandlers is traversed in reverse order so functions are executed
- last in, first out.
- """
-
- exc_info = None
- while _exithandlers:
- func, targs, kargs = _exithandlers.pop()
- try:
- func(*targs, **kargs)
- except SystemExit:
- exc_info = sys.exc_info()
- except: # noqa
- import traceback
-
- sys.stderr.write("Error in atexit._run_exitfuncs:\n")
- traceback.print_exc()
- exc_info = sys.exc_info()
-
- if exc_info is not None:
- raise exc_info[0](exc_info[1])
-
-
-def register(func, *targs, **kargs):
- """register a function to be executed upon normal program termination
-
- func - function to be called at exit
- targs - optional arguments to pass to func
- kargs - optional keyword arguments to pass to func
-
- func is returned to facilitate usage as a decorator.
- """
- if hasattr(atexit, "unregister"):
- atexit.register(func, *targs, **kargs)
- else:
- _exithandlers.append((func, targs, kargs))
- return func
-
-
-def unregister(func):
- """remove func from the list of functions that are registered
- doesn't do anything if func is not found
-
- func = function to be unregistered
- """
- if hasattr(atexit, "unregister"):
- atexit.unregister(func)
- else:
- handler_entries = [e for e in _exithandlers if e[0] == func]
- for e in handler_entries:
- _exithandlers.remove(e)
-
-
-if not hasattr(atexit, "unregister"):
- # Only in python 2.x
- atexit.register(_run_exitfuncs)
diff --git a/kazoo/recipe/lock.py b/kazoo/recipe/lock.py
index 8959964..59c6d8f 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)
@@ -199,11 +192,10 @@ class Lock(object):
pass
except KazooException:
# 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
if gotten:
self.is_acquired = gotten
if not gotten and not already_acquired:
diff --git a/kazoo/testing/harness.py b/kazoo/testing/harness.py
index fa4427f..2d28a5b 100644
--- a/kazoo/testing/harness.py
+++ b/kazoo/testing/harness.py
@@ -1,10 +1,10 @@
"""Kazoo testing harnesses"""
+import atexit
import logging
import os
import uuid
import unittest
-from kazoo import python2atexit as atexit
from kazoo.client import KazooClient
from kazoo.exceptions import KazooException
from kazoo.protocol.connection import _CONNECTION_DROP, _SESSION_EXPIRED
diff --git a/kazoo/tests/conftest.py b/kazoo/tests/conftest.py
index c7a4cc6..eef9965 100644
--- a/kazoo/tests/conftest.py
+++ b/kazoo/tests/conftest.py
@@ -4,7 +4,8 @@ log = logging.getLogger(__name__)
def pytest_exception_interact(node, call, report):
- cluster = node._testcase.cluster
- log.error("Zookeeper cluster logs:")
- for logs in cluster.get_logs():
- log.error(logs)
+ if hasattr(node._testcase, "cluster"):
+ cluster = node._testcase.cluster
+ log.error("Zookeeper cluster logs:")
+ for logs in cluster.get_logs():
+ log.error(logs)
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