1 package org.python.core; 3 4 7 8 public class PyMethod extends PyObject 9 { 10 public PyObject im_self; 11 public PyObject im_func; 12 public PyObject im_class; 13 public String __name__; 14 public PyObject __doc__; 15 16 public PyMethod(PyObject self, PyObject f, PyObject wherefound) { 17 im_func = f; 18 im_self = self; 19 im_class = wherefound; 20 } 21 22 public PyMethod(PyObject self, PyFunction f, PyObject wherefound) { 23 this(self, (PyObject)f, wherefound); 24 __name__ = f.__name__; 25 __doc__ = f.__doc__; 26 } 27 28 public PyMethod(PyObject self, PyReflectedFunction f, PyObject wherefound) 29 { 30 this(self, (PyObject)f, wherefound); 31 __name__ = f.__name__; 32 __doc__ = f.__doc__; 33 } 34 35 private static final String [] __members__ = { 36 "im_self", "im_func", "im_class", 37 "__doc__", "__name__", "__dict__", 38 }; 39 40 public PyObject __dir__() { 42 PyString members[] = new PyString[__members__.length]; 43 for (int i = 0; i < __members__.length; i++) 44 members[i] = new PyString(__members__[i]); 45 PyList ret = new PyList(members); 46 PyObject k = im_func.__getattr__("__dict__").invoke("keys"); 47 ret.extend(k); 48 return ret; 49 } 50 51 private void throwReadonly(String name) { 52 for (int i = 0; i < __members__.length; i++) 53 if (__members__[i] == name) 54 throw Py.TypeError("readonly attribute"); 55 throw Py.AttributeError(name); 56 } 57 58 public PyObject __findattr__(String name) { 59 PyObject ret = super.__findattr__(name); 60 if (ret != null) 61 return ret; 62 return im_func.__findattr__(name); 63 } 64 65 public void __delattr__(String name) { 66 im_func.__delattr__(name); 67 } 68 69 public PyObject _doget(PyObject container) { 70 return _doget(container, null); 71 } 72 73 public PyObject _doget(PyObject container, PyObject wherefound) { 74 75 if (container == null) 76 return this; 77 if (__builtin__.issubclass(container.fastGetClass(), im_class)) 78 if (im_func instanceof PyFunction) 79 return new PyMethod(container, (PyFunction)im_func, 80 im_class); 81 else if (im_func instanceof PyReflectedFunction) 82 return new PyMethod(container, (PyReflectedFunction)im_func, 83 im_class); 84 else 85 return new PyMethod(container, im_func, im_class); 86 return this; 87 } 88 89 90 public PyObject __call__(PyObject[] args, String [] keywords) { 91 if (im_self != null) 92 return im_func.__call__(im_self, args, keywords); 94 boolean badcall = false; 96 if (im_class == null) 97 ; 105 else if (args.length < 1) 106 badcall = true; 107 else badcall = ! __builtin__.issubclass(args[0].fastGetClass(),im_class); 111 if (badcall) { 112 String got ="nothing"; 113 if (args.length>=1) 114 got = class_name(args[0].fastGetClass())+" instance"; 115 throw Py.TypeError("unbound method " + __name__ + "() must be " + 116 "called with "+class_name(im_class)+ " instance as first argument"+ 117 " (got "+got+" instead)"); 118 } 119 else 120 return im_func.__call__(args, keywords); 121 } 122 123 public int __cmp__(PyObject other) { 124 if (other instanceof PyMethod) { 125 PyMethod mother = (PyMethod)other; 126 if (im_self != mother.im_self) 127 return System.identityHashCode(im_self) < 128 System.identityHashCode(mother.im_self) ? -1 : 1; 129 if (im_func != mother.im_func) 130 return System.identityHashCode(im_func) < 131 System.identityHashCode(mother.im_func) ? -1 : 1; 132 return 0; 133 } 134 return -2; 135 } 136 137 public String safeRepr() throws PyIgnoreMethodTag { 138 return "'method' object"; 139 } 140 141 private String class_name(PyObject cls) { 142 if (cls instanceof PyClass) 143 return ((PyClass)cls).__name__; 144 if (cls instanceof PyType) 145 return ((PyType)cls).fastGetName(); 146 return "?"; 147 } 148 149 public String toString() { 150 String classname = "?"; 151 if (im_class != null) 152 classname = class_name(im_class); 153 if (im_self == null) 154 return "<unbound method " + classname + "." + __name__ + ">"; 156 else 157 return "<method " + classname + "." + 158 __name__ + " of " + class_name(im_self.fastGetClass()) + 159 " instance " + Py.idstr(im_self) + ">"; 160 } 161 } 162 | Popular Tags |