diff --git a/billiard/common.py b/billiard/common.py index 0b7bec20..3fc4532d 100644 --- a/billiard/common.py +++ b/billiard/common.py @@ -8,28 +8,18 @@ import signal import sys -import pickle as pypickle -try: - import cPickle as cpickle -except ImportError: # pragma: no cover - cpickle = None # noqa +import pickle from .exceptions import RestartFreqExceeded -from .five import monotonic +from time import monotonic -pickle = cpickle or pypickle pickle_load = pickle.load pickle_loads = pickle.loads # cPickle.loads does not support buffer() objects, # but we can just create a StringIO and use load. -if sys.version_info[0] == 3: - from io import BytesIO -else: - try: - from cStringIO import StringIO as BytesIO # noqa - except ImportError: - from StringIO import StringIO as BytesIO # noqa +from io import BytesIO + SIGMAP = dict( (getattr(signal, n), n) for n in dir(signal) if n.startswith('SIG') diff --git a/billiard/compat.py b/billiard/compat.py index b5ce7c76..ac53f572 100644 --- a/billiard/compat.py +++ b/billiard/compat.py @@ -5,7 +5,7 @@ import os import sys -from .five import range, zip_longest +from itertools import zip_longest if sys.platform == 'win32': try: @@ -20,31 +20,15 @@ except ImportError: # pragma: no cover resource = None -try: - from io import UnsupportedOperation - FILENO_ERRORS = (AttributeError, ValueError, UnsupportedOperation) -except ImportError: # pragma: no cover - # Py2 - FILENO_ERRORS = (AttributeError, ValueError) # noqa +from io import UnsupportedOperation +FILENO_ERRORS = (AttributeError, ValueError, UnsupportedOperation) -if sys.version_info > (2, 7, 5): - buf_t, is_new_buffer = memoryview, True # noqa -else: - buf_t, is_new_buffer = buffer, False # noqa - if hasattr(os, 'write'): __write__ = os.write - if is_new_buffer: - - def send_offset(fd, buf, offset): - return __write__(fd, buf[offset:]) - - else: # Py<2.7.6 - - def send_offset(fd, buf, offset): # noqa - return __write__(fd, buf_t(buf, offset)) + def send_offset(fd, buf, offset): + return __write__(fd, buf[offset:]) else: # non-posix platform @@ -99,21 +83,6 @@ def fsdecode(filename): del _fscodec -if sys.version_info[0] == 3: - bytes = bytes -else: - _bytes = bytes - - # the 'bytes' alias in Python2 does not support an encoding argument. - - class bytes(_bytes): # noqa - - def __new__(cls, *args): - if len(args) > 1: - return _bytes(args[0]).encode(*args[1:]) - return _bytes(*args) - - def maybe_fileno(f): """Get object fileno, or :const:`None` if not defined.""" if isinstance(f, numbers.Integral): @@ -191,13 +160,8 @@ def get_errno(exc): try: return exc.errno except AttributeError: - try: - # e.args = (errno, reason) - if isinstance(exc.args, tuple) and len(exc.args) == 2: - return exc.args[0] - except AttributeError: - pass - return 0 + return 0 + try: import _posixsubprocess diff --git a/billiard/connection.py b/billiard/connection.py index 1cc40f56..a748e006 100644 --- a/billiard/connection.py +++ b/billiard/connection.py @@ -26,7 +26,7 @@ from . import AuthenticationError, BufferTooShort from ._ext import _billiard from .compat import setblocking, send_offset -from .five import monotonic +from time import monotonic from .reduction import ForkingPickler try: diff --git a/billiard/dummy/__init__.py b/billiard/dummy/__init__.py index 032f5f15..147fd27e 100644 --- a/billiard/dummy/__init__.py +++ b/billiard/dummy/__init__.py @@ -45,7 +45,7 @@ from threading import Lock, RLock, Semaphore, BoundedSemaphore from threading import Event -from billiard.five import Queue +from queue import Queue from billiard.connection import Pipe diff --git a/billiard/five.py b/billiard/five.py deleted file mode 100644 index 5997f65e..00000000 --- a/billiard/five.py +++ /dev/null @@ -1,191 +0,0 @@ -# -*- coding: utf-8 -*- -""" - celery.five - ~~~~~~~~~~~ - - Compatibility implementations of features - only available in newer Python versions. - - -""" -from __future__ import absolute_import - -# ############# py3k ######################################################### -import sys -PY3 = sys.version_info[0] == 3 - -try: - reload = reload # noqa -except NameError: # pragma: no cover - try: - from importlib import reload # noqa - except ImportError: # pragma: no cover - from imp import reload # noqa - -try: - from UserList import UserList # noqa -except ImportError: # pragma: no cover - from collections import UserList # noqa - -try: - from UserDict import UserDict # noqa -except ImportError: # pragma: no cover - from collections import UserDict # noqa - -# ############# time.monotonic ############################################### - -if sys.version_info < (3, 3): - - import platform - SYSTEM = platform.system() - - try: - import ctypes - except ImportError: # pragma: no cover - ctypes = None # noqa - - if SYSTEM == 'Darwin' and ctypes is not None: - from ctypes.util import find_library - libSystem = ctypes.CDLL(find_library('libSystem.dylib')) - CoreServices = ctypes.CDLL(find_library('CoreServices'), - use_errno=True) - mach_absolute_time = libSystem.mach_absolute_time - mach_absolute_time.restype = ctypes.c_uint64 - absolute_to_nanoseconds = CoreServices.AbsoluteToNanoseconds - absolute_to_nanoseconds.restype = ctypes.c_uint64 - absolute_to_nanoseconds.argtypes = [ctypes.c_uint64] - - def _monotonic(): - return absolute_to_nanoseconds(mach_absolute_time()) * 1e-9 - - elif SYSTEM == 'Linux' and ctypes is not None: - # from stackoverflow: - # questions/1205722/how-do-i-get-monotonic-time-durations-in-python - import ctypes - import os - - CLOCK_MONOTONIC = 1 # see - - class timespec(ctypes.Structure): - _fields_ = [ - ('tv_sec', ctypes.c_long), - ('tv_nsec', ctypes.c_long), - ] - - librt = ctypes.CDLL('librt.so.1', use_errno=True) - clock_gettime = librt.clock_gettime - clock_gettime.argtypes = [ - ctypes.c_int, ctypes.POINTER(timespec), - ] - - def _monotonic(): # noqa - t = timespec() - if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0: - errno_ = ctypes.get_errno() - raise OSError(errno_, os.strerror(errno_)) - return t.tv_sec + t.tv_nsec * 1e-9 - else: - from time import time as _monotonic - -try: - from time import monotonic -except ImportError: - monotonic = _monotonic # noqa - -if PY3: - import builtins - - from queue import Queue, Empty, Full - from itertools import zip_longest - from io import StringIO, BytesIO - - map = map - string = str - string_t = str - long_t = int - text_t = str - range = range - int_types = (int, ) - - def items(d): - return d.items() - - def keys(d): - return d.keys() - - def values(d): - return d.values() - - def nextfun(it): - return it.__next__ - - exec_ = getattr(builtins, 'exec') - - def reraise(tp, value, tb=None): - if value.__traceback__ is not tb: - raise value.with_traceback(tb) - raise value - - class WhateverIO(StringIO): - - def write(self, data): - if isinstance(data, bytes): - data = data.encode() - StringIO.write(self, data) - -else: - import __builtin__ as builtins # noqa - from Queue import Queue, Empty, Full # noqa - from itertools import imap as map, izip_longest as zip_longest # noqa - from StringIO import StringIO # noqa - string = unicode # noqa - string_t = basestring # noqa - text_t = unicode - long_t = long # noqa - range = xrange - int_types = (int, long) - - def items(d): # noqa - return d.iteritems() - - def keys(d): # noqa - return d.iterkeys() - - def values(d): # noqa - return d.itervalues() - - def nextfun(it): # noqa - return it.next - - def exec_(code, globs=None, locs=None): - """Execute code in a namespace.""" - if globs is None: - frame = sys._getframe(1) - globs = frame.f_globals - if locs is None: - locs = frame.f_locals - del frame - elif locs is None: - locs = globs - exec("""exec code in globs, locs""") - - exec_("""def reraise(tp, value, tb=None): raise tp, value, tb""") - - BytesIO = WhateverIO = StringIO # noqa - - -def with_metaclass(Type, skip_attrs=set(['__dict__', '__weakref__'])): - """Class decorator to set metaclass. - - Works with both Python 2 and Python 3 and it does not add - an extra class in the lookup order like ``six.with_metaclass`` does - (that is -- it copies the original class instead of using inheritance). - - """ - - def _clone_with_metaclass(Class): - attrs = dict((key, value) for key, value in items(vars(Class)) - if key not in skip_attrs) - return Type(Class.__name__, Class.__bases__, attrs) - - return _clone_with_metaclass diff --git a/billiard/managers.py b/billiard/managers.py index 93edbf50..8889a9bd 100644 --- a/billiard/managers.py +++ b/billiard/managers.py @@ -27,7 +27,8 @@ from . import util from . import get_context -from .five import Queue, items, monotonic +from queue import Queue +from time import monotonic __all__ = ['BaseManager', 'SyncManager', 'BaseProxy', 'Token'] @@ -641,7 +642,7 @@ def register(cls, typeid, callable=None, proxytype=None, exposed=None, ) if method_to_typeid: - for key, value in items(method_to_typeid): + for key, value in method_to_typeid.items(): assert type(key) is str, '%r is not a string' % key assert type(value) is str, '%r is not a string' % value @@ -931,9 +932,9 @@ def __init__(self, **kwds): self.__dict__.update(kwds) def __repr__(self): - items = list(self.__dict__.items()) + _items = list(self.__dict__.items()) temp = [] - for name, value in items: + for name, value in _items: if not name.startswith('_'): temp.append('%s=%r' % (name, value)) temp.sort() diff --git a/billiard/pool.py b/billiard/pool.py index 81c3ab3b..c7fed4ff 100644 --- a/billiard/pool.py +++ b/billiard/pool.py @@ -43,7 +43,8 @@ TimeoutError, WorkerLostError, ) -from .five import Empty, Queue, range, values, reraise, monotonic +from time import monotonic +from queue import Queue, Empty from .util import Finalize, debug, warning MAXMEM_USED_FMT = """\ @@ -1240,7 +1241,7 @@ def _join_exited_workers(self, shutdown=False): elif sched_for and not sched_for._is_alive(): self.on_job_process_down(job, sched_for.pid) - for worker in values(cleaned): + for worker in cleaned.values(): if self.on_process_down: if not shutdown: self._process_cleanup_queues(worker) @@ -1355,9 +1356,7 @@ def maintain_pool(self): raise except OSError as exc: if get_errno(exc) == errno.ENOMEM: - reraise(MemoryError, - MemoryError(str(exc)), - sys.exc_info()[2]) + raise MemoryError from exc raise def _setup_queues(self): diff --git a/billiard/process.py b/billiard/process.py index 660af6a0..8d568574 100644 --- a/billiard/process.py +++ b/billiard/process.py @@ -22,8 +22,6 @@ from multiprocessing import process as _mproc -from .five import items, string_t - try: ORIGINAL_DIR = os.path.abspath(os.getcwd()) except OSError: @@ -173,7 +171,7 @@ def name(self): @name.setter def name(self, name): # noqa - assert isinstance(name, string_t), 'name must be a string' + assert isinstance(name, str), 'name must be a string' self._name = name @property @@ -396,7 +394,7 @@ def __init__(self): _exitcode_to_name = {} -for name, signum in items(signal.__dict__): +for name, signum in signal.__dict__.items(): if name[:3] == 'SIG' and '_' not in name: _exitcode_to_name[-signum] = name diff --git a/billiard/queues.py b/billiard/queues.py index 0a2daa5e..67a09cb5 100644 --- a/billiard/queues.py +++ b/billiard/queues.py @@ -19,7 +19,8 @@ from . import context from .compat import get_errno -from .five import monotonic, Empty, Full +from time import monotonic +from queue import Empty, Full from .util import ( debug, error, info, Finalize, register_after_fork, is_exiting, ) diff --git a/billiard/sharedctypes.py b/billiard/sharedctypes.py index 97675df4..169ca174 100644 --- a/billiard/sharedctypes.py +++ b/billiard/sharedctypes.py @@ -15,7 +15,6 @@ from . import heap from . import get_context from .context import assert_spawning -from .five import int_types from .reduction import ForkingPickler __all__ = ['RawValue', 'RawArray', 'Value', 'Array', 'copy', 'synchronized'] @@ -54,7 +53,7 @@ def RawArray(typecode_or_type, size_or_initializer): Returns a ctypes array allocated from shared memory ''' type_ = typecode_to_type.get(typecode_or_type, typecode_or_type) - if isinstance(size_or_initializer, int_types): + if isinstance(size_or_initializer, int): type_ = type_ * size_or_initializer obj = _new_value(type_) ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj)) diff --git a/billiard/synchronize.py b/billiard/synchronize.py index b97fbfb5..b93e09c9 100644 --- a/billiard/synchronize.py +++ b/billiard/synchronize.py @@ -18,7 +18,7 @@ from . import util from ._ext import _billiard, ensure_SemLock -from .five import range, monotonic +from time import monotonic __all__ = [ 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event', diff --git a/setup.py b/setup.py index ee6dbfff..1f6f99a3 100644 --- a/setup.py +++ b/setup.py @@ -215,11 +215,7 @@ def run_setup(with_extensions=True): 'Intended Audience :: Developers', 'Programming Language :: Python', 'Programming Language :: C', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9',