changeset 566:dc2e2384c8c7

fast_interpolate_variables(): allow omission of the cache variable. Allow also setting the cache variable to None. All these settings disable the interpolation cache.
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 07 Jan 2022 00:37:04 +0100
parents 5c617d870bb8
children 059260191371
files configmix/_speedups.c
diffstat 1 files changed, 25 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/configmix/_speedups.c	Thu Jan 06 22:42:09 2022 +0100
+++ b/configmix/_speedups.c	Fri Jan 07 00:37:04 2022 +0100
@@ -951,7 +951,7 @@
 {
     PyObject *config;
     PyObject *s;
-    PyObject *cache;
+    PyObject *cache = NULL;
 
     Py_ssize_t s_len;
     Py_ssize_t start, rest, end;
@@ -966,9 +966,13 @@
     int use_cache, cacheable;
     struct speedups_state *sstate;
 
-    if (!PyArg_UnpackTuple(args, "s", 3, 3, &config, &s, &cache)) {
+    if (!PyArg_UnpackTuple(args, "s", 2, 3, &config, &s, &cache)) {
         return NULL;
     }
+    /* Disable caching if the cache param is given as None */
+    if ((cache != NULL) && py_object_is(cache, Py_None)) {
+        cache = NULL;
+    }
     s_len = PyUnicode_GetLength(s);   /* also an implicit type check */
     if (s_len < 0) {
         return NULL;
@@ -991,16 +995,18 @@
         return Py_NewRef(s);
     }
 
-    result = PyDict_GetItem(cache, s);    /* borrowed */
-    if (result != NULL) {
-        if (result == sstate->MISSING) {
-            return PyErr_Format(
-                PyExc_KeyError,
-                "Cannot interpolate variables in string %R (cached)",
-                s);
-        }
-        else {
-            return Py_NewRef(result);
+    if (cache != NULL) {
+        result = PyDict_GetItem(cache, s);    /* borrowed */
+        if (result != NULL) {
+            if (result == sstate->MISSING) {
+                return PyErr_Format(
+                    PyExc_KeyError,
+                    "Cannot interpolate variables in string %R (cached)",
+                    s);
+            }
+            else {
+                return Py_NewRef(result);
+            }
         }
     }
 
@@ -1070,10 +1076,12 @@
                         varvalue = Py_NewRef(sstate->EMPTY_STR);
                     }
                     else {
-                        PyErr_Fetch(&err_type, &err_value, &err_tb);
-                        /* this does NOT steal */
-                        PyDict_SetItem(cache, s, sstate->MISSING);
-                        PyErr_Restore(err_type, err_value, err_tb);
+                        if (cache != NULL) {
+                            PyErr_Fetch(&err_type, &err_value, &err_tb);
+                            /* this does NOT steal */
+                            PyDict_SetItem(cache, s, sstate->MISSING);
+                            PyErr_Restore(err_type, err_value, err_tb);
+                        }
                         goto error;
                     }
                 }
@@ -1161,7 +1169,7 @@
     py_transfer_owned(&result, &tmp);
 
 success:
-    if (use_cache) {
+    if (use_cache && (cache != NULL)) {
         if (PyDict_SetItem(cache, s, result) < 0) {
             PyErr_Clear();    /* clear any cache-related error */
         }