Browse files

clean up some code in the serialiser

  • Loading branch information...
1 parent 09361b4 commit 64a1120596a61afb7b517738cd6c0a015a106359 @scoder scoder committed Apr 1, 2013
Showing with 28 additions and 25 deletions.
  1. +28 −25 src/lxml/serializer.pxi
View
53 src/lxml/serializer.pxi
@@ -332,8 +332,8 @@ cdef void _writePrevSiblings(tree.xmlOutputBuffer* c_buffer, xmlNode* c_node,
# we are at a root node, so add PI and comment siblings
c_sibling = c_node
while c_sibling.prev and \
- (c_sibling.prev.type == tree.XML_PI_NODE or \
- c_sibling.prev.type == tree.XML_COMMENT_NODE):
+ (c_sibling.prev.type == tree.XML_PI_NODE or
+ c_sibling.prev.type == tree.XML_COMMENT_NODE):
c_sibling = c_sibling.prev
while c_sibling is not c_node and not c_buffer.error:
tree.xmlNodeDumpOutput(c_buffer, c_node.doc, c_sibling, 0,
@@ -350,8 +350,8 @@ cdef void _writeNextSiblings(tree.xmlOutputBuffer* c_buffer, xmlNode* c_node,
# we are at a root node, so add PI and comment siblings
c_sibling = c_node.next
while not c_buffer.error and c_sibling and \
- (c_sibling.type == tree.XML_PI_NODE or \
- c_sibling.type == tree.XML_COMMENT_NODE):
+ (c_sibling.type == tree.XML_PI_NODE or
+ c_sibling.type == tree.XML_COMMENT_NODE):
if pretty_print:
tree.xmlOutputBufferWriteString(c_buffer, "\n")
tree.xmlNodeDumpOutput(c_buffer, c_node.doc, c_sibling, 0,
@@ -371,7 +371,7 @@ cdef class _FilelikeWriter:
def __cinit__(self, filelike, exc_context=None, compression=None):
if compression is not None and compression > 0:
filelike = gzip.GzipFile(
- fileobj=filelike, mode=u'wb', compresslevel=compression)
+ fileobj=filelike, mode='wb', compresslevel=compression)
self._close_filelike = filelike.close
self._filelike = filelike
if exc_context is None:
@@ -412,8 +412,8 @@ cdef class _FilelikeWriter:
self._exc_context._store_raised()
return -1
-cdef int _writeFilelikeWriter(void* ctxt, char* c_buffer, int len):
- return (<_FilelikeWriter>ctxt).write(c_buffer, len)
+cdef int _writeFilelikeWriter(void* ctxt, char* c_buffer, int length):
+ return (<_FilelikeWriter>ctxt).write(c_buffer, length)
cdef int _closeFilelikeWriter(void* ctxt):
return (<_FilelikeWriter>ctxt).close()
@@ -445,15 +445,15 @@ cdef _tofilelike(f, _Element element, encoding, doctype, method,
if compression:
bytes_out = BytesIO()
gzip_file = gzip.GzipFile(
- fileobj=bytes_out, mode=u'wb', compresslevel=compression)
+ fileobj=bytes_out, mode='wb', compresslevel=compression)
try:
gzip_file.write(data)
finally:
gzip_file.close()
data = bytes_out
if _isString(f):
filename8 = _encodeFilename(f)
- f = open(filename8, u'wb')
+ f = open(filename8, 'wb')
try:
f.write(data)
finally:
@@ -499,7 +499,7 @@ cdef _create_output_buffer(f, const_char* c_enc, int compression,
if c_buffer is NULL:
return python.PyErr_SetFromErrno(IOError) # raises IOError
writer = None
- elif hasattr(f, u'write'):
+ elif hasattr(f, 'write'):
writer = _FilelikeWriter(f, compression=compression)
c_buffer = writer._createOutputBuffer(enchandler)
else:
@@ -540,32 +540,35 @@ cdef _tofilelikeC14N(f, _Element element, bint exclusive, bint with_comments,
cdef char* c_filename
cdef xmlDoc* c_base_doc
cdef xmlDoc* c_doc
- cdef int bytes = -1
+ cdef int bytes_count, error = 0
c_base_doc = element._c_node.doc
c_doc = _fakeRootDoc(c_base_doc, element._c_node)
try:
- c_inclusive_ns_prefixes = _convert_ns_prefixes(c_doc.dict, inclusive_ns_prefixes) if inclusive_ns_prefixes else NULL
+ c_inclusive_ns_prefixes = (
+ _convert_ns_prefixes(c_doc.dict, inclusive_ns_prefixes)
+ if inclusive_ns_prefixes else NULL)
if _isString(f):
filename8 = _encodeFilename(f)
c_filename = _cstr(filename8)
with nogil:
- bytes = c14n.xmlC14NDocSave(c_doc, NULL, exclusive, c_inclusive_ns_prefixes,
- with_comments, c_filename, compression)
- elif hasattr(f, u'write'):
+ error = c14n.xmlC14NDocSave(
+ c_doc, NULL, exclusive, c_inclusive_ns_prefixes,
+ with_comments, c_filename, compression)
+ elif hasattr(f, 'write'):
writer = _FilelikeWriter(f, compression=compression)
c_buffer = writer._createOutputBuffer(NULL)
with writer.error_log:
- bytes = c14n.xmlC14NDocSaveTo(c_doc, NULL, exclusive, c_inclusive_ns_prefixes,
- with_comments, c_buffer)
- if bytes >= 0:
- bytes = tree.xmlOutputBufferClose(c_buffer)
- else:
- tree.xmlOutputBufferClose(c_buffer)
+ bytes_count = c14n.xmlC14NDocSaveTo(
+ c_doc, NULL, exclusive, c_inclusive_ns_prefixes,
+ with_comments, c_buffer)
+ error = tree.xmlOutputBufferClose(c_buffer)
+ if bytes_count < 0:
+ error = bytes_count
else:
- raise TypeError, \
- u"File or filename expected, got '%s'" % python._fqtypename(f).decode('UTF-8')
+ raise TypeError(u"File or filename expected, got '%s'" %
+ python._fqtypename(f).decode('UTF-8'))
finally:
_destroyFakeDoc(c_base_doc, c_doc)
if c_inclusive_ns_prefixes is not NULL:
@@ -574,13 +577,13 @@ cdef _tofilelikeC14N(f, _Element element, bint exclusive, bint with_comments,
if writer is not None:
writer._exc_context._raise_if_stored()
- if bytes < 0:
+ if error < 0:
message = u"C14N failed"
if writer is not None:
errors = writer.error_log
if len(errors):
message = errors[0].message
- raise C14NError, message
+ raise C14NError(message)
# incremental serialisation

0 comments on commit 64a1120

Please sign in to comment.