mercurial/thirdparty/zope/interface/_compat.py
author Gregory Szorc <gregory.szorc@gmail.com>
Wed, 21 Mar 2018 19:52:30 -0700
changeset 37178 68ee61822182
parent 37176 943d77fc07a3
permissions -rw-r--r--
thirdparty: port zope.interface to relative imports By using relative imports, we're guaranteed to get modules vendored with Mercurial rather than other random modules that might be in sys.path. My editor strips trailing whitespace on save. So some minor source code cleanup was also performed as part of this commit. # no-check-commit because some modified lines have double newlines Differential Revision: https://phab.mercurial-scm.org/D2930
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37176
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
##############################################################################
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# Copyright (c) 2006 Zope Foundation and Contributors.
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
# All Rights Reserved.
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
#
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
# This software is subject to the provisions of the Zope Public License,
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
# FOR A PARTICULAR PURPOSE.
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
#
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
##############################################################################
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
"""Basic components support
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
"""
37178
68ee61822182 thirdparty: port zope.interface to relative imports
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37176
diff changeset
    16
from __future__ import absolute_import
68ee61822182 thirdparty: port zope.interface to relative imports
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37176
diff changeset
    17
37176
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
import sys
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    19
import types
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    20
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
if sys.version_info[0] < 3:
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
    def _normalize_name(name):
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
        if isinstance(name, basestring):
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
            return unicode(name)
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
        raise TypeError("name must be a regular or unicode string")
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
    CLASS_TYPES = (type, types.ClassType)
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
    STRING_TYPES = (basestring,)
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
    _BUILTINS = '__builtin__'
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
    PYTHON3 = False
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
    PYTHON2 = True
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
else:
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
    def _normalize_name(name):
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
        if isinstance(name, bytes):
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
            name = str(name, 'ascii')
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
        if isinstance(name, str):
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
            return name
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
        raise TypeError("name must be a string or ASCII-only bytes")
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
    CLASS_TYPES = (type,)
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
    STRING_TYPES = (str,)
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
    _BUILTINS = 'builtins'
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
    PYTHON3 = True
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
    PYTHON2 = False
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
def _skip_under_py3k(test_method):
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
    import unittest
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
    return unittest.skipIf(sys.version_info[0] >= 3, "Only on Python 2")(test_method)
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
def _skip_under_py2(test_method):
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
    import unittest
943d77fc07a3 thirdparty: vendor zope.interface 4.4.3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
    return unittest.skipIf(sys.version_info[0] < 3, "Only on Python 3")(test_method)