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

Delta Between Two Patch Sets: Modules/_functoolsmodule.c

Issue 14373: C implementation of functools.lru_cache
Left Patch Set: Created 4 years, 4 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:
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
(no file at all)
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 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 Py_DECREF(m); 1191 Py_DECREF(m);
1191 return NULL; 1192 return NULL;
1192 } 1193 }
1193 name = strchr(typelist[i]->tp_name, '.'); 1194 name = strchr(typelist[i]->tp_name, '.');
1194 assert (name != NULL); 1195 assert (name != NULL);
1195 Py_INCREF(typelist[i]); 1196 Py_INCREF(typelist[i]);
1196 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); 1197 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
1197 } 1198 }
1198 return m; 1199 return m;
1199 } 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+