Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Include/internal/pycore_pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ PyAPI_FUNC(PyObject*) _PyErr_FormatFromCause(
...
);

extern int _PyException_AddNote(
// Export for 'pyexpat' shared extension.
PyAPI_FUNC(int) _PyException_AddNote(
PyObject *exc,
PyObject *note);

Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_pyexpat.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,34 @@ def _test_exception(self, have_source):
self.assertIn('call_with_frame("StartElement"',
entries[1].line)

def test_invalid_NotStandalone(self):
parser = expat.ParserCreate()
parser.NotStandaloneHandler = mock.Mock(return_value="bad value")
parser.ElementDeclHandler = lambda _1, _2: None

payload = b"""\
<!DOCTYPE quotations SYSTEM "quotations.dtd" [<!ELEMENT root ANY>]><root/>
"""
with self.assertRaises(TypeError) as cm:
parser.Parse(payload, True)
parser.NotStandaloneHandler.assert_called_once()

notes = ["invalid 'NotStandalone' event handler return value"]
self.assertEqual(cm.exception.__notes__, notes)

def test_invalid_ExternalEntityRefHandler(self):
parser = expat.ParserCreate()
parser.UseForeignDTD()
parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS)
parser.ExternalEntityRefHandler = mock.Mock(return_value=None)

with self.assertRaises(TypeError) as cm:
parser.Parse(b"<?xml version='1.0'?><element/>", True)
parser.ExternalEntityRefHandler.assert_called_once()

notes = ["invalid 'ExternalEntityRef' event handler return value"]
self.assertEqual(cm.exception.__notes__, notes)


# Test Current* members:
class PositionTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`xml.parsers.expat`: add an exception note when a custom Expat handler
return value cannot be properly interpreted. Patch by Bénédikt Tran.
25 changes: 25 additions & 0 deletions Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,28 @@ my_StartElementHandler(void *userData,
}
}

static inline void
invalid_expat_handler_rv(const char *name)
{
PyObject *exc = PyErr_GetRaisedException();
assert(exc != NULL);
PyObject *note = PyUnicode_FromFormat("invalid '%s' event handler return value", name);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@encukou Do you have a better wording for this? maybe we should say "expecting X and not Y" ? as it's invoked from a macro I can add more arguments if necessary.

if (note == NULL) {
goto error;
}
int rc = _PyException_AddNote(exc, note);
Py_DECREF(note);
if (rc < 0) {
goto error;
};
goto done;

error:
PyErr_Clear();
done:
PyErr_SetRaisedException(exc);
}

#define RC_HANDLER(RETURN_TYPE, NAME, PARAMS, \
INIT, PARSE_FORMAT, CONVERSION, \
RETURN_VARIABLE, GETUSERDATA) \
Expand Down Expand Up @@ -536,6 +558,9 @@ my_ ## NAME ## Handler PARAMS { \
} \
CONVERSION \
Py_DECREF(rv); \
if (PyErr_Occurred()) { \
invalid_expat_handler_rv(#NAME); \
} \
return RETURN_VARIABLE; \
}

Expand Down
Loading