[Lxml-checkins] r35415 - in lxml/trunk: . src/lxml src/lxml/tests

scoder at codespeak.net scoder at codespeak.net
Thu Dec 7 08:59:56 CET 2006


Author: scoder
Date: Thu Dec  7 08:59:52 2006
New Revision: 35415

Modified:
   lxml/trunk/CHANGES.txt
   lxml/trunk/src/lxml/python.pxd
   lxml/trunk/src/lxml/serializer.pxi
   lxml/trunk/src/lxml/tests/test_elementtree.py
Log:
ElementTree.write() did not raise an exception when the file was not writable

Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt	(original)
+++ lxml/trunk/CHANGES.txt	Thu Dec  7 08:59:52 2006
@@ -23,6 +23,8 @@
 Bugs fixed
 ----------
 
+* ElementTree.write() did not raise an exception when the file was not writable
+
 * Error handling could crash under Python <= 2.4.1 - fixed by disabling thread
   support in these environments
 

Modified: lxml/trunk/src/lxml/python.pxd
==============================================================================
--- lxml/trunk/src/lxml/python.pxd	(original)
+++ lxml/trunk/src/lxml/python.pxd	Thu Dec  7 08:59:52 2006
@@ -73,7 +73,10 @@
     cdef void* PyMem_Malloc(size_t size)
     cdef void* PyMem_Realloc(void* p, size_t size)
     cdef void PyMem_Free(void* p)
-    cdef object PyErr_NoMemory() # always returns NULL to pass on exception
+
+    # these two always return NULL to pass on the exception
+    cdef object PyErr_NoMemory()
+    cdef object PyErr_SetFromErrno(object type)
 
     ctypedef enum PyGILState_STATE:
         PyGILState_LOCKED

Modified: lxml/trunk/src/lxml/serializer.pxi
==============================================================================
--- lxml/trunk/src/lxml/serializer.pxi	(original)
+++ lxml/trunk/src/lxml/serializer.pxi	Thu Dec  7 08:59:52 2006
@@ -166,6 +166,8 @@
         filename8 = _encodeFilename(f)
         c_buffer = tree.xmlOutputBufferCreateFilename(
             _cstr(filename8), enchandler, 0)
+        if c_buffer is NULL:
+            python.PyErr_SetFromErrno(IOError)
         state = python.PyEval_SaveThread()
     elif hasattr(f, 'write'):
         writer   = _FilelikeWriter(f)
@@ -217,10 +219,11 @@
         writer._exc_context._raise_if_stored()
 
     if bytes < 0:
-        if writer is not None and len(writer.error_log):
-            message = writer.error_log[0].message
-        else:
-            message = "C14N failed"
+        message = "C14N failed"
+        if writer is not None:
+            errors = writer.error_log
+            if len(errors):
+                message = errors[0].message
         raise C14NError, message
 
 # dump node to file (mainly for debug)

Modified: lxml/trunk/src/lxml/tests/test_elementtree.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_elementtree.py	(original)
+++ lxml/trunk/src/lxml/tests/test_elementtree.py	Thu Dec  7 08:59:52 2006
@@ -623,6 +623,14 @@
             self.assertEquals(
                 '<doc%s>This is a test.</doc%s>' % (i, i),
                 canonicalize(data))
+        
+    def test_write_fail(self):
+        ElementTree = self.etree.ElementTree
+        XML = self.etree.XML
+
+        tree = ElementTree( XML('<doc>This is a test.</doc>') )
+        self.assertRaises(IOError, tree.write,
+                          "definitely////\\-\\nonexisting\\-\\////FILE")
 
     # this could trigger a crash, apparently because the document
     # reference was prematurely garbage collected


More information about the lxml-checkins mailing list