python版本兼容性开发

 这是elftools.common里的一个模块,用于兼容python2和python3不同的版本,在开发中如果需要兼容不同python版本的应用程序,可参考如下设计,另外最新的elftools已经删去了这个模块,所以如果在使用最新的elftools模块时,遇到找不到该模块的错误,可依据使用的python版本自行调整代码,部分接口移到了utils模块,也可以在使用utils模块里的接口。

#-------------------------------------------------------------------------------
# elftools: common/py3compat.py
#
# Python 2/3 compatibility code
#
# Eli Bendersky (eliben@gmail.com)
# This code is in the public domain
#-------------------------------------------------------------------------------
import sys
PY3 = sys.version_info[0] == 3


if PY3:
    import io
    from pathlib import Path

    StringIO = io.StringIO
    BytesIO = io.BytesIO

    # Functions for acting on bytestrings and strings. In Python 2 and 3,
    # strings and bytes are the same and chr/ord can be used to convert between
    # numeric byte values and their string representations. In Python 3, bytes
    # and strings are different types and bytes hold numeric values when
    # iterated over.

    def bytes2hex(b, sep=''):
        if not sep:
            return b.hex()
        return sep.join(map('{:02x}'.format, b))

    def bytes2str(b): return b.decode('latin-1')
    def str2bytes(s): return s.encode('latin-1')
    def int2byte(i): return bytes((i,))
    def byte2int(b): return b

    def iterbytes(b):
        """Return an iterator over the elements of a bytes object.

        For example, for b'abc' yields b'a', b'b' and then b'c'.
        """
        for i in range(len(b)):
            yield b[i:i+1]

    ifilter = filter

    maxint = sys.maxsize

    def path_to_posix(s):
        return Path(s).as_posix()

else:
    import cStringIO
    import os
    import posixpath

    StringIO = BytesIO = cStringIO.StringIO

    def bytes2hex(b, sep=''):
        res = b.encode('hex')
        if not sep:
            return res
        return sep.join(res[i:i+2] for i in range(0, len(res), 2))

    def bytes2str(b): return b
    def str2bytes(s): return s
    int2byte = chr
    byte2int = ord
    def iterbytes(b):
        return iter(b)

    from itertools import ifilter

    maxint = sys.maxint

    def path_to_posix(s):
        return posixpath.join(*os.path.split(s))


def iterkeys(d):
    """Return an iterator over the keys of a dictionary."""
    return getattr(d, 'keys' if PY3 else 'iterkeys')()

def itervalues(d):
    """Return an iterator over the values of a dictionary."""
    return getattr(d, 'values' if PY3 else 'itervalues')()

def iteritems(d):
    """Return an iterator over the items of a dictionary."""
    return getattr(d, 'items' if PY3 else 'iteritems')()

try:
    from collections.abc import Mapping  # python >= 3.3
except ImportError:
    from collections import Mapping  # python < 3.3

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值