Return a list of slot names for a given class. This needs to find slots defined by the class and its bases, so we can't simply return the __slots__ attribute. We must walk down the Method Resolution Order and concatenate the __slots__ of each class found there. (This assumes class
(cls)
| 110 | return cls.__new__(cls, *args, **kwargs) |
| 111 | |
| 112 | def _slotnames(cls): |
| 113 | """Return a list of slot names for a given class. |
| 114 | |
| 115 | This needs to find slots defined by the class and its bases, so we |
| 116 | can't simply return the __slots__ attribute. We must walk down |
| 117 | the Method Resolution Order and concatenate the __slots__ of each |
| 118 | class found there. (This assumes classes don't modify their |
| 119 | __slots__ attribute to misrepresent their slots after the class is |
| 120 | defined.) |
| 121 | """ |
| 122 | |
| 123 | # Get the value from a cache in the class if possible |
| 124 | names = cls.__dict__.get("__slotnames__") |
| 125 | if names is not None: |
| 126 | return names |
| 127 | |
| 128 | # Not cached -- calculate the value |
| 129 | names = [] |
| 130 | if not hasattr(cls, "__slots__"): |
| 131 | # This class has no slots |
| 132 | pass |
| 133 | else: |
| 134 | # Slots found -- gather slot names from all base classes |
| 135 | for c in cls.__mro__: |
| 136 | if "__slots__" in c.__dict__: |
| 137 | slots = c.__dict__['__slots__'] |
| 138 | # if class has a single slot, it can be given as a string |
| 139 | if isinstance(slots, str): |
| 140 | slots = (slots,) |
| 141 | for name in slots: |
| 142 | # special descriptors |
| 143 | if name in ("__dict__", "__weakref__"): |
| 144 | continue |
| 145 | # mangled names |
| 146 | elif name.startswith('__') and not name.endswith('__'): |
| 147 | stripped = c.__name__.lstrip('_') |
| 148 | if stripped: |
| 149 | names.append('_%s%s' % (stripped, name)) |
| 150 | else: |
| 151 | names.append(name) |
| 152 | else: |
| 153 | names.append(name) |
| 154 | |
| 155 | # Cache the outcome in the class if at all possible |
| 156 | try: |
| 157 | cls.__slotnames__ = names |
| 158 | except: |
| 159 | pass # But don't die if we can't |
| 160 | |
| 161 | return names |
| 162 | |
| 163 | # A registry of extension codes. This is an ad-hoc compression |
| 164 | # mechanism. Whenever a global reference to <module>, <name> is about |
nothing calls this directly
no test coverage detected
searching dependent graphs…