Skip to content

Commit

Permalink
Add type checking for the capsule
Browse files Browse the repository at this point in the history
adopt_external_document() should type check its argument
  • Loading branch information
kovidgoyal committed Feb 19, 2017
1 parent 9585896 commit 9b7a17c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lxml/includes/etree_defs.h
Expand Up @@ -267,6 +267,12 @@ static void* lxml_unpack_xmldoc_capsule(PyObject* capsule, int* is_owned) {
xmlDoc *c_doc;
void *context;
*is_owned = 0;
if (unlikely_condition(!PyCapsule_IsValid(capsule, (const char*)"libxml2:xmlDoc"))) {
PyErr_SetString(
PyExc_TypeError,
"Not a valid capsule. The capsule argument must be a capsule object with name libxml2:xmlDoc");
return NULL;
}
c_doc = (xmlDoc*) PyCapsule_GetPointer(capsule, (const char*)"libxml2:xmlDoc");
if (unlikely_condition(!c_doc)) return NULL;

Expand Down
26 changes: 26 additions & 0 deletions src/lxml/tests/test_external_document.py
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
Test cases related to direct loading of external libxml2 documents
"""

import unittest
import sys

from common_imports import etree, HelperTestCase


@unittest.skipIf(sys.version_info[:2] < (2, 7),
'Not supported for python < 2.7')
class ExternalDocumentTestCase(HelperTestCase):
def test_external_document_type_checking(self):
self.assertRaises(TypeError, etree.adopt_external_document, None)


def test_suite():
suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ExternalDocumentTestCase)])
return suite


if __name__ == '__main__':
print('to test use test.py %s' % __file__)

0 comments on commit 9b7a17c

Please sign in to comment.