1 package org.python.core; 3 4 7 8 public class PyFunction extends PyObject 9 { 10 public String __name__; 11 public PyObject __doc__; 12 public PyObject func_globals; 13 public PyObject[] func_defaults; 14 public PyCode func_code; 15 public PyObject __dict__; 16 public PyObject func_closure; 18 public PyFunction(PyObject globals, PyObject[] defaults, PyCode code, 19 PyObject doc,PyObject[] closure_cells) 20 { 21 func_globals = globals; 22 __name__ = code.co_name; 23 if (doc == null) 24 __doc__ = Py.None; 25 else 26 __doc__ = doc; 27 func_defaults = defaults; 28 func_code = code; 29 if (closure_cells != null) { 30 func_closure = new PyTuple(closure_cells); 31 } else { 32 func_closure = null; 33 } 34 } 35 36 public PyFunction(PyObject globals, PyObject[] defaults, PyCode code, 37 PyObject doc) { 38 this(globals,defaults,code,doc,null); 39 } 40 41 public PyFunction(PyObject globals, PyObject[] defaults, PyCode code) { 42 this(globals, defaults, code, null,null); 43 } 44 45 public PyFunction(PyObject globals, PyObject[] defaults, PyCode code, 46 PyObject[] closure_cells) 47 { 48 this(globals, defaults, code, null,closure_cells); 49 } 50 51 52 private static final String [] __members__ = { 53 "__doc__", "func_doc", 54 "__name__", "func_name", "__dict__", 55 "func_globals", "func_defaults", "func_code", 56 "func_closure" 57 }; 58 59 public PyObject __dir__() { 60 PyString members[] = new PyString[__members__.length]; 61 for (int i = 0; i < __members__.length; i++) 62 members[i] = new PyString(__members__[i]); 63 PyList ret = new PyList(members); 64 PyDictionary accum = new PyDictionary(); 65 addKeys(accum, "__dict__"); 66 ret.extend(accum.keys()); 67 ret.sort(); 68 return ret; 69 } 70 71 private void throwReadonly(String name) { 72 for (int i = 0; i < __members__.length; i++) 73 if (__members__[i] == name) 74 throw Py.TypeError("readonly attribute"); 75 throw Py.AttributeError(name); 76 } 77 78 public void __setattr__(String name, PyObject value) { 79 if (name == "func_doc" || name == "__doc__") 83 __doc__ = value; 84 else if (name == "func_closure") { 85 if (!(value instanceof PyTuple)) { 86 throw Py.TypeError("func_closure must be set to a tuple"); 87 } 88 func_closure = value; 89 } 90 else if (name == "func_defaults") 94 throwReadonly(name); 95 else if (name == "func_code") { 96 if (value instanceof PyCode) 97 func_code = (PyCode) value; 98 else 99 throw Py.TypeError("func_code must be set to a code object"); 100 } else if (name == "__dict__" || name == "func_dict") { 101 if (value instanceof PyDictionary || value instanceof PyStringMap) 102 __dict__ = value; 103 else 104 throw Py.TypeError("setting function's dictionary " + 105 "to a non-dict"); 106 } else { 107 if (__dict__ == null) 108 __dict__ = new PyStringMap(); 109 __dict__.__setitem__(name, value); 110 } 111 } 112 113 public void __delattr__(String name) { 114 if (name == "__dict__" || name == "func_dict") { 115 throw Py.TypeError("function's dictionary may not be deleted"); 116 } else if (name == "func_defaults") { 117 func_defaults = Py.EmptyObjects; 118 return; 119 } else if (name == "func_doc" || name == "__doc__") { 120 __doc__ = Py.None; 121 return; 122 } 123 if (__dict__ == null) 124 throw Py.AttributeError(name); 125 __dict__.__delitem__(name); 126 } 127 128 public boolean isMappingType() { return false; } 129 public boolean isNumberType() { return false; } 130 public boolean isSequenceType() { return false; } 131 132 public PyObject __findattr__(String name) { 133 if (name == "func_doc") 135 return __doc__; 136 if (name == "func_name") 137 return new PyString(__name__); 138 if (name == "func_closure") { 139 if (func_closure != null) return func_closure; 140 return Py.None; 141 } 142 if (name == "func_defaults") { 143 if (func_defaults.length == 0) 144 return Py.None; 145 return new PyTuple(func_defaults); 146 } 147 if (name == "__dict__" || name == "func_dict") { 148 if (__dict__ == null) 149 __dict__ = new PyStringMap(); 150 return __dict__; 151 } 152 if (__dict__ != null) { 153 PyObject ret = __dict__.__finditem__(name); 154 if (ret != null) 155 return ret; 156 } 157 return super.__findattr__(name); 158 } 159 160 public PyObject _doget(PyObject container) { 161 return _doget(container, null); 163 } 164 165 public PyObject _doget(PyObject container, PyObject wherefound) { 166 return new PyMethod(container, this, wherefound); 169 } 170 171 public PyObject __call__() { 172 return func_code.call(func_globals, func_defaults, func_closure); 173 } 174 public PyObject __call__(PyObject arg) { 175 return func_code.call(arg, func_globals, func_defaults, func_closure); 176 } 177 public PyObject __call__(PyObject arg1, PyObject arg2) { 178 return func_code.call(arg1, arg2, func_globals, func_defaults, 179 func_closure); 180 } 181 public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) { 182 return func_code.call(arg1, arg2, arg3, func_globals, func_defaults, 183 func_closure); 184 } 185 186 public PyObject __call__(PyObject[] args, String [] keywords) { 187 return func_code.call(args, keywords, func_globals, func_defaults, 188 func_closure); 189 } 190 public PyObject __call__(PyObject arg1, PyObject[] args, 191 String [] keywords) 192 { 193 return func_code.call(arg1, args, keywords, func_globals, 194 func_defaults, func_closure); 195 } 196 public String toString() { 197 return "<function "+__name__+" "+Py.idstr(this)+">"; 198 } 199 } 200 | Popular Tags |