Skip to content

Commit

Permalink
Remove some dead Py2 code.
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Jan 5, 2024
1 parent 5bba8c4 commit c13c5af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 43 deletions.
10 changes: 0 additions & 10 deletions src/lxml/includes/etree_defs.h
Expand Up @@ -51,16 +51,6 @@
# endif
#endif

#if IS_PYPY
# undef PyFile_AsFile
# define PyFile_AsFile(o) (NULL)
# undef PyByteArray_Check
# define PyByteArray_Check(o) (0)
#else
/* Python 3+ doesn't have PyFile_*() anymore */
# define PyFile_AsFile(o) (NULL)
#endif

#if IS_PYPY
# ifndef PyUnicode_FromFormat
# define PyUnicode_FromFormat PyString_FromFormat
Expand Down
47 changes: 14 additions & 33 deletions src/lxml/parser.pxi
Expand Up @@ -317,66 +317,47 @@ cdef class _FileReaderContext:
close()

cdef xmlparser.xmlParserInputBuffer* _createParserInputBuffer(self) noexcept:
cdef stdio.FILE* c_stream
cdef xmlparser.xmlParserInputBuffer* c_buffer
c_buffer = xmlparser.xmlAllocParserInputBuffer(0)
c_stream = python.PyFile_AsFile(self._filelike)
if c_stream is NULL:
cdef xmlparser.xmlParserInputBuffer* c_buffer = xmlparser.xmlAllocParserInputBuffer(0)
if c_buffer:
c_buffer.readcallback = _readFilelikeParser
c_buffer.context = <python.PyObject*>self
else:
c_buffer.readcallback = _readFileParser
c_buffer.context = c_stream
c_buffer.context = <python.PyObject*> self
return c_buffer

cdef xmlparser.xmlParserInput* _createParserInput(
self, xmlparser.xmlParserCtxt* ctxt) noexcept:
cdef xmlparser.xmlParserInputBuffer* c_buffer
c_buffer = self._createParserInputBuffer()
cdef xmlparser.xmlParserInputBuffer* c_buffer = self._createParserInputBuffer()
if not c_buffer:
return NULL
return xmlparser.xmlNewIOInputStream(ctxt, c_buffer, 0)

cdef tree.xmlDtd* _readDtd(self) noexcept:
cdef xmlparser.xmlParserInputBuffer* c_buffer
c_buffer = self._createParserInputBuffer()
cdef xmlparser.xmlParserInputBuffer* c_buffer = self._createParserInputBuffer()
if not c_buffer:
return NULL
with nogil:
return xmlparser.xmlIOParseDTD(NULL, c_buffer, 0)

cdef xmlDoc* _readDoc(self, xmlparser.xmlParserCtxt* ctxt, int options) noexcept:
cdef xmlDoc* result
cdef char* c_encoding
cdef stdio.FILE* c_stream
cdef xmlparser.xmlInputReadCallback c_read_callback
cdef xmlparser.xmlInputCloseCallback c_close_callback
cdef void* c_callback_context

if self._encoding is None:
c_encoding = NULL
else:
c_encoding = _cstr(self._encoding)

c_stream = python.PyFile_AsFile(self._filelike)
if c_stream is NULL:
c_read_callback = _readFilelikeParser
c_callback_context = <python.PyObject*>self
else:
c_read_callback = _readFileParser
c_callback_context = c_stream
cdef void* c_callback_context = <python.PyObject*> self
cdef char* c_encoding = _cstr(self._encoding) if self._encoding is not None else NULL

orig_options = ctxt.options
with nogil:
if ctxt.html:
result = htmlparser.htmlCtxtReadIO(
ctxt, c_read_callback, NULL, c_callback_context,
ctxt, _readFilelikeParser, NULL, c_callback_context,
self._c_url, c_encoding, options)
if result is not NULL:
if _fixHtmlDictNames(ctxt.dict, result) < 0:
tree.xmlFreeDoc(result)
result = NULL
else:
result = xmlparser.xmlCtxtReadIO(
ctxt, c_read_callback, NULL, c_callback_context,
ctxt, _readFilelikeParser, NULL, c_callback_context,
self._c_url, c_encoding, options)
ctxt.options = orig_options # work around libxml2 problem

try:
self._close_file()
except:
Expand Down

0 comments on commit c13c5af

Please sign in to comment.