mirror of
https://github.com/python/cpython.git
synced 2024-11-23 18:04:37 +08:00
ParserCreate(): Allow an empty string for the namespace_separator argument;
while not generally a good idea, this is used by RDF users, and works to implement RDF-style namespace+localname concatenation as defined in the RDF specifications. (This also corrects a backwards-compatibility bug.) Be more conservative while clearing out handlers; set the slot in the self->handlers array to NULL before DECREFing the callback. Still more adjustments to make the code style internally consistent.
This commit is contained in:
parent
c09cee4d92
commit
cde79131ea
@ -963,8 +963,8 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
|
||||
xmlparseobject *new_parser;
|
||||
int i;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate", &context,
|
||||
&encoding)) {
|
||||
if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate",
|
||||
&context, &encoding)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -1143,7 +1143,7 @@ newxmlparseobject(char *encoding, char *namespace_separator)
|
||||
self->specified_attributes = 0;
|
||||
self->in_callback = 0;
|
||||
self->handlers = NULL;
|
||||
if (namespace_separator) {
|
||||
if (namespace_separator != NULL) {
|
||||
self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
|
||||
}
|
||||
else {
|
||||
@ -1186,8 +1186,11 @@ xmlparse_dealloc(xmlparseobject *self)
|
||||
self->itself = NULL;
|
||||
|
||||
if (self->handlers != NULL) {
|
||||
PyObject *temp;
|
||||
for (i = 0; handler_info[i].name != NULL; i++) {
|
||||
Py_XDECREF(self->handlers[i]);
|
||||
temp = self->handlers[i];
|
||||
self->handlers[i] = NULL;
|
||||
Py_XDECREF(temp);
|
||||
}
|
||||
free(self->handlers);
|
||||
}
|
||||
@ -1390,10 +1393,10 @@ pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
|
||||
&encoding, &namespace_separator))
|
||||
return NULL;
|
||||
if (namespace_separator != NULL
|
||||
&& strlen(namespace_separator) != 1) {
|
||||
&& strlen(namespace_separator) > 1) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"namespace_separator must be one character,"
|
||||
" omitted, or None");
|
||||
"namespace_separator must be at most one"
|
||||
" character, omitted, or None");
|
||||
return NULL;
|
||||
}
|
||||
return newxmlparseobject(encoding, namespace_separator);
|
||||
@ -1429,10 +1432,6 @@ static struct PyMethodDef pyexpat_methods[] = {
|
||||
static char pyexpat_module_documentation[] =
|
||||
"Python wrapper for Expat parser.";
|
||||
|
||||
/* Initialization function for the module */
|
||||
|
||||
void initpyexpat(void); /* avoid compiler warnings */
|
||||
|
||||
#if PY_VERSION_HEX < 0x20000F0
|
||||
|
||||
/* 1.5 compatibility: PyModule_AddObject */
|
||||
@ -1486,11 +1485,23 @@ get_version_string(void)
|
||||
return PyString_FromStringAndSize(rev, i);
|
||||
}
|
||||
|
||||
/* Initialization function for the module */
|
||||
|
||||
#ifndef MODULE_NAME
|
||||
#define MODULE_NAME "pyexpat"
|
||||
#endif
|
||||
|
||||
#ifndef MODULE_INITFUNC
|
||||
#define MODULE_INITFUNC initpyexpat
|
||||
#endif
|
||||
|
||||
void MODULE_INITFUNC(void); /* avoid compiler warnings */
|
||||
|
||||
DL_EXPORT(void)
|
||||
initpyexpat(void)
|
||||
MODULE_INITFUNC(void)
|
||||
{
|
||||
PyObject *m, *d;
|
||||
PyObject *errmod_name = PyString_FromString("pyexpat.errors");
|
||||
PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
|
||||
PyObject *errors_module;
|
||||
PyObject *modelmod_name;
|
||||
PyObject *model_module;
|
||||
@ -1498,14 +1509,14 @@ initpyexpat(void)
|
||||
|
||||
if (errmod_name == NULL)
|
||||
return;
|
||||
modelmod_name = PyString_FromString("pyexpat.model");
|
||||
modelmod_name = PyString_FromString(MODULE_NAME ".model");
|
||||
if (modelmod_name == NULL)
|
||||
return;
|
||||
|
||||
Xmlparsetype.ob_type = &PyType_Type;
|
||||
|
||||
/* Create the module and add the functions */
|
||||
m = Py_InitModule3("pyexpat", pyexpat_methods,
|
||||
m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
|
||||
pyexpat_module_documentation);
|
||||
|
||||
/* Add some symbolic constants to the module */
|
||||
@ -1547,7 +1558,7 @@ initpyexpat(void)
|
||||
d = PyModule_GetDict(m);
|
||||
errors_module = PyDict_GetItem(d, errmod_name);
|
||||
if (errors_module == NULL) {
|
||||
errors_module = PyModule_New("pyexpat.errors");
|
||||
errors_module = PyModule_New(MODULE_NAME ".errors");
|
||||
if (errors_module != NULL) {
|
||||
PyDict_SetItem(sys_modules, errmod_name, errors_module);
|
||||
/* gives away the reference to errors_module */
|
||||
@ -1557,7 +1568,7 @@ initpyexpat(void)
|
||||
Py_DECREF(errmod_name);
|
||||
model_module = PyDict_GetItem(d, modelmod_name);
|
||||
if (model_module == NULL) {
|
||||
model_module = PyModule_New("pyexpat.model");
|
||||
model_module = PyModule_New(MODULE_NAME ".model");
|
||||
if (model_module != NULL) {
|
||||
PyDict_SetItem(sys_modules, modelmod_name, model_module);
|
||||
/* gives away the reference to model_module */
|
||||
@ -1633,10 +1644,13 @@ static void
|
||||
clear_handlers(xmlparseobject *self, int decref)
|
||||
{
|
||||
int i = 0;
|
||||
PyObject *temp;
|
||||
|
||||
for (; handler_info[i].name!=NULL; i++) {
|
||||
if (decref){
|
||||
Py_XDECREF(self->handlers[i]);
|
||||
if (decref) {
|
||||
temp = self->handlers[i];
|
||||
self->handlers[i] = NULL;
|
||||
Py_XDECREF(temp);
|
||||
}
|
||||
self->handlers[i]=NULL;
|
||||
handler_info[i].setter(self->itself, NULL);
|
||||
@ -1651,16 +1665,16 @@ pyxml_UpdatePairedHandlers(xmlparseobject *self,
|
||||
int endHandler,
|
||||
pairsetter setter)
|
||||
{
|
||||
void *start_handler=NULL;
|
||||
void *end_handler=NULL;
|
||||
void *start_handler = NULL;
|
||||
void *end_handler = NULL;
|
||||
|
||||
if (self->handlers[startHandler]
|
||||
&& self->handlers[endHandler]!=Py_None) {
|
||||
start_handler=handler_info[startHandler].handler;
|
||||
&& self->handlers[endHandler] != Py_None) {
|
||||
start_handler = handler_info[startHandler].handler;
|
||||
}
|
||||
if (self->handlers[EndElement]
|
||||
&& self->handlers[EndElement] !=Py_None) {
|
||||
end_handler=handler_info[endHandler].handler;
|
||||
&& self->handlers[EndElement] != Py_None) {
|
||||
end_handler = handler_info[endHandler].handler;
|
||||
}
|
||||
setter(self->itself, start_handler, end_handler);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user