Merge lp:~cjwatson/lazr.restful/py3-path-parts into lp:lazr.restful

Proposed by Colin Watson
Status: Merged
Merged at revision: 279
Proposed branch: lp:~cjwatson/lazr.restful/py3-path-parts
Merge into: lp:lazr.restful
Diff against target: 43 lines (+12/-3)
2 files modified
src/lazr/restful/docs/absoluteurl.rst (+7/-1)
src/lazr/restful/simple.py (+5/-2)
To merge this branch: bzr merge lp:~cjwatson/lazr.restful/py3-path-parts
Reviewer Review Type Date Requested Status
Cristian Gonzalez (community) Approve
Review via email: mp+396484@code.launchpad.net

Commit message

Fix __path_parts__ error handling for Python 3.

Description of the change

On Python 3, str objects have an __iter__ method. Fix MultiplePathPartAbsoluteURL.__str__ to account for that.

To post a comment you must log in.
Revision history for this message
Cristian Gonzalez (cristiangsp) wrote :

Good catch!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restful/docs/absoluteurl.rst'
2--- src/lazr/restful/docs/absoluteurl.rst 2020-02-04 11:52:59 +0000
3+++ src/lazr/restful/docs/absoluteurl.rst 2021-01-19 10:30:21 +0000
4@@ -205,7 +205,13 @@
5 >>> str(getMultiAdapter((resource, request), IAbsoluteURL))
6 Traceback (most recent call last):
7 ...
8- TypeError: Expected an iterable for __path_parts__.
9+ TypeError: Expected an iterable of strings for __path_parts__.
10+
11+ >>> resource.__path_parts__ = 0
12+ >>> str(getMultiAdapter((resource, request), IAbsoluteURL))
13+ Traceback (most recent call last):
14+ ...
15+ TypeError: Expected an iterable of strings for __path_parts__.
16
17 If the __parent__ or __path_parts__ is missing or None, an attempt to
18 get the URL raises the same exception as AbsoluteURL does.
19
20=== modified file 'src/lazr/restful/simple.py'
21--- src/lazr/restful/simple.py 2020-07-22 23:22:26 +0000
22+++ src/lazr/restful/simple.py 2021-01-19 10:30:21 +0000
23@@ -20,6 +20,7 @@
24
25 import traceback
26
27+import six
28 from six.moves.urllib.parse import (
29 quote,
30 unquote,
31@@ -370,8 +371,10 @@
32 parts = getattr(self.context, '__path_parts__', None)
33 if parts is None:
34 raise TypeError(_insufficientContext)
35- if not hasattr(parts, '__iter__'):
36- raise TypeError("Expected an iterable for __path_parts__.")
37+ if (isinstance(parts, six.string_types) or
38+ not hasattr(parts, '__iter__')):
39+ raise TypeError(
40+ "Expected an iterable of strings for __path_parts__.")
41
42 escaped_parts = [quote(part.encode('utf-8'), _safe) for part in parts]
43 return start_url + "/" + "/".join(escaped_parts)

Subscribers

People subscribed via source and target branches