Gael Pasgrimaud avatar Gael Pasgrimaud committed 01a9157

root only work with py <3

Comments (0)

Files changed (2)

pyquery/pyquery.py

     def root(self):
         """return the xml root element
         """
+        if self._parent is not no_default:
+            return self._parent.getroottree()
         return self[0].getroottree()
 
     @property
     def encoding(self):
         """return the xml encoding of the root element
         """
-        return self.root.docinfo.encoding
+        root = self.root
+        if root is not None:
+            return self.root.docinfo.encoding
 
     ##############
     # Traversing #
 PY3k = sys.version_info >= (3,)
 
 if PY3k:
+    from io import StringIO
     import pyquery
     from pyquery.pyquery import PyQuery as pq
     from http.client import HTTPConnection
     pqa = pq
 else:
+    from cStringIO import StringIO
     import pyquery
     from httplib import HTTPConnection
     from webob import Request, Response, exc
            </html>
            """
 
+    @not_py3k
     def test_get_root(self):
-        doc = pq('<?xml version="1.0" encoding="UTF-8"?><root/>')
+        doc = pq('<?xml version="1.0" encoding="UTF-8"?><root><p/></root>')
+        self.assertEqual(isinstance(doc.root, etree._ElementTree), True)
         self.assertEqual(doc.encoding, 'UTF-8')
 
     def test_selector_from_doc(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.