Lichen

Annotated lib/__builtins__/core.py

274:5be8a7aa0f86
2016-11-29 Paul Boddie Support attribute accesses on the type class using other classes as contexts, treating self (the context) in type methods as either class or instance. This simplifies the str and repr functions but introduces a special case in the deducer for the type class and makes the __test_context operation more expensive for non-instance contexts.
paul@6 1
#!/usr/bin/env python
paul@6 2
paul@6 3
"""
paul@6 4
Core objects.
paul@6 5
paul@107 6
Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
paul@6 7
paul@6 8
This program is free software; you can redistribute it and/or modify it under
paul@6 9
the terms of the GNU General Public License as published by the Free Software
paul@6 10
Foundation; either version 3 of the License, or (at your option) any later
paul@6 11
version.
paul@6 12
paul@6 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@6 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@6 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@6 16
details.
paul@6 17
paul@6 18
You should have received a copy of the GNU General Public License along with
paul@6 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@6 20
"""
paul@6 21
paul@230 22
from native import _get_using
paul@230 23
paul@6 24
class object:
paul@107 25
paul@107 26
    "The root class of all objects except functions."
paul@107 27
paul@6 28
    def __init__(self):
paul@230 29
paul@6 30
        "No-operation."
paul@230 31
paul@6 32
        pass
paul@107 33
paul@6 34
    def __bool__(self):
paul@230 35
paul@6 36
        "Objects are true by default."
paul@230 37
paul@6 38
        return True
paul@6 39
paul@230 40
    def __str__(self):
paul@230 41
paul@230 42
        "Return a string representation."
paul@230 43
paul@254 44
        return str(buffer(["<", self.__name__, " instance>"]))
paul@230 45
paul@230 46
    __repr__ = __str__
paul@230 47
paul@269 48
class module:
paul@269 49
paul@269 50
    "The class of module objects."
paul@269 51
paul@271 52
    def __init__(self):
paul@271 53
paul@271 54
        """
paul@271 55
        Reserve special attributes for module instances.
paul@271 56
        """
paul@271 57
paul@271 58
        self.__file__ = None
paul@271 59
        self.__mname__ = None
paul@271 60
paul@269 61
    def __str__(self):
paul@269 62
paul@269 63
        "Return a string representation."
paul@269 64
paul@271 65
        return self.__mname__
paul@269 66
paul@269 67
    __repr__ = __str__
paul@269 68
paul@107 69
class function:
paul@107 70
paul@107 71
    """
paul@107 72
    The class of all function objects.
paul@107 73
    Note that as a special case, function does not inherit from object.
paul@107 74
    """
paul@107 75
paul@6 76
    def __init__(self):
paul@6 77
paul@107 78
        """
paul@107 79
        Reserve special attributes for function instances.
paul@107 80
        """
paul@6 81
paul@6 82
        self.__fn__ = None
paul@6 83
        self.__args__ = None
paul@251 84
        self.__fname__ = None
paul@6 85
paul@107 86
    def __bool__(self):
paul@230 87
paul@107 88
        "Functions are true by default."
paul@230 89
paul@107 90
        return True
paul@107 91
paul@230 92
    def __str__(self):
paul@230 93
paul@230 94
        "Return a string representation."
paul@230 95
paul@251 96
        return self.__fname__
paul@230 97
paul@230 98
    __repr__ = __str__
paul@230 99
paul@274 100
class type:
paul@107 101
paul@274 102
    """
paul@274 103
    The class of all classes. Methods of this class do not treat contexts as
paul@274 104
    instances, even though classes are meant to be instances of this class.
paul@274 105
    Instead, contexts are either classes or instances.
paul@274 106
    """
paul@107 107
paul@274 108
    def __str__(self):
paul@274 109
paul@274 110
        "Return a string representation."
paul@230 111
paul@274 112
        return self.__name__
paul@274 113
paul@274 114
    __repr__ = __str__
paul@6 115
paul@233 116
class BaseException:
paul@233 117
paul@233 118
    "The root of all exception types."
paul@233 119
paul@233 120
    pass
paul@233 121
paul@6 122
class Exception(BaseException): pass
paul@260 123
class MemoryError(Exception): pass
paul@233 124
class TypeError(Exception): pass
paul@193 125
class UnboundMethodInvocation(Exception): pass
paul@6 126
class Warning(object): pass
paul@6 127
paul@230 128
def get_using(callable, instance):
paul@230 129
paul@230 130
    "Return 'callable' bound to 'instance'."
paul@230 131
paul@230 132
    return _get_using(callable, instance)
paul@121 133
paul@6 134
# vim: tabstop=4 expandtab shiftwidth=4