Skip to content

Commit

Permalink
Fix some more C string handling issues in Cython 3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Dec 30, 2023
1 parent 24beafa commit 7f6ca63
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/lxml/apihelpers.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,8 @@ cdef inline bint _nsTagMatchesExactly(const_xmlChar* c_node_href,
if c_qname.href is NULL:
return 1
c_href = python.__cstr(c_qname.href)
if c_href[0] == '\0':
return c_node_href is NULL or c_node_href[0] == '\0'
if c_href[0] == b'\0':
return c_node_href is NULL or c_node_href[0] == b'\0'
elif c_node_href is NULL:
return 0
else:
Expand Down
8 changes: 4 additions & 4 deletions src/lxml/parser.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ cdef const_char* _findEncodingName(const_xmlChar* buffer, int size):
cdef tree.xmlCharEncoding enc
enc = tree.xmlDetectCharEncoding(buffer, size)
if enc == tree.XML_CHAR_ENCODING_UTF16LE:
if size >= 4 and (buffer[0] == <const_xmlChar>'\xFF' and
buffer[1] == <const_xmlChar>'\xFE' and
if size >= 4 and (buffer[0] == <const_xmlChar> b'\xFF' and
buffer[1] == <const_xmlChar> b'\xFE' and
buffer[2] == 0 and buffer[3] == 0):
return "UTF-32LE" # according to BOM
else:
Expand Down Expand Up @@ -1106,13 +1106,13 @@ cdef class _BaseParser:
c_encoding = NULL
# libxml2 (at least 2.9.3) does not recognise UTF-32 BOMs
# NOTE: limit to problematic cases because it changes character offsets
if c_len >= 4 and (c_text[0] == '\xFF' and c_text[1] == '\xFE' and
if c_len >= 4 and (c_text[0] == b'\xFF' and c_text[1] == b'\xFE' and
c_text[2] == 0 and c_text[3] == 0):
c_encoding = "UTF-32LE"
c_text += 4
c_len -= 4
elif c_len >= 4 and (c_text[0] == 0 and c_text[1] == 0 and
c_text[2] == '\xFE' and c_text[3] == '\xFF'):
c_text[2] == b'\xFE' and c_text[3] == b'\xFF'):
c_encoding = "UTF-32BE"
c_text += 4
c_len -= 4
Expand Down
10 changes: 5 additions & 5 deletions src/lxml/serializer.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ cdef unsigned char *xmlSerializeHexCharRef(unsigned char *out, int val) noexcept
cdef xmlChar *ptr
cdef const xmlChar* hexdigits = b"0123456789ABCDEF"

out[0] = '&'
out[0] = b'&'
out += 1
out[0] = '#'
out[0] = b'#'
out += 1
out[0] = 'x'
out[0] = b'x'
out += 1

if val < 0x10:
Expand All @@ -434,7 +434,7 @@ cdef unsigned char *xmlSerializeHexCharRef(unsigned char *out, int val) noexcept
ptr -= 1
val >>= 4

out[0] = ';'
out[0] = b';'
out += 1
out[0] = 0

Expand Down Expand Up @@ -488,7 +488,7 @@ cdef _write_attr_string(tree.xmlOutputBuffer* buf, const char *string):
cur += 1
base = cur

elif cur[0] == '<':
elif cur[0] == b'<':
if base != cur:
tree.xmlOutputBufferWrite(buf, cur - base, base)

Expand Down
2 changes: 1 addition & 1 deletion src/lxml/xmlerror.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ cdef class _LogEntry:
if self._c_message is NULL:
return None
size = cstring_h.strlen(self._c_message)
if size > 0 and self._c_message[size-1] == '\n':
if size > 0 and self._c_message[size-1] == b'\n':
size -= 1 # strip EOL
# cannot use funicode() here because the message may contain
# byte encoded file paths etc.
Expand Down

0 comments on commit 7f6ca63

Please sign in to comment.