Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 1 | """Generic metaclass. |
| 2 | |
| 3 | XXX This is very much a work in progress. |
| 4 | |
| 5 | """ |
| 6 | |
| 7 | import types |
| 8 | |
| 9 | class MetaMethodWrapper: |
| 10 | |
| 11 | def __init__(self, func, inst): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 12 | self.func = func |
| 13 | self.inst = inst |
| 14 | self.__name__ = self.func.__name__ |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 15 | |
| 16 | def __call__(self, *args, **kw): |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 17 | return self.func(self.inst, *args, **kw) |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 18 | |
| 19 | class MetaHelper: |
| 20 | |
| 21 | __methodwrapper__ = MetaMethodWrapper # For derived helpers to override |
| 22 | |
| 23 | def __helperinit__(self, formalclass): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 24 | self.__formalclass__ = formalclass |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 25 | |
| 26 | def __getattr__(self, name): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 27 | # Invoked for any attr not in the instance's __dict__ |
| 28 | try: |
| 29 | raw = self.__formalclass__.__getattr__(name) |
| 30 | except AttributeError: |
| 31 | try: |
| 32 | ga = self.__formalclass__.__getattr__('__usergetattr__') |
Guido van Rossum | e13be40 | 2001-01-15 16:53:58 +0000 | [diff] [blame] | 33 | except (KeyError, AttributeError): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 34 | raise AttributeError(name) |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 35 | return ga(self, name) |
| 36 | if type(raw) != types.FunctionType: |
| 37 | return raw |
| 38 | return self.__methodwrapper__(raw, self) |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 39 | |
| 40 | class MetaClass: |
| 41 | |
| 42 | """A generic metaclass. |
| 43 | |
| 44 | This can be subclassed to implement various kinds of meta-behavior. |
| 45 | |
| 46 | """ |
| 47 | |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 48 | __helper__ = MetaHelper # For derived metaclasses to override |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 49 | |
| 50 | __inited = 0 |
| 51 | |
| 52 | def __init__(self, name, bases, dict): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 53 | try: |
| 54 | ga = dict['__getattr__'] |
| 55 | except KeyError: |
| 56 | pass |
| 57 | else: |
| 58 | dict['__usergetattr__'] = ga |
| 59 | del dict['__getattr__'] |
| 60 | self.__name__ = name |
| 61 | self.__bases__ = bases |
| 62 | self.__realdict__ = dict |
| 63 | self.__inited = 1 |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 64 | |
| 65 | def __getattr__(self, name): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 66 | try: |
| 67 | return self.__realdict__[name] |
| 68 | except KeyError: |
| 69 | for base in self.__bases__: |
| 70 | try: |
| 71 | return base.__getattr__(name) |
| 72 | except AttributeError: |
| 73 | pass |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 74 | raise AttributeError(name) |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 75 | |
| 76 | def __setattr__(self, name, value): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 77 | if not self.__inited: |
| 78 | self.__dict__[name] = value |
| 79 | else: |
| 80 | self.__realdict__[name] = value |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 81 | |
| 82 | def __call__(self, *args, **kw): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 83 | inst = self.__helper__() |
| 84 | inst.__helperinit__(self) |
| 85 | try: |
| 86 | init = inst.__getattr__('__init__') |
| 87 | except AttributeError: |
| 88 | init = lambda: None |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 89 | init(*args, **kw) |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 90 | return inst |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 92 | |
| 93 | Meta = MetaClass('Meta', (), {}) |
| 94 | |
| 95 | |
| 96 | def _test(): |
| 97 | class C(Meta): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 98 | def __init__(self, *args): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 99 | print("__init__, args =", args) |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 100 | def m1(self, x): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 101 | print("m1(x=%r)" % (x,)) |
| 102 | print(C) |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 103 | x = C() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 104 | print(x) |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 105 | x.m1(12) |
Guido van Rossum | 1fb071c | 1997-08-25 21:36:44 +0000 | [diff] [blame] | 106 | class D(C): |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 107 | def __getattr__(self, name): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 108 | if name[:2] == '__': raise AttributeError(name) |
Guido van Rossum | 4117e54 | 1998-09-14 16:44:15 +0000 | [diff] [blame] | 109 | return "getattr:%s" % name |
Guido van Rossum | 1fb071c | 1997-08-25 21:36:44 +0000 | [diff] [blame] | 110 | x = D() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 111 | print(x.foo) |
| 112 | print(x._foo) |
Guido van Rossum | 1fb071c | 1997-08-25 21:36:44 +0000 | [diff] [blame] | 113 | ## print x.__foo |
| 114 | ## print x.__foo__ |
| 115 | |
Guido van Rossum | 27e4aa3 | 1997-08-25 15:37:59 +0000 | [diff] [blame] | 116 | |
| 117 | if __name__ == '__main__': |
| 118 | _test() |