mitogen: Consolidate back compatibility fallbacks and polyfills in mitogen.core

This saves some bytes on the wire ad simplifies reasoning about the code.
pull/1127/head
Alex Willmer 2 months ago
parent ce6297b0e9
commit c6cf08ab39

@ -746,9 +746,7 @@ def set_file_mode(path, spec, fd=None):
""" """
Update the permissions of a file using the same syntax as chmod(1). Update the permissions of a file using the same syntax as chmod(1).
""" """
if isinstance(spec, int): if isinstance(spec, mitogen.core.integer_types):
new_mode = spec
elif not mitogen.core.PY3 and isinstance(spec, long):
new_mode = spec new_mode = spec
elif spec.isdigit(): elif spec.isdigit():
new_mode = int(spec, 8) new_mode = int(spec, 8)

@ -21,6 +21,8 @@ To avail of fixes in an unreleased version, please download a ZIP file
Unreleased Unreleased
---------- ----------
* :gh:issue:`1127` :mod:`mitogen`: Consolidate mitogen backward compatibility
fallbacks and polyfills into :mod:`mitogen.core`
v0.3.10 (2024-09-20) v0.3.10 (2024-09-20)

@ -102,21 +102,6 @@ try:
except ImportError: except ImportError:
cProfile = None cProfile = None
try:
import thread
except ImportError:
import threading as thread
try:
import cPickle as pickle
except ImportError:
import pickle
try:
from cStringIO import StringIO as BytesIO
except ImportError:
from io import BytesIO
try: try:
BaseException BaseException
except NameError: except NameError:
@ -169,31 +154,35 @@ STUB_CALL_SERVICE = 111
#: :meth:`mitogen.core.Router.add_handler` callbacks to clean up. #: :meth:`mitogen.core.Router.add_handler` callbacks to clean up.
IS_DEAD = 999 IS_DEAD = 999
try:
BaseException
except NameError:
BaseException = Exception
PY24 = sys.version_info < (2, 5) PY24 = sys.version_info < (2, 5)
PY3 = sys.version_info > (3,) PY3 = sys.version_info > (3,)
if PY3: if PY3:
import pickle
import _thread as thread
from io import BytesIO
b = str.encode b = str.encode
BytesType = bytes BytesType = bytes
UnicodeType = str UnicodeType = str
FsPathTypes = (str,) FsPathTypes = (str,)
BufferType = lambda buf, start: memoryview(buf)[start:] BufferType = lambda buf, start: memoryview(buf)[start:]
long = int integer_types = (int,)
iteritems, iterkeys, itervalues = dict.items, dict.keys, dict.values
else: else:
import cPickle as pickle
import thread
from cStringIO import StringIO as BytesIO
b = str b = str
BytesType = str BytesType = str
FsPathTypes = (str, unicode) FsPathTypes = (str, unicode)
BufferType = buffer BufferType = buffer
UnicodeType = unicode UnicodeType = unicode
integer_types = (int, long)
iteritems, iterkeys, itervalues = dict.iteritems, dict.iterkeys, dict.itervalues
AnyTextType = (BytesType, UnicodeType) AnyTextType = (BytesType, UnicodeType)
try: try:
next next = next
except NameError: except NameError:
next = lambda it: it.next() next = lambda it: it.next()
@ -400,12 +389,19 @@ now = getattr(time, 'monotonic', time.time)
# Python 2.4 # Python 2.4
try: try:
any all, any = all, any
except NameError: except NameError:
def all(it):
for elem in it:
if not elem:
return False
return True
def any(it): def any(it):
for elem in it: for elem in it:
if elem: if elem:
return True return True
return False
def _partition(s, sep, find): def _partition(s, sep, find):
@ -1065,8 +1061,8 @@ class Sender(object):
def _unpickle_sender(router, context_id, dst_handle): def _unpickle_sender(router, context_id, dst_handle):
if not (isinstance(router, Router) and if not (isinstance(router, Router) and
isinstance(context_id, (int, long)) and context_id >= 0 and isinstance(context_id, integer_types) and context_id >= 0 and
isinstance(dst_handle, (int, long)) and dst_handle > 0): isinstance(dst_handle, integer_types) and dst_handle > 0):
raise TypeError('cannot unpickle Sender: bad input or missing router') raise TypeError('cannot unpickle Sender: bad input or missing router')
return Sender(Context(router, context_id), dst_handle) return Sender(Context(router, context_id), dst_handle)
@ -2508,7 +2504,7 @@ class Context(object):
def _unpickle_context(context_id, name, router=None): def _unpickle_context(context_id, name, router=None):
if not (isinstance(context_id, (int, long)) and context_id >= 0 and ( if not (isinstance(context_id, integer_types) and context_id >= 0 and (
(name is None) or (name is None) or
(isinstance(name, UnicodeType) and len(name) < 100)) (isinstance(name, UnicodeType) and len(name) < 100))
): ):

@ -74,9 +74,11 @@ import mitogen.core
import mitogen.minify import mitogen.minify
import mitogen.parent import mitogen.parent
from mitogen.core import any
from mitogen.core import b from mitogen.core import b
from mitogen.core import IOLOG from mitogen.core import IOLOG
from mitogen.core import LOG from mitogen.core import LOG
from mitogen.core import next
from mitogen.core import str_partition from mitogen.core import str_partition
from mitogen.core import str_rpartition from mitogen.core import str_rpartition
from mitogen.core import to_text from mitogen.core import to_text
@ -84,17 +86,6 @@ from mitogen.core import to_text
imap = getattr(itertools, 'imap', map) imap = getattr(itertools, 'imap', map)
izip = getattr(itertools, 'izip', zip) izip = getattr(itertools, 'izip', zip)
try:
any
except NameError:
from mitogen.core import any
try:
next
except NameError:
from mitogen.core import next
RLOG = logging.getLogger('mitogen.ctx') RLOG = logging.getLogger('mitogen.ctx')

@ -56,15 +56,13 @@ import zlib
# Absolute imports for <2.5. # Absolute imports for <2.5.
select = __import__('select') select = __import__('select')
try:
import thread
except ImportError:
import threading as thread
import mitogen.core import mitogen.core
from mitogen.core import b from mitogen.core import b
from mitogen.core import bytes_partition from mitogen.core import bytes_partition
from mitogen.core import IOLOG from mitogen.core import IOLOG
from mitogen.core import itervalues
from mitogen.core import next
from mitogen.core import thread
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -80,15 +78,6 @@ except IOError:
SELINUX_ENABLED = False SELINUX_ENABLED = False
try:
next
except NameError:
# Python 2.4/2.5
from mitogen.core import next
itervalues = getattr(dict, 'itervalues', dict.values)
if mitogen.core.PY3: if mitogen.core.PY3:
xrange = range xrange = range
closure_attr = '__closure__' closure_attr = '__closure__'

@ -39,18 +39,10 @@ import threading
import mitogen.core import mitogen.core
import mitogen.select import mitogen.select
from mitogen.core import all
from mitogen.core import b from mitogen.core import b
from mitogen.core import str_rpartition from mitogen.core import str_rpartition
try:
all
except NameError:
def all(it):
for elem in it:
if not elem:
return False
return True
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

@ -43,11 +43,6 @@ except ImportError:
import mitogen.parent import mitogen.parent
from mitogen.core import b from mitogen.core import b
try:
any
except NameError:
from mitogen.core import any
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

@ -34,11 +34,6 @@ import re
import mitogen.core import mitogen.core
import mitogen.parent import mitogen.parent
try:
any
except NameError:
from mitogen.core import any
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

@ -37,13 +37,7 @@ import sys
import mitogen.core import mitogen.core
import mitogen.master import mitogen.master
from mitogen.core import iteritems
iteritems = getattr(dict, 'iteritems', dict.items)
if mitogen.core.PY3:
iteritems = dict.items
else:
iteritems = dict.iteritems
def setup_gil(): def setup_gil():

@ -2,11 +2,7 @@ import unittest
import mitogen.core import mitogen.core
try: from mitogen.core import next
next
except NameError:
def next(it):
return it.next()
class IterSplitTest(unittest.TestCase): class IterSplitTest(unittest.TestCase):

@ -3,9 +3,6 @@ import os
import mitogen.lxc import mitogen.lxc
import mitogen.parent import mitogen.parent
try:
any
except NameError:
from mitogen.core import any from mitogen.core import any
import testlib import testlib

@ -8,14 +8,10 @@ import unittest
import mitogen.core import mitogen.core
import mitogen.parent import mitogen.parent
import testlib
try:
next
except NameError:
# Python 2.4
from mitogen.core import next from mitogen.core import next
import testlib
class SockMixin(object): class SockMixin(object):
def tearDown(self): def tearDown(self):

Loading…
Cancel
Save