Revision: 4289
Author: pekka.klarck
Date: Wed Nov 17 08:03:22 2010
Log: workaround for elementtree 1.2.7 but with ironpython, related to issue
154
http://code.google.com/p/robotframework/source/detail?r=4289
Modified:
/trunk/src/robot/utils/domwrapper.py
=======================================
--- /trunk/src/robot/utils/domwrapper.py Thu Nov 11 09:24:29 2010
+++ /trunk/src/robot/utils/domwrapper.py Wed Nov 17 08:03:22 2010
@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import sys
from StringIO import StringIO
try:
import xml.etree.cElementTree as ET
@@ -32,11 +33,6 @@
class DomWrapper(object):
- """A wrapper for Python's XML DOM for simplifying reading data from it.
-
- See documentation of AbstractDomWrapper for further usage information.
- """
-
def __init__(self, path=None, string=None, node=None):
"""Initialize by giving 'path' to an xml file or xml as a 'string'.
@@ -49,20 +45,30 @@
self.name = node.tag
self.attrs = dict(node.items())
self.text = node.text or ''
- self.children = [ DomWrapper(path, node=child) for child in
list(node) ]
+ self.children = [DomWrapper(path, node=child) for child in
list(node)]
def _get_node(self, path, string, node):
# This should NOT be changed to 'if not node:'. See chapter Truth
Testing
# from http://effbot.org/zone/element.htm#the-element-type
if node is not None:
return node
- # Cannot give path to ET.parse because it doesn't close files it
opens
- # http://bugs.jython.org/issue1598
- source = open(path, 'rb') if path else StringIO(string)
+ source = self._get_source(path, string)
try:
return ET.parse(source).getroot()
finally:
- source.close()
+ if hasattr(source, 'close'):
+ source.close()
+
+ def _get_source(self, path, string):
+ if not path:
+ return StringIO(string)
+ # ElementTree 1.2.7 preview (first ET with IronPython support)
doesn't
+ # handler non-ASCII chars correctly if an open file given to it.
+ if sys.platform == 'cli':
+ return path
+ # ET.parse doesn't close files it opens, which causes serious
problems
+ # with Jython 2.5(.1) on Windows: http://bugs.jython.org/issue1598
+ return open(path, 'rb')
def get_nodes(self, path):
"""Returns a list of descendants matching given 'path'.