view mercurial/thirdparty/attr/_compat.py @ 35935:00b9e26d727b

sshpeer: establish SSH connection before class instantiation We want to move the handshake to before peers are created so we can instantiate a different peer class depending on the results of the handshake. This necessitates moving the SSH process invocation to outside the peer class. As part of the code move, some variables were renamed for clarity. util.popen4() returns stdin, stdout, and stderr in their typical file descriptor order. However, stdin and stdout were being mapped to "pipeo" and "pipei" respectively. "o" for "stdin" and "i" for "stdout" is a bit confusing. Although it does make sense for "output" and "input" from the perspective of the client. But in the context of the new function, it makes sense to refer to these as their file descriptor names. In addition, the last use of self._path disappeared, so we stop setting that attribute and we can delete the redundant URL parsing necessary to set it. Differential Revision: https://phab.mercurial-scm.org/D2031
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 05 Feb 2018 14:05:59 -0800
parents 765eb17a7eb8
children e1c586b9a43c
line wrap: on
line source

from __future__ import absolute_import, division, print_function

import sys
import types


PY2 = sys.version_info[0] == 2


if PY2:
    from UserDict import IterableUserDict

    # We 'bundle' isclass instead of using inspect as importing inspect is
    # fairly expensive (order of 10-15 ms for a modern machine in 2016)
    def isclass(klass):
        return isinstance(klass, (type, types.ClassType))

    # TYPE is used in exceptions, repr(int) is different on Python 2 and 3.
    TYPE = "type"

    def iteritems(d):
        return d.iteritems()

    def iterkeys(d):
        return d.iterkeys()

    # Python 2 is bereft of a read-only dict proxy, so we make one!
    class ReadOnlyDict(IterableUserDict):
        """
        Best-effort read-only dict wrapper.
        """

        def __setitem__(self, key, val):
            # We gently pretend we're a Python 3 mappingproxy.
            raise TypeError("'mappingproxy' object does not support item "
                            "assignment")

        def update(self, _):
            # We gently pretend we're a Python 3 mappingproxy.
            raise AttributeError("'mappingproxy' object has no attribute "
                                 "'update'")

        def __delitem__(self, _):
            # We gently pretend we're a Python 3 mappingproxy.
            raise TypeError("'mappingproxy' object does not support item "
                            "deletion")

        def clear(self):
            # We gently pretend we're a Python 3 mappingproxy.
            raise AttributeError("'mappingproxy' object has no attribute "
                                 "'clear'")

        def pop(self, key, default=None):
            # We gently pretend we're a Python 3 mappingproxy.
            raise AttributeError("'mappingproxy' object has no attribute "
                                 "'pop'")

        def popitem(self):
            # We gently pretend we're a Python 3 mappingproxy.
            raise AttributeError("'mappingproxy' object has no attribute "
                                 "'popitem'")

        def setdefault(self, key, default=None):
            # We gently pretend we're a Python 3 mappingproxy.
            raise AttributeError("'mappingproxy' object has no attribute "
                                 "'setdefault'")

        def __repr__(self):
            # Override to be identical to the Python 3 version.
            return "mappingproxy(" + repr(self.data) + ")"

    def metadata_proxy(d):
        res = ReadOnlyDict()
        res.data.update(d)  # We blocked update, so we have to do it like this.
        return res

else:
    def isclass(klass):
        return isinstance(klass, type)

    TYPE = "class"

    def iteritems(d):
        return d.items()

    def iterkeys(d):
        return d.keys()

    def metadata_proxy(d):
        return types.MappingProxyType(dict(d))