2002-02-03 15:08:05 +00:00
/*
* libxml . c : this modules implements the main part of the glue of the
* libxml2 library and the Python interpreter . It provides the
* entry points where an automatically generated stub is either
* unpractical or would not match cleanly the Python model .
*
2002-02-22 22:51:13 +00:00
* If compiled with MERGED_MODULES , the entry point will be used to
* initialize both the libxml2 and the libxslt wrappers
*
2002-02-03 15:08:05 +00:00
* See Copyright for the status of this software .
*
* daniel @ veillard . com
*/
2020-11-10 15:42:36 +01:00
# define PY_SSIZE_T_CLEAN
2002-01-30 16:37:32 +00:00
# include <Python.h>
2002-09-12 15:00:57 +00:00
# include <fileobject.h>
2002-11-23 11:22:49 +00:00
/* #include "config.h" */
2002-01-31 23:42:44 +00:00
# include <libxml/xmlmemory.h>
2002-01-30 16:37:32 +00:00
# include <libxml/parser.h>
# include <libxml/tree.h>
2002-02-01 17:56:45 +00:00
# include <libxml/xpath.h>
2002-02-02 21:49:17 +00:00
# include <libxml/xmlerror.h>
2002-02-01 17:56:45 +00:00
# include <libxml/xpathInternals.h>
2002-02-03 20:13:06 +00:00
# include <libxml/xmlmemory.h>
2002-09-12 15:00:57 +00:00
# include <libxml/xmlIO.h>
2004-03-09 09:03:28 +00:00
# include <libxml/c14n.h>
2008-05-12 12:58:46 +00:00
# include <libxml/xmlreader.h>
2008-09-01 13:08:57 +00:00
# include <libxml/xmlsave.h>
2002-01-30 16:37:32 +00:00
# include "libxml_wrap.h"
2002-01-30 20:52:23 +00:00
# include "libxml2-py.h"
2002-01-30 16:37:32 +00:00
2012-08-13 16:50:48 +08:00
# if defined(WITH_TRIO)
2003-03-26 00:38:10 +00:00
# include "trio.h"
# define vsnprintf trio_vsnprintf
2002-12-23 14:43:32 +00:00
# endif
2002-01-30 16:37:32 +00:00
/* #define DEBUG */
2002-02-12 13:46:21 +00:00
/* #define DEBUG_SAX */
2002-02-01 17:56:45 +00:00
/* #define DEBUG_XPATH */
2002-02-02 21:49:17 +00:00
/* #define DEBUG_ERROR */
2002-02-03 20:13:06 +00:00
/* #define DEBUG_MEMORY */
2002-09-12 15:00:57 +00:00
/* #define DEBUG_FILES */
/* #define DEBUG_LOADER */
2002-02-03 20:13:06 +00:00
2013-03-29 13:46:24 +08:00
# if PY_MAJOR_VERSION >= 3
PyObject * PyInit_libxml2mod ( void ) ;
# define PY_IMPORT_STRING_SIZE PyUnicode_FromStringAndSize
# define PY_IMPORT_STRING PyUnicode_FromString
# else
2002-03-15 22:24:56 +00:00
void initlibxml2mod ( void ) ;
2013-03-29 13:46:24 +08:00
# define PY_IMPORT_STRING_SIZE PyString_FromStringAndSize
# define PY_IMPORT_STRING PyString_FromString
# endif
2002-03-15 22:24:56 +00:00
2002-09-12 15:00:57 +00:00
/**
* TODO :
*
* macro to flag unimplemented blocks
*/
# define TODO \
xmlGenericError ( xmlGenericErrorContext , \
" Unimplemented block at %s:%d \n " , \
__FILE__ , __LINE__ ) ;
2004-07-03 23:28:52 +00:00
/*
* the following vars are used for XPath extensions , but
* are also referenced within the parser cleanup routine .
*/
static int libxml_xpathCallbacksInitialized = 0 ;
typedef struct libxml_xpathCallback {
xmlXPathContextPtr ctx ;
xmlChar * name ;
xmlChar * ns_uri ;
PyObject * function ;
} libxml_xpathCallback , * libxml_xpathCallbackPtr ;
typedef libxml_xpathCallback libxml_xpathCallbackArray [ ] ;
static int libxml_xpathCallbacksAllocd = 10 ;
static libxml_xpathCallbackArray * libxml_xpathCallbacks = NULL ;
static int libxml_xpathCallbacksNb = 0 ;
2002-09-12 15:00:57 +00:00
2002-02-03 20:13:06 +00:00
/************************************************************************
* *
* Memory debug interface *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-07-29 20:44:53 +00:00
#if 0
2002-02-03 20:13:06 +00:00
extern void xmlMemFree ( void * ptr ) ;
extern void * xmlMemMalloc ( size_t size ) ;
2002-03-15 22:24:56 +00:00
extern void * xmlMemRealloc ( void * ptr , size_t size ) ;
2002-02-03 20:13:06 +00:00
extern char * xmlMemoryStrdup ( const char * str ) ;
2003-07-29 20:44:53 +00:00
# endif
2002-02-03 20:13:06 +00:00
static int libxmlMemoryDebugActivated = 0 ;
static long libxmlMemoryAllocatedBase = 0 ;
static int libxmlMemoryDebug = 0 ;
static xmlFreeFunc freeFunc = NULL ;
static xmlMallocFunc mallocFunc = NULL ;
static xmlReallocFunc reallocFunc = NULL ;
static xmlStrdupFunc strdupFunc = NULL ;
2004-07-01 12:56:30 +00:00
static void
libxml_xmlErrorInitialize ( void ) ; /* forward declare */
2004-07-02 12:23:44 +00:00
PyObject *
2004-07-16 10:39:30 +00:00
libxml_xmlMemoryUsed ( PyObject * self ATTRIBUTE_UNUSED ,
PyObject * args ATTRIBUTE_UNUSED )
2004-07-02 12:23:44 +00:00
{
long ret ;
PyObject * py_retval ;
ret = xmlMemUsed ( ) ;
py_retval = libxml_longWrap ( ret ) ;
return ( py_retval ) ;
}
2002-02-03 20:13:06 +00:00
PyObject *
2004-07-16 10:39:30 +00:00
libxml_xmlDebugMemory ( PyObject * self ATTRIBUTE_UNUSED , PyObject * args )
2002-03-15 22:24:56 +00:00
{
2002-02-03 20:13:06 +00:00
int activate ;
PyObject * py_retval ;
long ret ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " i:xmlDebugMemory " , & activate ) )
return ( NULL ) ;
2002-02-03 20:13:06 +00:00
# ifdef DEBUG_MEMORY
printf ( " libxml_xmlDebugMemory(%d) called \n " , activate ) ;
# endif
if ( activate ! = 0 ) {
2002-03-15 22:24:56 +00:00
if ( libxmlMemoryDebug = = 0 ) {
/*
* First initialize the library and grab the old memory handlers
* and switch the library to memory debugging
*/
xmlMemGet ( ( xmlFreeFunc * ) & freeFunc ,
( xmlMallocFunc * ) & mallocFunc ,
( xmlReallocFunc * ) & reallocFunc ,
( xmlStrdupFunc * ) & strdupFunc ) ;
if ( ( freeFunc = = xmlMemFree ) & & ( mallocFunc = = xmlMemMalloc ) & &
( reallocFunc = = xmlMemRealloc ) & &
( strdupFunc = = xmlMemoryStrdup ) ) {
libxmlMemoryAllocatedBase = xmlMemUsed ( ) ;
} else {
2004-07-01 12:56:30 +00:00
/*
* cleanup first , because some memory has been
* allocated with the non - debug malloc in xmlInitParser
* when the python module was imported
*/
xmlCleanupParser ( ) ;
2002-03-15 22:24:56 +00:00
ret = ( long ) xmlMemSetup ( xmlMemFree , xmlMemMalloc ,
xmlMemRealloc , xmlMemoryStrdup ) ;
if ( ret < 0 )
goto error ;
libxmlMemoryAllocatedBase = xmlMemUsed ( ) ;
2004-07-01 12:56:30 +00:00
/* reinitialize */
xmlInitParser ( ) ;
libxml_xmlErrorInitialize ( ) ;
2002-03-15 22:24:56 +00:00
}
ret = 0 ;
} else if ( libxmlMemoryDebugActivated = = 0 ) {
libxmlMemoryAllocatedBase = xmlMemUsed ( ) ;
ret = 0 ;
} else {
ret = xmlMemUsed ( ) - libxmlMemoryAllocatedBase ;
}
libxmlMemoryDebug = 1 ;
libxmlMemoryDebugActivated = 1 ;
2002-02-03 20:13:06 +00:00
} else {
2002-03-15 22:24:56 +00:00
if ( libxmlMemoryDebugActivated = = 1 )
ret = xmlMemUsed ( ) - libxmlMemoryAllocatedBase ;
else
ret = 0 ;
libxmlMemoryDebugActivated = 0 ;
2002-02-03 20:13:06 +00:00
}
2002-03-15 22:24:56 +00:00
error :
2002-02-03 20:13:06 +00:00
py_retval = libxml_longWrap ( ret ) ;
2002-03-15 22:24:56 +00:00
return ( py_retval ) ;
2002-02-03 20:13:06 +00:00
}
2004-07-01 12:56:30 +00:00
PyObject *
libxml_xmlPythonCleanupParser ( PyObject * self ATTRIBUTE_UNUSED ,
PyObject * args ATTRIBUTE_UNUSED ) {
2004-07-03 23:28:52 +00:00
int ix ;
long freed = - 1 ;
2004-07-01 12:56:30 +00:00
if ( libxmlMemoryDebug ) {
freed = xmlMemUsed ( ) ;
}
xmlCleanupParser ( ) ;
2004-07-03 23:28:52 +00:00
/*
* Need to confirm whether we really want to do this ( required for
* memcheck ) in all cases . . .
*/
if ( libxml_xpathCallbacks ! = NULL ) { /* if ext funcs declared */
for ( ix = 0 ; ix < libxml_xpathCallbacksNb ; ix + + ) {
if ( ( * libxml_xpathCallbacks ) [ ix ] . name ! = NULL )
xmlFree ( ( * libxml_xpathCallbacks ) [ ix ] . name ) ;
if ( ( * libxml_xpathCallbacks ) [ ix ] . ns_uri ! = NULL )
xmlFree ( ( * libxml_xpathCallbacks ) [ ix ] . ns_uri ) ;
}
libxml_xpathCallbacksNb = 0 ;
xmlFree ( libxml_xpathCallbacks ) ;
libxml_xpathCallbacks = NULL ;
}
2004-07-01 12:56:30 +00:00
if ( libxmlMemoryDebug ) {
freed - = xmlMemUsed ( ) ;
libxmlMemoryAllocatedBase - = freed ;
if ( libxmlMemoryAllocatedBase < 0 )
libxmlMemoryAllocatedBase = 0 ;
}
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2002-02-03 20:13:06 +00:00
PyObject *
2002-03-15 22:24:56 +00:00
libxml_xmlDumpMemory ( ATTRIBUTE_UNUSED PyObject * self ,
ATTRIBUTE_UNUSED PyObject * args )
{
2002-02-03 20:13:06 +00:00
if ( libxmlMemoryDebug ! = 0 )
2002-03-15 22:24:56 +00:00
xmlMemoryDump ( ) ;
2002-02-03 20:13:06 +00:00
Py_INCREF ( Py_None ) ;
2002-03-15 22:24:56 +00:00
return ( Py_None ) ;
2002-02-03 20:13:06 +00:00
}
2002-01-30 16:37:32 +00:00
2002-09-12 15:00:57 +00:00
/************************************************************************
* *
* Handling Python FILE I / O at the C level *
2019-09-30 17:04:54 +02:00
* The raw I / O attack directly the File objects , while the *
2002-09-12 15:00:57 +00:00
* other routines address the ioWrapper instance instead *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* xmlPythonFileCloseUnref :
* @ context : the I / O context
*
* Close an I / O channel
*/
static int
xmlPythonFileCloseRaw ( void * context ) {
PyObject * file , * ret ;
# ifdef DEBUG_FILES
printf ( " xmlPythonFileCloseUnref \n " ) ;
# endif
file = ( PyObject * ) context ;
if ( file = = NULL ) return ( - 1 ) ;
2002-09-24 14:13:13 +00:00
ret = PyEval_CallMethod ( file , ( char * ) " close " , ( char * ) " () " ) ;
2002-09-12 15:00:57 +00:00
if ( ret ! = NULL ) {
Py_DECREF ( ret ) ;
}
Py_DECREF ( file ) ;
return ( 0 ) ;
}
/**
* xmlPythonFileReadRaw :
* @ context : the I / O context
* @ buffer : where to drop data
* @ len : number of bytes to write
*
* Read @ len bytes to @ buffer from the Python file in the I / O channel
*
* Returns the number of bytes read
*/
static int
xmlPythonFileReadRaw ( void * context , char * buffer , int len ) {
PyObject * file ;
PyObject * ret ;
int lenread = - 1 ;
char * data ;
# ifdef DEBUG_FILES
printf ( " xmlPythonFileReadRaw: %d \n " , len ) ;
# endif
file = ( PyObject * ) context ;
if ( file = = NULL ) return ( - 1 ) ;
2002-09-24 14:13:13 +00:00
ret = PyEval_CallMethod ( file , ( char * ) " read " , ( char * ) " (i) " , len ) ;
2002-09-12 15:00:57 +00:00
if ( ret = = NULL ) {
printf ( " xmlPythonFileReadRaw: result is NULL \n " ) ;
return ( - 1 ) ;
2013-03-29 13:46:24 +08:00
} else if ( PyBytes_Check ( ret ) ) {
lenread = PyBytes_Size ( ret ) ;
data = PyBytes_AsString ( ret ) ;
# ifdef PyUnicode_Check
2020-02-28 12:48:14 +01:00
} else if ( PyUnicode_Check ( ret ) ) {
2013-03-29 13:46:24 +08:00
# if PY_VERSION_HEX >= 0x03030000
2013-05-30 19:00:50 +02:00
Py_ssize_t size ;
2013-03-29 13:46:24 +08:00
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
2002-09-12 15:00:57 +00:00
} else {
printf ( " xmlPythonFileReadRaw: result is not a String \n " ) ;
Py_DECREF ( ret ) ;
2013-03-29 13:46:24 +08:00
return ( - 1 ) ;
2002-09-12 15:00:57 +00:00
}
2013-03-29 13:46:24 +08:00
if ( lenread > len )
memcpy ( buffer , data , len ) ;
else
memcpy ( buffer , data , lenread ) ;
Py_DECREF ( ret ) ;
2002-09-12 15:00:57 +00:00
return ( lenread ) ;
}
/**
* xmlPythonFileRead :
* @ context : the I / O context
* @ buffer : where to drop data
* @ len : number of bytes to write
*
* Read @ len bytes to @ buffer from the I / O channel .
*
* Returns the number of bytes read
*/
static int
xmlPythonFileRead ( void * context , char * buffer , int len ) {
PyObject * file ;
PyObject * ret ;
int lenread = - 1 ;
char * data ;
# ifdef DEBUG_FILES
printf ( " xmlPythonFileRead: %d \n " , len ) ;
# endif
file = ( PyObject * ) context ;
if ( file = = NULL ) return ( - 1 ) ;
2002-09-24 14:13:13 +00:00
ret = PyEval_CallMethod ( file , ( char * ) " io_read " , ( char * ) " (i) " , len ) ;
2002-09-12 15:00:57 +00:00
if ( ret = = NULL ) {
printf ( " xmlPythonFileRead: result is NULL \n " ) ;
return ( - 1 ) ;
2013-03-29 13:46:24 +08:00
} else if ( PyBytes_Check ( ret ) ) {
lenread = PyBytes_Size ( ret ) ;
data = PyBytes_AsString ( ret ) ;
# ifdef PyUnicode_Check
2020-02-28 12:48:14 +01:00
} else if ( PyUnicode_Check ( ret ) ) {
2013-03-29 13:46:24 +08:00
# if PY_VERSION_HEX >= 0x03030000
2013-05-30 19:00:50 +02:00
Py_ssize_t size ;
2013-03-29 13:46:24 +08:00
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
2002-09-12 15:00:57 +00:00
} else {
printf ( " xmlPythonFileRead: result is not a String \n " ) ;
Py_DECREF ( ret ) ;
2013-03-29 13:46:24 +08:00
return ( - 1 ) ;
2002-09-12 15:00:57 +00:00
}
2013-03-29 13:46:24 +08:00
if ( lenread > len )
memcpy ( buffer , data , len ) ;
else
memcpy ( buffer , data , lenread ) ;
Py_DECREF ( ret ) ;
2002-09-12 15:00:57 +00:00
return ( lenread ) ;
}
/**
* xmlFileWrite :
* @ context : the I / O context
* @ buffer : where to drop data
* @ len : number of bytes to write
*
* Write @ len bytes from @ buffer to the I / O channel .
*
* Returns the number of bytes written
*/
static int
xmlPythonFileWrite ( void * context , const char * buffer , int len ) {
PyObject * file ;
PyObject * string ;
2003-12-04 12:31:49 +00:00
PyObject * ret = NULL ;
2002-09-12 15:00:57 +00:00
int written = - 1 ;
# ifdef DEBUG_FILES
printf ( " xmlPythonFileWrite: %d \n " , len ) ;
# endif
file = ( PyObject * ) context ;
if ( file = = NULL ) return ( - 1 ) ;
2013-03-29 13:46:24 +08:00
string = PY_IMPORT_STRING_SIZE ( buffer , len ) ;
2002-09-12 15:00:57 +00:00
if ( string = = NULL ) return ( - 1 ) ;
2003-12-04 12:31:49 +00:00
if ( PyObject_HasAttrString ( file , ( char * ) " io_write " ) ) {
ret = PyEval_CallMethod ( file , ( char * ) " io_write " , ( char * ) " (O) " ,
string ) ;
} else if ( PyObject_HasAttrString ( file , ( char * ) " write " ) ) {
ret = PyEval_CallMethod ( file , ( char * ) " write " , ( char * ) " (O) " ,
string ) ;
}
2002-09-12 15:00:57 +00:00
Py_DECREF ( string ) ;
if ( ret = = NULL ) {
printf ( " xmlPythonFileWrite: result is NULL \n " ) ;
return ( - 1 ) ;
2013-03-29 13:46:24 +08:00
} else if ( PyLong_Check ( ret ) ) {
written = ( int ) PyLong_AsLong ( ret ) ;
2002-09-12 15:00:57 +00:00
Py_DECREF ( ret ) ;
} else if ( ret = = Py_None ) {
written = len ;
Py_DECREF ( ret ) ;
} else {
printf ( " xmlPythonFileWrite: result is not an Int nor None \n " ) ;
Py_DECREF ( ret ) ;
}
return ( written ) ;
}
/**
* xmlPythonFileClose :
* @ context : the I / O context
*
* Close an I / O channel
*/
static int
xmlPythonFileClose ( void * context ) {
2003-12-04 12:31:49 +00:00
PyObject * file , * ret = NULL ;
2002-09-12 15:00:57 +00:00
# ifdef DEBUG_FILES
printf ( " xmlPythonFileClose \n " ) ;
# endif
file = ( PyObject * ) context ;
if ( file = = NULL ) return ( - 1 ) ;
2003-12-04 12:31:49 +00:00
if ( PyObject_HasAttrString ( file , ( char * ) " io_close " ) ) {
ret = PyEval_CallMethod ( file , ( char * ) " io_close " , ( char * ) " () " ) ;
} else if ( PyObject_HasAttrString ( file , ( char * ) " flush " ) ) {
ret = PyEval_CallMethod ( file , ( char * ) " flush " , ( char * ) " () " ) ;
}
2002-09-12 15:00:57 +00:00
if ( ret ! = NULL ) {
Py_DECREF ( ret ) ;
}
return ( 0 ) ;
}
2003-09-29 13:20:24 +00:00
# ifdef LIBXML_OUTPUT_ENABLED
2002-09-12 15:00:57 +00:00
/**
* xmlOutputBufferCreatePythonFile :
* @ file : a PyFile_Type
* @ encoder : the encoding converter or NULL
*
* Create a buffered output for the progressive saving to a PyFile_Type
* buffered C I / O
*
* Returns the new parser output or NULL
*/
2002-09-24 14:13:13 +00:00
static xmlOutputBufferPtr
2002-09-12 15:00:57 +00:00
xmlOutputBufferCreatePythonFile ( PyObject * file ,
xmlCharEncodingHandlerPtr encoder ) {
xmlOutputBufferPtr ret ;
if ( file = = NULL ) return ( NULL ) ;
ret = xmlAllocOutputBuffer ( encoder ) ;
if ( ret ! = NULL ) {
ret - > context = file ;
/* Py_INCREF(file); */
ret - > writecallback = xmlPythonFileWrite ;
ret - > closecallback = xmlPythonFileClose ;
}
return ( ret ) ;
}
PyObject *
libxml_xmlCreateOutputBuffer ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args ) {
PyObject * py_retval ;
PyObject * file ;
xmlChar * encoding ;
xmlCharEncodingHandlerPtr handler = NULL ;
xmlOutputBufferPtr buffer ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " Oz:xmlOutputBufferCreate " ,
& file , & encoding ) )
return ( NULL ) ;
if ( ( encoding ! = NULL ) & & ( encoding [ 0 ] ! = 0 ) ) {
2002-09-24 14:13:13 +00:00
handler = xmlFindCharEncodingHandler ( ( const char * ) encoding ) ;
2002-09-12 15:00:57 +00:00
}
buffer = xmlOutputBufferCreatePythonFile ( file , handler ) ;
if ( buffer = = NULL )
printf ( " libxml_xmlCreateOutputBuffer: buffer == NULL \n " ) ;
py_retval = libxml_xmlOutputBufferPtrWrap ( buffer ) ;
return ( py_retval ) ;
}
2003-12-04 12:31:49 +00:00
/**
* libxml_outputBufferGetPythonFile :
* @ buffer : the I / O buffer
*
* read the Python I / O from the CObject
*
* Returns the new parser output or NULL
*/
static PyObject *
libxml_outputBufferGetPythonFile ( ATTRIBUTE_UNUSED PyObject * self ,
PyObject * args ) {
PyObject * buffer ;
PyObject * file ;
xmlOutputBufferPtr obj ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:outputBufferGetPythonFile " ,
& buffer ) )
return ( NULL ) ;
obj = PyoutputBuffer_Get ( buffer ) ;
if ( obj = = NULL ) {
fprintf ( stderr ,
" outputBufferGetPythonFile: obj == NULL \n " ) ;
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
if ( obj - > closecallback ! = xmlPythonFileClose ) {
fprintf ( stderr ,
" outputBufferGetPythonFile: not a python file wrapper \n " ) ;
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
file = ( PyObject * ) obj - > context ;
if ( file = = NULL ) {
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
Py_INCREF ( file ) ;
return ( file ) ;
}
2004-03-09 09:03:28 +00:00
static PyObject *
2003-12-04 12:31:49 +00:00
libxml_xmlOutputBufferClose ( PyObject * self ATTRIBUTE_UNUSED , PyObject * args ) {
PyObject * py_retval ;
int c_retval ;
xmlOutputBufferPtr out ;
PyObject * pyobj_out ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlOutputBufferClose " , & pyobj_out ) )
return ( NULL ) ;
out = ( xmlOutputBufferPtr ) PyoutputBuffer_Get ( pyobj_out ) ;
2004-10-04 10:26:54 +00:00
/* Buffer may already have been destroyed elsewhere. This is harmless. */
if ( out = = NULL ) {
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2003-12-04 12:31:49 +00:00
c_retval = xmlOutputBufferClose ( out ) ;
py_retval = libxml_intWrap ( ( int ) c_retval ) ;
return ( py_retval ) ;
}
2004-03-09 09:03:28 +00:00
static PyObject *
2003-12-04 12:31:49 +00:00
libxml_xmlOutputBufferFlush ( PyObject * self ATTRIBUTE_UNUSED , PyObject * args ) {
PyObject * py_retval ;
int c_retval ;
xmlOutputBufferPtr out ;
PyObject * pyobj_out ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlOutputBufferFlush " , & pyobj_out ) )
return ( NULL ) ;
out = ( xmlOutputBufferPtr ) PyoutputBuffer_Get ( pyobj_out ) ;
c_retval = xmlOutputBufferFlush ( out ) ;
py_retval = libxml_intWrap ( ( int ) c_retval ) ;
return ( py_retval ) ;
}
2004-10-04 10:26:54 +00:00
static PyObject *
libxml_xmlSaveFileTo ( PyObject * self ATTRIBUTE_UNUSED , PyObject * args ) {
PyObject * py_retval ;
int c_retval ;
xmlOutputBufferPtr buf ;
PyObject * pyobj_buf ;
xmlDocPtr cur ;
PyObject * pyobj_cur ;
char * encoding ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " OOz:xmlSaveFileTo " , & pyobj_buf , & pyobj_cur , & encoding ) )
return ( NULL ) ;
buf = ( xmlOutputBufferPtr ) PyoutputBuffer_Get ( pyobj_buf ) ;
cur = ( xmlDocPtr ) PyxmlNode_Get ( pyobj_cur ) ;
c_retval = xmlSaveFileTo ( buf , cur , encoding ) ;
/* xmlSaveTo() freed the memory pointed to by buf, so record that in the
* Python object . */
( ( PyoutputBuffer_Object * ) ( pyobj_buf ) ) - > obj = NULL ;
py_retval = libxml_intWrap ( ( int ) c_retval ) ;
return ( py_retval ) ;
}
static PyObject *
libxml_xmlSaveFormatFileTo ( PyObject * self ATTRIBUTE_UNUSED , PyObject * args ) {
PyObject * py_retval ;
int c_retval ;
xmlOutputBufferPtr buf ;
PyObject * pyobj_buf ;
xmlDocPtr cur ;
PyObject * pyobj_cur ;
char * encoding ;
int format ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " OOzi:xmlSaveFormatFileTo " , & pyobj_buf , & pyobj_cur , & encoding , & format ) )
return ( NULL ) ;
buf = ( xmlOutputBufferPtr ) PyoutputBuffer_Get ( pyobj_buf ) ;
cur = ( xmlDocPtr ) PyxmlNode_Get ( pyobj_cur ) ;
c_retval = xmlSaveFormatFileTo ( buf , cur , encoding , format ) ;
/* xmlSaveFormatFileTo() freed the memory pointed to by buf, so record that
* in the Python object */
( ( PyoutputBuffer_Object * ) ( pyobj_buf ) ) - > obj = NULL ;
py_retval = libxml_intWrap ( ( int ) c_retval ) ;
return ( py_retval ) ;
}
2003-09-29 13:20:24 +00:00
# endif /* LIBXML_OUTPUT_ENABLED */
2002-09-12 15:00:57 +00:00
/**
* xmlParserInputBufferCreatePythonFile :
* @ file : a PyFile_Type
* @ encoder : the encoding converter or NULL
*
* Create a buffered output for the progressive saving to a PyFile_Type
* buffered C I / O
*
* Returns the new parser output or NULL
*/
2002-09-24 14:13:13 +00:00
static xmlParserInputBufferPtr
2002-09-12 15:00:57 +00:00
xmlParserInputBufferCreatePythonFile ( PyObject * file ,
xmlCharEncoding encoding ) {
xmlParserInputBufferPtr ret ;
if ( file = = NULL ) return ( NULL ) ;
ret = xmlAllocParserInputBuffer ( encoding ) ;
if ( ret ! = NULL ) {
ret - > context = file ;
/* Py_INCREF(file); */
ret - > readcallback = xmlPythonFileRead ;
ret - > closecallback = xmlPythonFileClose ;
}
return ( ret ) ;
}
PyObject *
libxml_xmlCreateInputBuffer ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args ) {
PyObject * py_retval ;
PyObject * file ;
xmlChar * encoding ;
xmlCharEncoding enc = XML_CHAR_ENCODING_NONE ;
xmlParserInputBufferPtr buffer ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " Oz:xmlParserInputBufferCreate " ,
& file , & encoding ) )
return ( NULL ) ;
if ( ( encoding ! = NULL ) & & ( encoding [ 0 ] ! = 0 ) ) {
2002-09-24 14:13:13 +00:00
enc = xmlParseCharEncoding ( ( const char * ) encoding ) ;
2002-09-12 15:00:57 +00:00
}
buffer = xmlParserInputBufferCreatePythonFile ( file , enc ) ;
if ( buffer = = NULL )
printf ( " libxml_xmlParserInputBufferCreate: buffer == NULL \n " ) ;
py_retval = libxml_xmlParserInputBufferPtrWrap ( buffer ) ;
return ( py_retval ) ;
}
/************************************************************************
* *
* Providing the resolver at the Python level *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static xmlExternalEntityLoader defaultExternalEntityLoader = NULL ;
static PyObject * pythonExternalEntityLoaderObjext ;
static xmlParserInputPtr
pythonExternalEntityLoader ( const char * URL , const char * ID ,
xmlParserCtxtPtr ctxt ) {
xmlParserInputPtr result = NULL ;
if ( pythonExternalEntityLoaderObjext ! = NULL ) {
PyObject * ret ;
PyObject * ctxtobj ;
ctxtobj = libxml_xmlParserCtxtPtrWrap ( ctxt ) ;
# ifdef DEBUG_LOADER
printf ( " pythonExternalEntityLoader: ready to call \n " ) ;
# endif
ret = PyObject_CallFunction ( pythonExternalEntityLoaderObjext ,
2002-09-24 14:13:13 +00:00
( char * ) " (ssO) " , URL , ID , ctxtobj ) ;
2003-01-14 14:40:25 +00:00
Py_XDECREF ( ctxtobj ) ;
2002-09-12 15:00:57 +00:00
# ifdef DEBUG_LOADER
printf ( " pythonExternalEntityLoader: result " ) ;
2013-02-25 16:07:09 +08:00
PyObject_Print ( ret , stdout , 0 ) ;
2002-09-12 15:00:57 +00:00
printf ( " \n " ) ;
# endif
if ( ret ! = NULL ) {
2002-09-24 14:13:13 +00:00
if ( PyObject_HasAttrString ( ret , ( char * ) " read " ) ) {
2002-09-12 15:00:57 +00:00
xmlParserInputBufferPtr buf ;
buf = xmlAllocParserInputBuffer ( XML_CHAR_ENCODING_NONE ) ;
if ( buf ! = NULL ) {
buf - > context = ret ;
buf - > readcallback = xmlPythonFileReadRaw ;
buf - > closecallback = xmlPythonFileCloseRaw ;
result = xmlNewIOInputStream ( ctxt , buf ,
XML_CHAR_ENCODING_NONE ) ;
}
2005-07-29 10:12:45 +00:00
#if 0
2002-09-12 15:00:57 +00:00
} else {
2005-07-29 10:12:45 +00:00
if ( URL ! = NULL )
printf ( " pythonExternalEntityLoader: can't read %s \n " ,
URL ) ;
# endif
2002-09-12 15:00:57 +00:00
}
if ( result = = NULL ) {
Py_DECREF ( ret ) ;
2003-02-24 11:47:13 +00:00
} else if ( URL ! = NULL ) {
2003-08-05 15:52:22 +00:00
result - > filename = ( char * ) xmlStrdup ( ( const xmlChar * ) URL ) ;
2003-02-24 11:47:13 +00:00
result - > directory = xmlParserGetDirectory ( ( const char * ) URL ) ;
2002-09-12 15:00:57 +00:00
}
}
}
if ( ( result = = NULL ) & & ( defaultExternalEntityLoader ! = NULL ) ) {
result = defaultExternalEntityLoader ( URL , ID , ctxt ) ;
}
return ( result ) ;
}
PyObject *
libxml_xmlSetEntityLoader ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args ) {
PyObject * py_retval ;
PyObject * loader ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:libxml_xmlSetEntityLoader " ,
& loader ) )
return ( NULL ) ;
2013-02-25 16:07:09 +08:00
if ( ! PyCallable_Check ( loader ) ) {
PyErr_SetString ( PyExc_ValueError , " entity loader is not callable " ) ;
return ( NULL ) ;
}
2002-09-12 15:00:57 +00:00
# ifdef DEBUG_LOADER
printf ( " libxml_xmlSetEntityLoader \n " ) ;
# endif
if ( defaultExternalEntityLoader = = NULL )
defaultExternalEntityLoader = xmlGetExternalEntityLoader ( ) ;
2013-02-25 16:07:09 +08:00
Py_XDECREF ( pythonExternalEntityLoaderObjext ) ;
2002-09-12 15:00:57 +00:00
pythonExternalEntityLoaderObjext = loader ;
2013-02-25 16:07:09 +08:00
Py_XINCREF ( pythonExternalEntityLoaderObjext ) ;
2002-09-12 15:00:57 +00:00
xmlSetExternalEntityLoader ( pythonExternalEntityLoader ) ;
2013-03-29 13:46:24 +08:00
py_retval = PyLong_FromLong ( 0 ) ;
2002-09-12 15:00:57 +00:00
return ( py_retval ) ;
}
2013-02-25 15:54:25 +08:00
/************************************************************************
* *
* 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 ) ;
}
2002-09-12 15:00:57 +00:00
2002-01-30 16:37:32 +00:00
/************************************************************************
* *
2002-02-03 15:08:05 +00:00
* Handling SAX / xmllib / sgmlop callback interfaces *
2002-01-30 16:37:32 +00:00
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-31 20:29:19 +00:00
2002-02-04 14:07:26 +00:00
static void
pythonStartElement ( void * user_data , const xmlChar * name ,
const xmlChar * * attrs )
{
int i ;
PyObject * handler ;
PyObject * dict ;
PyObject * attrname ;
PyObject * attrvalue ;
2002-03-15 22:24:56 +00:00
PyObject * result = NULL ;
2002-02-04 14:07:26 +00:00
int type = 0 ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonStartElement(%s) called \n " , name ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " startElement " ) )
2002-02-04 14:07:26 +00:00
type = 1 ;
2002-03-15 22:24:56 +00:00
else if ( PyObject_HasAttrString ( handler , ( char * ) " start " ) )
2002-02-04 14:07:26 +00:00
type = 2 ;
if ( type ! = 0 ) {
/*
2016-04-13 16:56:07 +02:00
* the xmllib interface always generates a dictionary ,
2002-02-04 14:07:26 +00:00
* possibly empty
*/
if ( ( attrs = = NULL ) & & ( type = = 1 ) ) {
Py_XINCREF ( Py_None ) ;
dict = Py_None ;
2002-03-15 22:24:56 +00:00
} else if ( attrs = = NULL ) {
dict = PyDict_New ( ) ;
2002-02-04 14:07:26 +00:00
} else {
dict = PyDict_New ( ) ;
for ( i = 0 ; attrs [ i ] ! = NULL ; i + + ) {
2013-03-29 13:46:24 +08:00
attrname = PY_IMPORT_STRING ( ( char * ) attrs [ i ] ) ;
2002-02-04 14:07:26 +00:00
i + + ;
if ( attrs [ i ] ! = NULL ) {
2013-03-29 13:46:24 +08:00
attrvalue = PY_IMPORT_STRING ( ( char * ) attrs [ i ] ) ;
2002-02-04 14:07:26 +00:00
} else {
Py_XINCREF ( Py_None ) ;
attrvalue = Py_None ;
}
PyDict_SetItem ( dict , attrname , attrvalue ) ;
2007-01-09 21:24:34 +00:00
Py_DECREF ( attrname ) ;
Py_DECREF ( attrvalue ) ;
2002-02-04 14:07:26 +00:00
}
}
if ( type = = 1 )
2002-03-15 22:24:56 +00:00
result = PyObject_CallMethod ( handler , ( char * ) " startElement " ,
( char * ) " sO " , name , dict ) ;
2002-02-04 14:07:26 +00:00
else if ( type = = 2 )
2002-03-15 22:24:56 +00:00
result = PyObject_CallMethod ( handler , ( char * ) " start " ,
( char * ) " sO " , name , dict ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( dict ) ;
Py_XDECREF ( result ) ;
}
}
static void
pythonStartDocument ( void * user_data )
{
PyObject * handler ;
PyObject * result ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonStartDocument() called \n " ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " startDocument " ) ) {
result =
PyObject_CallMethod ( handler , ( char * ) " startDocument " , NULL ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonEndDocument ( void * user_data )
{
PyObject * handler ;
PyObject * result ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonEndDocument() called \n " ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " endDocument " ) ) {
result =
PyObject_CallMethod ( handler , ( char * ) " endDocument " , NULL ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
/*
* The reference to the handler is released there
*/
Py_XDECREF ( handler ) ;
}
static void
pythonEndElement ( void * user_data , const xmlChar * name )
{
PyObject * handler ;
PyObject * result ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonEndElement(%s) called \n " , name ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " endElement " ) ) {
result = PyObject_CallMethod ( handler , ( char * ) " endElement " ,
( char * ) " s " , name ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
2002-03-15 22:24:56 +00:00
} else if ( PyObject_HasAttrString ( handler , ( char * ) " end " ) ) {
result = PyObject_CallMethod ( handler , ( char * ) " end " ,
( char * ) " s " , name ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-12 13:46:21 +00:00
Py_XDECREF ( result ) ;
2002-02-04 14:07:26 +00:00
}
}
static void
pythonReference ( void * user_data , const xmlChar * name )
{
PyObject * handler ;
PyObject * result ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonReference(%s) called \n " , name ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " reference " ) ) {
result = PyObject_CallMethod ( handler , ( char * ) " reference " ,
( char * ) " s " , name ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonCharacters ( void * user_data , const xmlChar * ch , int len )
{
PyObject * handler ;
2002-03-15 22:24:56 +00:00
PyObject * result = NULL ;
2002-02-04 14:07:26 +00:00
int type = 0 ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonCharacters(%s, %d) called \n " , ch , len ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " characters " ) )
type = 1 ;
else if ( PyObject_HasAttrString ( handler , ( char * ) " data " ) )
type = 2 ;
2002-02-04 14:07:26 +00:00
if ( type ! = 0 ) {
2002-03-15 22:24:56 +00:00
if ( type = = 1 )
result = PyObject_CallMethod ( handler , ( char * ) " characters " ,
2020-11-10 15:42:36 +01:00
( char * ) " s# " , ch , ( Py_ssize_t ) len ) ;
2002-03-15 22:24:56 +00:00
else if ( type = = 2 )
result = PyObject_CallMethod ( handler , ( char * ) " data " ,
2020-11-10 15:42:36 +01:00
( char * ) " s# " , ch , ( Py_ssize_t ) len ) ;
2002-03-15 22:24:56 +00:00
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonIgnorableWhitespace ( void * user_data , const xmlChar * ch , int len )
{
PyObject * handler ;
2002-03-15 22:24:56 +00:00
PyObject * result = NULL ;
2002-02-04 14:07:26 +00:00
int type = 0 ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonIgnorableWhitespace(%s, %d) called \n " , ch , len ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " ignorableWhitespace " ) )
2002-02-04 14:07:26 +00:00
type = 1 ;
2002-03-15 22:24:56 +00:00
else if ( PyObject_HasAttrString ( handler , ( char * ) " data " ) )
2002-02-04 14:07:26 +00:00
type = 2 ;
if ( type ! = 0 ) {
if ( type = = 1 )
result =
2002-03-15 22:24:56 +00:00
PyObject_CallMethod ( handler ,
( char * ) " ignorableWhitespace " ,
2020-11-10 15:42:36 +01:00
( char * ) " s# " , ch , ( Py_ssize_t ) len ) ;
2002-02-04 14:07:26 +00:00
else if ( type = = 2 )
2002-03-15 22:24:56 +00:00
result =
PyObject_CallMethod ( handler , ( char * ) " data " ,
2020-11-10 15:42:36 +01:00
( char * ) " s# " , ch , ( Py_ssize_t ) len ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonProcessingInstruction ( void * user_data ,
const xmlChar * target , const xmlChar * data )
{
PyObject * handler ;
PyObject * result ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonProcessingInstruction(%s, %s) called \n " , target , data ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " processingInstruction " ) ) {
result = PyObject_CallMethod ( handler , ( char * )
" processingInstruction " ,
( char * ) " ss " , target , data ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonComment ( void * user_data , const xmlChar * value )
{
PyObject * handler ;
PyObject * result ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonComment(%s) called \n " , value ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " comment " ) ) {
result =
PyObject_CallMethod ( handler , ( char * ) " comment " , ( char * ) " s " ,
value ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonWarning ( void * user_data , const char * msg , . . . )
{
PyObject * handler ;
PyObject * result ;
va_list args ;
char buf [ 1024 ] ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonWarning(%s) called \n " , msg ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " warning " ) ) {
2002-02-04 14:07:26 +00:00
va_start ( args , msg ) ;
vsnprintf ( buf , 1023 , msg , args ) ;
2002-03-15 22:24:56 +00:00
va_end ( args ) ;
buf [ 1023 ] = 0 ;
result =
PyObject_CallMethod ( handler , ( char * ) " warning " , ( char * ) " s " ,
buf ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonError ( void * user_data , const char * msg , . . . )
{
PyObject * handler ;
PyObject * result ;
va_list args ;
char buf [ 1024 ] ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonError(%s) called \n " , msg ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " error " ) ) {
2002-02-04 14:07:26 +00:00
va_start ( args , msg ) ;
vsnprintf ( buf , 1023 , msg , args ) ;
2002-03-15 22:24:56 +00:00
va_end ( args ) ;
buf [ 1023 ] = 0 ;
result =
PyObject_CallMethod ( handler , ( char * ) " error " , ( char * ) " s " ,
buf ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonFatalError ( void * user_data , const char * msg , . . . )
{
PyObject * handler ;
PyObject * result ;
va_list args ;
char buf [ 1024 ] ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonFatalError(%s) called \n " , msg ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " fatalError " ) ) {
2002-02-04 14:07:26 +00:00
va_start ( args , msg ) ;
vsnprintf ( buf , 1023 , msg , args ) ;
2002-03-15 22:24:56 +00:00
va_end ( args ) ;
buf [ 1023 ] = 0 ;
result =
PyObject_CallMethod ( handler , ( char * ) " fatalError " ,
( char * ) " s " , buf ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonCdataBlock ( void * user_data , const xmlChar * ch , int len )
{
PyObject * handler ;
2002-03-15 22:24:56 +00:00
PyObject * result = NULL ;
2002-02-04 14:07:26 +00:00
int type = 0 ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonCdataBlock(%s, %d) called \n " , ch , len ) ;
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " cdataBlock " ) )
type = 1 ;
else if ( PyObject_HasAttrString ( handler , ( char * ) " cdata " ) )
type = 2 ;
2002-02-04 14:07:26 +00:00
if ( type ! = 0 ) {
2002-03-15 22:24:56 +00:00
if ( type = = 1 )
result =
PyObject_CallMethod ( handler , ( char * ) " cdataBlock " ,
2020-11-10 15:42:36 +01:00
( char * ) " s# " , ch , ( Py_ssize_t ) len ) ;
2002-03-15 22:24:56 +00:00
else if ( type = = 2 )
result =
PyObject_CallMethod ( handler , ( char * ) " cdata " ,
2020-11-10 15:42:36 +01:00
( char * ) " s# " , ch , ( Py_ssize_t ) len ) ;
2002-03-15 22:24:56 +00:00
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonExternalSubset ( void * user_data ,
const xmlChar * name ,
const xmlChar * externalID , const xmlChar * systemID )
{
PyObject * handler ;
PyObject * result ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonExternalSubset(%s, %s, %s) called \n " ,
2002-03-15 22:24:56 +00:00
name , externalID , systemID ) ;
2002-02-12 13:46:21 +00:00
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " externalSubset " ) ) {
2002-02-04 14:07:26 +00:00
result =
2002-03-15 22:24:56 +00:00
PyObject_CallMethod ( handler , ( char * ) " externalSubset " ,
( char * ) " sss " , name , externalID ,
systemID ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonEntityDecl ( void * user_data ,
const xmlChar * name ,
int type ,
const xmlChar * publicId ,
const xmlChar * systemId , xmlChar * content )
{
PyObject * handler ;
PyObject * result ;
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " entityDecl " ) ) {
result = PyObject_CallMethod ( handler , ( char * ) " entityDecl " ,
( char * ) " sisss " , name , type ,
publicId , systemId , content ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonNotationDecl ( void * user_data ,
const xmlChar * name ,
const xmlChar * publicId , const xmlChar * systemId )
{
PyObject * handler ;
PyObject * result ;
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " notationDecl " ) ) {
result = PyObject_CallMethod ( handler , ( char * ) " notationDecl " ,
( char * ) " sss " , name , publicId ,
systemId ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonAttributeDecl ( void * user_data ,
const xmlChar * elem ,
const xmlChar * name ,
int type ,
int def ,
2002-03-15 22:24:56 +00:00
const xmlChar * defaultValue , xmlEnumerationPtr tree )
2002-02-04 14:07:26 +00:00
{
PyObject * handler ;
PyObject * nameList ;
PyObject * newName ;
xmlEnumerationPtr node ;
PyObject * result ;
int count ;
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " attributeDecl " ) ) {
2002-02-04 14:07:26 +00:00
count = 0 ;
for ( node = tree ; node ! = NULL ; node = node - > next ) {
count + + ;
}
nameList = PyList_New ( count ) ;
count = 0 ;
for ( node = tree ; node ! = NULL ; node = node - > next ) {
2013-03-29 13:46:24 +08:00
newName = PY_IMPORT_STRING ( ( char * ) node - > name ) ;
2002-02-04 14:07:26 +00:00
PyList_SetItem ( nameList , count , newName ) ;
2007-01-09 21:24:34 +00:00
Py_DECREF ( newName ) ;
2002-02-04 14:07:26 +00:00
count + + ;
}
2002-03-15 22:24:56 +00:00
result = PyObject_CallMethod ( handler , ( char * ) " attributeDecl " ,
( char * ) " ssiisO " , elem , name , type ,
def , defaultValue , nameList ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( nameList ) ;
Py_XDECREF ( result ) ;
}
}
static void
pythonElementDecl ( void * user_data ,
const xmlChar * name ,
2002-03-15 22:24:56 +00:00
int type , ATTRIBUTE_UNUSED xmlElementContentPtr content )
2002-02-04 14:07:26 +00:00
{
PyObject * handler ;
PyObject * obj ;
PyObject * result ;
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " elementDecl " ) ) {
/* TODO: wrap in an elementContent object */
printf
( " pythonElementDecl: xmlElementContentPtr wrapper missing ! \n " ) ;
obj = Py_None ;
/* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
result = PyObject_CallMethod ( handler , ( char * ) " elementDecl " ,
( char * ) " siO " , name , type , obj ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonUnparsedEntityDecl ( void * user_data ,
const xmlChar * name ,
const xmlChar * publicId ,
const xmlChar * systemId ,
const xmlChar * notationName )
{
PyObject * handler ;
PyObject * result ;
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " unparsedEntityDecl " ) ) {
result =
PyObject_CallMethod ( handler , ( char * ) " unparsedEntityDecl " ,
( char * ) " ssss " , name , publicId , systemId ,
notationName ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static void
pythonInternalSubset ( void * user_data , const xmlChar * name ,
const xmlChar * ExternalID , const xmlChar * SystemID )
{
PyObject * handler ;
PyObject * result ;
2002-02-12 13:46:21 +00:00
# ifdef DEBUG_SAX
printf ( " pythonInternalSubset(%s, %s, %s) called \n " ,
2002-03-15 22:24:56 +00:00
name , ExternalID , SystemID ) ;
2002-02-12 13:46:21 +00:00
# endif
2002-02-04 14:07:26 +00:00
handler = ( PyObject * ) user_data ;
2002-03-15 22:24:56 +00:00
if ( PyObject_HasAttrString ( handler , ( char * ) " internalSubset " ) ) {
result = PyObject_CallMethod ( handler , ( char * ) " internalSubset " ,
( char * ) " sss " , name , ExternalID ,
SystemID ) ;
if ( PyErr_Occurred ( ) )
PyErr_Print ( ) ;
2002-02-04 14:07:26 +00:00
Py_XDECREF ( result ) ;
}
}
static xmlSAXHandler pythonSaxHandler = {
pythonInternalSubset ,
2002-03-15 22:24:56 +00:00
NULL , /* TODO pythonIsStandalone, */
NULL , /* TODO pythonHasInternalSubset, */
NULL , /* TODO pythonHasExternalSubset, */
NULL , /* TODO pythonResolveEntity, */
NULL , /* TODO pythonGetEntity, */
2002-02-04 14:07:26 +00:00
pythonEntityDecl ,
pythonNotationDecl ,
pythonAttributeDecl ,
pythonElementDecl ,
pythonUnparsedEntityDecl ,
2002-03-15 22:24:56 +00:00
NULL , /* OBSOLETED pythonSetDocumentLocator, */
2002-02-04 14:07:26 +00:00
pythonStartDocument ,
pythonEndDocument ,
pythonStartElement ,
pythonEndElement ,
pythonReference ,
pythonCharacters ,
pythonIgnorableWhitespace ,
pythonProcessingInstruction ,
pythonComment ,
pythonWarning ,
pythonError ,
pythonFatalError ,
2002-03-15 22:24:56 +00:00
NULL , /* TODO pythonGetParameterEntity, */
2002-02-04 14:07:26 +00:00
pythonCdataBlock ,
pythonExternalSubset ,
2003-09-29 13:20:24 +00:00
1 ,
2019-09-30 17:04:54 +02:00
NULL , /* TODO migrate to SAX2 */
2003-09-29 13:20:24 +00:00
NULL ,
2003-10-18 04:53:14 +00:00
NULL ,
2003-09-29 13:20:24 +00:00
NULL
2002-02-04 14:07:26 +00:00
} ;
2002-01-30 16:37:32 +00:00
2002-02-03 15:08:05 +00:00
/************************************************************************
* *
* Handling of specific parser context *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-31 20:29:19 +00:00
PyObject *
2002-03-15 22:24:56 +00:00
libxml_xmlCreatePushParser ( ATTRIBUTE_UNUSED PyObject * self ,
PyObject * args )
{
const char * chunk ;
2002-02-03 15:08:05 +00:00
int size ;
2002-03-15 22:24:56 +00:00
const char * URI ;
2002-02-04 14:07:26 +00:00
PyObject * pyobj_SAX = NULL ;
2002-02-03 15:08:05 +00:00
xmlSAXHandlerPtr SAX = NULL ;
xmlParserCtxtPtr ret ;
PyObject * pyret ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple
( args , ( char * ) " Oziz:xmlCreatePushParser " , & pyobj_SAX , & chunk ,
& size , & URI ) )
return ( NULL ) ;
2002-01-31 20:29:19 +00:00
2002-03-05 15:41:29 +00:00
# ifdef DEBUG
2002-02-03 15:08:05 +00:00
printf ( " libxml_xmlCreatePushParser(%p, %s, %d, %s) called \n " ,
2002-03-15 22:24:56 +00:00
pyobj_SAX , chunk , size , URI ) ;
2002-01-31 20:29:19 +00:00
# endif
2002-02-03 15:08:05 +00:00
if ( pyobj_SAX ! = Py_None ) {
2002-03-15 22:24:56 +00:00
SAX = & pythonSaxHandler ;
Py_INCREF ( pyobj_SAX ) ;
/* The reference is released in pythonEndDocument() */
2002-01-31 20:29:19 +00:00
}
2002-02-04 14:07:26 +00:00
ret = xmlCreatePushParserCtxt ( SAX , pyobj_SAX , chunk , size , URI ) ;
2002-02-03 15:08:05 +00:00
pyret = libxml_xmlParserCtxtPtrWrap ( ret ) ;
2002-03-15 22:24:56 +00:00
return ( pyret ) ;
2002-02-01 17:56:45 +00:00
}
2002-02-02 21:49:17 +00:00
2002-02-03 20:13:06 +00:00
PyObject *
2002-03-15 22:24:56 +00:00
libxml_htmlCreatePushParser ( ATTRIBUTE_UNUSED PyObject * self ,
PyObject * args )
{
2004-04-30 23:11:45 +00:00
# ifdef LIBXML_HTML_ENABLED
2002-03-15 22:24:56 +00:00
const char * chunk ;
2002-02-03 20:13:06 +00:00
int size ;
2002-03-15 22:24:56 +00:00
const char * URI ;
2002-02-04 14:07:26 +00:00
PyObject * pyobj_SAX = NULL ;
2002-02-03 20:13:06 +00:00
xmlSAXHandlerPtr SAX = NULL ;
xmlParserCtxtPtr ret ;
PyObject * pyret ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple
( args , ( char * ) " Oziz:htmlCreatePushParser " , & pyobj_SAX , & chunk ,
& size , & URI ) )
return ( NULL ) ;
2002-02-03 20:13:06 +00:00
2002-03-05 15:41:29 +00:00
# ifdef DEBUG
2002-02-03 20:13:06 +00:00
printf ( " libxml_htmlCreatePushParser(%p, %s, %d, %s) called \n " ,
2002-03-15 22:24:56 +00:00
pyobj_SAX , chunk , size , URI ) ;
2002-02-03 20:13:06 +00:00
# endif
if ( pyobj_SAX ! = Py_None ) {
2002-03-15 22:24:56 +00:00
SAX = & pythonSaxHandler ;
Py_INCREF ( pyobj_SAX ) ;
/* The reference is released in pythonEndDocument() */
2002-02-03 20:13:06 +00:00
}
2002-02-04 14:07:26 +00:00
ret = htmlCreatePushParserCtxt ( SAX , pyobj_SAX , chunk , size , URI ,
2002-03-15 22:24:56 +00:00
XML_CHAR_ENCODING_NONE ) ;
2002-02-03 20:13:06 +00:00
pyret = libxml_xmlParserCtxtPtrWrap ( ret ) ;
2002-03-15 22:24:56 +00:00
return ( pyret ) ;
2004-04-30 23:11:45 +00:00
# else
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
# endif /* LIBXML_HTML_ENABLED */
2002-02-03 20:13:06 +00:00
}
2002-03-05 15:41:29 +00:00
PyObject *
2002-03-15 22:24:56 +00:00
libxml_xmlSAXParseFile ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
2012-07-03 14:13:59 +09:00
# ifdef LIBXML_SAX1_ENABLED
2002-03-05 15:41:29 +00:00
int recover ;
2002-03-15 22:24:56 +00:00
const char * URI ;
2002-03-05 15:41:29 +00:00
PyObject * pyobj_SAX = NULL ;
xmlSAXHandlerPtr SAX = NULL ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " Osi:xmlSAXParseFile " , & pyobj_SAX ,
& URI , & recover ) )
return ( NULL ) ;
2002-03-05 15:41:29 +00:00
# ifdef DEBUG
printf ( " libxml_xmlSAXParseFile(%p, %s, %d) called \n " ,
2002-03-15 22:24:56 +00:00
pyobj_SAX , URI , recover ) ;
2002-03-05 15:41:29 +00:00
# endif
if ( pyobj_SAX = = Py_None ) {
2002-03-15 22:24:56 +00:00
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
2002-03-05 15:41:29 +00:00
}
SAX = & pythonSaxHandler ;
Py_INCREF ( pyobj_SAX ) ;
/* The reference is released in pythonEndDocument() */
2005-03-31 11:06:29 +00:00
xmlSAXUserParseFile ( SAX , pyobj_SAX , URI ) ;
2012-07-03 14:13:59 +09:00
# endif /* LIBXML_SAX1_ENABLED */
2002-03-05 15:41:29 +00:00
Py_INCREF ( Py_None ) ;
2002-03-15 22:24:56 +00:00
return ( Py_None ) ;
2002-03-05 15:41:29 +00:00
}
PyObject *
2002-03-15 22:24:56 +00:00
libxml_htmlSAXParseFile ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
2004-04-30 23:11:45 +00:00
# ifdef LIBXML_HTML_ENABLED
2002-03-15 22:24:56 +00:00
const char * URI ;
const char * encoding ;
2002-03-05 15:41:29 +00:00
PyObject * pyobj_SAX = NULL ;
xmlSAXHandlerPtr SAX = NULL ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple
( args , ( char * ) " Osz:htmlSAXParseFile " , & pyobj_SAX , & URI ,
& encoding ) )
return ( NULL ) ;
2002-03-05 15:41:29 +00:00
# ifdef DEBUG
printf ( " libxml_htmlSAXParseFile(%p, %s, %s) called \n " ,
2002-03-15 22:24:56 +00:00
pyobj_SAX , URI , encoding ) ;
2002-03-05 15:41:29 +00:00
# endif
if ( pyobj_SAX = = Py_None ) {
2002-03-15 22:24:56 +00:00
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
2002-03-05 15:41:29 +00:00
}
SAX = & pythonSaxHandler ;
Py_INCREF ( pyobj_SAX ) ;
/* The reference is released in pythonEndDocument() */
htmlSAXParseFile ( URI , encoding , SAX , pyobj_SAX ) ;
Py_INCREF ( Py_None ) ;
2002-03-15 22:24:56 +00:00
return ( Py_None ) ;
2004-04-30 23:11:45 +00:00
# else
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
# endif /* LIBXML_HTML_ENABLED */
2002-03-05 15:41:29 +00:00
}
2002-02-02 21:49:17 +00:00
/************************************************************************
* *
* Error message callback *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static PyObject * libxml_xmlPythonErrorFuncHandler = NULL ;
static PyObject * libxml_xmlPythonErrorFuncCtxt = NULL ;
2003-01-14 11:42:39 +00:00
/* helper to build a xmlMalloc'ed string from a format and va_list */
2004-10-22 11:05:37 +00:00
/*
* disabled the loop , the repeated call to vsnprintf without reset of ap
* in case the initial buffer was too small segfaulted on x86_64
* we now directly vsnprintf on a large buffer .
*/
2003-01-14 11:42:39 +00:00
static char *
libxml_buildMessage ( const char * msg , va_list ap )
2002-03-15 22:24:56 +00:00
{
int chars ;
2003-01-14 11:42:39 +00:00
char * str ;
2004-10-22 11:05:37 +00:00
str = ( char * ) xmlMalloc ( 1000 ) ;
2003-01-14 11:42:39 +00:00
if ( str = = NULL )
return NULL ;
2004-10-22 11:05:37 +00:00
chars = vsnprintf ( str , 999 , msg , ap ) ;
if ( chars > = 998 )
str [ 999 ] = 0 ;
2003-01-14 11:42:39 +00:00
return str ;
}
static void
libxml_xmlErrorFuncHandler ( ATTRIBUTE_UNUSED void * ctx , const char * msg ,
. . . )
{
2002-03-15 22:24:56 +00:00
va_list ap ;
2002-02-02 21:49:17 +00:00
PyObject * list ;
PyObject * message ;
PyObject * result ;
2004-10-22 11:05:37 +00:00
char str [ 1000 ] ;
2002-02-02 21:49:17 +00:00
# ifdef DEBUG_ERROR
printf ( " libxml_xmlErrorFuncHandler(%p, %s, ...) called \n " , ctx , msg ) ;
# endif
if ( libxml_xmlPythonErrorFuncHandler = = NULL ) {
2002-03-15 22:24:56 +00:00
va_start ( ap , msg ) ;
2003-09-17 20:07:28 +00:00
vfprintf ( stderr , msg , ap ) ;
2002-03-15 22:24:56 +00:00
va_end ( ap ) ;
2002-02-02 21:49:17 +00:00
} else {
2003-01-14 11:42:39 +00:00
va_start ( ap , msg ) ;
2004-10-22 11:05:37 +00:00
if ( vsnprintf ( str , 999 , msg , ap ) > = 998 )
str [ 999 ] = 0 ;
2003-01-14 11:42:39 +00:00
va_end ( ap ) ;
2002-03-15 22:24:56 +00:00
list = PyTuple_New ( 2 ) ;
PyTuple_SetItem ( list , 0 , libxml_xmlPythonErrorFuncCtxt ) ;
Py_XINCREF ( libxml_xmlPythonErrorFuncCtxt ) ;
2004-10-22 11:05:37 +00:00
message = libxml_charPtrConstWrap ( str ) ;
2002-03-15 22:24:56 +00:00
PyTuple_SetItem ( list , 1 , message ) ;
result = PyEval_CallObject ( libxml_xmlPythonErrorFuncHandler , list ) ;
Py_XDECREF ( list ) ;
Py_XDECREF ( result ) ;
2002-02-02 21:49:17 +00:00
}
}
static void
2002-03-15 22:24:56 +00:00
libxml_xmlErrorInitialize ( void )
{
2002-02-02 21:49:17 +00:00
# ifdef DEBUG_ERROR
printf ( " libxml_xmlErrorInitialize() called \n " ) ;
# endif
xmlSetGenericErrorFunc ( NULL , libxml_xmlErrorFuncHandler ) ;
2003-05-15 22:11:36 +00:00
xmlThrDefSetGenericErrorFunc ( NULL , libxml_xmlErrorFuncHandler ) ;
2002-02-02 21:49:17 +00:00
}
2003-07-29 20:44:53 +00:00
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_xmlRegisterErrorHandler ( ATTRIBUTE_UNUSED PyObject * self ,
PyObject * args )
{
2002-02-02 21:49:17 +00:00
PyObject * py_retval ;
PyObject * pyobj_f ;
PyObject * pyobj_ctx ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple
( args , ( char * ) " OO:xmlRegisterErrorHandler " , & pyobj_f ,
& pyobj_ctx ) )
return ( NULL ) ;
2002-02-02 21:49:17 +00:00
# ifdef DEBUG_ERROR
2004-07-03 23:28:52 +00:00
printf ( " libxml_xmlRegisterErrorHandler(%p, %p) called \n " , pyobj_ctx ,
2002-03-15 22:24:56 +00:00
pyobj_f ) ;
2002-02-02 21:49:17 +00:00
# endif
if ( libxml_xmlPythonErrorFuncHandler ! = NULL ) {
2002-03-15 22:24:56 +00:00
Py_XDECREF ( libxml_xmlPythonErrorFuncHandler ) ;
2002-02-02 21:49:17 +00:00
}
if ( libxml_xmlPythonErrorFuncCtxt ! = NULL ) {
2002-03-15 22:24:56 +00:00
Py_XDECREF ( libxml_xmlPythonErrorFuncCtxt ) ;
2002-02-02 21:49:17 +00:00
}
Py_XINCREF ( pyobj_ctx ) ;
Py_XINCREF ( pyobj_f ) ;
/* TODO: check f is a function ! */
libxml_xmlPythonErrorFuncHandler = pyobj_f ;
libxml_xmlPythonErrorFuncCtxt = pyobj_ctx ;
py_retval = libxml_intWrap ( 1 ) ;
2002-03-15 22:24:56 +00:00
return ( py_retval ) ;
2002-02-02 21:49:17 +00:00
}
2002-03-15 22:24:56 +00:00
2003-01-14 11:42:39 +00:00
/************************************************************************
* *
* Per parserCtxt error handler *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-20 21:26:34 +00:00
typedef struct
{
PyObject * f ;
PyObject * arg ;
} xmlParserCtxtPyCtxt ;
typedef xmlParserCtxtPyCtxt * xmlParserCtxtPyCtxtPtr ;
static void
libxml_xmlParserCtxtGenericErrorFuncHandler ( void * ctx , int severity , char * str )
2003-01-14 11:42:39 +00:00
{
PyObject * list ;
PyObject * result ;
2003-01-20 21:26:34 +00:00
xmlParserCtxtPtr ctxt ;
2003-01-14 11:42:39 +00:00
xmlParserCtxtPyCtxtPtr pyCtxt ;
# ifdef DEBUG_ERROR
2004-11-10 11:55:47 +00:00
printf ( " libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called \n " , ctx , str ) ;
2003-01-14 11:42:39 +00:00
# endif
2003-01-20 21:26:34 +00:00
ctxt = ( xmlParserCtxtPtr ) ctx ;
pyCtxt = ( xmlParserCtxtPyCtxtPtr ) ctxt - > _private ;
2003-01-14 11:42:39 +00:00
2003-01-20 21:26:34 +00:00
list = PyTuple_New ( 4 ) ;
PyTuple_SetItem ( list , 0 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
PyTuple_SetItem ( list , 1 , libxml_charPtrWrap ( str ) ) ;
PyTuple_SetItem ( list , 2 , libxml_intWrap ( severity ) ) ;
PyTuple_SetItem ( list , 3 , Py_None ) ;
Py_INCREF ( Py_None ) ;
result = PyEval_CallObject ( pyCtxt - > f , list ) ;
if ( result = = NULL )
{
/* TODO: manage for the exception to be propagated... */
PyErr_Print ( ) ;
2003-01-14 11:42:39 +00:00
}
2003-01-20 21:26:34 +00:00
Py_XDECREF ( list ) ;
Py_XDECREF ( result ) ;
}
static void
libxml_xmlParserCtxtErrorFuncHandler ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlParserCtxtGenericErrorFuncHandler ( ctx , XML_PARSER_SEVERITY_ERROR , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
}
static void
libxml_xmlParserCtxtWarningFuncHandler ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlParserCtxtGenericErrorFuncHandler ( ctx , XML_PARSER_SEVERITY_WARNING , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
}
static void
libxml_xmlParserCtxtValidityErrorFuncHandler ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlParserCtxtGenericErrorFuncHandler ( ctx , XML_PARSER_SEVERITY_VALIDITY_ERROR , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
}
static void
libxml_xmlParserCtxtValidityWarningFuncHandler ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlParserCtxtGenericErrorFuncHandler ( ctx , XML_PARSER_SEVERITY_VALIDITY_WARNING , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
2003-01-14 11:42:39 +00:00
}
2003-07-29 20:44:53 +00:00
static PyObject *
2003-01-20 21:26:34 +00:00
libxml_xmlParserCtxtSetErrorHandler ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2003-01-14 11:42:39 +00:00
{
PyObject * py_retval ;
xmlParserCtxtPtr ctxt ;
2003-01-14 14:40:25 +00:00
xmlParserCtxtPyCtxtPtr pyCtxt ;
2003-01-14 11:42:39 +00:00
PyObject * pyobj_ctxt ;
PyObject * pyobj_f ;
PyObject * pyobj_arg ;
2003-01-20 21:26:34 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " OOO:xmlParserCtxtSetErrorHandler " ,
2003-01-14 11:42:39 +00:00
& pyobj_ctxt , & pyobj_f , & pyobj_arg ) )
return ( NULL ) ;
ctxt = ( xmlParserCtxtPtr ) PyparserCtxt_Get ( pyobj_ctxt ) ;
if ( ctxt - > _private = = NULL ) {
pyCtxt = xmlMalloc ( sizeof ( xmlParserCtxtPyCtxt ) ) ;
if ( pyCtxt = = NULL ) {
py_retval = libxml_intWrap ( - 1 ) ;
return ( py_retval ) ;
}
memset ( pyCtxt , 0 , sizeof ( xmlParserCtxtPyCtxt ) ) ;
ctxt - > _private = pyCtxt ;
}
2003-01-14 14:40:25 +00:00
else {
2003-01-20 21:26:34 +00:00
pyCtxt = ( xmlParserCtxtPyCtxtPtr ) ctxt - > _private ;
2003-01-14 14:40:25 +00:00
}
2003-01-14 11:42:39 +00:00
/* TODO: check f is a function ! */
2003-01-20 21:26:34 +00:00
Py_XDECREF ( pyCtxt - > f ) ;
2003-01-14 11:42:39 +00:00
Py_XINCREF ( pyobj_f ) ;
2003-01-20 21:26:34 +00:00
pyCtxt - > f = pyobj_f ;
Py_XDECREF ( pyCtxt - > arg ) ;
2003-01-14 11:42:39 +00:00
Py_XINCREF ( pyobj_arg ) ;
2003-01-20 21:26:34 +00:00
pyCtxt - > arg = pyobj_arg ;
2003-01-14 11:42:39 +00:00
2003-01-20 21:26:34 +00:00
if ( pyobj_f ! = Py_None ) {
ctxt - > sax - > error = libxml_xmlParserCtxtErrorFuncHandler ;
ctxt - > sax - > warning = libxml_xmlParserCtxtWarningFuncHandler ;
ctxt - > vctxt . error = libxml_xmlParserCtxtValidityErrorFuncHandler ;
ctxt - > vctxt . warning = libxml_xmlParserCtxtValidityWarningFuncHandler ;
}
else {
ctxt - > sax - > error = xmlParserError ;
ctxt - > vctxt . error = xmlParserValidityError ;
ctxt - > sax - > warning = xmlParserWarning ;
ctxt - > vctxt . warning = xmlParserValidityWarning ;
}
2003-01-14 11:42:39 +00:00
py_retval = libxml_intWrap ( 1 ) ;
return ( py_retval ) ;
}
2003-07-29 20:44:53 +00:00
static PyObject *
2003-01-20 21:26:34 +00:00
libxml_xmlParserCtxtGetErrorHandler ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2003-01-14 11:42:39 +00:00
{
2003-01-20 21:26:34 +00:00
PyObject * py_retval ;
xmlParserCtxtPtr ctxt ;
2003-01-14 11:42:39 +00:00
xmlParserCtxtPyCtxtPtr pyCtxt ;
2003-01-20 21:26:34 +00:00
PyObject * pyobj_ctxt ;
2003-01-14 11:42:39 +00:00
2003-01-20 21:26:34 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlParserCtxtGetErrorHandler " ,
& pyobj_ctxt ) )
return ( NULL ) ;
ctxt = ( xmlParserCtxtPtr ) PyparserCtxt_Get ( pyobj_ctxt ) ;
py_retval = PyTuple_New ( 2 ) ;
if ( ctxt - > _private ! = NULL ) {
pyCtxt = ( xmlParserCtxtPyCtxtPtr ) ctxt - > _private ;
2003-01-14 11:42:39 +00:00
2003-01-20 21:26:34 +00:00
PyTuple_SetItem ( py_retval , 0 , pyCtxt - > f ) ;
Py_XINCREF ( pyCtxt - > f ) ;
PyTuple_SetItem ( py_retval , 1 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
}
else {
/* no python error handler registered */
PyTuple_SetItem ( py_retval , 0 , Py_None ) ;
Py_XINCREF ( Py_None ) ;
PyTuple_SetItem ( py_retval , 1 , Py_None ) ;
Py_XINCREF ( Py_None ) ;
2003-01-14 11:42:39 +00:00
}
2003-01-20 21:26:34 +00:00
return ( py_retval ) ;
2003-01-14 11:42:39 +00:00
}
2003-07-29 20:44:53 +00:00
static PyObject *
2003-01-20 21:26:34 +00:00
libxml_xmlFreeParserCtxt ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args ) {
2003-01-14 11:42:39 +00:00
xmlParserCtxtPtr ctxt ;
PyObject * pyobj_ctxt ;
2003-01-20 21:26:34 +00:00
xmlParserCtxtPyCtxtPtr pyCtxt ;
2003-01-14 11:42:39 +00:00
2003-01-20 21:26:34 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlFreeParserCtxt " , & pyobj_ctxt ) )
2003-01-14 11:42:39 +00:00
return ( NULL ) ;
ctxt = ( xmlParserCtxtPtr ) PyparserCtxt_Get ( pyobj_ctxt ) ;
2003-01-20 21:26:34 +00:00
if ( ctxt ! = NULL ) {
pyCtxt = ( xmlParserCtxtPyCtxtPtr ) ( ( xmlParserCtxtPtr ) ctxt ) - > _private ;
if ( pyCtxt ) {
Py_XDECREF ( pyCtxt - > f ) ;
Py_XDECREF ( pyCtxt - > arg ) ;
xmlFree ( pyCtxt ) ;
2003-01-14 14:40:25 +00:00
}
2003-01-20 21:26:34 +00:00
xmlFreeParserCtxt ( ctxt ) ;
2003-01-14 14:40:25 +00:00
}
2003-01-14 11:42:39 +00:00
2003-01-20 21:26:34 +00:00
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
2003-01-14 11:42:39 +00:00
}
2004-11-10 11:55:47 +00:00
/***
* xmlValidCtxt stuff
*/
typedef struct
{
PyObject * warn ;
PyObject * error ;
PyObject * arg ;
} xmlValidCtxtPyCtxt ;
typedef xmlValidCtxtPyCtxt * xmlValidCtxtPyCtxtPtr ;
static void
2011-05-09 12:52:28 +03:00
libxml_xmlValidCtxtGenericErrorFuncHandler ( void * ctx , ATTRIBUTE_UNUSED int severity , char * str )
2004-11-10 11:55:47 +00:00
{
PyObject * list ;
PyObject * result ;
xmlValidCtxtPyCtxtPtr pyCtxt ;
# ifdef DEBUG_ERROR
printf ( " libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called \n " , ctx , severity , str ) ;
# endif
pyCtxt = ( xmlValidCtxtPyCtxtPtr ) ctx ;
list = PyTuple_New ( 2 ) ;
PyTuple_SetItem ( list , 0 , libxml_charPtrWrap ( str ) ) ;
PyTuple_SetItem ( list , 1 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
result = PyEval_CallObject ( pyCtxt - > error , list ) ;
if ( result = = NULL )
{
/* TODO: manage for the exception to be propagated... */
PyErr_Print ( ) ;
}
Py_XDECREF ( list ) ;
Py_XDECREF ( result ) ;
}
static void
2011-05-09 12:52:28 +03:00
libxml_xmlValidCtxtGenericWarningFuncHandler ( void * ctx , ATTRIBUTE_UNUSED int severity , char * str )
2004-11-10 11:55:47 +00:00
{
PyObject * list ;
PyObject * result ;
xmlValidCtxtPyCtxtPtr pyCtxt ;
# ifdef DEBUG_ERROR
printf ( " libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called \n " , ctx , severity , str ) ;
# endif
pyCtxt = ( xmlValidCtxtPyCtxtPtr ) ctx ;
list = PyTuple_New ( 2 ) ;
PyTuple_SetItem ( list , 0 , libxml_charPtrWrap ( str ) ) ;
PyTuple_SetItem ( list , 1 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
result = PyEval_CallObject ( pyCtxt - > warn , list ) ;
if ( result = = NULL )
{
/* TODO: manage for the exception to be propagated... */
PyErr_Print ( ) ;
}
Py_XDECREF ( list ) ;
Py_XDECREF ( result ) ;
}
static void
libxml_xmlValidCtxtErrorFuncHandler ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlValidCtxtGenericErrorFuncHandler ( ctx , XML_PARSER_SEVERITY_VALIDITY_ERROR , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
}
static void
libxml_xmlValidCtxtWarningFuncHandler ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlValidCtxtGenericWarningFuncHandler ( ctx , XML_PARSER_SEVERITY_VALIDITY_WARNING , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
}
static PyObject *
libxml_xmlSetValidErrors ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
PyObject * py_retval ;
PyObject * pyobj_error ;
PyObject * pyobj_warn ;
PyObject * pyobj_ctx ;
PyObject * pyobj_arg = Py_None ;
xmlValidCtxtPtr ctxt ;
xmlValidCtxtPyCtxtPtr pyCtxt ;
if ( ! PyArg_ParseTuple
( args , ( char * ) " OOO|O:xmlSetValidErrors " , & pyobj_ctx , & pyobj_error , & pyobj_warn , & pyobj_arg ) )
return ( NULL ) ;
# ifdef DEBUG_ERROR
printf ( " libxml_xmlSetValidErrors(%p, %p, %p) called \n " , pyobj_ctx , pyobj_error , pyobj_warn ) ;
# endif
ctxt = PyValidCtxt_Get ( pyobj_ctx ) ;
pyCtxt = xmlMalloc ( sizeof ( xmlValidCtxtPyCtxt ) ) ;
if ( pyCtxt = = NULL ) {
py_retval = libxml_intWrap ( - 1 ) ;
return ( py_retval ) ;
}
memset ( pyCtxt , 0 , sizeof ( xmlValidCtxtPyCtxt ) ) ;
/* TODO: check warn and error is a function ! */
Py_XDECREF ( pyCtxt - > error ) ;
Py_XINCREF ( pyobj_error ) ;
pyCtxt - > error = pyobj_error ;
Py_XDECREF ( pyCtxt - > warn ) ;
Py_XINCREF ( pyobj_warn ) ;
pyCtxt - > warn = pyobj_warn ;
Py_XDECREF ( pyCtxt - > arg ) ;
Py_XINCREF ( pyobj_arg ) ;
pyCtxt - > arg = pyobj_arg ;
ctxt - > error = libxml_xmlValidCtxtErrorFuncHandler ;
ctxt - > warning = libxml_xmlValidCtxtWarningFuncHandler ;
ctxt - > userData = pyCtxt ;
py_retval = libxml_intWrap ( 1 ) ;
return ( py_retval ) ;
}
2005-03-02 10:47:41 +00:00
2005-03-30 07:40:35 +00:00
static PyObject *
2005-03-02 10:47:41 +00:00
libxml_xmlFreeValidCtxt ( PyObject * self ATTRIBUTE_UNUSED , PyObject * args ) {
xmlValidCtxtPtr cur ;
xmlValidCtxtPyCtxtPtr pyCtxt ;
PyObject * pyobj_cur ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlFreeValidCtxt " , & pyobj_cur ) )
return ( NULL ) ;
cur = ( xmlValidCtxtPtr ) PyValidCtxt_Get ( pyobj_cur ) ;
pyCtxt = ( xmlValidCtxtPyCtxtPtr ) ( cur - > userData ) ;
if ( pyCtxt ! = NULL )
{
Py_XDECREF ( pyCtxt - > error ) ;
Py_XDECREF ( pyCtxt - > warn ) ;
Py_XDECREF ( pyCtxt - > arg ) ;
xmlFree ( pyCtxt ) ;
}
xmlFreeValidCtxt ( cur ) ;
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2008-05-12 12:58:46 +00:00
# ifdef LIBXML_READER_ENABLED
2003-01-16 22:45:08 +00:00
/************************************************************************
* *
* Per xmlTextReader error handler *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
typedef struct
{
PyObject * f ;
PyObject * arg ;
} xmlTextReaderPyCtxt ;
typedef xmlTextReaderPyCtxt * xmlTextReaderPyCtxtPtr ;
static void
libxml_xmlTextReaderErrorCallback ( void * arg ,
const char * msg ,
2003-01-20 21:26:34 +00:00
int severity ,
xmlTextReaderLocatorPtr locator )
2003-01-16 22:45:08 +00:00
{
xmlTextReaderPyCtxt * pyCtxt = ( xmlTextReaderPyCtxt * ) arg ;
PyObject * list ;
PyObject * result ;
2003-01-20 21:26:34 +00:00
list = PyTuple_New ( 4 ) ;
2003-01-16 22:45:08 +00:00
PyTuple_SetItem ( list , 0 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
PyTuple_SetItem ( list , 1 , libxml_charPtrConstWrap ( msg ) ) ;
2003-01-20 21:26:34 +00:00
PyTuple_SetItem ( list , 2 , libxml_intWrap ( severity ) ) ;
PyTuple_SetItem ( list , 3 , libxml_xmlTextReaderLocatorPtrWrap ( locator ) ) ;
2003-01-16 22:45:08 +00:00
result = PyEval_CallObject ( pyCtxt - > f , list ) ;
if ( result = = NULL )
{
2003-01-20 21:26:34 +00:00
/* TODO: manage for the exception to be propagated... */
2003-01-16 22:45:08 +00:00
PyErr_Print ( ) ;
}
Py_XDECREF ( list ) ;
Py_XDECREF ( result ) ;
}
2003-07-29 20:44:53 +00:00
static PyObject *
2003-01-16 22:45:08 +00:00
libxml_xmlTextReaderSetErrorHandler ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
xmlTextReaderPtr reader ;
xmlTextReaderPyCtxtPtr pyCtxt ;
xmlTextReaderErrorFunc f ;
void * arg ;
PyObject * pyobj_reader ;
PyObject * pyobj_f ;
PyObject * pyobj_arg ;
PyObject * py_retval ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " OOO:xmlTextReaderSetErrorHandler " , & pyobj_reader , & pyobj_f , & pyobj_arg ) )
return ( NULL ) ;
reader = ( xmlTextReaderPtr ) PyxmlTextReader_Get ( pyobj_reader ) ;
/* clear previous error handler */
xmlTextReaderGetErrorHandler ( reader , & f , & arg ) ;
if ( arg ! = NULL ) {
2003-07-29 20:44:53 +00:00
if ( f = = ( xmlTextReaderErrorFunc ) libxml_xmlTextReaderErrorCallback ) {
2003-01-16 22:45:08 +00:00
/* ok, it's our error handler! */
pyCtxt = ( xmlTextReaderPyCtxtPtr ) arg ;
Py_XDECREF ( pyCtxt - > f ) ;
Py_XDECREF ( pyCtxt - > arg ) ;
xmlFree ( pyCtxt ) ;
}
else {
/*
* there already an arg , and it ' s not ours ,
* there is definitely something wrong going on here . . .
* we don ' t know how to free it , so we bail out . . .
*/
py_retval = libxml_intWrap ( - 1 ) ;
return ( py_retval ) ;
}
}
xmlTextReaderSetErrorHandler ( reader , NULL , NULL ) ;
/* set new error handler */
if ( pyobj_f ! = Py_None )
{
pyCtxt = ( xmlTextReaderPyCtxtPtr ) xmlMalloc ( sizeof ( xmlTextReaderPyCtxt ) ) ;
if ( pyCtxt = = NULL ) {
py_retval = libxml_intWrap ( - 1 ) ;
return ( py_retval ) ;
}
Py_XINCREF ( pyobj_f ) ;
pyCtxt - > f = pyobj_f ;
Py_XINCREF ( pyobj_arg ) ;
pyCtxt - > arg = pyobj_arg ;
2003-07-29 20:44:53 +00:00
xmlTextReaderSetErrorHandler ( reader ,
( xmlTextReaderErrorFunc ) libxml_xmlTextReaderErrorCallback ,
pyCtxt ) ;
2003-01-16 22:45:08 +00:00
}
py_retval = libxml_intWrap ( 1 ) ;
return ( py_retval ) ;
}
2003-07-29 20:44:53 +00:00
static PyObject *
2003-01-16 22:45:08 +00:00
libxml_xmlTextReaderGetErrorHandler ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
xmlTextReaderPtr reader ;
xmlTextReaderPyCtxtPtr pyCtxt ;
xmlTextReaderErrorFunc f ;
void * arg ;
PyObject * pyobj_reader ;
PyObject * py_retval ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlTextReaderSetErrorHandler " , & pyobj_reader ) )
return ( NULL ) ;
reader = ( xmlTextReaderPtr ) PyxmlTextReader_Get ( pyobj_reader ) ;
xmlTextReaderGetErrorHandler ( reader , & f , & arg ) ;
py_retval = PyTuple_New ( 2 ) ;
2003-07-29 20:44:53 +00:00
if ( f = = ( xmlTextReaderErrorFunc ) libxml_xmlTextReaderErrorCallback ) {
2003-01-16 22:45:08 +00:00
/* ok, it's our error handler! */
pyCtxt = ( xmlTextReaderPyCtxtPtr ) arg ;
PyTuple_SetItem ( py_retval , 0 , pyCtxt - > f ) ;
Py_XINCREF ( pyCtxt - > f ) ;
PyTuple_SetItem ( py_retval , 1 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
}
else
{
/* f is null or it's not our error handler */
PyTuple_SetItem ( py_retval , 0 , Py_None ) ;
Py_XINCREF ( Py_None ) ;
PyTuple_SetItem ( py_retval , 1 , Py_None ) ;
Py_XINCREF ( Py_None ) ;
}
return ( py_retval ) ;
}
2003-07-29 20:44:53 +00:00
static PyObject *
2003-01-16 22:45:08 +00:00
libxml_xmlFreeTextReader ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args ) {
xmlTextReaderPtr reader ;
PyObject * pyobj_reader ;
xmlTextReaderPyCtxtPtr pyCtxt ;
xmlTextReaderErrorFunc f ;
void * arg ;
2003-10-31 10:36:03 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlFreeTextReader " , & pyobj_reader ) )
return ( NULL ) ;
2013-03-29 13:46:24 +08:00
if ( ! PyCapsule_CheckExact ( pyobj_reader ) ) {
2003-10-30 13:12:43 +00:00
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2003-01-16 22:45:08 +00:00
reader = ( xmlTextReaderPtr ) PyxmlTextReader_Get ( pyobj_reader ) ;
2003-10-30 13:12:43 +00:00
if ( reader = = NULL ) {
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2003-01-16 22:45:08 +00:00
xmlTextReaderGetErrorHandler ( reader , & f , & arg ) ;
if ( arg ! = NULL ) {
2003-07-29 20:44:53 +00:00
if ( f = = ( xmlTextReaderErrorFunc ) libxml_xmlTextReaderErrorCallback ) {
2003-01-16 22:45:08 +00:00
/* ok, it's our error handler! */
pyCtxt = ( xmlTextReaderPyCtxtPtr ) arg ;
Py_XDECREF ( pyCtxt - > f ) ;
Py_XDECREF ( pyCtxt - > arg ) ;
xmlFree ( pyCtxt ) ;
}
/*
* else , something wrong happened , because the error handler is
* not owned by the python bindings . . .
*/
}
xmlFreeTextReader ( reader ) ;
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2008-05-12 12:58:46 +00:00
# endif
2003-01-16 22:45:08 +00:00
2002-02-01 17:56:45 +00:00
/************************************************************************
* *
* XPath extensions *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void
2002-03-15 22:24:56 +00:00
libxml_xmlXPathFuncCallback ( xmlXPathParserContextPtr ctxt , int nargs )
{
2002-02-01 17:56:45 +00:00
PyObject * list , * cur , * result ;
xmlXPathObjectPtr obj ;
2002-02-06 16:06:58 +00:00
xmlXPathContextPtr rctxt ;
PyObject * current_function = NULL ;
const xmlChar * name ;
const xmlChar * ns_uri ;
2002-02-01 17:56:45 +00:00
int i ;
2002-02-06 16:06:58 +00:00
if ( ctxt = = NULL )
2002-03-15 22:24:56 +00:00
return ;
2002-02-06 16:06:58 +00:00
rctxt = ctxt - > context ;
if ( rctxt = = NULL )
2002-03-15 22:24:56 +00:00
return ;
2002-02-06 16:06:58 +00:00
name = rctxt - > function ;
ns_uri = rctxt - > functionURI ;
2002-02-01 17:56:45 +00:00
# ifdef DEBUG_XPATH
2002-03-15 22:24:56 +00:00
printf ( " libxml_xmlXPathFuncCallback called name %s URI %s \n " , name ,
ns_uri ) ;
2002-02-01 17:56:45 +00:00
# endif
2002-02-06 16:06:58 +00:00
/*
* Find the function , it should be there it was there at lookup
*/
2002-03-15 22:24:56 +00:00
for ( i = 0 ; i < libxml_xpathCallbacksNb ; i + + ) {
if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
2004-07-03 23:28:52 +00:00
( xmlStrEqual ( name , ( * libxml_xpathCallbacks ) [ i ] . name ) ) & &
( xmlStrEqual ( ns_uri , ( * libxml_xpathCallbacks ) [ i ] . ns_uri ) ) ) {
current_function = ( * libxml_xpathCallbacks ) [ i ] . function ;
2002-03-15 22:24:56 +00:00
}
2002-02-06 16:06:58 +00:00
}
if ( current_function = = NULL ) {
2002-03-15 22:24:56 +00:00
printf
( " libxml_xmlXPathFuncCallback: internal error %s not found ! \n " ,
name ) ;
return ;
2002-02-06 16:06:58 +00:00
}
2002-02-08 13:28:40 +00:00
list = PyTuple_New ( nargs + 1 ) ;
PyTuple_SetItem ( list , 0 , libxml_xmlXPathParserContextPtrWrap ( ctxt ) ) ;
2004-01-25 20:01:35 +00:00
for ( i = nargs - 1 ; i > = 0 ; i - - ) {
2002-03-15 22:24:56 +00:00
obj = valuePop ( ctxt ) ;
cur = libxml_xmlXPathObjectPtrWrap ( obj ) ;
PyTuple_SetItem ( list , i + 1 , cur ) ;
2002-02-01 17:56:45 +00:00
}
result = PyEval_CallObject ( current_function , list ) ;
Py_DECREF ( list ) ;
obj = libxml_xmlXPathObjectPtrConvert ( result ) ;
valuePush ( ctxt , obj ) ;
}
static xmlXPathFunction
2002-03-15 22:24:56 +00:00
libxml_xmlXPathFuncLookupFunc ( void * ctxt , const xmlChar * name ,
const xmlChar * ns_uri )
{
2002-02-01 17:56:45 +00:00
int i ;
2002-03-15 22:24:56 +00:00
2002-02-01 17:56:45 +00:00
# ifdef DEBUG_XPATH
printf ( " libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called \n " ,
2002-03-15 22:24:56 +00:00
ctxt , name , ns_uri ) ;
2002-02-01 17:56:45 +00:00
# endif
2002-02-06 16:06:58 +00:00
/*
* This is called once only . The address is then stored in the
* XPath expression evaluation , the proper object to call can
* then still be found using the execution context function
* and functionURI fields .
*/
2002-03-15 22:24:56 +00:00
for ( i = 0 ; i < libxml_xpathCallbacksNb ; i + + ) {
2004-07-03 23:28:52 +00:00
if ( ( ctxt = = ( * libxml_xpathCallbacks ) [ i ] . ctx ) & &
( xmlStrEqual ( name , ( * libxml_xpathCallbacks ) [ i ] . name ) ) & &
( xmlStrEqual ( ns_uri , ( * libxml_xpathCallbacks ) [ i ] . ns_uri ) ) ) {
2002-03-15 22:24:56 +00:00
return ( libxml_xmlXPathFuncCallback ) ;
}
2002-02-01 17:56:45 +00:00
}
2002-03-15 22:24:56 +00:00
return ( NULL ) ;
2002-02-01 17:56:45 +00:00
}
static void
2002-03-15 22:24:56 +00:00
libxml_xpathCallbacksInitialize ( void )
{
2002-02-01 17:56:45 +00:00
int i ;
if ( libxml_xpathCallbacksInitialized ! = 0 )
2002-03-15 22:24:56 +00:00
return ;
2002-02-01 17:56:45 +00:00
# ifdef DEBUG_XPATH
printf ( " libxml_xpathCallbacksInitialized called \n " ) ;
# endif
2004-07-03 23:28:52 +00:00
libxml_xpathCallbacks = ( libxml_xpathCallbackArray * ) xmlMalloc (
libxml_xpathCallbacksAllocd * sizeof ( libxml_xpathCallback ) ) ;
2002-02-01 17:56:45 +00:00
2004-07-03 23:28:52 +00:00
for ( i = 0 ; i < libxml_xpathCallbacksAllocd ; i + + ) {
( * libxml_xpathCallbacks ) [ i ] . ctx = NULL ;
( * libxml_xpathCallbacks ) [ i ] . name = NULL ;
( * libxml_xpathCallbacks ) [ i ] . ns_uri = NULL ;
( * libxml_xpathCallbacks ) [ i ] . function = NULL ;
2002-02-01 17:56:45 +00:00
}
libxml_xpathCallbacksInitialized = 1 ;
}
PyObject *
2002-03-15 22:24:56 +00:00
libxml_xmlRegisterXPathFunction ( ATTRIBUTE_UNUSED PyObject * self ,
PyObject * args )
{
2002-02-01 17:56:45 +00:00
PyObject * py_retval ;
int c_retval = 0 ;
xmlChar * name ;
xmlChar * ns_uri ;
xmlXPathContextPtr ctx ;
PyObject * pyobj_ctx ;
PyObject * pyobj_f ;
int i ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple
( args , ( char * ) " OszO:registerXPathFunction " , & pyobj_ctx , & name ,
& ns_uri , & pyobj_f ) )
return ( NULL ) ;
2002-02-01 17:56:45 +00:00
ctx = ( xmlXPathContextPtr ) PyxmlXPathContext_Get ( pyobj_ctx ) ;
if ( libxml_xpathCallbacksInitialized = = 0 )
2002-03-15 22:24:56 +00:00
libxml_xpathCallbacksInitialize ( ) ;
2002-02-01 17:56:45 +00:00
xmlXPathRegisterFuncLookup ( ctx , libxml_xmlXPathFuncLookupFunc , ctx ) ;
if ( ( pyobj_ctx = = NULL ) | | ( name = = NULL ) | | ( pyobj_f = = NULL ) ) {
2002-03-15 22:24:56 +00:00
py_retval = libxml_intWrap ( - 1 ) ;
return ( py_retval ) ;
2002-02-01 17:56:45 +00:00
}
# ifdef DEBUG_XPATH
printf ( " libxml_registerXPathFunction(%p, %s, %s) called \n " ,
2002-03-15 22:24:56 +00:00
ctx , name , ns_uri ) ;
2002-02-01 17:56:45 +00:00
# endif
2002-03-15 22:24:56 +00:00
for ( i = 0 ; i < libxml_xpathCallbacksNb ; i + + ) {
2004-07-03 23:28:52 +00:00
if ( ( ctx = = ( * libxml_xpathCallbacks ) [ i ] . ctx ) & &
( xmlStrEqual ( name , ( * libxml_xpathCallbacks ) [ i ] . name ) ) & &
( xmlStrEqual ( ns_uri , ( * libxml_xpathCallbacks ) [ i ] . ns_uri ) ) ) {
2002-03-15 22:24:56 +00:00
Py_XINCREF ( pyobj_f ) ;
2004-07-03 23:28:52 +00:00
Py_XDECREF ( ( * libxml_xpathCallbacks ) [ i ] . function ) ;
( * libxml_xpathCallbacks ) [ i ] . function = pyobj_f ;
2002-03-15 22:24:56 +00:00
c_retval = 1 ;
goto done ;
}
2002-02-01 17:56:45 +00:00
}
2004-07-03 23:28:52 +00:00
if ( libxml_xpathCallbacksNb > = libxml_xpathCallbacksAllocd ) {
libxml_xpathCallbacksAllocd + = 10 ;
libxml_xpathCallbacks = ( libxml_xpathCallbackArray * ) xmlRealloc (
libxml_xpathCallbacks ,
libxml_xpathCallbacksAllocd * sizeof ( libxml_xpathCallback ) ) ;
}
i = libxml_xpathCallbacksNb + + ;
Py_XINCREF ( pyobj_f ) ;
( * libxml_xpathCallbacks ) [ i ] . ctx = ctx ;
( * libxml_xpathCallbacks ) [ i ] . name = xmlStrdup ( name ) ;
( * libxml_xpathCallbacks ) [ i ] . ns_uri = xmlStrdup ( ns_uri ) ;
( * libxml_xpathCallbacks ) [ i ] . function = pyobj_f ;
2002-03-15 22:24:56 +00:00
c_retval = 1 ;
2004-07-03 23:28:52 +00:00
2002-03-15 22:24:56 +00:00
done :
2002-02-01 17:56:45 +00:00
py_retval = libxml_intWrap ( ( int ) c_retval ) ;
2012-09-19 13:41:56 -04:00
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 ) ;
2002-03-15 22:24:56 +00:00
return ( py_retval ) ;
2002-02-01 17:56:45 +00:00
}
2002-01-30 16:37:32 +00:00
/************************************************************************
* *
* Global properties access *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_name ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2002-01-30 16:37:32 +00:00
{
PyObject * resultobj , * obj ;
xmlNodePtr cur ;
const xmlChar * res ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:name " , & obj ) )
2002-01-30 16:37:32 +00:00
return NULL ;
cur = PyxmlNode_Get ( obj ) ;
# ifdef DEBUG
printf ( " libxml_name: cur = %p type %d \n " , cur , cur - > type ) ;
# endif
2002-03-15 22:24:56 +00:00
switch ( cur - > type ) {
case XML_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# ifdef LIBXML_DOCB_ENABLED
2002-03-15 22:24:56 +00:00
case XML_DOCB_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# endif
2002-03-15 22:24:56 +00:00
case XML_HTML_DOCUMENT_NODE : {
xmlDocPtr doc = ( xmlDocPtr ) cur ;
res = doc - > URL ;
break ;
}
case XML_ATTRIBUTE_NODE : {
xmlAttrPtr attr = ( xmlAttrPtr ) cur ;
res = attr - > name ;
break ;
}
case XML_NAMESPACE_DECL : {
xmlNsPtr ns = ( xmlNsPtr ) cur ;
res = ns - > prefix ;
break ;
}
default :
res = cur - > name ;
break ;
2002-01-30 16:37:32 +00:00
}
2002-01-31 20:29:19 +00:00
resultobj = libxml_constxmlCharPtrWrap ( res ) ;
2002-01-30 16:37:32 +00:00
return resultobj ;
}
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_doc ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2002-01-30 16:37:32 +00:00
{
PyObject * resultobj , * obj ;
xmlNodePtr cur ;
xmlDocPtr res ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:doc " , & obj ) )
2002-01-30 16:37:32 +00:00
return NULL ;
cur = PyxmlNode_Get ( obj ) ;
# ifdef DEBUG
printf ( " libxml_doc: cur = %p \n " , cur ) ;
# endif
2002-03-15 22:24:56 +00:00
switch ( cur - > type ) {
case XML_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# ifdef LIBXML_DOCB_ENABLED
2002-03-15 22:24:56 +00:00
case XML_DOCB_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# endif
2002-03-15 22:24:56 +00:00
case XML_HTML_DOCUMENT_NODE :
res = NULL ;
break ;
case XML_ATTRIBUTE_NODE : {
xmlAttrPtr attr = ( xmlAttrPtr ) cur ;
res = attr - > doc ;
break ;
}
case XML_NAMESPACE_DECL :
res = NULL ;
break ;
default :
res = cur - > doc ;
break ;
2002-01-30 16:37:32 +00:00
}
resultobj = libxml_xmlDocPtrWrap ( res ) ;
return resultobj ;
}
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_properties ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2002-01-30 16:37:32 +00:00
{
PyObject * resultobj , * obj ;
2006-03-09 16:49:24 +00:00
xmlNodePtr cur ;
2002-01-30 16:37:32 +00:00
xmlAttrPtr res ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:properties " , & obj ) )
2002-01-30 16:37:32 +00:00
return NULL ;
cur = PyxmlNode_Get ( obj ) ;
2006-03-09 16:49:24 +00:00
if ( ( cur ! = NULL ) & & ( cur - > type = = XML_ELEMENT_NODE ) )
2002-03-15 22:24:56 +00:00
res = cur - > properties ;
2002-01-30 16:37:32 +00:00
else
2002-03-15 22:24:56 +00:00
res = NULL ;
2002-01-30 16:37:32 +00:00
resultobj = libxml_xmlAttrPtrWrap ( res ) ;
return resultobj ;
}
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_next ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2002-01-30 16:37:32 +00:00
{
PyObject * resultobj , * obj ;
xmlNodePtr cur ;
xmlNodePtr res ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:next " , & obj ) )
2002-01-30 16:37:32 +00:00
return NULL ;
cur = PyxmlNode_Get ( obj ) ;
# ifdef DEBUG
printf ( " libxml_next: cur = %p \n " , cur ) ;
# endif
2002-03-15 22:24:56 +00:00
switch ( cur - > type ) {
case XML_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# ifdef LIBXML_DOCB_ENABLED
2002-03-15 22:24:56 +00:00
case XML_DOCB_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# endif
2002-03-15 22:24:56 +00:00
case XML_HTML_DOCUMENT_NODE :
res = NULL ;
break ;
case XML_ATTRIBUTE_NODE : {
xmlAttrPtr attr = ( xmlAttrPtr ) cur ;
res = ( xmlNodePtr ) attr - > next ;
break ;
}
case XML_NAMESPACE_DECL : {
xmlNsPtr ns = ( xmlNsPtr ) cur ;
res = ( xmlNodePtr ) ns - > next ;
break ;
}
default :
res = cur - > next ;
break ;
2002-01-30 16:37:32 +00:00
}
resultobj = libxml_xmlNodePtrWrap ( res ) ;
return resultobj ;
}
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_prev ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2002-01-30 16:37:32 +00:00
{
PyObject * resultobj , * obj ;
xmlNodePtr cur ;
xmlNodePtr res ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:prev " , & obj ) )
2002-01-30 16:37:32 +00:00
return NULL ;
cur = PyxmlNode_Get ( obj ) ;
# ifdef DEBUG
printf ( " libxml_prev: cur = %p \n " , cur ) ;
# endif
2002-03-15 22:24:56 +00:00
switch ( cur - > type ) {
case XML_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# ifdef LIBXML_DOCB_ENABLED
2002-03-15 22:24:56 +00:00
case XML_DOCB_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# endif
2002-03-15 22:24:56 +00:00
case XML_HTML_DOCUMENT_NODE :
res = NULL ;
break ;
case XML_ATTRIBUTE_NODE : {
xmlAttrPtr attr = ( xmlAttrPtr ) cur ;
2002-11-24 13:53:43 +00:00
res = ( xmlNodePtr ) attr - > prev ;
2002-03-15 22:24:56 +00:00
}
2009-09-07 14:58:47 +02:00
break ;
2002-03-15 22:24:56 +00:00
case XML_NAMESPACE_DECL :
res = NULL ;
break ;
default :
2002-11-24 13:53:43 +00:00
res = cur - > prev ;
2002-03-15 22:24:56 +00:00
break ;
2002-01-30 16:37:32 +00:00
}
resultobj = libxml_xmlNodePtrWrap ( res ) ;
return resultobj ;
}
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_children ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2002-01-30 16:37:32 +00:00
{
PyObject * resultobj , * obj ;
xmlNodePtr cur ;
xmlNodePtr res ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:children " , & obj ) )
2002-01-30 16:37:32 +00:00
return NULL ;
cur = PyxmlNode_Get ( obj ) ;
# ifdef DEBUG
printf ( " libxml_children: cur = %p \n " , cur ) ;
# endif
2002-03-15 22:24:56 +00:00
switch ( cur - > type ) {
case XML_ELEMENT_NODE :
case XML_ENTITY_REF_NODE :
case XML_ENTITY_NODE :
case XML_PI_NODE :
case XML_COMMENT_NODE :
case XML_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# ifdef LIBXML_DOCB_ENABLED
2002-03-15 22:24:56 +00:00
case XML_DOCB_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# endif
2002-03-15 22:24:56 +00:00
case XML_HTML_DOCUMENT_NODE :
case XML_DTD_NODE :
res = cur - > children ;
break ;
case XML_ATTRIBUTE_NODE : {
xmlAttrPtr attr = ( xmlAttrPtr ) cur ;
res = attr - > children ;
break ;
}
default :
res = NULL ;
break ;
2002-01-30 16:37:32 +00:00
}
resultobj = libxml_xmlNodePtrWrap ( res ) ;
return resultobj ;
}
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_last ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2002-01-30 16:37:32 +00:00
{
PyObject * resultobj , * obj ;
xmlNodePtr cur ;
xmlNodePtr res ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:last " , & obj ) )
2002-01-30 16:37:32 +00:00
return NULL ;
cur = PyxmlNode_Get ( obj ) ;
# ifdef DEBUG
printf ( " libxml_last: cur = %p \n " , cur ) ;
# endif
2002-03-15 22:24:56 +00:00
switch ( cur - > type ) {
case XML_ELEMENT_NODE :
case XML_ENTITY_REF_NODE :
case XML_ENTITY_NODE :
case XML_PI_NODE :
case XML_COMMENT_NODE :
case XML_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# ifdef LIBXML_DOCB_ENABLED
2002-03-15 22:24:56 +00:00
case XML_DOCB_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# endif
2002-03-15 22:24:56 +00:00
case XML_HTML_DOCUMENT_NODE :
case XML_DTD_NODE :
res = cur - > last ;
break ;
case XML_ATTRIBUTE_NODE : {
xmlAttrPtr attr = ( xmlAttrPtr ) cur ;
res = attr - > last ;
2013-05-02 16:11:46 +08:00
break ;
2002-03-15 22:24:56 +00:00
}
default :
res = NULL ;
break ;
2002-01-30 16:37:32 +00:00
}
resultobj = libxml_xmlNodePtrWrap ( res ) ;
return resultobj ;
}
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_parent ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2002-01-30 16:37:32 +00:00
{
PyObject * resultobj , * obj ;
xmlNodePtr cur ;
xmlNodePtr res ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:parent " , & obj ) )
2002-01-30 16:37:32 +00:00
return NULL ;
cur = PyxmlNode_Get ( obj ) ;
# ifdef DEBUG
printf ( " libxml_parent: cur = %p \n " , cur ) ;
# endif
2002-03-15 22:24:56 +00:00
switch ( cur - > type ) {
case XML_DOCUMENT_NODE :
case XML_HTML_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# ifdef LIBXML_DOCB_ENABLED
2002-03-15 22:24:56 +00:00
case XML_DOCB_DOCUMENT_NODE :
2002-01-30 16:37:32 +00:00
# endif
2002-03-15 22:24:56 +00:00
res = NULL ;
break ;
case XML_ATTRIBUTE_NODE : {
xmlAttrPtr attr = ( xmlAttrPtr ) cur ;
res = attr - > parent ;
}
2005-08-01 05:20:16 +00:00
break ;
2002-03-15 22:24:56 +00:00
case XML_ENTITY_DECL :
case XML_NAMESPACE_DECL :
case XML_XINCLUDE_START :
case XML_XINCLUDE_END :
res = NULL ;
break ;
default :
res = cur - > parent ;
break ;
2002-01-30 16:37:32 +00:00
}
resultobj = libxml_xmlNodePtrWrap ( res ) ;
return resultobj ;
}
static PyObject *
2002-03-15 22:24:56 +00:00
libxml_type ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
2002-01-30 16:37:32 +00:00
{
PyObject * resultobj , * obj ;
xmlNodePtr cur ;
2002-03-15 22:24:56 +00:00
const xmlChar * res = NULL ;
2002-01-30 16:37:32 +00:00
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:last " , & obj ) )
2002-01-30 16:37:32 +00:00
return NULL ;
cur = PyxmlNode_Get ( obj ) ;
2013-03-29 13:46:24 +08:00
if ( cur = = NULL ) {
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2002-01-30 16:37:32 +00:00
# ifdef DEBUG
printf ( " libxml_type: cur = %p \n " , cur ) ;
# endif
2002-03-15 22:24:56 +00:00
switch ( cur - > type ) {
2002-01-30 16:37:32 +00:00
case XML_ELEMENT_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " element " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_ATTRIBUTE_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " attribute " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_TEXT_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " text " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_CDATA_SECTION_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " cdata " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_ENTITY_REF_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " entity_ref " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_ENTITY_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " entity " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_PI_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " pi " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_COMMENT_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " comment " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_DOCUMENT_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " document_xml " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_DOCUMENT_TYPE_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " doctype " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_DOCUMENT_FRAG_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " fragment " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_NOTATION_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " notation " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_HTML_DOCUMENT_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " document_html " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_DTD_NODE :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " dtd " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_ELEMENT_DECL :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " elem_decl " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_ATTRIBUTE_DECL :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " attribute_decl " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_ENTITY_DECL :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " entity_decl " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_NAMESPACE_DECL :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " namespace " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_XINCLUDE_START :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " xinclude_start " ;
break ;
2002-01-30 16:37:32 +00:00
case XML_XINCLUDE_END :
2002-03-15 22:24:56 +00:00
res = ( const xmlChar * ) " xinclude_end " ;
break ;
2002-01-30 16:37:32 +00:00
# ifdef LIBXML_DOCB_ENABLED
2002-03-15 22:24:56 +00:00
case XML_DOCB_DOCUMENT_NODE :
res = ( const xmlChar * ) " document_docbook " ;
break ;
2002-01-30 16:37:32 +00:00
# endif
}
# ifdef DEBUG
printf ( " libxml_type: cur = %p: %s \n " , cur , res ) ;
# endif
2002-01-31 20:29:19 +00:00
resultobj = libxml_constxmlCharPtrWrap ( res ) ;
2002-01-30 16:37:32 +00:00
return resultobj ;
}
2002-02-04 00:17:01 +00:00
/************************************************************************
* *
* Specific accessor functions *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
PyObject *
2002-03-15 22:24:56 +00:00
libxml_xmlNodeGetNsDefs ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
2002-02-04 00:17:01 +00:00
PyObject * py_retval ;
xmlNsPtr c_retval ;
xmlNodePtr node ;
PyObject * pyobj_node ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple
( args , ( char * ) " O:xmlNodeGetNsDefs " , & pyobj_node ) )
return ( NULL ) ;
2002-02-04 00:17:01 +00:00
node = ( xmlNodePtr ) PyxmlNode_Get ( pyobj_node ) ;
if ( ( node = = NULL ) | | ( node - > type ! = XML_ELEMENT_NODE ) ) {
2002-03-15 22:24:56 +00:00
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
2002-02-04 00:17:01 +00:00
}
c_retval = node - > nsDef ;
py_retval = libxml_xmlNsPtrWrap ( ( xmlNsPtr ) c_retval ) ;
2002-03-15 22:24:56 +00:00
return ( py_retval ) ;
2002-02-04 00:17:01 +00:00
}
2005-04-12 01:02:29 +00:00
PyObject *
libxml_xmlNodeRemoveNsDef ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
PyObject * py_retval ;
xmlNsPtr ns , prev ;
xmlNodePtr node ;
PyObject * pyobj_node ;
xmlChar * href ;
xmlNsPtr c_retval ;
2013-03-29 13:46:24 +08:00
2005-04-12 01:02:29 +00:00
if ( ! PyArg_ParseTuple
( args , ( char * ) " Oz:xmlNodeRemoveNsDef " , & pyobj_node , & href ) )
return ( NULL ) ;
node = ( xmlNodePtr ) PyxmlNode_Get ( pyobj_node ) ;
ns = NULL ;
if ( ( node = = NULL ) | | ( node - > type ! = XML_ELEMENT_NODE ) ) {
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
if ( href = = NULL ) {
ns = node - > nsDef ;
node - > nsDef = NULL ;
c_retval = 0 ;
}
else {
prev = NULL ;
ns = node - > nsDef ;
while ( ns ! = NULL ) {
if ( xmlStrEqual ( ns - > href , href ) ) {
if ( prev ! = NULL )
prev - > next = ns - > next ;
else
node - > nsDef = ns - > next ;
ns - > next = NULL ;
c_retval = 0 ;
break ;
}
prev = ns ;
ns = ns - > next ;
}
}
c_retval = ns ;
py_retval = libxml_xmlNsPtrWrap ( ( xmlNsPtr ) c_retval ) ;
return ( py_retval ) ;
}
2002-02-04 00:17:01 +00:00
PyObject *
2002-03-15 22:24:56 +00:00
libxml_xmlNodeGetNs ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
2002-02-04 00:17:01 +00:00
PyObject * py_retval ;
xmlNsPtr c_retval ;
xmlNodePtr node ;
PyObject * pyobj_node ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlNodeGetNs " , & pyobj_node ) )
return ( NULL ) ;
2002-02-04 00:17:01 +00:00
node = ( xmlNodePtr ) PyxmlNode_Get ( pyobj_node ) ;
2003-09-24 21:23:56 +00:00
if ( ( node = = NULL ) | |
( ( node - > type ! = XML_ELEMENT_NODE ) & &
( node - > type ! = XML_ATTRIBUTE_NODE ) ) ) {
2002-03-15 22:24:56 +00:00
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
2002-02-04 00:17:01 +00:00
}
c_retval = node - > ns ;
py_retval = libxml_xmlNsPtrWrap ( ( xmlNsPtr ) c_retval ) ;
2002-03-15 22:24:56 +00:00
return ( py_retval ) ;
2002-02-04 00:17:01 +00:00
}
2003-09-29 13:20:24 +00:00
# ifdef LIBXML_OUTPUT_ENABLED
2002-03-06 17:35:40 +00:00
/************************************************************************
* *
* Serialization front - end *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-15 22:24:56 +00:00
static PyObject *
libxml_serializeNode ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
2002-03-06 17:35:40 +00:00
PyObject * py_retval = NULL ;
xmlChar * c_retval ;
PyObject * pyobj_node ;
xmlNodePtr node ;
xmlDocPtr doc ;
2002-03-15 22:24:56 +00:00
const char * encoding ;
2002-03-06 17:35:40 +00:00
int format ;
2008-09-01 13:08:57 +00:00
xmlSaveCtxtPtr ctxt ;
xmlBufferPtr buf ;
int options = 0 ;
2002-03-06 17:35:40 +00:00
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " Ozi:serializeNode " , & pyobj_node ,
& encoding , & format ) )
return ( NULL ) ;
2002-03-06 17:35:40 +00:00
node = ( xmlNodePtr ) PyxmlNode_Get ( pyobj_node ) ;
if ( node = = NULL ) {
2002-03-15 22:24:56 +00:00
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
2002-03-06 17:35:40 +00:00
}
if ( node - > type = = XML_DOCUMENT_NODE ) {
2002-03-15 22:24:56 +00:00
doc = ( xmlDocPtr ) node ;
2008-09-01 13:08:57 +00:00
node = NULL ;
2004-04-30 23:11:45 +00:00
# ifdef LIBXML_HTML_ENABLED
2002-03-06 17:35:40 +00:00
} else if ( node - > type = = XML_HTML_DOCUMENT_NODE ) {
2002-03-15 22:24:56 +00:00
doc = ( xmlDocPtr ) node ;
2008-09-01 13:08:57 +00:00
node = NULL ;
# endif
2002-03-06 17:35:40 +00:00
} else {
2004-02-08 04:12:49 +00:00
if ( node - > type = = XML_NAMESPACE_DECL )
doc = NULL ;
else
doc = node - > doc ;
2002-11-17 22:37:35 +00:00
if ( ( doc = = NULL ) | | ( doc - > type = = XML_DOCUMENT_NODE ) ) {
2004-04-30 23:11:45 +00:00
# ifdef LIBXML_HTML_ENABLED
2002-03-15 22:24:56 +00:00
} else if ( doc - > type = = XML_HTML_DOCUMENT_NODE ) {
2004-04-30 23:11:45 +00:00
# endif /* LIBXML_HTML_ENABLED */
2002-03-15 22:24:56 +00:00
} else {
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2002-03-06 17:35:40 +00:00
}
2008-09-01 13:08:57 +00:00
buf = xmlBufferCreate ( ) ;
if ( buf = = NULL ) {
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
if ( format ) options | = XML_SAVE_FORMAT ;
ctxt = xmlSaveToBuffer ( buf , encoding , options ) ;
if ( ctxt = = NULL ) {
xmlBufferFree ( buf ) ;
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
if ( node = = NULL )
xmlSaveDoc ( ctxt , doc ) ;
else
xmlSaveTree ( ctxt , node ) ;
xmlSaveClose ( ctxt ) ;
c_retval = buf - > content ;
buf - > content = NULL ;
xmlBufferFree ( buf ) ;
py_retval = libxml_charPtrWrap ( ( char * ) c_retval ) ;
2002-03-15 22:24:56 +00:00
return ( py_retval ) ;
2002-03-06 17:35:40 +00:00
}
2002-03-15 22:24:56 +00:00
static PyObject *
libxml_saveNodeTo ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
2002-03-06 17:35:40 +00:00
PyObject * py_file = NULL ;
FILE * output ;
PyObject * pyobj_node ;
xmlNodePtr node ;
xmlDocPtr doc ;
2002-03-15 22:24:56 +00:00
const char * encoding ;
2002-03-06 17:35:40 +00:00
int format ;
int len ;
xmlOutputBufferPtr buf ;
xmlCharEncodingHandlerPtr handler = NULL ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " OOzi:serializeNode " , & pyobj_node ,
& py_file , & encoding , & format ) )
return ( NULL ) ;
2002-03-06 17:35:40 +00:00
node = ( xmlNodePtr ) PyxmlNode_Get ( pyobj_node ) ;
if ( node = = NULL ) {
2013-03-29 13:46:24 +08:00
return ( PyLong_FromLong ( ( long ) - 1 ) ) ;
2002-03-06 17:35:40 +00:00
}
2013-03-29 13:46:24 +08:00
output = PyFile_Get ( py_file ) ;
2002-03-06 17:35:40 +00:00
if ( output = = NULL ) {
2013-03-29 13:46:24 +08:00
return ( PyLong_FromLong ( ( long ) - 1 ) ) ;
2002-03-06 17:35:40 +00:00
}
if ( node - > type = = XML_DOCUMENT_NODE ) {
2002-03-15 22:24:56 +00:00
doc = ( xmlDocPtr ) node ;
2002-03-06 17:35:40 +00:00
} else if ( node - > type = = XML_HTML_DOCUMENT_NODE ) {
2002-03-15 22:24:56 +00:00
doc = ( xmlDocPtr ) node ;
2002-03-06 17:35:40 +00:00
} else {
2002-03-15 22:24:56 +00:00
doc = node - > doc ;
2002-03-06 17:35:40 +00:00
}
2004-04-30 23:11:45 +00:00
# ifdef LIBXML_HTML_ENABLED
2002-03-06 17:35:40 +00:00
if ( doc - > type = = XML_HTML_DOCUMENT_NODE ) {
2002-03-15 22:24:56 +00:00
if ( encoding = = NULL )
encoding = ( const char * ) htmlGetMetaEncoding ( doc ) ;
2002-03-06 17:35:40 +00:00
}
2004-04-30 23:11:45 +00:00
# endif /* LIBXML_HTML_ENABLED */
2002-03-06 17:35:40 +00:00
if ( encoding ! = NULL ) {
2002-03-15 22:24:56 +00:00
handler = xmlFindCharEncodingHandler ( encoding ) ;
if ( handler = = NULL ) {
2021-07-14 15:23:11 +01:00
PyFile_Release ( output ) ;
2013-03-29 13:46:24 +08:00
return ( PyLong_FromLong ( ( long ) - 1 ) ) ;
2002-03-15 22:24:56 +00:00
}
2002-03-06 17:35:40 +00:00
}
if ( doc - > type = = XML_HTML_DOCUMENT_NODE ) {
2002-03-15 22:24:56 +00:00
if ( handler = = NULL )
handler = xmlFindCharEncodingHandler ( " HTML " ) ;
if ( handler = = NULL )
handler = xmlFindCharEncodingHandler ( " ascii " ) ;
2002-03-06 17:35:40 +00:00
}
buf = xmlOutputBufferCreateFile ( output , handler ) ;
if ( node - > type = = XML_DOCUMENT_NODE ) {
2002-03-15 22:24:56 +00:00
len = xmlSaveFormatFileTo ( buf , doc , encoding , format ) ;
2004-04-30 23:11:45 +00:00
# ifdef LIBXML_HTML_ENABLED
2002-03-06 17:35:40 +00:00
} else if ( node - > type = = XML_HTML_DOCUMENT_NODE ) {
2002-03-15 22:24:56 +00:00
htmlDocContentDumpFormatOutput ( buf , doc , encoding , format ) ;
len = xmlOutputBufferClose ( buf ) ;
2002-03-06 17:35:40 +00:00
} else if ( doc - > type = = XML_HTML_DOCUMENT_NODE ) {
2002-03-15 22:24:56 +00:00
htmlNodeDumpFormatOutput ( buf , doc , node , encoding , format ) ;
len = xmlOutputBufferClose ( buf ) ;
2004-04-30 23:11:45 +00:00
# endif /* LIBXML_HTML_ENABLED */
2002-03-06 17:35:40 +00:00
} else {
2002-03-15 22:24:56 +00:00
xmlNodeDumpOutput ( buf , doc , node , 0 , format , encoding ) ;
len = xmlOutputBufferClose ( buf ) ;
2002-03-06 17:35:40 +00:00
}
2013-03-29 13:46:24 +08:00
PyFile_Release ( output ) ;
return ( PyLong_FromLong ( ( long ) len ) ) ;
2002-03-06 17:35:40 +00:00
}
2003-09-29 13:20:24 +00:00
# endif /* LIBXML_OUTPUT_ENABLED */
2002-03-06 17:35:40 +00:00
2002-03-01 13:00:53 +00:00
/************************************************************************
* *
* Extra stuff *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
PyObject *
2002-03-15 22:24:56 +00:00
libxml_xmlNewNode ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
2002-03-01 13:00:53 +00:00
PyObject * py_retval ;
2002-03-15 22:24:56 +00:00
xmlChar * name ;
2002-03-01 13:00:53 +00:00
xmlNodePtr node ;
2002-03-15 22:24:56 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " s:xmlNewNode " , & name ) )
return ( NULL ) ;
2002-03-01 13:00:53 +00:00
node = ( xmlNodePtr ) xmlNewNode ( NULL , name ) ;
2003-02-03 08:52:58 +00:00
# ifdef DEBUG
2002-03-15 22:24:56 +00:00
printf ( " NewNode: %s : %p \n " , name , ( void * ) node ) ;
2003-02-03 08:52:58 +00:00
# endif
2002-03-01 13:00:53 +00:00
if ( node = = NULL ) {
2002-03-15 22:24:56 +00:00
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
2002-03-01 13:00:53 +00:00
}
py_retval = libxml_xmlNodePtrWrap ( node ) ;
2002-03-15 22:24:56 +00:00
return ( py_retval ) ;
2002-03-01 13:00:53 +00:00
}
2003-04-23 07:36:50 +00:00
/************************************************************************
* *
* Local Catalog stuff *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static PyObject *
libxml_addLocalCatalog ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
xmlChar * URL ;
xmlParserCtxtPtr ctxt ;
PyObject * pyobj_ctxt ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " Os:addLocalCatalog " , & pyobj_ctxt , & URL ) )
return ( NULL ) ;
ctxt = ( xmlParserCtxtPtr ) PyparserCtxt_Get ( pyobj_ctxt ) ;
if ( URL ! = NULL ) {
ctxt - > catalogs = xmlCatalogAddLocal ( ctxt - > catalogs , URL ) ;
}
# ifdef DEBUG
printf ( " LocalCatalog: %s \n " , URL ) ;
# endif
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2003-07-29 20:44:53 +00:00
# ifdef LIBXML_SCHEMAS_ENABLED
/************************************************************************
* *
* RelaxNG error handler registration *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
typedef struct
{
PyObject * warn ;
PyObject * error ;
PyObject * arg ;
} xmlRelaxNGValidCtxtPyCtxt ;
typedef xmlRelaxNGValidCtxtPyCtxt * xmlRelaxNGValidCtxtPyCtxtPtr ;
static void
libxml_xmlRelaxNGValidityGenericErrorFuncHandler ( void * ctx , char * str )
{
PyObject * list ;
PyObject * result ;
xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt ;
# ifdef DEBUG_ERROR
printf ( " libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called \n " , ctx , str ) ;
# endif
pyCtxt = ( xmlRelaxNGValidCtxtPyCtxtPtr ) ctx ;
list = PyTuple_New ( 2 ) ;
PyTuple_SetItem ( list , 0 , libxml_charPtrWrap ( str ) ) ;
PyTuple_SetItem ( list , 1 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
result = PyEval_CallObject ( pyCtxt - > error , list ) ;
if ( result = = NULL )
{
/* TODO: manage for the exception to be propagated... */
PyErr_Print ( ) ;
}
Py_XDECREF ( list ) ;
Py_XDECREF ( result ) ;
}
static void
libxml_xmlRelaxNGValidityGenericWarningFuncHandler ( void * ctx , char * str )
{
PyObject * list ;
PyObject * result ;
xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt ;
# ifdef DEBUG_ERROR
printf ( " libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called \n " , ctx , str ) ;
# endif
pyCtxt = ( xmlRelaxNGValidCtxtPyCtxtPtr ) ctx ;
list = PyTuple_New ( 2 ) ;
PyTuple_SetItem ( list , 0 , libxml_charPtrWrap ( str ) ) ;
PyTuple_SetItem ( list , 1 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
result = PyEval_CallObject ( pyCtxt - > warn , list ) ;
if ( result = = NULL )
{
/* TODO: manage for the exception to be propagated... */
PyErr_Print ( ) ;
}
Py_XDECREF ( list ) ;
Py_XDECREF ( result ) ;
}
static void
libxml_xmlRelaxNGValidityErrorFunc ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlRelaxNGValidityGenericErrorFuncHandler ( ctx , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
}
static void
libxml_xmlRelaxNGValidityWarningFunc ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlRelaxNGValidityGenericWarningFuncHandler ( ctx , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
}
static PyObject *
libxml_xmlRelaxNGSetValidErrors ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
PyObject * py_retval ;
PyObject * pyobj_error ;
PyObject * pyobj_warn ;
PyObject * pyobj_ctx ;
PyObject * pyobj_arg = Py_None ;
xmlRelaxNGValidCtxtPtr ctxt ;
xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt ;
if ( ! PyArg_ParseTuple
( args , ( char * ) " OOO|O:xmlRelaxNGSetValidErrors " , & pyobj_ctx , & pyobj_error , & pyobj_warn , & pyobj_arg ) )
return ( NULL ) ;
# ifdef DEBUG_ERROR
printf ( " libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called \n " , pyobj_ctx , pyobj_error , pyobj_warn ) ;
# endif
ctxt = PyrelaxNgValidCtxt_Get ( pyobj_ctx ) ;
2003-08-05 15:52:22 +00:00
if ( xmlRelaxNGGetValidErrors ( ctxt , NULL , NULL , ( void * * ) & pyCtxt ) = = - 1 )
2003-07-29 20:44:53 +00:00
{
py_retval = libxml_intWrap ( - 1 ) ;
return ( py_retval ) ;
}
if ( pyCtxt = = NULL )
{
/* first time to set the error handlers */
pyCtxt = xmlMalloc ( sizeof ( xmlRelaxNGValidCtxtPyCtxt ) ) ;
if ( pyCtxt = = NULL ) {
py_retval = libxml_intWrap ( - 1 ) ;
return ( py_retval ) ;
}
memset ( pyCtxt , 0 , sizeof ( xmlRelaxNGValidCtxtPyCtxt ) ) ;
}
/* TODO: check warn and error is a function ! */
Py_XDECREF ( pyCtxt - > error ) ;
Py_XINCREF ( pyobj_error ) ;
pyCtxt - > error = pyobj_error ;
Py_XDECREF ( pyCtxt - > warn ) ;
Py_XINCREF ( pyobj_warn ) ;
pyCtxt - > warn = pyobj_warn ;
Py_XDECREF ( pyCtxt - > arg ) ;
Py_XINCREF ( pyobj_arg ) ;
pyCtxt - > arg = pyobj_arg ;
xmlRelaxNGSetValidErrors ( ctxt , & libxml_xmlRelaxNGValidityErrorFunc , & libxml_xmlRelaxNGValidityWarningFunc , pyCtxt ) ;
py_retval = libxml_intWrap ( 1 ) ;
return ( py_retval ) ;
}
static PyObject *
libxml_xmlRelaxNGFreeValidCtxt ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args ) {
xmlRelaxNGValidCtxtPtr ctxt ;
xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt ;
PyObject * pyobj_ctxt ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlRelaxNGFreeValidCtxt " , & pyobj_ctxt ) )
return ( NULL ) ;
ctxt = ( xmlRelaxNGValidCtxtPtr ) PyrelaxNgValidCtxt_Get ( pyobj_ctxt ) ;
2003-08-05 15:52:22 +00:00
if ( xmlRelaxNGGetValidErrors ( ctxt , NULL , NULL , ( void * * ) & pyCtxt ) = = 0 )
2003-07-29 20:44:53 +00:00
{
if ( pyCtxt ! = NULL )
{
Py_XDECREF ( pyCtxt - > error ) ;
Py_XDECREF ( pyCtxt - > warn ) ;
Py_XDECREF ( pyCtxt - > arg ) ;
xmlFree ( pyCtxt ) ;
}
}
xmlRelaxNGFreeValidCtxt ( ctxt ) ;
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2004-08-18 09:13:18 +00:00
typedef struct
{
PyObject * warn ;
PyObject * error ;
PyObject * arg ;
} xmlSchemaValidCtxtPyCtxt ;
typedef xmlSchemaValidCtxtPyCtxt * xmlSchemaValidCtxtPyCtxtPtr ;
static void
libxml_xmlSchemaValidityGenericErrorFuncHandler ( void * ctx , char * str )
{
PyObject * list ;
PyObject * result ;
xmlSchemaValidCtxtPyCtxtPtr pyCtxt ;
# ifdef DEBUG_ERROR
2019-09-30 17:04:54 +02:00
printf ( " libxml_xmlSchemaValidityGenericErrorFuncHandler(%p, %s, ...) called \n " , ctx , str ) ;
2004-08-18 09:13:18 +00:00
# endif
pyCtxt = ( xmlSchemaValidCtxtPyCtxtPtr ) ctx ;
list = PyTuple_New ( 2 ) ;
PyTuple_SetItem ( list , 0 , libxml_charPtrWrap ( str ) ) ;
PyTuple_SetItem ( list , 1 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
result = PyEval_CallObject ( pyCtxt - > error , list ) ;
if ( result = = NULL )
{
/* TODO: manage for the exception to be propagated... */
PyErr_Print ( ) ;
}
Py_XDECREF ( list ) ;
Py_XDECREF ( result ) ;
}
static void
libxml_xmlSchemaValidityGenericWarningFuncHandler ( void * ctx , char * str )
{
PyObject * list ;
PyObject * result ;
xmlSchemaValidCtxtPyCtxtPtr pyCtxt ;
# ifdef DEBUG_ERROR
printf ( " libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called \n " , ctx , str ) ;
# endif
pyCtxt = ( xmlSchemaValidCtxtPyCtxtPtr ) ctx ;
list = PyTuple_New ( 2 ) ;
PyTuple_SetItem ( list , 0 , libxml_charPtrWrap ( str ) ) ;
PyTuple_SetItem ( list , 1 , pyCtxt - > arg ) ;
Py_XINCREF ( pyCtxt - > arg ) ;
result = PyEval_CallObject ( pyCtxt - > warn , list ) ;
if ( result = = NULL )
{
/* TODO: manage for the exception to be propagated... */
PyErr_Print ( ) ;
}
Py_XDECREF ( list ) ;
Py_XDECREF ( result ) ;
}
static void
libxml_xmlSchemaValidityErrorFunc ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlSchemaValidityGenericErrorFuncHandler ( ctx , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
}
static void
libxml_xmlSchemaValidityWarningFunc ( void * ctx , const char * msg , . . . )
{
va_list ap ;
va_start ( ap , msg ) ;
libxml_xmlSchemaValidityGenericWarningFuncHandler ( ctx , libxml_buildMessage ( msg , ap ) ) ;
va_end ( ap ) ;
}
2004-08-22 13:11:39 +00:00
PyObject *
2004-08-18 09:13:18 +00:00
libxml_xmlSchemaSetValidErrors ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
PyObject * py_retval ;
PyObject * pyobj_error ;
PyObject * pyobj_warn ;
PyObject * pyobj_ctx ;
PyObject * pyobj_arg = Py_None ;
xmlSchemaValidCtxtPtr ctxt ;
xmlSchemaValidCtxtPyCtxtPtr pyCtxt ;
if ( ! PyArg_ParseTuple
( args , ( char * ) " OOO|O:xmlSchemaSetValidErrors " , & pyobj_ctx , & pyobj_error , & pyobj_warn , & pyobj_arg ) )
return ( NULL ) ;
# ifdef DEBUG_ERROR
printf ( " libxml_xmlSchemaSetValidErrors(%p, %p, %p) called \n " , pyobj_ctx , pyobj_error , pyobj_warn ) ;
# endif
ctxt = PySchemaValidCtxt_Get ( pyobj_ctx ) ;
if ( xmlSchemaGetValidErrors ( ctxt , NULL , NULL , ( void * * ) & pyCtxt ) = = - 1 )
{
py_retval = libxml_intWrap ( - 1 ) ;
return ( py_retval ) ;
}
if ( pyCtxt = = NULL )
{
/* first time to set the error handlers */
pyCtxt = xmlMalloc ( sizeof ( xmlSchemaValidCtxtPyCtxt ) ) ;
if ( pyCtxt = = NULL ) {
py_retval = libxml_intWrap ( - 1 ) ;
return ( py_retval ) ;
}
memset ( pyCtxt , 0 , sizeof ( xmlSchemaValidCtxtPyCtxt ) ) ;
}
/* TODO: check warn and error is a function ! */
Py_XDECREF ( pyCtxt - > error ) ;
Py_XINCREF ( pyobj_error ) ;
pyCtxt - > error = pyobj_error ;
Py_XDECREF ( pyCtxt - > warn ) ;
Py_XINCREF ( pyobj_warn ) ;
pyCtxt - > warn = pyobj_warn ;
Py_XDECREF ( pyCtxt - > arg ) ;
Py_XINCREF ( pyobj_arg ) ;
pyCtxt - > arg = pyobj_arg ;
xmlSchemaSetValidErrors ( ctxt , & libxml_xmlSchemaValidityErrorFunc , & libxml_xmlSchemaValidityWarningFunc , pyCtxt ) ;
py_retval = libxml_intWrap ( 1 ) ;
return ( py_retval ) ;
}
2005-03-30 07:40:35 +00:00
static PyObject *
2004-08-18 09:13:18 +00:00
libxml_xmlSchemaFreeValidCtxt ( ATTRIBUTE_UNUSED PyObject * self , PyObject * args )
{
xmlSchemaValidCtxtPtr ctxt ;
xmlSchemaValidCtxtPyCtxtPtr pyCtxt ;
PyObject * pyobj_ctxt ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:xmlSchemaFreeValidCtxt " , & pyobj_ctxt ) )
return ( NULL ) ;
ctxt = ( xmlSchemaValidCtxtPtr ) PySchemaValidCtxt_Get ( pyobj_ctxt ) ;
if ( xmlSchemaGetValidErrors ( ctxt , NULL , NULL , ( void * * ) & pyCtxt ) = = 0 )
{
if ( pyCtxt ! = NULL )
{
Py_XDECREF ( pyCtxt - > error ) ;
Py_XDECREF ( pyCtxt - > warn ) ;
Py_XDECREF ( pyCtxt - > arg ) ;
xmlFree ( pyCtxt ) ;
}
}
xmlSchemaFreeValidCtxt ( ctxt ) ;
Py_INCREF ( Py_None ) ;
return ( Py_None ) ;
}
2003-07-29 20:44:53 +00:00
# endif
2004-03-09 09:03:28 +00:00
# ifdef LIBXML_C14N_ENABLED
# ifdef LIBXML_OUTPUT_ENABLED
/************************************************************************
* *
* XML Canonicalization c14n *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int
PyxmlNodeSet_Convert ( PyObject * py_nodeset , xmlNodeSetPtr * result )
{
xmlNodeSetPtr nodeSet ;
int is_tuple = 0 ;
if ( PyTuple_Check ( py_nodeset ) )
is_tuple = 1 ;
else if ( PyList_Check ( py_nodeset ) )
is_tuple = 0 ;
else if ( py_nodeset = = Py_None ) {
* result = NULL ;
return 0 ;
}
else {
PyErr_SetString ( PyExc_TypeError ,
" must be a tuple or list of nodes. " ) ;
return - 1 ;
}
nodeSet = ( xmlNodeSetPtr ) xmlMalloc ( sizeof ( xmlNodeSet ) ) ;
if ( nodeSet = = NULL ) {
PyErr_SetString ( PyExc_MemoryError , " " ) ;
return - 1 ;
}
nodeSet - > nodeNr = 0 ;
nodeSet - > nodeMax = ( is_tuple
? PyTuple_GET_SIZE ( py_nodeset )
: PyList_GET_SIZE ( py_nodeset ) ) ;
nodeSet - > nodeTab
= ( xmlNodePtr * ) xmlMalloc ( nodeSet - > nodeMax
* sizeof ( xmlNodePtr ) ) ;
if ( nodeSet - > nodeTab = = NULL ) {
xmlFree ( nodeSet ) ;
PyErr_SetString ( PyExc_MemoryError , " " ) ;
return - 1 ;
}
memset ( nodeSet - > nodeTab , 0 ,
nodeSet - > nodeMax * sizeof ( xmlNodePtr ) ) ;
{
int idx ;
for ( idx = 0 ; idx < nodeSet - > nodeMax ; + + idx ) {
xmlNodePtr pynode =
PyxmlNode_Get ( is_tuple
? PyTuple_GET_ITEM ( py_nodeset , idx )
: PyList_GET_ITEM ( py_nodeset , idx ) ) ;
if ( pynode )
nodeSet - > nodeTab [ nodeSet - > nodeNr + + ] = pynode ;
}
}
* result = nodeSet ;
return 0 ;
}
static int
PystringSet_Convert ( PyObject * py_strings , xmlChar * * * result )
{
/* NOTE: the array should be freed, but the strings are shared
with the python strings and so must not be freed . */
xmlChar * * strings ;
int is_tuple = 0 ;
int count ;
int init_index = 0 ;
if ( PyTuple_Check ( py_strings ) )
is_tuple = 1 ;
else if ( PyList_Check ( py_strings ) )
is_tuple = 0 ;
else if ( py_strings = = Py_None ) {
* result = NULL ;
return 0 ;
}
else {
PyErr_SetString ( PyExc_TypeError ,
" must be a tuple or list of strings. " ) ;
return - 1 ;
}
count = ( is_tuple
? PyTuple_GET_SIZE ( py_strings )
: PyList_GET_SIZE ( py_strings ) ) ;
strings = ( xmlChar * * ) xmlMalloc ( sizeof ( xmlChar * ) * count ) ;
if ( strings = = NULL ) {
PyErr_SetString ( PyExc_MemoryError , " " ) ;
return - 1 ;
}
memset ( strings , 0 , sizeof ( xmlChar * ) * count ) ;
{
int idx ;
for ( idx = 0 ; idx < count ; + + idx ) {
2013-03-29 13:46:24 +08:00
char * s = PyBytes_AsString
2004-03-09 09:03:28 +00:00
( is_tuple
? PyTuple_GET_ITEM ( py_strings , idx )
: PyList_GET_ITEM ( py_strings , idx ) ) ;
if ( s )
strings [ init_index + + ] = ( xmlChar * ) s ;
else {
xmlFree ( strings ) ;
PyErr_SetString ( PyExc_TypeError ,
" must be a tuple or list of strings. " ) ;
return - 1 ;
}
}
}
* result = strings ;
return 0 ;
}
static PyObject *
libxml_C14NDocDumpMemory ( ATTRIBUTE_UNUSED PyObject * self ,
PyObject * args )
{
PyObject * py_retval = NULL ;
PyObject * pyobj_doc ;
PyObject * pyobj_nodes ;
int exclusive ;
PyObject * pyobj_prefixes ;
int with_comments ;
xmlDocPtr doc ;
xmlNodeSetPtr nodes ;
xmlChar * * prefixes = NULL ;
xmlChar * doc_txt ;
int result ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " OOiOi:C14NDocDumpMemory " ,
& pyobj_doc ,
& pyobj_nodes ,
& exclusive ,
& pyobj_prefixes ,
& with_comments ) )
return ( NULL ) ;
doc = ( xmlDocPtr ) PyxmlNode_Get ( pyobj_doc ) ;
if ( ! doc ) {
PyErr_SetString ( PyExc_TypeError , " bad document. " ) ;
return NULL ;
}
result = PyxmlNodeSet_Convert ( pyobj_nodes , & nodes ) ;
if ( result < 0 ) return NULL ;
if ( exclusive ) {
result = PystringSet_Convert ( pyobj_prefixes , & prefixes ) ;
if ( result < 0 ) {
if ( nodes ) {
xmlFree ( nodes - > nodeTab ) ;
xmlFree ( nodes ) ;
}
return NULL ;
}
}
result = xmlC14NDocDumpMemory ( doc ,
nodes ,
exclusive ,
prefixes ,
with_comments ,
& doc_txt ) ;
if ( nodes ) {
xmlFree ( nodes - > nodeTab ) ;
xmlFree ( nodes ) ;
}
if ( prefixes ) {
xmlChar * * idx = prefixes ;
while ( * idx ) xmlFree ( * ( idx + + ) ) ;
xmlFree ( prefixes ) ;
}
if ( result < 0 ) {
PyErr_SetString ( PyExc_Exception ,
" libxml2 xmlC14NDocDumpMemory failure. " ) ;
return NULL ;
}
else {
2013-03-29 13:46:24 +08:00
py_retval = PY_IMPORT_STRING_SIZE ( ( const char * ) doc_txt ,
result ) ;
2004-03-09 09:03:28 +00:00
xmlFree ( doc_txt ) ;
return py_retval ;
}
}
static PyObject *
libxml_C14NDocSaveTo ( ATTRIBUTE_UNUSED PyObject * self ,
PyObject * args )
{
PyObject * pyobj_doc ;
PyObject * py_file ;
PyObject * pyobj_nodes ;
int exclusive ;
PyObject * pyobj_prefixes ;
int with_comments ;
xmlDocPtr doc ;
xmlNodeSetPtr nodes ;
xmlChar * * prefixes = NULL ;
FILE * output ;
xmlOutputBufferPtr buf ;
int result ;
int len ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " OOiOiO:C14NDocSaveTo " ,
& pyobj_doc ,
& pyobj_nodes ,
& exclusive ,
& pyobj_prefixes ,
& with_comments ,
& py_file ) )
return ( NULL ) ;
doc = ( xmlDocPtr ) PyxmlNode_Get ( pyobj_doc ) ;
if ( ! doc ) {
PyErr_SetString ( PyExc_TypeError , " bad document. " ) ;
return NULL ;
}
2013-03-29 13:46:24 +08:00
output = PyFile_Get ( py_file ) ;
2004-03-09 09:03:28 +00:00
if ( output = = NULL ) {
PyErr_SetString ( PyExc_TypeError , " bad file. " ) ;
return NULL ;
}
buf = xmlOutputBufferCreateFile ( output , NULL ) ;
result = PyxmlNodeSet_Convert ( pyobj_nodes , & nodes ) ;
2021-07-14 15:28:56 +01:00
if ( result < 0 ) {
xmlOutputBufferClose ( buf ) ;
return NULL ;
}
2004-03-09 09:03:28 +00:00
if ( exclusive ) {
result = PystringSet_Convert ( pyobj_prefixes , & prefixes ) ;
if ( result < 0 ) {
if ( nodes ) {
xmlFree ( nodes - > nodeTab ) ;
xmlFree ( nodes ) ;
}
2021-07-14 15:28:56 +01:00
xmlOutputBufferClose ( buf ) ;
2004-03-09 09:03:28 +00:00
return NULL ;
}
}
result = xmlC14NDocSaveTo ( doc ,
nodes ,
exclusive ,
prefixes ,
with_comments ,
buf ) ;
if ( nodes ) {
xmlFree ( nodes - > nodeTab ) ;
xmlFree ( nodes ) ;
}
if ( prefixes ) {
xmlChar * * idx = prefixes ;
while ( * idx ) xmlFree ( * ( idx + + ) ) ;
xmlFree ( prefixes ) ;
}
2013-03-29 13:46:24 +08:00
PyFile_Release ( output ) ;
2004-03-09 09:03:28 +00:00
len = xmlOutputBufferClose ( buf ) ;
if ( result < 0 ) {
PyErr_SetString ( PyExc_Exception ,
" libxml2 xmlC14NDocSaveTo failure. " ) ;
return NULL ;
}
else
2013-03-29 13:46:24 +08:00
return PyLong_FromLong ( ( long ) len ) ;
2004-03-09 09:03:28 +00:00
}
# endif
# endif
2004-07-16 10:39:30 +00:00
static PyObject *
libxml_getObjDesc ( PyObject * self ATTRIBUTE_UNUSED , PyObject * args ) {
PyObject * obj ;
char * str ;
2004-03-09 09:03:28 +00:00
2004-07-16 10:39:30 +00:00
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:getObjDesc " , & obj ) )
return NULL ;
2013-03-29 13:46:24 +08:00
str = PyCapsule_GetPointer ( obj , PyCapsule_GetName ( obj ) ) ;
2004-07-16 10:39:30 +00:00
return Py_BuildValue ( ( char * ) " s " , str ) ;
}
2004-03-09 09:03:28 +00:00
2006-06-26 18:25:40 +00:00
static PyObject *
libxml_compareNodesEqual ( PyObject * self ATTRIBUTE_UNUSED , PyObject * args ) {
PyObject * py_node1 , * py_node2 ;
xmlNodePtr node1 , node2 ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " OO:compareNodesEqual " ,
& py_node1 , & py_node2 ) )
return NULL ;
/* To compare two node objects, we compare their pointer addresses */
node1 = PyxmlNode_Get ( py_node1 ) ;
node2 = PyxmlNode_Get ( py_node2 ) ;
if ( node1 = = node2 )
return Py_BuildValue ( ( char * ) " i " , 1 ) ;
else
return Py_BuildValue ( ( char * ) " i " , 0 ) ;
}
static PyObject *
libxml_nodeHash ( PyObject * self ATTRIBUTE_UNUSED , PyObject * args ) {
PyObject * py_node1 ;
xmlNodePtr node1 ;
if ( ! PyArg_ParseTuple ( args , ( char * ) " O:nodeHash " , & py_node1 ) )
return NULL ;
/* For simplicity, we use the node pointer address as a hash value */
node1 = PyxmlNode_Get ( py_node1 ) ;
return PyLong_FromVoidPtr ( node1 ) ;
}
2002-01-30 16:37:32 +00:00
/************************************************************************
* *
* The registration stuff *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static PyMethodDef libxmlMethods [ ] = {
2002-01-30 20:52:23 +00:00
# include "libxml2-export.c"
2002-03-15 22:24:56 +00:00
{ ( char * ) " name " , libxml_name , METH_VARARGS , NULL } ,
{ ( char * ) " children " , libxml_children , METH_VARARGS , NULL } ,
{ ( char * ) " properties " , libxml_properties , METH_VARARGS , NULL } ,
{ ( char * ) " last " , libxml_last , METH_VARARGS , NULL } ,
{ ( char * ) " prev " , libxml_prev , METH_VARARGS , NULL } ,
{ ( char * ) " next " , libxml_next , METH_VARARGS , NULL } ,
{ ( char * ) " parent " , libxml_parent , METH_VARARGS , NULL } ,
{ ( char * ) " type " , libxml_type , METH_VARARGS , NULL } ,
{ ( char * ) " doc " , libxml_doc , METH_VARARGS , NULL } ,
{ ( char * ) " xmlNewNode " , libxml_xmlNewNode , METH_VARARGS , NULL } ,
2005-04-12 01:02:29 +00:00
{ ( char * ) " xmlNodeRemoveNsDef " , libxml_xmlNodeRemoveNsDef , METH_VARARGS , NULL } ,
2004-11-10 11:55:47 +00:00
{ ( char * ) " xmlSetValidErrors " , libxml_xmlSetValidErrors , METH_VARARGS , NULL } ,
2005-03-02 10:47:41 +00:00
{ ( char * ) " xmlFreeValidCtxt " , libxml_xmlFreeValidCtxt , METH_VARARGS , NULL } ,
2003-09-29 13:20:24 +00:00
# ifdef LIBXML_OUTPUT_ENABLED
2002-03-15 22:24:56 +00:00
{ ( char * ) " serializeNode " , libxml_serializeNode , METH_VARARGS , NULL } ,
{ ( char * ) " saveNodeTo " , libxml_saveNodeTo , METH_VARARGS , NULL } ,
2002-09-12 15:00:57 +00:00
{ ( char * ) " outputBufferCreate " , libxml_xmlCreateOutputBuffer , METH_VARARGS , NULL } ,
2003-12-04 12:31:49 +00:00
{ ( char * ) " outputBufferGetPythonFile " , libxml_outputBufferGetPythonFile , METH_VARARGS , NULL } ,
{ ( char * ) " xmlOutputBufferClose " , libxml_xmlOutputBufferClose , METH_VARARGS , NULL } ,
{ ( char * ) " xmlOutputBufferFlush " , libxml_xmlOutputBufferFlush , METH_VARARGS , NULL } ,
2004-10-04 10:26:54 +00:00
{ ( char * ) " xmlSaveFileTo " , libxml_xmlSaveFileTo , METH_VARARGS , NULL } ,
{ ( char * ) " xmlSaveFormatFileTo " , libxml_xmlSaveFormatFileTo , METH_VARARGS , NULL } ,
2003-09-29 13:20:24 +00:00
# endif /* LIBXML_OUTPUT_ENABLED */
2002-09-12 15:00:57 +00:00
{ ( char * ) " inputBufferCreate " , libxml_xmlCreateInputBuffer , METH_VARARGS , NULL } ,
{ ( char * ) " setEntityLoader " , libxml_xmlSetEntityLoader , METH_VARARGS , NULL } ,
2003-01-10 13:14:40 +00:00
{ ( char * ) " xmlRegisterErrorHandler " , libxml_xmlRegisterErrorHandler , METH_VARARGS , NULL } ,
2003-01-20 21:26:34 +00:00
{ ( char * ) " xmlParserCtxtSetErrorHandler " , libxml_xmlParserCtxtSetErrorHandler , METH_VARARGS , NULL } ,
{ ( char * ) " xmlParserCtxtGetErrorHandler " , libxml_xmlParserCtxtGetErrorHandler , METH_VARARGS , NULL } ,
2003-01-14 11:42:39 +00:00
{ ( char * ) " xmlFreeParserCtxt " , libxml_xmlFreeParserCtxt , METH_VARARGS , NULL } ,
2008-05-12 12:58:46 +00:00
# ifdef LIBXML_READER_ENABLED
2003-01-16 22:45:08 +00:00
{ ( char * ) " xmlTextReaderSetErrorHandler " , libxml_xmlTextReaderSetErrorHandler , METH_VARARGS , NULL } ,
{ ( char * ) " xmlTextReaderGetErrorHandler " , libxml_xmlTextReaderGetErrorHandler , METH_VARARGS , NULL } ,
{ ( char * ) " xmlFreeTextReader " , libxml_xmlFreeTextReader , METH_VARARGS , NULL } ,
2008-05-12 12:58:46 +00:00
# endif
2003-04-23 07:36:50 +00:00
{ ( char * ) " addLocalCatalog " , libxml_addLocalCatalog , METH_VARARGS , NULL } ,
2003-07-29 20:44:53 +00:00
# ifdef LIBXML_SCHEMAS_ENABLED
{ ( char * ) " xmlRelaxNGSetValidErrors " , libxml_xmlRelaxNGSetValidErrors , METH_VARARGS , NULL } ,
{ ( char * ) " xmlRelaxNGFreeValidCtxt " , libxml_xmlRelaxNGFreeValidCtxt , METH_VARARGS , NULL } ,
2004-10-29 12:10:55 +00:00
{ ( char * ) " xmlSchemaSetValidErrors " , libxml_xmlSchemaSetValidErrors , METH_VARARGS , NULL } ,
2005-03-30 07:40:35 +00:00
{ ( char * ) " xmlSchemaFreeValidCtxt " , libxml_xmlSchemaFreeValidCtxt , METH_VARARGS , NULL } ,
2004-03-09 09:03:28 +00:00
# endif
# ifdef LIBXML_C14N_ENABLED
# ifdef LIBXML_OUTPUT_ENABLED
{ ( char * ) " xmlC14NDocDumpMemory " , libxml_C14NDocDumpMemory , METH_VARARGS , NULL } ,
{ ( char * ) " xmlC14NDocSaveTo " , libxml_C14NDocSaveTo , METH_VARARGS , NULL } ,
# endif
2003-07-29 20:44:53 +00:00
# endif
2004-07-16 10:39:30 +00:00
{ ( char * ) " getObjDesc " , libxml_getObjDesc , METH_VARARGS , NULL } ,
2006-06-26 18:25:40 +00:00
{ ( char * ) " compareNodesEqual " , libxml_compareNodesEqual , METH_VARARGS , NULL } ,
{ ( char * ) " nodeHash " , libxml_nodeHash , METH_VARARGS , NULL } ,
2013-02-25 15:54:25 +08:00
{ ( char * ) " xmlRegisterInputCallback " , libxml_xmlRegisterInputCallback , METH_VARARGS , NULL } ,
{ ( char * ) " xmlUnregisterInputCallback " , libxml_xmlUnregisterInputCallback , METH_VARARGS , NULL } ,
2002-03-15 22:24:56 +00:00
{ NULL , NULL , 0 , NULL }
2002-01-30 16:37:32 +00:00
} ;
2013-03-29 13:46:24 +08:00
# 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
2002-02-22 22:51:13 +00:00
# ifdef MERGED_MODULES
2002-02-23 10:10:33 +00:00
extern void initlibxsltmod ( void ) ;
2002-02-22 22:51:13 +00:00
# endif
2013-03-29 13:46:24 +08:00
# endif
2002-03-08 15:05:20 +00:00
2013-03-29 13:46:24 +08:00
# if PY_MAJOR_VERSION >= 3
PyObject * PyInit_libxml2mod ( void )
# else
void initlibxml2mod ( void )
# endif
{
PyObject * module ;
2004-07-03 23:28:52 +00:00
2013-03-29 13:46:24 +08:00
# if PY_MAJOR_VERSION >= 3
module = PyModule_Create ( & moduledef ) ;
# else
2019-09-30 17:04:54 +02:00
/* initialize the python extension module */
2013-03-29 13:46:24 +08:00
module = Py_InitModule ( ( char * ) " libxml2mod " , libxmlMethods ) ;
# endif
if ( module = = NULL )
INITERROR ;
2004-07-01 12:56:30 +00:00
/* initialize libxml2 */
xmlInitParser ( ) ;
2013-03-29 13:46:24 +08:00
/* TODO this probably need to be revamped for Python3 */
2002-02-02 21:49:17 +00:00
libxml_xmlErrorInitialize ( ) ;
2002-02-23 10:10:33 +00:00
2013-03-29 13:46:24 +08:00
# if PY_MAJOR_VERSION < 3
2002-02-22 22:51:13 +00:00
# ifdef MERGED_MODULES
initlibxsltmod ( ) ;
# endif
2013-03-29 13:46:24 +08:00
# endif
# if PY_MAJOR_VERSION >= 3
return module ;
# endif
2002-01-30 16:37:32 +00:00
}