diff options
author | Aron Xu <aron@debian.org> | 2014-07-09 04:01:07 +0800 |
---|---|---|
committer | Aron Xu <aron@debian.org> | 2014-07-09 04:01:07 +0800 |
commit | 7042e17490515a990a45aa7237d11bc49ab0eaf0 (patch) | |
tree | 62b661911406394bbeaca8951d660bb6d8aac0de /python/libxml.c | |
parent | 2c8fe012ef1ff6e0613480dd182dec099aa9636e (diff) | |
download | libxml2-7042e17490515a990a45aa7237d11bc49ab0eaf0.tar.gz |
Imported Upstream version 2.8.0+dfsg1
Diffstat (limited to 'python/libxml.c')
-rw-r--r-- | python/libxml.c | 324 |
1 files changed, 57 insertions, 267 deletions
diff --git a/python/libxml.c b/python/libxml.c index 03cfb9f..9dabf89 100644 --- a/python/libxml.c +++ b/python/libxml.c @@ -28,7 +28,9 @@ #include "libxml_wrap.h" #include "libxml2-py.h" -#if defined(WITH_TRIO) +#if defined(_MSC_VER) && !defined(vsnprintf) +#define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a) +#elif defined(WITH_TRIO) && !defined(vsnprintf) #include "trio.h" #define vsnprintf trio_vsnprintf #endif @@ -41,17 +43,7 @@ /* #define DEBUG_FILES */ /* #define DEBUG_LOADER */ -#if PY_MAJOR_VERSION >= 3 -PyObject *PyInit_libxml2mod(void); - -#define PY_IMPORT_STRING_SIZE PyUnicode_FromStringAndSize -#define PY_IMPORT_STRING PyUnicode_FromString -#else void initlibxml2mod(void); -#define PY_IMPORT_STRING_SIZE PyString_FromStringAndSize -#define PY_IMPORT_STRING PyString_FromString -#endif - /** * TODO: @@ -290,42 +282,18 @@ xmlPythonFileReadRaw (void * context, char * buffer, int len) { if (ret == NULL) { printf("xmlPythonFileReadRaw: result is NULL\n"); return(-1); - } else if (PyBytes_Check(ret)) { - lenread = PyBytes_Size(ret); - data = PyBytes_AsString(ret); -#ifdef PyUnicode_Check - } else if PyUnicode_Check (ret) { -#if PY_VERSION_HEX >= 0x03030000 - size_t size; - const char *tmp; - - /* tmp doesn't need to be deallocated */ - tmp = PyUnicode_AsUTF8AndSize(ret, &size); - - lenread = (int) size; - data = (char *) tmp; -#else - PyObject *b; - b = PyUnicode_AsUTF8String(ret); - if (b == NULL) { - printf("xmlPythonFileReadRaw: failed to convert to UTF-8\n"); - return(-1); - } - lenread = PyBytes_Size(b); - data = PyBytes_AsString(b); - Py_DECREF(b); -#endif -#endif + } else if (PyString_Check(ret)) { + lenread = PyString_Size(ret); + data = PyString_AsString(ret); + if (lenread > len) + memcpy(buffer, data, len); + else + memcpy(buffer, data, lenread); + Py_DECREF(ret); } else { printf("xmlPythonFileReadRaw: result is not a String\n"); Py_DECREF(ret); - return(-1); } - if (lenread > len) - memcpy(buffer, data, len); - else - memcpy(buffer, data, lenread); - Py_DECREF(ret); return(lenread); } @@ -355,42 +323,18 @@ xmlPythonFileRead (void * context, char * buffer, int len) { if (ret == NULL) { printf("xmlPythonFileRead: result is NULL\n"); return(-1); - } else if (PyBytes_Check(ret)) { - lenread = PyBytes_Size(ret); - data = PyBytes_AsString(ret); -#ifdef PyUnicode_Check - } else if PyUnicode_Check (ret) { -#if PY_VERSION_HEX >= 0x03030000 - size_t size; - const char *tmp; - - /* tmp doesn't need to be deallocated */ - tmp = PyUnicode_AsUTF8AndSize(ret, &size); - - lenread = (int) size; - data = (char *) tmp; -#else - PyObject *b; - b = PyUnicode_AsUTF8String(ret); - if (b == NULL) { - printf("xmlPythonFileRead: failed to convert to UTF-8\n"); - return(-1); - } - lenread = PyBytes_Size(b); - data = PyBytes_AsString(b); - Py_DECREF(b); -#endif -#endif + } else if (PyString_Check(ret)) { + lenread = PyString_Size(ret); + data = PyString_AsString(ret); + if (lenread > len) + memcpy(buffer, data, len); + else + memcpy(buffer, data, lenread); + Py_DECREF(ret); } else { printf("xmlPythonFileRead: result is not a String\n"); Py_DECREF(ret); - return(-1); } - if (lenread > len) - memcpy(buffer, data, len); - else - memcpy(buffer, data, lenread); - Py_DECREF(ret); return(lenread); } @@ -416,7 +360,7 @@ xmlPythonFileWrite (void * context, const char * buffer, int len) { #endif file = (PyObject *) context; if (file == NULL) return(-1); - string = PY_IMPORT_STRING_SIZE(buffer, len); + string = PyString_FromStringAndSize(buffer, len); if (string == NULL) return(-1); if (PyObject_HasAttrString(file, (char *) "io_write")) { ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)", @@ -429,8 +373,8 @@ xmlPythonFileWrite (void * context, const char * buffer, int len) { if (ret == NULL) { printf("xmlPythonFileWrite: result is NULL\n"); return(-1); - } else if (PyLong_Check(ret)) { - written = (int) PyLong_AsLong(ret); + } else if (PyInt_Check(ret)) { + written = (int) PyInt_AsLong(ret); Py_DECREF(ret); } else if (ret == Py_None) { written = len; @@ -723,7 +667,7 @@ pythonExternalEntityLoader(const char *URL, const char *ID, Py_XDECREF(ctxtobj); #ifdef DEBUG_LOADER printf("pythonExternalEntityLoader: result "); - PyObject_Print(ret, stdout, 0); + PyObject_Print(ret, stderr, 0); printf("\n"); #endif @@ -769,114 +713,19 @@ libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { &loader)) return(NULL); - if (!PyCallable_Check(loader)) { - PyErr_SetString(PyExc_ValueError, "entity loader is not callable"); - return(NULL); - } - #ifdef DEBUG_LOADER printf("libxml_xmlSetEntityLoader\n"); #endif if (defaultExternalEntityLoader == NULL) defaultExternalEntityLoader = xmlGetExternalEntityLoader(); - Py_XDECREF(pythonExternalEntityLoaderObjext); pythonExternalEntityLoaderObjext = loader; - Py_XINCREF(pythonExternalEntityLoaderObjext); xmlSetExternalEntityLoader(pythonExternalEntityLoader); - py_retval = PyLong_FromLong(0); + py_retval = PyInt_FromLong(0); return(py_retval); } -/************************************************************************ - * * - * Input callback registration * - * * - ************************************************************************/ -static PyObject *pythonInputOpenCallbackObject; -static int pythonInputCallbackID = -1; - -static int -pythonInputMatchCallback(ATTRIBUTE_UNUSED const char *URI) -{ - /* Always return success, real decision whether URI is supported will be - * made in open callback. */ - return 1; -} - -static void * -pythonInputOpenCallback(const char *URI) -{ - PyObject *ret; - - ret = PyObject_CallFunction(pythonInputOpenCallbackObject, - (char *)"s", URI); - if (ret == Py_None) { - Py_DECREF(Py_None); - return NULL; - } - return ret; -} - -PyObject * -libxml_xmlRegisterInputCallback(ATTRIBUTE_UNUSED PyObject *self, - PyObject *args) { - PyObject *cb; - - if (!PyArg_ParseTuple(args, - (const char *)"O:libxml_xmlRegisterInputCallback", &cb)) - return(NULL); - - if (!PyCallable_Check(cb)) { - PyErr_SetString(PyExc_ValueError, "input callback is not callable"); - return(NULL); - } - - /* Python module registers a single callback and manages the list of - * all callbacks internally. This is necessitated by xmlInputMatchCallback - * API, which does not allow for passing of data objects to discriminate - * different Python methods. */ - if (pythonInputCallbackID == -1) { - pythonInputCallbackID = xmlRegisterInputCallbacks( - pythonInputMatchCallback, pythonInputOpenCallback, - xmlPythonFileReadRaw, xmlPythonFileCloseRaw); - if (pythonInputCallbackID == -1) - return PyErr_NoMemory(); - pythonInputOpenCallbackObject = cb; - Py_INCREF(pythonInputOpenCallbackObject); - } - - Py_INCREF(Py_None); - return(Py_None); -} - -PyObject * -libxml_xmlUnregisterInputCallback(ATTRIBUTE_UNUSED PyObject *self, - ATTRIBUTE_UNUSED PyObject *args) { - int ret; - - ret = xmlPopInputCallbacks(); - if (pythonInputCallbackID != -1) { - /* Assert that the right input callback was popped. libxml's API does not - * allow removal by ID, so all that could be done is an assert. */ - if (pythonInputCallbackID == ret) { - pythonInputCallbackID = -1; - Py_DECREF(pythonInputOpenCallbackObject); - pythonInputOpenCallbackObject = NULL; - } else { - PyErr_SetString(PyExc_AssertionError, "popped non-python input callback"); - return(NULL); - } - } else if (ret == -1) { - /* No more callbacks to pop */ - PyErr_SetString(PyExc_IndexError, "no input callbacks to pop"); - return(NULL); - } - - Py_INCREF(Py_None); - return(Py_None); -} /************************************************************************ * * @@ -917,10 +766,10 @@ pythonStartElement(void *user_data, const xmlChar * name, } else { dict = PyDict_New(); for (i = 0; attrs[i] != NULL; i++) { - attrname = PY_IMPORT_STRING((char *) attrs[i]); + attrname = PyString_FromString((char *) attrs[i]); i++; if (attrs[i] != NULL) { - attrvalue = PY_IMPORT_STRING((char *) attrs[i]); + attrvalue = PyString_FromString((char *) attrs[i]); } else { Py_XINCREF(Py_None); attrvalue = Py_None; @@ -1323,7 +1172,7 @@ pythonAttributeDecl(void *user_data, nameList = PyList_New(count); count = 0; for (node = tree; node != NULL; node = node->next) { - newName = PY_IMPORT_STRING((char *) node->name); + newName = PyString_FromString((char *) node->name); PyList_SetItem(nameList, count, newName); Py_DECREF(newName); count++; @@ -1518,7 +1367,6 @@ libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self, PyObject * libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) { -#ifdef LIBXML_SAX1_ENABLED int recover; const char *URI; PyObject *pyobj_SAX = NULL; @@ -1540,7 +1388,6 @@ libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) Py_INCREF(pyobj_SAX); /* The reference is released in pythonEndDocument() */ xmlSAXUserParseFile(SAX, pyobj_SAX, URI); -#endif /* LIBXML_SAX1_ENABLED */ Py_INCREF(Py_None); return (Py_None); } @@ -2186,7 +2033,7 @@ libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader)) return(NULL); - if (!PyCapsule_CheckExact(pyobj_reader)) { + if (!PyCObject_Check(pyobj_reader)) { Py_INCREF(Py_None); return(Py_None); } @@ -2387,32 +2234,6 @@ libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self, return (py_retval); } -PyObject * -libxml_xmlXPathRegisterVariable(ATTRIBUTE_UNUSED PyObject * self, - PyObject * args) -{ - PyObject *py_retval; - int c_retval = 0; - xmlChar *name; - xmlChar *ns_uri; - xmlXPathContextPtr ctx; - xmlXPathObjectPtr val; - PyObject *pyobj_ctx; - PyObject *pyobj_value; - - if (!PyArg_ParseTuple - (args, (char *) "OszO:xpathRegisterVariable", &pyobj_ctx, &name, - &ns_uri, &pyobj_value)) - return (NULL); - - ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx); - val = libxml_xmlXPathObjectPtrConvert(pyobj_value); - - c_retval = xmlXPathRegisterVariableNS(ctx, name, ns_uri, val); - py_retval = libxml_intWrap(c_retval); - return (py_retval); -} - /************************************************************************ * * * Global properties access * @@ -2745,10 +2566,6 @@ libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) if (!PyArg_ParseTuple(args, (char *) "O:last", &obj)) return NULL; cur = PyxmlNode_Get(obj); - if (cur == NULL) { - Py_INCREF(Py_None); - return (Py_None); - } #ifdef DEBUG printf("libxml_type: cur = %p\n", cur); @@ -2865,7 +2682,7 @@ libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) PyObject *pyobj_node; xmlChar *href; xmlNsPtr c_retval; - + if (!PyArg_ParseTuple (args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href)) return (NULL); @@ -3027,12 +2844,16 @@ libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) &py_file, &encoding, &format)) return (NULL); node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); + if (node == NULL) { - return (PyLong_FromLong((long) -1)); + return (PyInt_FromLong((long) -1)); + } + if ((py_file == NULL) || (!(PyFile_Check(py_file)))) { + return (PyInt_FromLong((long) -1)); } - output = PyFile_Get(py_file); + output = PyFile_AsFile(py_file); if (output == NULL) { - return (PyLong_FromLong((long) -1)); + return (PyInt_FromLong((long) -1)); } if (node->type == XML_DOCUMENT_NODE) { @@ -3051,7 +2872,7 @@ libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) if (encoding != NULL) { handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) { - return (PyLong_FromLong((long) -1)); + return (PyInt_FromLong((long) -1)); } } if (doc->type == XML_HTML_DOCUMENT_NODE) { @@ -3076,8 +2897,7 @@ libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) xmlNodeDumpOutput(buf, doc, node, 0, format, encoding); len = xmlOutputBufferClose(buf); } - PyFile_Release(output); - return (PyLong_FromLong((long) len)); + return (PyInt_FromLong((long) len)); } #endif /* LIBXML_OUTPUT_ENABLED */ @@ -3583,7 +3403,7 @@ PystringSet_Convert(PyObject *py_strings, xmlChar *** result) { int idx; for (idx=0; idx < count; ++idx) { - char* s = PyBytes_AsString + char* s = PyString_AsString (is_tuple ? PyTuple_GET_ITEM(py_strings, idx) : PyList_GET_ITEM(py_strings, idx)); @@ -3672,8 +3492,8 @@ libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self, return NULL; } else { - py_retval = PY_IMPORT_STRING_SIZE((const char *) doc_txt, - result); + py_retval = PyString_FromStringAndSize((const char *) doc_txt, + result); xmlFree(doc_txt); return py_retval; } @@ -3714,7 +3534,11 @@ libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self, return NULL; } - output = PyFile_Get(py_file); + if ((py_file == NULL) || (!(PyFile_Check(py_file)))) { + PyErr_SetString(PyExc_TypeError, "bad file."); + return NULL; + } + output = PyFile_AsFile(py_file); if (output == NULL) { PyErr_SetString(PyExc_TypeError, "bad file."); return NULL; @@ -3752,7 +3576,6 @@ libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self, xmlFree(prefixes); } - PyFile_Release(output); len = xmlOutputBufferClose(buf); if (result < 0) { @@ -3761,7 +3584,7 @@ libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self, return NULL; } else - return PyLong_FromLong((long) len); + return PyInt_FromLong((long) len); } #endif @@ -3775,7 +3598,7 @@ libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) { if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj)) return NULL; - str = PyCapsule_GetPointer(obj, PyCapsule_GetName(obj)); + str = PyCObject_GetDesc(obj); return Py_BuildValue((char *)"s", str); } @@ -3870,64 +3693,31 @@ static PyMethodDef libxmlMethods[] = { {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL}, {(char *) "compareNodesEqual", libxml_compareNodesEqual, METH_VARARGS, NULL}, {(char *) "nodeHash", libxml_nodeHash, METH_VARARGS, NULL}, - {(char *) "xmlRegisterInputCallback", libxml_xmlRegisterInputCallback, METH_VARARGS, NULL}, - {(char *) "xmlUnregisterInputCallback", libxml_xmlUnregisterInputCallback, METH_VARARGS, NULL}, {NULL, NULL, 0, NULL} }; -#if PY_MAJOR_VERSION >= 3 -#define INITERROR return NULL - -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "libxml2mod", - NULL, - -1, - libxmlMethods, - NULL, - NULL, - NULL, - NULL -}; - -#else -#define INITERROR return - #ifdef MERGED_MODULES extern void initlibxsltmod(void); #endif -#endif - -#if PY_MAJOR_VERSION >= 3 -PyObject *PyInit_libxml2mod(void) -#else -void initlibxml2mod(void) -#endif +void +initlibxml2mod(void) { - PyObject *module; + static int initialized = 0; + + if (initialized != 0) + return; -#if PY_MAJOR_VERSION >= 3 - module = PyModule_Create(&moduledef); -#else /* intialize the python extension module */ - module = Py_InitModule((char *) "libxml2mod", libxmlMethods); -#endif - if (module == NULL) - INITERROR; + Py_InitModule((char *) "libxml2mod", libxmlMethods); /* initialize libxml2 */ xmlInitParser(); - /* TODO this probably need to be revamped for Python3 */ libxml_xmlErrorInitialize(); -#if PY_MAJOR_VERSION < 3 + initialized = 1; + #ifdef MERGED_MODULES initlibxsltmod(); #endif -#endif - -#if PY_MAJOR_VERSION >= 3 - return module; -#endif } |