mitogen: Replace uses mitogen.core.{PY24,PY3} with sys.version_info

Helps static analysis by type checkers, LSPs, etc.
pull/1415/head
Alex Willmer 4 days ago
parent 52ec693ef7
commit 0447950acd

@ -96,7 +96,7 @@ def shlex_split_b(s):
bytes.
"""
assert isinstance(s, mitogen.core.BytesType)
if mitogen.core.PY3:
if sys.version_info >= (3, 0):
return [
t.encode('latin1')
for t in shlex.split(s.decode('latin1'), comments=True)
@ -979,7 +979,7 @@ class NewStyleRunner(ScriptRunner):
True, # dont_inherit
))
if mitogen.core.PY3:
if sys.version_info >= (3, 0):
main_module_name = '__main__'
else:
main_module_name = b'__main__'
@ -1000,7 +1000,7 @@ class NewStyleRunner(ScriptRunner):
def _run_code(self, code, mod):
try:
if mitogen.core.PY3:
if sys.version_info >= (3, 0):
exec(code, vars(mod))
else:
exec('exec code in vars(mod)')
@ -1017,7 +1017,7 @@ class NewStyleRunner(ScriptRunner):
pkg, sep, _ = self.py_module_name.rpartition('.')
if not sep:
return None
if mitogen.core.PY3:
if sys.version_info >= (3, 0):
return pkg
return pkg.encode()

@ -90,7 +90,7 @@ if sys.version_info >= (3, 6):
else:
ModuleNotFoundError = ImportError
if sys.version_info > (3,5):
if sys.version_info >= (3, 5):
from os import get_blocking, set_blocking
else:
def get_blocking(fd):
@ -216,7 +216,7 @@ IS_DEAD = 999
PY24 = sys.version_info < (2, 5)
PY3 = sys.version_info > (3,)
if PY3:
if sys.version_info >= (3, 0):
import pickle
import _thread as thread
from io import BytesIO
@ -336,7 +336,7 @@ class Secret(UnicodeType):
def __repr__(self):
return '[secret]'
if not PY3:
if sys.version_info < (3, 0):
# TODO: what is this needed for in 2.x?
def __str__(self):
return UnicodeType(self)
@ -355,7 +355,7 @@ class Kwargs(dict):
whereas Python 3 produces keyword argument dicts whose keys are Unicode,
requiring a helper for Python 2.4/2.5, where bytes are required.
"""
if PY3:
if sys.version_info >= (3, 0):
def __init__(self, dct):
for k, v in dct.items():
if type(k) is bytes:
@ -769,19 +769,19 @@ class Py24Pickler(py_pickle.Pickler):
else:
py_pickle.Pickler.save_inst(self, obj)
if PY24:
if sys.version_info < (2, 5):
dispatch = py_pickle.Pickler.dispatch.copy()
dispatch[py_pickle.InstanceType] = save_exc_inst
if PY3:
if sys.version_info >= (3, 0):
# In 3.x Unpickler is a class exposing find_class as an overridable, but it
# cannot be overridden without subclassing.
class _Unpickler(pickle.Unpickler):
def find_class(self, module, func):
return self.find_global(module, func)
pickle__dumps = pickle.dumps
elif PY24:
elif sys.version_info < (2, 5):
# On Python 2.4, we must use a pure-Python pickler.
pickle__dumps = Py24Pickler.dumps
_Unpickler = pickle.Unpickler
@ -950,7 +950,7 @@ class Message(object):
LOG.debug('dropping reply to message with no return address: %r',
msg)
if PY3:
if sys.version_info >= (3, 0):
UNPICKLER_KWARGS = {'encoding': 'bytes'}
else:
UNPICKLER_KWARGS = {}
@ -1324,7 +1324,7 @@ class Importer(object):
'org',
]
if PY3:
if sys.version_info >= (3, 0):
ALWAYS_BLACKLIST += ['cStringIO']
def __init__(self, router, context, core_src, whitelist=(), blacklist=()):
@ -1556,7 +1556,7 @@ class Importer(object):
self._lock.acquire()
try:
self._cache[fullname] = tup
if tup[2] is not None and PY24:
if sys.version_info < (2, 5) and tup[2] is not None:
_update_linecache(
path='master:' + tup[2],
data=zlib.decompress(tup[3])
@ -1679,7 +1679,7 @@ class Importer(object):
else:
mod.__package__ = str_rpartition(fullname, '.')[0] or None
if mod.__package__ and not PY3:
if sys.version_info < (3, 0) and mod.__package__:
# 2.x requires __package__ to be exactly a string.
mod.__package__, _ = _codecs.utf_8_encode(mod.__package__)
@ -1690,7 +1690,7 @@ class Importer(object):
LOG.exception('while importing %r', fullname)
raise
if PY3:
if sys.version_info >= (3, 0):
exec(code, vars(mod))
else:
exec('exec code in vars(mod)')
@ -1717,7 +1717,7 @@ class Importer(object):
raise ModuleNotFoundError(self.absent_msg % (fullname,))
source = zlib.decompress(self._cache[fullname][3])
if PY3:
if sys.version_info >= (3, 0):
return to_text(source)
return source

@ -79,7 +79,7 @@ except IOError:
SELINUX_ENABLED = False
if mitogen.core.PY3:
if sys.version_info >= (3, 0):
xrange = range
closure_attr = '__closure__'
IM_SELF_ATTR = '__self__'

@ -52,7 +52,7 @@ _pool_pid = None
_pool_lock = threading.Lock()
if mitogen.core.PY3:
if sys.version_info >= (3, 0):
def func_code(func):
return func.__code__
def func_name(func):

Loading…
Cancel
Save