[Lxml-checkins] r45875 - in lxml/branch/lxml-1.3: . src/lxml src/lxml/tests

scoder at codespeak.net scoder at codespeak.net
Mon Aug 20 11:14:58 CEST 2007


Author: scoder
Date: Mon Aug 20 11:14:56 2007
New Revision: 45875

Modified:
   lxml/branch/lxml-1.3/CHANGES.txt
   lxml/branch/lxml-1.3/src/lxml/apihelpers.pxi
   lxml/branch/lxml-1.3/src/lxml/tests/test_etree.py
Log:
raise Warning instead of Error for ':' tag names

Modified: lxml/branch/lxml-1.3/CHANGES.txt
==============================================================================
--- lxml/branch/lxml-1.3/CHANGES.txt	(original)
+++ lxml/branch/lxml-1.3/CHANGES.txt	Mon Aug 20 11:14:56 2007
@@ -24,6 +24,15 @@
 Other changes
 -------------
 
+* lxml now raises a TagNameWarning about tag names containing ':' instead of
+  an Error as 1.3.3 did.  The reason is that a number of projects currently
+  misuse the previous lack of tag name validation to generate namespace
+  prefixes without declaring namespaces.  Apart from the danger of generating
+  broken XML this way, it also breaks most of the namespace-aware tools in
+  XML, including XPath, XSLT and validation.  lxml 1.3.x will continue to
+  support this bug with a Warning, while lxml 2.0 will be strict about
+  well-formed tag names (not only regarding ':').
+
 * Serialising an Element no longer includes its comment and PI siblings (only
   ElementTree serialisation includes them).
 

Modified: lxml/branch/lxml-1.3/src/lxml/apihelpers.pxi
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/apihelpers.pxi	(original)
+++ lxml/branch/lxml-1.3/src/lxml/apihelpers.pxi	Mon Aug 20 11:14:56 2007
@@ -691,6 +691,17 @@
     else:
         raise TypeError, "Argument must be string or unicode."
 
+cdef object warnings
+import warnings
+class TagNameWarning(SyntaxWarning):
+    pass
+
+cdef int warnAboutTagName() except -1:
+    warnings.warn("Tag names must not contain ':', "
+                 "lxml 2.0 will enforce well-formed tag names "
+                 "as required by the XML specification.",
+                 TagNameWarning)
+
 cdef _getNsTag(tag):
     """Given a tag, find namespace URI and tag name.
     Return None for NS uri if no namespace URI available.
@@ -709,7 +720,7 @@
         if c_ns_end is NULL:
             raise ValueError, "Invalid tag name"
         if cstd.strchr(c_ns_end, c':') is not NULL:
-            raise ValueError, "Invalid tag name"
+            warnAboutTagName()
         nslen  = c_ns_end - c_tag
         taglen = python.PyString_GET_SIZE(tag) - nslen - 2
         if taglen == 0:
@@ -720,7 +731,7 @@
     elif python.PyString_GET_SIZE(tag) == 0:
         raise ValueError, "Empty tag name"
     elif cstd.strchr(c_tag, c':') is not NULL:
-        raise ValueError, "Invalid tag name"
+        warnAboutTagName()
     return ns, tag
 
 cdef object _namespacedName(xmlNode* c_node):

Modified: lxml/branch/lxml-1.3/src/lxml/tests/test_etree.py
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/tests/test_etree.py	(original)
+++ lxml/branch/lxml-1.3/src/lxml/tests/test_etree.py	Mon Aug 20 11:14:56 2007
@@ -8,7 +8,7 @@
 """
 
 
-import unittest, copy, sys
+import unittest, copy, sys, warnings
 
 from common_imports import etree, StringIO, HelperTestCase, fileInTestDir
 from common_imports import SillyFileLike, canonicalize, doctest
@@ -32,6 +32,8 @@
         seq.sort()
         return seq
 
+warnings.simplefilter("error", etree.TagNameWarning)
+
 class ETreeOnlyTestCase(HelperTestCase):
     """Tests only for etree, not ElementTree"""
     etree = etree
@@ -68,11 +70,14 @@
 
     def test_element_name_colon(self):
         Element = self.etree.Element
-        self.assertRaises(ValueError, Element, 'p:name')
-        self.assertRaises(ValueError, Element, '{test}p:name')
+        self.assertRaises(self.etree.TagNameWarning,
+                          Element, 'p:name')
+        self.assertRaises(self.etree.TagNameWarning,
+                          Element, '{test}p:name')
 
         el = Element('name')
-        self.assertRaises(ValueError, setattr, el, 'tag', 'p:name')
+        self.assertRaises(self.etree.TagNameWarning,
+                          setattr, el, 'tag', 'p:name')
 
     def test_attribute_set(self):
         # ElementTree accepts arbitrary attribute values


More information about the lxml-checkins mailing list