Merge lp:~rharding/wadllib/fix_tests_922599 into lp:wadllib

Proposed by Richard Harding on 2012-01-27
Status: Merged
Merged at revision: 24
Proposed branch: lp:~rharding/wadllib/fix_tests_922599
Merge into: lp:wadllib
Diff against target: 58 lines (+13/-6)
1 file modified
src/wadllib/application.py (+13/-6)
To merge this branch: bzr merge lp:~rharding/wadllib/fix_tests_922599
Reviewer Review Type Date Requested Status
Benji York (community) code 2012-01-27 Approve on 2012-01-27
Review via email: mp+90492@code.launchpad.net

Commit Message

Update to using BytesIO and catching string v stream inputs.

Description of the Change

= Summary =
Tests are failing under oneriec python 2

== Implementation Details ==
Most of this was put together by allenap but the idea is to try to standardize the input into the old from_string (now from_stream) parser so that in py3 and py2 both get a byte stream into the iterator.

== Tests ==
python setup.py test

To post a comment you must log in.
Benji York (benji) wrote :

This looks good to me. We might want a comment in _from_string about
how it looks like a noop (ET.tostring(ET.fromstring(...)) but it's
actually there for Python 2/3 compatibility.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/wadllib/application.py'
2--- src/wadllib/application.py 2012-01-18 10:31:21 +0000
3+++ src/wadllib/application.py 2012-01-27 18:15:28 +0000
4@@ -45,9 +45,9 @@
5 # Make sure to try the Python 2 form first; we don't want to use
6 # io.StringIO in Python 2, because it is strict about only accepting
7 # unicode input.
8- from cStringIO import StringIO
9+ from cStringIO import StringIO as BytesIO
10 except ImportError:
11- from io import StringIO
12+ from io import BytesIO
13 import datetime
14 try:
15 from email.mime.multipart import MIMEMultipart
16@@ -1066,8 +1066,9 @@
17 """
18 self.markup_url = markup_url
19 if hasattr(markup, 'read'):
20- markup = markup.read()
21- self.doc = self._from_string(_make_unicode(markup))
22+ self.doc = self._from_stream(markup)
23+ else:
24+ self.doc = self._from_string(markup)
25 self.resources = self.doc.find(wadl_xpath('resources'))
26 self.resource_base = self.resources.attrib.get('base')
27 self.representation_definitions = {}
28@@ -1082,7 +1083,7 @@
29 id = resource_type.attrib['id']
30 self.resource_types[id] = ResourceType(resource_type)
31
32- def _from_string(self, markup):
33+ def _from_stream(self, stream):
34 """Turns markup into a document.
35
36 Just a wrapper around ElementTree which keeps track of namespaces.
37@@ -1091,7 +1092,7 @@
38 root = None
39 ns_map = []
40
41- for event, elem in ET.iterparse(StringIO(markup), events):
42+ for event, elem in ET.iterparse(stream, events):
43 if event == "start-ns":
44 ns_map.append(elem)
45 elif event == "end-ns":
46@@ -1102,6 +1103,12 @@
47 elem.set(NS_MAP, dict(ns_map))
48 return ET.ElementTree(root)
49
50+ def _from_string(self, markup):
51+ """Turns markup into a document.
52+
53+ Just a wrapper around ElementTree which keeps track of namespaces.
54+ """
55+ return self._from_stream(BytesIO(ET.tostring(ET.fromstring(markup))))
56
57 def get_resource_type(self, resource_type_url):
58 """Retrieve a resource type by the URL of its description."""

Subscribers

People subscribed via source and target branches