I think it might be possible to further improve this by matching up more closely with what libxml2 does in its xmlOutputBufferClose() and looking at the buffer error state before calling it. PR welcome for that. Probably worth a helper function that wraps the call.
This should improve the situation: /github. com/lxml/ lxml/commit/ cfceec54a8d5b68 4e2572b02addf0a df5e786f2f
https:/
While it's still possible that the serialised data size hits exactly -1 on integer wrap-around, it's much less likely than hitting a negative number.
I think it might be possible to further improve this by matching up more closely with what libxml2 does in its xmlOutputBuffer Close() and looking at the buffer error state before calling it. PR welcome for that. Probably worth a helper function that wraps the call.