1 ########################################################################### |
|
2 # |
|
3 # Psyco class support module. |
|
4 # Copyright (C) 2001-2002 Armin Rigo et.al. |
|
5 |
|
6 """Psyco class support module. |
|
7 |
|
8 'psyco.classes.psyobj' is an alternate Psyco-optimized root for classes. |
|
9 Any class inheriting from it or using the metaclass '__metaclass__' might |
|
10 get optimized specifically for Psyco. It is equivalent to call |
|
11 psyco.bind() on the class object after its creation. |
|
12 |
|
13 Importing everything from psyco.classes in a module will import the |
|
14 '__metaclass__' name, so all classes defined after a |
|
15 |
|
16 from psyco.classes import * |
|
17 |
|
18 will automatically use the Psyco-optimized metaclass. |
|
19 """ |
|
20 ########################################################################### |
|
21 |
|
22 __all__ = ['psyobj', 'psymetaclass', '__metaclass__'] |
|
23 |
|
24 |
|
25 from _psyco import compacttype |
|
26 import core |
|
27 from types import FunctionType |
|
28 |
|
29 class psymetaclass(compacttype): |
|
30 "Psyco-optimized meta-class. Turns all methods into Psyco proxies." |
|
31 |
|
32 def __new__(cls, name, bases, dict): |
|
33 bindlist = dict.get('__psyco__bind__') |
|
34 if bindlist is None: |
|
35 bindlist = [key for key, value in dict.items() |
|
36 if isinstance(value, FunctionType)] |
|
37 for attr in bindlist: |
|
38 dict[attr] = core.proxy(dict[attr]) |
|
39 return super(psymetaclass, cls).__new__(cls, name, bases, dict) |
|
40 |
|
41 psyobj = psymetaclass("psyobj", (), {}) |
|
42 __metaclass__ = psymetaclass |
|