futurize.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:pytestlab 作者: sangoma 项目源码 文件源码
def monkey_patch_support_for_python2():
    from future import standard_library
    standard_library.install_aliases()

    # Monkey patch the backported ipaddress module with alternative
    # versions that accept string addresses, in addition to unicode
    # addresses, in python2 code.
    #
    # This keep compatibility simple. Slightly complicated by the fact
    # that some of these classes inherit from each other.
    import ipaddress

    def python2_compat(cls, bases=()):
        def __init__(self, address, *args, **kwargs):
            if isinstance(address, str) and len(address) > 4:
                address = address.decode('utf-8')
            return cls.__init__(self, address, *args, **kwargs)

        return type(cls.__name__, (cls,) + bases, {'__init__': __init__})

    ipaddress.IPv4Network = python2_compat(ipaddress.IPv4Network)
    ipaddress.IPv4Address = python2_compat(ipaddress.IPv4Address)
    ipaddress.IPv4Interface = python2_compat(ipaddress.IPv4Interface,
                                             bases=(ipaddress.IPv4Address,))

    ipaddress.IPv6Network = python2_compat(ipaddress.IPv6Network)
    ipaddress.IPv6Address = python2_compat(ipaddress.IPv6Address)
    ipaddress.IPv6Interface = python2_compat(ipaddress.IPv6Interface,
                                             bases=(ipaddress.IPv6Address,))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号