[4suite-checkins] [XML1_0-maint] In 4Suite/Ft/Xml/src/domlette, files expat_module.c

Jeremy Kloth jkloth at 4suite.org
Sun Dec 10 11:59:30 MST 2006


Branch: XML1_0-maint

Modified Files:
    expat_module.c

Log Message:
Backport of revision 1.112

ViewCVS diff:
  http://cvs.4suite.org/viewcvs/4Suite/Ft/Xml/src/domlette/expat_module.c.diff?r1=1.105.2.3&r2=1.105.2.4
ViewCVS view:
  http://cvs.4suite.org/viewcvs/4Suite/Ft/Xml/src/domlette/expat_module.c?rev=1.105.2.4&content-type=text/vnd.viewcvs-markup

Index: expat_module.c
===================================================================
RCS file: /var/local/cvsroot/4Suite/Ft/Xml/src/domlette/expat_module.c,v
retrieving revision 1.105.2.3
retrieving revision 1.105.2.4
diff -U2 -r1.105.2.3 -r1.105.2.4
--- expat_module.c	2 Dec 2006 01:15:48 -0000	1.105.2.3
+++ expat_module.c	10 Dec 2006 18:59:30 -0000	1.105.2.4
@@ -5512,5 +5512,5 @@
 {
   PyObject *prefix, *uri;
-  XML_Char *context, *ptr, *temp;
+  XML_Char *context, *ptr;
   Py_ssize_t i, used, size, new_len;
 
@@ -5528,5 +5528,28 @@
   }
 
+#define XMLCHAR_CONCAT_AND_DEL(dst, ob) do { \
+  assert(sizeof(XML_Char) == sizeof(Py_UNICODE)); \
+  Py_UNICODE_COPY(dst, PyUnicode_AS_UNICODE(ob), PyUnicode_GET_SIZE(ob)); \
+  dst += PyUnicode_GET_SIZE(ob); \
+  Py_DECREF(ob); \
+} while (0)
+
+  /* the default namespace (`None` prefix) must be the first entry.
+   * Adjust the initial allocation to ensure that the URI will fit. */
   size = 1024;
+  if ((uri = PyDict_GetItem(namespaces, Py_None)) != NULL) {
+    uri = PyObject_Unicode(uri);
+    if (uri == NULL) {
+      Py_DECREF(namespaces);
+      return NULL;
+    }
+    /* +2 for NAMESPACE_SEP ('=') and CONTEXT_SEP ('\f') */
+    used = PyUnicode_GET_SIZE(uri) + 2;
+    if (used >= size) {
+      size = ROUND_UP(used, 1024);
+    }
+  } else {
+    used = 0;
+  }
   ptr = context = PyMem_New(XML_Char, size);
   if (context == NULL) {
@@ -5535,11 +5558,20 @@
     return NULL;
   }
-  used = 0;
+  /* copy the uri to the context */
+  if (uri) {
+    XMLCHAR_CONCAT_AND_DEL(ptr, uri);
+    *ptr++ = '\f';
+  }
 
   i = 0;
   while (PyDict_Next(namespaces, &i, &prefix, &uri)) {
+    if (prefix == Py_None) continue;
     prefix = PyObject_Unicode(prefix);
     uri = PyObject_Unicode(uri);
-    if (prefix == NULL || uri == NULL) return NULL;
+    if (prefix == NULL || uri == NULL) {
+      Py_DECREF(namespaces);
+      PyMem_Del(context);
+      return NULL;
+    }
 
     /* +2 for NAMESPACE_SEP ('=') and CONTEXT_SEP ('\f') */
@@ -5558,35 +5590,14 @@
       context = ptr;
       ptr += used;
-      used = new_len;
     }
+    used = new_len;
 
     /* copy the prefix to the context */
-    temp = XMLChar_FromObject(prefix);
-    if (temp == NULL) {
-      Py_DECREF(prefix);
-      Py_DECREF(uri);
-      Py_DECREF(namespaces);
-      PyMem_Del(context);
-      return NULL;
-    }
-    memcpy(ptr, temp, PyUnicode_GET_SIZE(prefix) * sizeof(XML_Char));
-    ptr += PyUnicode_GET_SIZE(prefix);
+    XMLCHAR_CONCAT_AND_DEL(ptr, prefix);
     *ptr++ = '=';
-    free(temp);
-    Py_DECREF(prefix);
 
     /* copy the uri to the context */
-    temp = XMLChar_FromObject(uri);
-    if (temp == NULL) {
-      Py_DECREF(uri);
-      Py_DECREF(namespaces);
-      PyMem_Del(context);
-      return NULL;
-    }
-    memcpy(ptr, temp, PyUnicode_GET_SIZE(uri) * sizeof(XML_Char));
-    ptr += PyUnicode_GET_SIZE(uri);
+    XMLCHAR_CONCAT_AND_DEL(ptr, uri);
     *ptr++ = '\f';
-    free(temp);
-    Py_DECREF(uri);
   }
   Py_DECREF(namespaces);


More information about the 4suite-checkins mailing list