pyke/mapping.c: Export `gmap_pyupdate' as a separately useful thing.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 28 Mar 2020 10:00:09 +0000 (10:00 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 14:23:48 +0000 (15:23 +0100)
It adds all of the things in a Python mapping into a gmappish object.
What's not to like?

pyke/mapping.c
pyke/pyke.h

index ad02d25..b108178 100644 (file)
@@ -848,7 +848,7 @@ end:
   return (rc);
 }
 
-static int update_core(PyObject *me, PyObject *map)
+int gmap_pyupdate(PyObject *me, PyObject *map)
 {
   const gmap_ops *gmops = GMAP_OPS(me);
   PyObject *i = 0, *item = 0, *k = 0, *v = 0;
@@ -856,6 +856,7 @@ static int update_core(PyObject *me, PyObject *map)
   unsigned foundp;
   int rc = -1;
 
+  if (!map) return (0);
   v = PyObject_CallMethod(map, PY23("iteritems", "items"), 0);
 #ifdef PY3
   if (v) { i = PyObject_GetIter(v); Py_DECREF(v); v = 0; }
@@ -899,8 +900,8 @@ PyObject *gmapmeth_update(PyObject *me, PyObject *arg, PyObject *kw)
   PyObject *map = 0;
 
   if (!PyArg_ParseTuple(arg, "|O:update", &map)) return (0);
-  if (map && update_core(me, map)) return (0);
-  if (kw && update_core(me, kw)) return (0);
+  if (gmap_pyupdate(me, map)) return (0);
+  if (gmap_pyupdate(me, kw)) return (0);
   RETURN_ME;
 }
 
index a60fd76..ada47fc 100644 (file)
@@ -687,6 +687,7 @@ extern PyObject *gmap_pyiter(PyObject *); /* for `tp_iter' */
 extern PyObject *gmap_pylookup(PyObject *, PyObject *); /* for `mp_subscript' */
 extern int gmap_pystore(PyObject *, PyObject *, PyObject *); /* for `mp_ass_subscript' */
 extern int gmap_pyhaskey(PyObject *, PyObject *); /* for `sq_contains' */
+extern int gmap_pyupdate(PyObject *me, PyObject *map);
 extern const PySequenceMethods gmap_pysequence; /* for `tp_as_sequence' */
 extern const PyMethodDef gmapro_pymethods[]; /* read-only methods */
 extern const PyMethodDef gmap_pymethods[]; /* all the standard methods */