Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2071)

Delta Between Two Patch Sets: Modules/_functoolsmodule.c

Issue 14373: C implementation of functools.lru_cache
Left Patch Set: Created 4 years, 5 months ago
Right Patch Set: Created 4 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 1
2 #include "Python.h" 2 #include "Python.h"
3 #include "structmember.h" 3 #include "structmember.h"
4 4
5 /* _functools module written and maintained 5 /* _functools module written and maintained
6 by Hye-Shik Chang <perky@FreeBSD.org> 6 by Hye-Shik Chang <perky@FreeBSD.org>
7 with adaptations by Raymond Hettinger <python@rcn.com> 7 with adaptations by Raymond Hettinger <python@rcn.com>
8 Copyright (c) 2004, 2005, 2006 Python Software Foundation. 8 Copyright (c) 2004, 2005, 2006 Python Software Foundation.
9 All rights reserved. 9 All rights reserved.
10 */ 10 */
(...skipping 881 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 Py_INCREF(result); /* for return */ 892 Py_INCREF(result); /* for return */
893 self->full = (PyDict_Size(self->cache) >= self->maxsize); 893 self->full = (PyDict_Size(self->cache) >= self->maxsize);
894 } 894 }
895 self->misses++; 895 self->misses++;
896 return result; 896 return result;
897 } 897 }
898 898
899 static PyObject * 899 static PyObject *
900 lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw) 900 lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw)
901 { 901 {
902 PyObject *func, *maxsize_O, *cache_info_type; 902 PyObject *func, *maxsize_O, *cache_info_type, *cachedict;
903 int typed; 903 int typed;
904 lru_cache_object *obj; 904 lru_cache_object *obj;
905 Py_ssize_t maxsize; 905 Py_ssize_t maxsize;
906 PyObject *(*wrapper)(lru_cache_object *, PyObject *, PyObject *); 906 PyObject *(*wrapper)(lru_cache_object *, PyObject *, PyObject *);
907 static char *keywords[] = {"user_function", "maxsize", "typed", 907 static char *keywords[] = {"user_function", "maxsize", "typed",
908 "cache_info_type", NULL}; 908 "cache_info_type", NULL};
909 909
910 if (!PyArg_ParseTupleAndKeywords(args, kw, "OOpO:lru_cache", keywords, 910 if (!PyArg_ParseTupleAndKeywords(args, kw, "OOpO:lru_cache", keywords,
911 &func, &maxsize_O, &typed, 911 &func, &maxsize_O, &typed,
912 &cache_info_type)) { 912 &cache_info_type)) {
(...skipping 17 matching lines...) Expand all
930 return NULL; 930 return NULL;
931 if (maxsize == 0) 931 if (maxsize == 0)
932 wrapper = uncached_lru_cache_wrapper; 932 wrapper = uncached_lru_cache_wrapper;
933 else 933 else
934 wrapper = bounded_lru_cache_wrapper; 934 wrapper = bounded_lru_cache_wrapper;
935 } else { 935 } else {
936 PyErr_SetString(PyExc_TypeError, "maxsize should be integer or None"); 936 PyErr_SetString(PyExc_TypeError, "maxsize should be integer or None");
937 return NULL; 937 return NULL;
938 } 938 }
939 939
940 if (!(cachedict = PyDict_New()))
941 return NULL;
942
940 obj = (lru_cache_object *)type->tp_alloc(type, 0); 943 obj = (lru_cache_object *)type->tp_alloc(type, 0);
941 if (obj == NULL) 944 if (obj == NULL) {
942 return NULL; 945 Py_DECREF(cachedict);
943 946 return NULL;
944 if (!(obj->cache = PyDict_New())) { 947 }
945 Py_DECREF(obj); 948
946 return NULL; 949 obj->cache = cachedict;
947 }
948
949 obj->root.prev = &obj->root; 950 obj->root.prev = &obj->root;
950 obj->root.next = &obj->root; 951 obj->root.next = &obj->root;
951 obj->maxsize = maxsize; 952 obj->maxsize = maxsize;
952 Py_INCREF(maxsize_O); 953 Py_INCREF(maxsize_O);
953 obj->maxsize_O = maxsize_O; 954 obj->maxsize_O = maxsize_O;
954 Py_INCREF(func); 955 Py_INCREF(func);
955 obj->func = func; 956 obj->func = func;
956 obj->wrapper = wrapper; 957 obj->wrapper = wrapper;
957 obj->misses = obj->hits = 0; 958 obj->misses = obj->hits = 0;
958 obj->typed = typed; 959 obj->typed = typed;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
994 Py_XDECREF(obj->dict); 995 Py_XDECREF(obj->dict);
995 Py_XDECREF(obj->cache_info_type); 996 Py_XDECREF(obj->cache_info_type);
996 lru_cache_clear_list(list); 997 lru_cache_clear_list(list);
997 Py_TYPE(obj)->tp_free(obj); 998 Py_TYPE(obj)->tp_free(obj);
998 } 999 }
999 1000
1000 static PyObject * 1001 static PyObject *
1001 lru_cache_call(lru_cache_object *self, PyObject *args, PyObject *kwds) 1002 lru_cache_call(lru_cache_object *self, PyObject *args, PyObject *kwds)
1002 { 1003 {
1003 return self->wrapper(self, args, kwds); 1004 return self->wrapper(self, args, kwds);
1005 }
1006
1007 static PyObject *
1008 lru_cache_descr_get(PyObject *self, PyObject *obj, PyObject *type)
1009 {
1010 if (obj == Py_None || obj == NULL) {
1011 Py_INCREF(self);
1012 return self;
1013 }
1014 return PyMethod_New(self, obj);
1004 } 1015 }
1005 1016
1006 static PyObject * 1017 static PyObject *
1007 lru_cache_cache_info(lru_cache_object *self, PyObject *unused) 1018 lru_cache_cache_info(lru_cache_object *self, PyObject *unused)
1008 { 1019 {
1009 return PyObject_CallFunction(self->cache_info_type, "nnOn", 1020 return PyObject_CallFunction(self->cache_info_type, "nnOn",
1010 self->hits, self->misses, self->maxsize_O, 1021 self->hits, self->misses, self->maxsize_O,
1011 PyDict_Size(self->cache)); 1022 PyDict_Size(self->cache));
1012 } 1023 }
1013 1024
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1108 (inquiry)lru_cache_tp_clear, /* tp_clear */ 1119 (inquiry)lru_cache_tp_clear, /* tp_clear */
1109 0, /* tp_richcompare */ 1120 0, /* tp_richcompare */
1110 0, /* tp_weaklistoffset */ 1121 0, /* tp_weaklistoffset */
1111 0, /* tp_iter */ 1122 0, /* tp_iter */
1112 0, /* tp_iternext */ 1123 0, /* tp_iternext */
1113 lru_cache_methods, /* tp_methods */ 1124 lru_cache_methods, /* tp_methods */
1114 0, /* tp_members */ 1125 0, /* tp_members */
1115 lru_cache_getsetlist, /* tp_getset */ 1126 lru_cache_getsetlist, /* tp_getset */
1116 0, /* tp_base */ 1127 0, /* tp_base */
1117 0, /* tp_dict */ 1128 0, /* tp_dict */
1118 0, /* tp_descr_get */ 1129 lru_cache_descr_get, /* tp_descr_get */
1119 0, /* tp_descr_set */ 1130 0, /* tp_descr_set */
1120 offsetof(lru_cache_object, dict), /* tp_dictoffset */ 1131 offsetof(lru_cache_object, dict), /* tp_dictoffset */
1121 0, /* tp_init */ 1132 0, /* tp_init */
1122 0, /* tp_alloc */ 1133 0, /* tp_alloc */
1123 lru_cache_new, /* tp_new */ 1134 lru_cache_new, /* tp_new */
1124 }; 1135 };
1125 1136
1126 /* module level code ********************************************************/ 1137 /* module level code ********************************************************/
1127 1138
1128 PyDoc_STRVAR(module_doc, 1139 PyDoc_STRVAR(module_doc,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1180 Py_DECREF(m); 1191 Py_DECREF(m);
1181 return NULL; 1192 return NULL;
1182 } 1193 }
1183 name = strchr(typelist[i]->tp_name, '.'); 1194 name = strchr(typelist[i]->tp_name, '.');
1184 assert (name != NULL); 1195 assert (name != NULL);
1185 Py_INCREF(typelist[i]); 1196 Py_INCREF(typelist[i]);
1186 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); 1197 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
1187 } 1198 }
1188 return m; 1199 return m;
1189 } 1200 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+