Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/netaddr/compat.py: 47%
47 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:45 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:45 +0000
1#-----------------------------------------------------------------------------
2# Copyright (c) 2008 by David P. D. Moss. All rights reserved.
3#
4# Released under the BSD license. See the LICENSE file for details.
5#-----------------------------------------------------------------------------
6"""
7Compatibility wrappers providing uniform behaviour for Python code required to
8run under both Python 2.x and 3.x.
10All operations emulate 2.x behaviour where applicable.
11"""
12import sys as _sys
14if _sys.version_info[0] == 3:
15 # Python 3.x specific logic.
16 _sys_maxint = _sys.maxsize
18 _int_type = int
20 _str_type = str
22 _bytes_type = lambda x: bytes(x, 'UTF-8')
24 _is_str = lambda x: isinstance(x, (str, type(''.encode())))
26 _is_int = lambda x: isinstance(x, int)
28 _callable = lambda x: hasattr(x, '__call__')
30 _dict_keys = lambda x: list(x.keys())
32 _dict_items = lambda x: list(x.items())
34 _iter_dict_keys = lambda x: x.keys()
36 def _bytes_join(*args):
37 return ''.encode().join(*args)
39 def _zip(*args):
40 return list(zip(*args))
42 def _range(*args, **kwargs):
43 return list(range(*args, **kwargs))
45 _iter_range = range
47 def _iter_next(x):
48 return next(x)
50elif _sys.version_info[0:2] > [2, 3]:
51 # Python 2.4 or higher.
52 _sys_maxint = _sys.maxint
54 _int_type = (int, long)
56 _str_type = basestring
58 _bytes_type = str
60 _is_str = lambda x: isinstance(x, basestring)
62 _is_int = lambda x: isinstance(x, (int, long))
64 _callable = lambda x: callable(x)
66 _dict_keys = lambda x: x.keys()
68 _dict_items = lambda x: x.items()
70 _iter_dict_keys = lambda x: iter(x.keys())
72 def _bytes_join(*args):
73 return ''.join(*args)
75 def _zip(*args):
76 return zip(*args)
78 def _range(*args, **kwargs):
79 return range(*args, **kwargs)
81 _iter_range = xrange
83 def _iter_next(x):
84 return x.next()
85else:
86 # Unsupported versions.
87 raise RuntimeError(
88 'this module only supports Python 2.4.x or higher (including 3.x)!')
90try:
91 from importlib import resources as _importlib_resources
92except ImportError:
93 import importlib_resources as _importlib_resources