| Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Function object implementation */ | 
 | 3 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5 | #include "code.h" | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6 | #include "eval.h" | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 7 | #include "structmember.h" | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 9 | PyObject * | 
| Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 10 | PyFunction_New(PyObject *code, PyObject *globals) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | { | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 12 | 	PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 13 | 					    &PyFunction_Type); | 
| Martin v. Löwis | 321c9ab | 2004-03-23 18:40:15 +0000 | [diff] [blame] | 14 | 	static PyObject *__name__ = 0; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 15 | 	if (op != NULL) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 16 | 		PyObject *doc; | 
 | 17 | 		PyObject *consts; | 
| Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 18 | 		PyObject *module; | 
| Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 19 | 		op->func_weakreflist = NULL; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 20 | 		Py_INCREF(code); | 
| Guido van Rossum | 846e431 | 1990-11-18 17:44:06 +0000 | [diff] [blame] | 21 | 		op->func_code = code; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 22 | 		Py_INCREF(globals); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 23 | 		op->func_globals = globals; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 24 | 		op->func_name = ((PyCodeObject *)code)->co_name; | 
 | 25 | 		Py_INCREF(op->func_name); | 
| Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 26 | 		op->func_defaults = NULL; /* No default arguments */ | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 27 | 		op->func_kwdefaults = NULL; /* No keyword only defaults */ | 
| Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 28 | 		op->func_closure = NULL; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 29 | 		consts = ((PyCodeObject *)code)->co_consts; | 
 | 30 | 		if (PyTuple_Size(consts) >= 1) { | 
 | 31 | 			doc = PyTuple_GetItem(consts, 0); | 
| Neal Norwitz | 7e95bef | 2007-08-26 03:56:04 +0000 | [diff] [blame] | 32 | 			if (!PyUnicode_Check(doc)) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 33 | 				doc = Py_None; | 
| Guido van Rossum | 5bd3805 | 1995-01-07 12:01:30 +0000 | [diff] [blame] | 34 | 		} | 
 | 35 | 		else | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 36 | 			doc = Py_None; | 
 | 37 | 		Py_INCREF(doc); | 
| Guido van Rossum | 5bd3805 | 1995-01-07 12:01:30 +0000 | [diff] [blame] | 38 | 		op->func_doc = doc; | 
| Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 39 | 		op->func_dict = NULL; | 
| Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 40 | 		op->func_module = NULL; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 41 | 		op->func_annotations = NULL; | 
| Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 42 |  | 
 | 43 | 		/* __module__: If module name is in globals, use it. | 
 | 44 | 		   Otherwise, use None. | 
 | 45 | 		*/ | 
| Martin v. Löwis | 321c9ab | 2004-03-23 18:40:15 +0000 | [diff] [blame] | 46 | 		if (!__name__) { | 
| Neal Norwitz | 7e95bef | 2007-08-26 03:56:04 +0000 | [diff] [blame] | 47 | 			__name__ = PyUnicode_InternFromString("__name__"); | 
| Martin v. Löwis | 321c9ab | 2004-03-23 18:40:15 +0000 | [diff] [blame] | 48 | 			if (!__name__) { | 
 | 49 | 				Py_DECREF(op); | 
 | 50 | 				return NULL; | 
 | 51 | 			} | 
 | 52 | 		} | 
 | 53 | 		module = PyDict_GetItem(globals, __name__); | 
| Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 54 | 		if (module) { | 
 | 55 | 		    Py_INCREF(module); | 
 | 56 | 		    op->func_module = module; | 
 | 57 | 		} | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | 	} | 
| Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 59 | 	else | 
 | 60 | 		return NULL; | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 61 | 	_PyObject_GC_TRACK(op); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | 	return (PyObject *)op; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | } | 
 | 64 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 65 | PyObject * | 
| Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 66 | PyFunction_GetCode(PyObject *op) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 68 | 	if (!PyFunction_Check(op)) { | 
 | 69 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | 		return NULL; | 
 | 71 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 72 | 	return ((PyFunctionObject *) op) -> func_code; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | } | 
 | 74 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 75 | PyObject * | 
| Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 76 | PyFunction_GetGlobals(PyObject *op) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | 	if (!PyFunction_Check(op)) { | 
 | 79 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | 		return NULL; | 
 | 81 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | 	return ((PyFunctionObject *) op) -> func_globals; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | } | 
 | 84 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 85 | PyObject * | 
| Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 86 | PyFunction_GetModule(PyObject *op) | 
 | 87 | { | 
 | 88 | 	if (!PyFunction_Check(op)) { | 
 | 89 | 		PyErr_BadInternalCall(); | 
 | 90 | 		return NULL; | 
 | 91 | 	} | 
 | 92 | 	return ((PyFunctionObject *) op) -> func_module; | 
 | 93 | } | 
 | 94 |  | 
 | 95 | PyObject * | 
| Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 96 | PyFunction_GetDefaults(PyObject *op) | 
| Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 97 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 98 | 	if (!PyFunction_Check(op)) { | 
 | 99 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 100 | 		return NULL; | 
 | 101 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 102 | 	return ((PyFunctionObject *) op) -> func_defaults; | 
| Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 103 | } | 
 | 104 |  | 
 | 105 | int | 
| Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 106 | PyFunction_SetDefaults(PyObject *op, PyObject *defaults) | 
| Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 107 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 108 | 	if (!PyFunction_Check(op)) { | 
 | 109 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 110 | 		return -1; | 
 | 111 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 112 | 	if (defaults == Py_None) | 
| Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 113 | 		defaults = NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 114 | 	else if (defaults && PyTuple_Check(defaults)) { | 
 | 115 | 		Py_INCREF(defaults); | 
| Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 116 | 	} | 
| Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 117 | 	else { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | 		PyErr_SetString(PyExc_SystemError, "non-tuple default args"); | 
| Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 119 | 		return -1; | 
 | 120 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 121 | 	Py_XDECREF(((PyFunctionObject *) op) -> func_defaults); | 
 | 122 | 	((PyFunctionObject *) op) -> func_defaults = defaults; | 
| Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 123 | 	return 0; | 
 | 124 | } | 
 | 125 |  | 
| Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 126 | PyObject * | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 127 | PyFunction_GetKwDefaults(PyObject *op) | 
 | 128 | { | 
 | 129 | 	if (!PyFunction_Check(op)) { | 
 | 130 | 		PyErr_BadInternalCall(); | 
 | 131 | 		return NULL; | 
 | 132 | 	} | 
 | 133 | 	return ((PyFunctionObject *) op) -> func_kwdefaults; | 
 | 134 | } | 
 | 135 |  | 
 | 136 | int | 
 | 137 | PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults) | 
 | 138 | { | 
 | 139 | 	if (!PyFunction_Check(op)) { | 
 | 140 | 		PyErr_BadInternalCall(); | 
 | 141 | 		return -1; | 
 | 142 | 	} | 
 | 143 | 	if (defaults == Py_None) | 
 | 144 | 		defaults = NULL; | 
 | 145 | 	else if (defaults && PyDict_Check(defaults)) { | 
 | 146 | 		Py_INCREF(defaults); | 
 | 147 | 	} | 
 | 148 | 	else { | 
 | 149 | 		PyErr_SetString(PyExc_SystemError, | 
 | 150 | 				"non-dict keyword only default args"); | 
 | 151 | 		return -1; | 
 | 152 | 	} | 
 | 153 | 	Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults); | 
 | 154 | 	((PyFunctionObject *) op) -> func_kwdefaults = defaults; | 
 | 155 | 	return 0; | 
 | 156 | } | 
 | 157 |  | 
 | 158 | PyObject * | 
| Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 159 | PyFunction_GetClosure(PyObject *op) | 
 | 160 | { | 
 | 161 | 	if (!PyFunction_Check(op)) { | 
 | 162 | 		PyErr_BadInternalCall(); | 
 | 163 | 		return NULL; | 
 | 164 | 	} | 
 | 165 | 	return ((PyFunctionObject *) op) -> func_closure; | 
 | 166 | } | 
 | 167 |  | 
 | 168 | int | 
 | 169 | PyFunction_SetClosure(PyObject *op, PyObject *closure) | 
 | 170 | { | 
 | 171 | 	if (!PyFunction_Check(op)) { | 
 | 172 | 		PyErr_BadInternalCall(); | 
 | 173 | 		return -1; | 
 | 174 | 	} | 
 | 175 | 	if (closure == Py_None) | 
 | 176 | 		closure = NULL; | 
 | 177 | 	else if (PyTuple_Check(closure)) { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 178 | 		Py_INCREF(closure); | 
| Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 179 | 	} | 
 | 180 | 	else { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 181 | 		PyErr_Format(PyExc_SystemError,  | 
 | 182 | 			     "expected tuple for closure, got '%.100s'", | 
 | 183 | 			     closure->ob_type->tp_name); | 
| Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 184 | 		return -1; | 
 | 185 | 	} | 
 | 186 | 	Py_XDECREF(((PyFunctionObject *) op) -> func_closure); | 
 | 187 | 	((PyFunctionObject *) op) -> func_closure = closure; | 
 | 188 | 	return 0; | 
 | 189 | } | 
 | 190 |  | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 191 | PyObject * | 
 | 192 | PyFunction_GetAnnotations(PyObject *op) | 
 | 193 | { | 
 | 194 | 	if (!PyFunction_Check(op)) { | 
 | 195 | 		PyErr_BadInternalCall(); | 
 | 196 | 		return NULL; | 
 | 197 | 	} | 
 | 198 | 	return ((PyFunctionObject *) op) -> func_annotations; | 
 | 199 | } | 
 | 200 |  | 
 | 201 | int | 
 | 202 | PyFunction_SetAnnotations(PyObject *op, PyObject *annotations) | 
 | 203 | { | 
 | 204 | 	if (!PyFunction_Check(op)) { | 
 | 205 | 		PyErr_BadInternalCall(); | 
 | 206 | 		return -1; | 
 | 207 | 	} | 
 | 208 | 	if (annotations == Py_None) | 
 | 209 | 		annotations = NULL; | 
 | 210 | 	else if (annotations && PyDict_Check(annotations)) { | 
 | 211 | 		Py_INCREF(annotations); | 
 | 212 | 	} | 
 | 213 | 	else { | 
 | 214 | 		PyErr_SetString(PyExc_SystemError, | 
 | 215 | 				"non-dict annotations"); | 
 | 216 | 		return -1; | 
 | 217 | 	} | 
 | 218 | 	Py_XDECREF(((PyFunctionObject *)op) -> func_annotations); | 
 | 219 | 	((PyFunctionObject *) op) -> func_annotations = annotations; | 
 | 220 | 	return 0; | 
 | 221 | } | 
 | 222 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | /* Methods */ | 
 | 224 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 225 | #define OFF(x) offsetof(PyFunctionObject, x) | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 226 |  | 
| Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 227 | static PyMemberDef func_memberlist[] = { | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 228 |         {"__closure__",   T_OBJECT,     OFF(func_closure), | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 229 | 	 RESTRICTED|READONLY}, | 
| Christian Heimes | bce52be | 2007-11-17 19:08:41 +0000 | [diff] [blame] | 230 |         {"__doc__",       T_OBJECT,     OFF(func_doc), PY_WRITE_RESTRICTED}, | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 231 |         {"__globals__",   T_OBJECT,     OFF(func_globals), | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 232 | 	 RESTRICTED|READONLY}, | 
| Christian Heimes | bce52be | 2007-11-17 19:08:41 +0000 | [diff] [blame] | 233 |         {"__module__",    T_OBJECT,     OFF(func_module), PY_WRITE_RESTRICTED}, | 
| Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 234 |         {NULL}  /* Sentinel */ | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 235 | }; | 
 | 236 |  | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 237 | static PyObject * | 
 | 238 | func_get_dict(PyFunctionObject *op) | 
 | 239 | { | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 240 | 	if (op->func_dict == NULL) { | 
 | 241 | 		op->func_dict = PyDict_New(); | 
 | 242 | 		if (op->func_dict == NULL) | 
| Barry Warsaw | 142865c | 2001-08-14 18:23:58 +0000 | [diff] [blame] | 243 | 			return NULL; | 
 | 244 | 	} | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 245 | 	Py_INCREF(op->func_dict); | 
 | 246 | 	return op->func_dict; | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 247 | } | 
 | 248 |  | 
| Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 249 | static int | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 250 | func_set_dict(PyFunctionObject *op, PyObject *value) | 
| Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 251 | { | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 252 | 	PyObject *tmp; | 
| Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 253 |  | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 254 | 	/* It is illegal to del f.func_dict */ | 
 | 255 | 	if (value == NULL) { | 
 | 256 | 		PyErr_SetString(PyExc_TypeError, | 
 | 257 | 				"function's dictionary may not be deleted"); | 
| Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 258 | 		return -1; | 
 | 259 | 	} | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 260 | 	/* Can only set func_dict to a dictionary */ | 
 | 261 | 	if (!PyDict_Check(value)) { | 
 | 262 | 		PyErr_SetString(PyExc_TypeError, | 
| Barry Warsaw | 142865c | 2001-08-14 18:23:58 +0000 | [diff] [blame] | 263 | 				"setting function's dictionary to a non-dict"); | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 264 | 		return -1; | 
| Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 265 | 	} | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 266 | 	tmp = op->func_dict; | 
 | 267 | 	Py_INCREF(value); | 
 | 268 | 	op->func_dict = value; | 
 | 269 | 	Py_XDECREF(tmp); | 
 | 270 | 	return 0; | 
| Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 271 | } | 
 | 272 |  | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 273 | static PyObject * | 
 | 274 | func_get_code(PyFunctionObject *op) | 
 | 275 | { | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 276 | 	Py_INCREF(op->func_code); | 
 | 277 | 	return op->func_code; | 
 | 278 | } | 
 | 279 |  | 
 | 280 | static int | 
 | 281 | func_set_code(PyFunctionObject *op, PyObject *value) | 
 | 282 | { | 
 | 283 | 	PyObject *tmp; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 284 | 	Py_ssize_t nfree, nclosure; | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 285 |  | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 286 | 	/* Not legal to del f.func_code or to set it to anything | 
 | 287 | 	 * other than a code object. */ | 
 | 288 | 	if (value == NULL || !PyCode_Check(value)) { | 
 | 289 | 		PyErr_SetString(PyExc_TypeError, | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 290 | 				"__code__ must be set to a code object"); | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 291 | 		return -1; | 
 | 292 | 	} | 
| Armin Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 293 | 	nfree = PyCode_GetNumFree((PyCodeObject *)value); | 
 | 294 | 	nclosure = (op->func_closure == NULL ? 0 : | 
 | 295 | 		    PyTuple_GET_SIZE(op->func_closure)); | 
 | 296 | 	if (nclosure != nfree) { | 
 | 297 | 		PyErr_Format(PyExc_ValueError, | 
| Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 298 | 			     "%s() requires a code object with %zd free vars," | 
 | 299 | 			     " not %zd", | 
| Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 300 | 			     _PyUnicode_AsString(op->func_name), | 
| Martin v. Löwis | e0e89f7 | 2006-02-16 06:59:22 +0000 | [diff] [blame] | 301 | 			     nclosure, nfree); | 
| Armin Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 302 | 		return -1; | 
 | 303 | 	} | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 304 | 	tmp = op->func_code; | 
 | 305 | 	Py_INCREF(value); | 
 | 306 | 	op->func_code = value; | 
 | 307 | 	Py_DECREF(tmp); | 
 | 308 | 	return 0; | 
 | 309 | } | 
 | 310 |  | 
 | 311 | static PyObject * | 
| Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 312 | func_get_name(PyFunctionObject *op) | 
 | 313 | { | 
| Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 314 | 	Py_INCREF(op->func_name); | 
 | 315 | 	return op->func_name; | 
 | 316 | } | 
 | 317 |  | 
 | 318 | static int | 
 | 319 | func_set_name(PyFunctionObject *op, PyObject *value) | 
 | 320 | { | 
 | 321 | 	PyObject *tmp; | 
 | 322 |  | 
| Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 323 | 	/* Not legal to del f.func_name or to set it to anything | 
 | 324 | 	 * other than a string object. */ | 
| Neal Norwitz | 7e95bef | 2007-08-26 03:56:04 +0000 | [diff] [blame] | 325 | 	if (value == NULL || !PyUnicode_Check(value)) { | 
| Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 326 | 		PyErr_SetString(PyExc_TypeError, | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 327 | 				"__name__ must be set to a string object"); | 
| Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 328 | 		return -1; | 
 | 329 | 	} | 
 | 330 | 	tmp = op->func_name; | 
 | 331 | 	Py_INCREF(value); | 
 | 332 | 	op->func_name = value; | 
 | 333 | 	Py_DECREF(tmp); | 
 | 334 | 	return 0; | 
 | 335 | } | 
 | 336 |  | 
 | 337 | static PyObject * | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 338 | func_get_defaults(PyFunctionObject *op) | 
 | 339 | { | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 340 | 	if (op->func_defaults == NULL) { | 
 | 341 | 		Py_INCREF(Py_None); | 
 | 342 | 		return Py_None; | 
 | 343 | 	} | 
 | 344 | 	Py_INCREF(op->func_defaults); | 
 | 345 | 	return op->func_defaults; | 
 | 346 | } | 
 | 347 |  | 
 | 348 | static int | 
 | 349 | func_set_defaults(PyFunctionObject *op, PyObject *value) | 
 | 350 | { | 
 | 351 | 	PyObject *tmp; | 
 | 352 |  | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 353 | 	/* Legal to del f.func_defaults. | 
 | 354 | 	 * Can only set func_defaults to NULL or a tuple. */ | 
 | 355 | 	if (value == Py_None) | 
 | 356 | 		value = NULL; | 
 | 357 | 	if (value != NULL && !PyTuple_Check(value)) { | 
 | 358 | 		PyErr_SetString(PyExc_TypeError, | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 359 | 				"__defaults__ must be set to a tuple object"); | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 360 | 		return -1; | 
 | 361 | 	} | 
 | 362 | 	tmp = op->func_defaults; | 
 | 363 | 	Py_XINCREF(value); | 
 | 364 | 	op->func_defaults = value; | 
 | 365 | 	Py_XDECREF(tmp); | 
 | 366 | 	return 0; | 
 | 367 | } | 
 | 368 |  | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 369 | static PyObject * | 
 | 370 | func_get_kwdefaults(PyFunctionObject *op) | 
 | 371 | { | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 372 | 	if (op->func_kwdefaults == NULL) { | 
 | 373 | 		Py_INCREF(Py_None); | 
 | 374 | 		return Py_None; | 
 | 375 | 	} | 
 | 376 | 	Py_INCREF(op->func_kwdefaults); | 
 | 377 | 	return op->func_kwdefaults; | 
 | 378 | } | 
 | 379 |  | 
 | 380 | static int | 
 | 381 | func_set_kwdefaults(PyFunctionObject *op, PyObject *value) | 
 | 382 | { | 
 | 383 | 	PyObject *tmp; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 384 |  | 
 | 385 | 	if (value == Py_None) | 
 | 386 | 		value = NULL; | 
| Georg Brandl | 80b331c | 2007-02-26 12:28:57 +0000 | [diff] [blame] | 387 | 	/* Legal to del f.func_kwdefaults. | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 388 | 	 * Can only set func_kwdefaults to NULL or a dict. */ | 
 | 389 | 	if (value != NULL && !PyDict_Check(value)) { | 
 | 390 | 		PyErr_SetString(PyExc_TypeError, | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 391 | 			"__kwdefaults__ must be set to a dict object"); | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 392 | 		return -1; | 
 | 393 | 	} | 
 | 394 | 	tmp = op->func_kwdefaults; | 
 | 395 | 	Py_XINCREF(value); | 
 | 396 | 	op->func_kwdefaults = value; | 
 | 397 | 	Py_XDECREF(tmp); | 
 | 398 | 	return 0; | 
 | 399 | } | 
 | 400 |  | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 401 | static PyObject * | 
 | 402 | func_get_annotations(PyFunctionObject *op) | 
 | 403 | { | 
 | 404 | 	if (op->func_annotations == NULL) { | 
 | 405 | 		op->func_annotations = PyDict_New(); | 
 | 406 | 		if (op->func_annotations == NULL) | 
 | 407 | 			return NULL; | 
 | 408 | 	} | 
 | 409 | 	Py_INCREF(op->func_annotations); | 
 | 410 | 	return op->func_annotations; | 
 | 411 | } | 
 | 412 |  | 
 | 413 | static int | 
 | 414 | func_set_annotations(PyFunctionObject *op, PyObject *value) | 
 | 415 | { | 
 | 416 | 	PyObject *tmp; | 
 | 417 |  | 
 | 418 | 	if (value == Py_None) | 
 | 419 | 		value = NULL; | 
 | 420 | 	/* Legal to del f.func_annotations. | 
 | 421 | 	 * Can only set func_annotations to NULL (through C api) | 
 | 422 | 	 * or a dict. */ | 
 | 423 | 	if (value != NULL && !PyDict_Check(value)) { | 
 | 424 | 		PyErr_SetString(PyExc_TypeError, | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 425 | 			"__annotations__ must be set to a dict object"); | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 426 | 		return -1; | 
 | 427 | 	} | 
 | 428 | 	tmp = op->func_annotations; | 
 | 429 | 	Py_XINCREF(value); | 
 | 430 | 	op->func_annotations = value; | 
 | 431 | 	Py_XDECREF(tmp); | 
 | 432 | 	return 0; | 
 | 433 | } | 
 | 434 |  | 
| Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 435 | static PyGetSetDef func_getsetlist[] = { | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 436 |         {"__code__", (getter)func_get_code, (setter)func_set_code}, | 
 | 437 |         {"__defaults__", (getter)func_get_defaults, | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 438 | 	 (setter)func_set_defaults}, | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 439 | 	{"__kwdefaults__", (getter)func_get_kwdefaults, | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 440 | 	 (setter)func_set_kwdefaults}, | 
| Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 441 | 	{"__annotations__", (getter)func_get_annotations, | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 442 | 	 (setter)func_set_annotations}, | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 443 | 	{"__dict__", (getter)func_get_dict, (setter)func_set_dict}, | 
| Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 444 | 	{"__name__", (getter)func_get_name, (setter)func_set_name}, | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 445 | 	{NULL} /* Sentinel */ | 
 | 446 | }; | 
 | 447 |  | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 448 | PyDoc_STRVAR(func_doc, | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 449 | "function(code, globals[, name[, argdefs[, closure]]])\n\ | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 450 | \n\ | 
 | 451 | Create a function object from a code object and a dictionary.\n\ | 
 | 452 | The optional name string overrides the name from the code object.\n\ | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 453 | The optional argdefs tuple specifies the default argument values.\n\ | 
 | 454 | The optional closure tuple supplies the bindings for free variables."); | 
 | 455 |  | 
 | 456 | /* func_new() maintains the following invariants for closures.  The | 
 | 457 |    closure must correspond to the free variables of the code object. | 
 | 458 |     | 
 | 459 |    if len(code.co_freevars) == 0:  | 
 | 460 |            closure = NULL | 
 | 461 |    else: | 
 | 462 |            len(closure) == len(code.co_freevars) | 
 | 463 |    for every elt in closure, type(elt) == cell | 
 | 464 | */ | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 465 |  | 
 | 466 | static PyObject * | 
 | 467 | func_new(PyTypeObject* type, PyObject* args, PyObject* kw) | 
 | 468 | { | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 469 | 	PyCodeObject *code; | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 470 | 	PyObject *globals; | 
 | 471 | 	PyObject *name = Py_None; | 
 | 472 | 	PyObject *defaults = Py_None; | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 473 | 	PyObject *closure = Py_None; | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 474 | 	PyFunctionObject *newfunc; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 475 | 	Py_ssize_t nfree, nclosure; | 
| Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 476 | 	static char *kwlist[] = {"code", "globals", "name", | 
| Raymond Hettinger | 8657845 | 2003-05-06 09:01:41 +0000 | [diff] [blame] | 477 | 				 "argdefs", "closure", 0}; | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 478 |  | 
| Raymond Hettinger | 8657845 | 2003-05-06 09:01:41 +0000 | [diff] [blame] | 479 | 	if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function", | 
 | 480 | 			      kwlist, | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 481 | 			      &PyCode_Type, &code, | 
 | 482 | 			      &PyDict_Type, &globals, | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 483 | 			      &name, &defaults, &closure)) | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 484 | 		return NULL; | 
| Neal Norwitz | 7e95bef | 2007-08-26 03:56:04 +0000 | [diff] [blame] | 485 | 	if (name != Py_None && !PyUnicode_Check(name)) { | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 486 | 		PyErr_SetString(PyExc_TypeError, | 
 | 487 | 				"arg 3 (name) must be None or string"); | 
 | 488 | 		return NULL; | 
 | 489 | 	} | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 490 | 	if (defaults != Py_None && !PyTuple_Check(defaults)) { | 
 | 491 | 		PyErr_SetString(PyExc_TypeError, | 
 | 492 | 				"arg 4 (defaults) must be None or tuple"); | 
 | 493 | 		return NULL; | 
 | 494 | 	} | 
 | 495 | 	nfree = PyTuple_GET_SIZE(code->co_freevars); | 
 | 496 | 	if (!PyTuple_Check(closure)) { | 
 | 497 | 		if (nfree && closure == Py_None) { | 
 | 498 | 			PyErr_SetString(PyExc_TypeError, | 
 | 499 | 					"arg 5 (closure) must be tuple"); | 
 | 500 | 			return NULL; | 
 | 501 | 		} | 
 | 502 | 		else if (closure != Py_None) { | 
 | 503 | 			PyErr_SetString(PyExc_TypeError, | 
 | 504 | 				"arg 5 (closure) must be None or tuple"); | 
 | 505 | 			return NULL; | 
 | 506 | 		} | 
 | 507 | 	} | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 508 |  | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 509 | 	/* check that the closure is well-formed */ | 
 | 510 | 	nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); | 
 | 511 | 	if (nfree != nclosure) | 
 | 512 | 		return PyErr_Format(PyExc_ValueError, | 
| Walter Dörwald | a29d1d7 | 2007-06-11 15:00:18 +0000 | [diff] [blame] | 513 | 		                    "%U requires closure of length %zd, not %zd", | 
 | 514 | 		                    code->co_name, nfree, nclosure); | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 515 | 	if (nclosure) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 516 | 		Py_ssize_t i; | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 517 | 		for (i = 0; i < nclosure; i++) { | 
 | 518 | 			PyObject *o = PyTuple_GET_ITEM(closure, i); | 
 | 519 | 			if (!PyCell_Check(o)) { | 
 | 520 | 				return PyErr_Format(PyExc_TypeError, | 
 | 521 | 				    "arg 5 (closure) expected cell, found %s", | 
 | 522 | 						    o->ob_type->tp_name); | 
 | 523 | 			} | 
 | 524 | 		} | 
 | 525 | 	} | 
 | 526 | 	 | 
 | 527 | 	newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,  | 
 | 528 | 						     globals); | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 529 | 	if (newfunc == NULL) | 
 | 530 | 		return NULL; | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 531 | 	 | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 532 | 	if (name != Py_None) { | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 533 | 		Py_INCREF(name); | 
 | 534 | 		Py_DECREF(newfunc->func_name); | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 535 | 		newfunc->func_name = name; | 
 | 536 | 	} | 
 | 537 | 	if (defaults != Py_None) { | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 538 | 		Py_INCREF(defaults); | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 539 | 		newfunc->func_defaults  = defaults; | 
 | 540 | 	} | 
| Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 541 | 	if (closure != Py_None) { | 
 | 542 | 		Py_INCREF(closure); | 
 | 543 | 		newfunc->func_closure = closure; | 
 | 544 | 	} | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 545 |  | 
 | 546 | 	return (PyObject *)newfunc; | 
 | 547 | } | 
 | 548 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 549 | static void | 
| Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 550 | func_dealloc(PyFunctionObject *op) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | { | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 552 | 	_PyObject_GC_UNTRACK(op); | 
| Fred Drake | c916f5a | 2001-10-26 17:56:51 +0000 | [diff] [blame] | 553 | 	if (op->func_weakreflist != NULL) | 
 | 554 | 		PyObject_ClearWeakRefs((PyObject *) op); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 555 | 	Py_DECREF(op->func_code); | 
 | 556 | 	Py_DECREF(op->func_globals); | 
| Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 557 | 	Py_XDECREF(op->func_module); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 558 | 	Py_DECREF(op->func_name); | 
 | 559 | 	Py_XDECREF(op->func_defaults); | 
| Georg Brandl | 80b331c | 2007-02-26 12:28:57 +0000 | [diff] [blame] | 560 | 	Py_XDECREF(op->func_kwdefaults); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 561 | 	Py_XDECREF(op->func_doc); | 
| Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 562 | 	Py_XDECREF(op->func_dict); | 
| Jeremy Hylton | a52e8fe | 2001-03-01 06:06:37 +0000 | [diff] [blame] | 563 | 	Py_XDECREF(op->func_closure); | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 564 | 	Py_XDECREF(op->func_annotations); | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 565 | 	PyObject_GC_Del(op); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 566 | } | 
 | 567 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 568 | static PyObject* | 
| Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 569 | func_repr(PyFunctionObject *op) | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 570 | { | 
| Neal Norwitz | 7e95bef | 2007-08-26 03:56:04 +0000 | [diff] [blame] | 571 | 	return PyUnicode_FromFormat("<function %U at %p>", | 
 | 572 | 				   op->func_name, op); | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 573 | } | 
 | 574 |  | 
| Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 575 | static int | 
| Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 576 | func_traverse(PyFunctionObject *f, visitproc visit, void *arg) | 
 | 577 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 578 | 	Py_VISIT(f->func_code); | 
 | 579 | 	Py_VISIT(f->func_globals); | 
 | 580 | 	Py_VISIT(f->func_module); | 
 | 581 | 	Py_VISIT(f->func_defaults); | 
| Georg Brandl | 80b331c | 2007-02-26 12:28:57 +0000 | [diff] [blame] | 582 | 	Py_VISIT(f->func_kwdefaults); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 583 | 	Py_VISIT(f->func_doc); | 
 | 584 | 	Py_VISIT(f->func_name); | 
 | 585 | 	Py_VISIT(f->func_dict); | 
 | 586 | 	Py_VISIT(f->func_closure); | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 587 | 	Py_VISIT(f->func_annotations); | 
| Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 588 | 	return 0; | 
 | 589 | } | 
 | 590 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 591 | static PyObject * | 
 | 592 | function_call(PyObject *func, PyObject *arg, PyObject *kw) | 
 | 593 | { | 
 | 594 | 	PyObject *result; | 
 | 595 | 	PyObject *argdefs; | 
 | 596 | 	PyObject **d, **k; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 597 | 	Py_ssize_t nk, nd; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 598 |  | 
 | 599 | 	argdefs = PyFunction_GET_DEFAULTS(func); | 
 | 600 | 	if (argdefs != NULL && PyTuple_Check(argdefs)) { | 
 | 601 | 		d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0); | 
 | 602 | 		nd = PyTuple_Size(argdefs); | 
 | 603 | 	} | 
 | 604 | 	else { | 
 | 605 | 		d = NULL; | 
 | 606 | 		nd = 0; | 
 | 607 | 	} | 
 | 608 |  | 
 | 609 | 	if (kw != NULL && PyDict_Check(kw)) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 610 | 		Py_ssize_t pos, i; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 611 | 		nk = PyDict_Size(kw); | 
 | 612 | 		k = PyMem_NEW(PyObject *, 2*nk); | 
 | 613 | 		if (k == NULL) { | 
 | 614 | 			PyErr_NoMemory(); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 615 | 			return NULL; | 
 | 616 | 		} | 
 | 617 | 		pos = i = 0; | 
 | 618 | 		while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) | 
 | 619 | 			i += 2; | 
 | 620 | 		nk = i/2; | 
 | 621 | 		/* XXX This is broken if the caller deletes dict items! */ | 
 | 622 | 	} | 
 | 623 | 	else { | 
 | 624 | 		k = NULL; | 
 | 625 | 		nk = 0; | 
 | 626 | 	} | 
 | 627 |  | 
 | 628 | 	result = PyEval_EvalCodeEx( | 
 | 629 | 		(PyCodeObject *)PyFunction_GET_CODE(func), | 
 | 630 | 		PyFunction_GET_GLOBALS(func), (PyObject *)NULL, | 
 | 631 | 		&PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg), | 
 | 632 | 		k, nk, d, nd, | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 633 | 		PyFunction_GET_KW_DEFAULTS(func), | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 634 | 		PyFunction_GET_CLOSURE(func)); | 
 | 635 |  | 
 | 636 | 	if (k != NULL) | 
 | 637 | 		PyMem_DEL(k); | 
 | 638 |  | 
 | 639 | 	return result; | 
 | 640 | } | 
 | 641 |  | 
 | 642 | /* Bind a function to an object */ | 
 | 643 | static PyObject * | 
 | 644 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) | 
 | 645 | { | 
| Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 646 | 	if (obj == Py_None || obj == NULL) { | 
 | 647 | 		Py_INCREF(func); | 
 | 648 | 		return func; | 
 | 649 | 	} | 
| Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 650 | 	return PyMethod_New(func, obj); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 651 | } | 
 | 652 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 653 | PyTypeObject PyFunction_Type = { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 654 | 	PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 655 | 	"function", | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 656 | 	sizeof(PyFunctionObject), | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 657 | 	0, | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 658 | 	(destructor)func_dealloc,		/* tp_dealloc */ | 
 | 659 | 	0,					/* tp_print */ | 
 | 660 | 	0,					/* tp_getattr */ | 
 | 661 | 	0,					/* tp_setattr */ | 
 | 662 | 	0,					/* tp_compare */ | 
 | 663 | 	(reprfunc)func_repr,			/* tp_repr */ | 
 | 664 | 	0,					/* tp_as_number */ | 
 | 665 | 	0,					/* tp_as_sequence */ | 
 | 666 | 	0,					/* tp_as_mapping */ | 
 | 667 | 	0,					/* tp_hash */ | 
 | 668 | 	function_call,				/* tp_call */ | 
 | 669 | 	0,					/* tp_str */ | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 670 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
 | 671 | 	PyObject_GenericSetAttr,		/* tp_setattro */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 672 | 	0,					/* tp_as_buffer */ | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 673 | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 674 | 	func_doc,				/* tp_doc */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 675 | 	(traverseproc)func_traverse,		/* tp_traverse */ | 
 | 676 | 	0,					/* tp_clear */ | 
 | 677 | 	0,					/* tp_richcompare */ | 
| Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 678 | 	offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 679 | 	0,					/* tp_iter */ | 
 | 680 | 	0,					/* tp_iternext */ | 
 | 681 | 	0,					/* tp_methods */ | 
 | 682 | 	func_memberlist,			/* tp_members */ | 
| Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 683 | 	func_getsetlist,			/* tp_getset */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 684 | 	0,					/* tp_base */ | 
 | 685 | 	0,					/* tp_dict */ | 
 | 686 | 	func_descr_get,				/* tp_descr_get */ | 
 | 687 | 	0,					/* tp_descr_set */ | 
 | 688 | 	offsetof(PyFunctionObject, func_dict),	/* tp_dictoffset */ | 
| Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 689 | 	0,					/* tp_init */ | 
 | 690 | 	0,					/* tp_alloc */ | 
 | 691 | 	func_new,				/* tp_new */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 692 | }; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 693 |  | 
 | 694 |  | 
 | 695 | /* Class method object */ | 
 | 696 |  | 
 | 697 | /* A class method receives the class as implicit first argument, | 
 | 698 |    just like an instance method receives the instance. | 
 | 699 |    To declare a class method, use this idiom: | 
 | 700 |  | 
 | 701 |      class C: | 
 | 702 |          def f(cls, arg1, arg2, ...): ... | 
 | 703 | 	 f = classmethod(f) | 
 | 704 |     | 
 | 705 |    It can be called either on the class (e.g. C.f()) or on an instance | 
 | 706 |    (e.g. C().f()); the instance is ignored except for its class. | 
 | 707 |    If a class method is called for a derived class, the derived class | 
 | 708 |    object is passed as the implied first argument. | 
 | 709 |  | 
 | 710 |    Class methods are different than C++ or Java static methods. | 
 | 711 |    If you want those, see static methods below. | 
 | 712 | */ | 
 | 713 |  | 
 | 714 | typedef struct { | 
 | 715 | 	PyObject_HEAD | 
 | 716 | 	PyObject *cm_callable; | 
 | 717 | } classmethod; | 
 | 718 |  | 
 | 719 | static void | 
 | 720 | cm_dealloc(classmethod *cm) | 
 | 721 | { | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 722 | 	_PyObject_GC_UNTRACK((PyObject *)cm); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 723 | 	Py_XDECREF(cm->cm_callable); | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 724 | 	Py_TYPE(cm)->tp_free((PyObject *)cm); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 725 | } | 
 | 726 |  | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 727 | static int | 
 | 728 | cm_traverse(classmethod *cm, visitproc visit, void *arg) | 
 | 729 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 730 | 	Py_VISIT(cm->cm_callable); | 
 | 731 | 	return 0; | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 732 | } | 
 | 733 |  | 
 | 734 | static int | 
 | 735 | cm_clear(classmethod *cm) | 
 | 736 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 737 | 	Py_CLEAR(cm->cm_callable); | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 738 | 	return 0; | 
 | 739 | } | 
 | 740 |  | 
 | 741 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 742 | static PyObject * | 
 | 743 | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) | 
 | 744 | { | 
 | 745 | 	classmethod *cm = (classmethod *)self; | 
 | 746 |  | 
 | 747 | 	if (cm->cm_callable == NULL) { | 
 | 748 | 		PyErr_SetString(PyExc_RuntimeError, | 
 | 749 | 				"uninitialized classmethod object"); | 
 | 750 | 		return NULL; | 
 | 751 | 	} | 
| Guido van Rossum | 7e30548 | 2002-03-18 03:09:06 +0000 | [diff] [blame] | 752 | 	if (type == NULL) | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 753 | 		type = (PyObject *)(Py_TYPE(obj)); | 
| Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 754 |  	return PyMethod_New(cm->cm_callable, type); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 755 | } | 
 | 756 |  | 
 | 757 | static int | 
 | 758 | cm_init(PyObject *self, PyObject *args, PyObject *kwds) | 
 | 759 | { | 
 | 760 | 	classmethod *cm = (classmethod *)self; | 
 | 761 | 	PyObject *callable; | 
 | 762 |  | 
| Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 763 | 	if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable)) | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 764 | 		return -1; | 
| Georg Brandl | d02db40 | 2006-02-21 22:13:44 +0000 | [diff] [blame] | 765 | 	if (!_PyArg_NoKeywords("classmethod", kwds)) | 
 | 766 | 		return -1; | 
| Raymond Hettinger | be97153 | 2003-06-18 01:13:41 +0000 | [diff] [blame] | 767 | 	if (!PyCallable_Check(callable)) { | 
 | 768 | 		PyErr_Format(PyExc_TypeError, "'%s' object is not callable", | 
 | 769 | 		     callable->ob_type->tp_name); | 
 | 770 | 		return -1; | 
 | 771 | 	} | 
 | 772 | 	 | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 773 | 	Py_INCREF(callable); | 
 | 774 | 	cm->cm_callable = callable; | 
 | 775 | 	return 0; | 
 | 776 | } | 
 | 777 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 778 | PyDoc_STRVAR(classmethod_doc, | 
| Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 779 | "classmethod(function) -> method\n\ | 
 | 780 | \n\ | 
 | 781 | Convert a function to be a class method.\n\ | 
 | 782 | \n\ | 
 | 783 | A class method receives the class as implicit first argument,\n\ | 
 | 784 | just like an instance method receives the instance.\n\ | 
 | 785 | To declare a class method, use this idiom:\n\ | 
 | 786 | \n\ | 
 | 787 |   class C:\n\ | 
 | 788 |       def f(cls, arg1, arg2, ...): ...\n\ | 
 | 789 |       f = classmethod(f)\n\ | 
 | 790 | \n\ | 
 | 791 | It can be called either on the class (e.g. C.f()) or on an instance\n\ | 
 | 792 | (e.g. C().f()).  The instance is ignored except for its class.\n\ | 
 | 793 | If a class method is called for a derived class, the derived class\n\ | 
 | 794 | object is passed as the implied first argument.\n\ | 
| Sjoerd Mullender | 564980b | 2001-12-17 11:39:56 +0000 | [diff] [blame] | 795 | \n\ | 
| Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 796 | Class methods are different than C++ or Java static methods.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 797 | If you want those, see the staticmethod builtin."); | 
| Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 798 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 799 | PyTypeObject PyClassMethod_Type = { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 800 | 	PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 801 | 	"classmethod", | 
 | 802 | 	sizeof(classmethod), | 
 | 803 | 	0, | 
 | 804 | 	(destructor)cm_dealloc,			/* tp_dealloc */ | 
 | 805 | 	0,					/* tp_print */ | 
 | 806 | 	0,					/* tp_getattr */ | 
 | 807 | 	0,					/* tp_setattr */ | 
 | 808 | 	0,					/* tp_compare */ | 
 | 809 | 	0,					/* tp_repr */ | 
 | 810 | 	0,					/* tp_as_number */ | 
 | 811 | 	0,					/* tp_as_sequence */ | 
 | 812 | 	0,					/* tp_as_mapping */ | 
 | 813 | 	0,					/* tp_hash */ | 
 | 814 | 	0,					/* tp_call */ | 
 | 815 | 	0,					/* tp_str */ | 
 | 816 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
 | 817 | 	0,					/* tp_setattro */ | 
 | 818 | 	0,					/* tp_as_buffer */ | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 819 | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, | 
| Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 820 | 	classmethod_doc,			/* tp_doc */ | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 821 | 	(traverseproc)cm_traverse,		/* tp_traverse */ | 
 | 822 | 	(inquiry)cm_clear,			/* tp_clear */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 823 | 	0,					/* tp_richcompare */ | 
 | 824 | 	0,					/* tp_weaklistoffset */ | 
 | 825 | 	0,					/* tp_iter */ | 
 | 826 | 	0,					/* tp_iternext */ | 
 | 827 | 	0,					/* tp_methods */ | 
 | 828 | 	0,					/* tp_members */ | 
 | 829 | 	0,					/* tp_getset */ | 
 | 830 | 	0,					/* tp_base */ | 
 | 831 | 	0,					/* tp_dict */ | 
 | 832 | 	cm_descr_get,				/* tp_descr_get */ | 
 | 833 | 	0,					/* tp_descr_set */ | 
 | 834 | 	0,					/* tp_dictoffset */ | 
 | 835 | 	cm_init,				/* tp_init */ | 
 | 836 | 	PyType_GenericAlloc,			/* tp_alloc */ | 
 | 837 | 	PyType_GenericNew,			/* tp_new */ | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 838 | 	PyObject_GC_Del,	                /* tp_free */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 839 | }; | 
 | 840 |  | 
 | 841 | PyObject * | 
 | 842 | PyClassMethod_New(PyObject *callable) | 
 | 843 | { | 
 | 844 | 	classmethod *cm = (classmethod *) | 
 | 845 | 		PyType_GenericAlloc(&PyClassMethod_Type, 0); | 
 | 846 | 	if (cm != NULL) { | 
 | 847 | 		Py_INCREF(callable); | 
 | 848 | 		cm->cm_callable = callable; | 
 | 849 | 	} | 
 | 850 | 	return (PyObject *)cm; | 
 | 851 | } | 
 | 852 |  | 
 | 853 |  | 
 | 854 | /* Static method object */ | 
 | 855 |  | 
 | 856 | /* A static method does not receive an implicit first argument. | 
 | 857 |    To declare a static method, use this idiom: | 
 | 858 |  | 
 | 859 |      class C: | 
 | 860 |          def f(arg1, arg2, ...): ... | 
 | 861 | 	 f = staticmethod(f) | 
 | 862 |  | 
 | 863 |    It can be called either on the class (e.g. C.f()) or on an instance | 
 | 864 |    (e.g. C().f()); the instance is ignored except for its class. | 
 | 865 |  | 
 | 866 |    Static methods in Python are similar to those found in Java or C++. | 
 | 867 |    For a more advanced concept, see class methods above. | 
 | 868 | */ | 
 | 869 |  | 
 | 870 | typedef struct { | 
 | 871 | 	PyObject_HEAD | 
 | 872 | 	PyObject *sm_callable; | 
 | 873 | } staticmethod; | 
 | 874 |  | 
 | 875 | static void | 
 | 876 | sm_dealloc(staticmethod *sm) | 
 | 877 | { | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 878 | 	_PyObject_GC_UNTRACK((PyObject *)sm); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 879 | 	Py_XDECREF(sm->sm_callable); | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 880 | 	Py_TYPE(sm)->tp_free((PyObject *)sm); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 881 | } | 
 | 882 |  | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 883 | static int | 
 | 884 | sm_traverse(staticmethod *sm, visitproc visit, void *arg) | 
 | 885 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 886 | 	Py_VISIT(sm->sm_callable); | 
 | 887 | 	return 0; | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 888 | } | 
 | 889 |  | 
 | 890 | static int | 
 | 891 | sm_clear(staticmethod *sm) | 
 | 892 | { | 
 | 893 | 	Py_XDECREF(sm->sm_callable); | 
 | 894 | 	sm->sm_callable = NULL; | 
 | 895 |  | 
 | 896 | 	return 0; | 
 | 897 | } | 
 | 898 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 899 | static PyObject * | 
 | 900 | sm_descr_get(PyObject *self, PyObject *obj, PyObject *type) | 
 | 901 | { | 
 | 902 | 	staticmethod *sm = (staticmethod *)self; | 
 | 903 |  | 
 | 904 | 	if (sm->sm_callable == NULL) { | 
 | 905 | 		PyErr_SetString(PyExc_RuntimeError, | 
 | 906 | 				"uninitialized staticmethod object"); | 
 | 907 | 		return NULL; | 
 | 908 | 	} | 
 | 909 | 	Py_INCREF(sm->sm_callable); | 
 | 910 | 	return sm->sm_callable; | 
 | 911 | } | 
 | 912 |  | 
 | 913 | static int | 
 | 914 | sm_init(PyObject *self, PyObject *args, PyObject *kwds) | 
 | 915 | { | 
 | 916 | 	staticmethod *sm = (staticmethod *)self; | 
 | 917 | 	PyObject *callable; | 
 | 918 |  | 
| Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 919 | 	if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable)) | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 920 | 		return -1; | 
| Georg Brandl | d02db40 | 2006-02-21 22:13:44 +0000 | [diff] [blame] | 921 | 	if (!_PyArg_NoKeywords("staticmethod", kwds)) | 
 | 922 | 		return -1; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 923 | 	Py_INCREF(callable); | 
 | 924 | 	sm->sm_callable = callable; | 
 | 925 | 	return 0; | 
 | 926 | } | 
 | 927 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 928 | PyDoc_STRVAR(staticmethod_doc, | 
| Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 929 | "staticmethod(function) -> method\n\ | 
 | 930 | \n\ | 
 | 931 | Convert a function to be a static method.\n\ | 
 | 932 | \n\ | 
 | 933 | A static method does not receive an implicit first argument.\n\ | 
 | 934 | To declare a static method, use this idiom:\n\ | 
 | 935 | \n\ | 
 | 936 |      class C:\n\ | 
 | 937 |          def f(arg1, arg2, ...): ...\n\ | 
 | 938 | 	 f = staticmethod(f)\n\ | 
 | 939 | \n\ | 
 | 940 | It can be called either on the class (e.g. C.f()) or on an instance\n\ | 
 | 941 | (e.g. C().f()).  The instance is ignored except for its class.\n\ | 
 | 942 | \n\ | 
 | 943 | Static methods in Python are similar to those found in Java or C++.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 944 | For a more advanced concept, see the classmethod builtin."); | 
| Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 945 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 946 | PyTypeObject PyStaticMethod_Type = { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 947 | 	PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 948 | 	"staticmethod", | 
 | 949 | 	sizeof(staticmethod), | 
 | 950 | 	0, | 
 | 951 | 	(destructor)sm_dealloc,			/* tp_dealloc */ | 
 | 952 | 	0,					/* tp_print */ | 
 | 953 | 	0,					/* tp_getattr */ | 
 | 954 | 	0,					/* tp_setattr */ | 
 | 955 | 	0,					/* tp_compare */ | 
 | 956 | 	0,					/* tp_repr */ | 
 | 957 | 	0,					/* tp_as_number */ | 
 | 958 | 	0,					/* tp_as_sequence */ | 
 | 959 | 	0,					/* tp_as_mapping */ | 
 | 960 | 	0,					/* tp_hash */ | 
 | 961 | 	0,					/* tp_call */ | 
 | 962 | 	0,					/* tp_str */ | 
 | 963 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
 | 964 | 	0,					/* tp_setattro */ | 
 | 965 | 	0,					/* tp_as_buffer */ | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 966 | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, | 
| Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 967 | 	staticmethod_doc,			/* tp_doc */ | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 968 | 	(traverseproc)sm_traverse,		/* tp_traverse */ | 
 | 969 | 	(inquiry)sm_clear,			/* tp_clear */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 970 | 	0,					/* tp_richcompare */ | 
 | 971 | 	0,					/* tp_weaklistoffset */ | 
 | 972 | 	0,					/* tp_iter */ | 
 | 973 | 	0,					/* tp_iternext */ | 
 | 974 | 	0,					/* tp_methods */ | 
 | 975 | 	0,					/* tp_members */ | 
 | 976 | 	0,					/* tp_getset */ | 
 | 977 | 	0,					/* tp_base */ | 
 | 978 | 	0,					/* tp_dict */ | 
 | 979 | 	sm_descr_get,				/* tp_descr_get */ | 
 | 980 | 	0,					/* tp_descr_set */ | 
 | 981 | 	0,					/* tp_dictoffset */ | 
 | 982 | 	sm_init,				/* tp_init */ | 
 | 983 | 	PyType_GenericAlloc,			/* tp_alloc */ | 
 | 984 | 	PyType_GenericNew,			/* tp_new */ | 
| Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 985 | 	PyObject_GC_Del,           		/* tp_free */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 986 | }; | 
 | 987 |  | 
 | 988 | PyObject * | 
 | 989 | PyStaticMethod_New(PyObject *callable) | 
 | 990 | { | 
 | 991 | 	staticmethod *sm = (staticmethod *) | 
 | 992 | 		PyType_GenericAlloc(&PyStaticMethod_Type, 0); | 
 | 993 | 	if (sm != NULL) { | 
 | 994 | 		Py_INCREF(callable); | 
 | 995 | 		sm->sm_callable = callable; | 
 | 996 | 	} | 
 | 997 | 	return (PyObject *)sm; | 
 | 998 | } |