[py3k] unicode vs str vs bytes vs the world
authorRémi Cardona <remi.cardona@logilab.fr>
Tue, 22 Sep 2015 14:18:14 +0200
changeset 10694 7ece2df9cc5c
parent 10693 be0bd5aa21b4
child 10695 321b99973b69
[py3k] unicode vs str vs bytes vs the world
req.py
web/test/unittest_views_staticcontrollers.py
web/views/staticcontrollers.py
--- a/req.py	Wed Sep 16 16:07:29 2015 +0200
+++ b/req.py	Tue Sep 22 14:18:14 2015 +0200
@@ -22,7 +22,7 @@
 from warnings import warn
 from datetime import time, datetime, timedelta
 
-from six import PY2, text_type
+from six import PY2, PY3, text_type
 from six.moves.urllib.parse import parse_qs, parse_qsl, quote as urlquote, unquote as urlunquote, urlsplit, urlunsplit
 
 from logilab.common.decorators import cached
@@ -324,6 +324,8 @@
         decoding is based on `self.encoding` which is the encoding
         used in `url_quote`
         """
+        if PY3:
+            return urlunquote(quoted)
         if isinstance(quoted, unicode):
             quoted = quoted.encode(self.encoding)
         try:
@@ -333,6 +335,9 @@
 
     def url_parse_qsl(self, querystring):
         """return a list of (key, val) found in the url quoted query string"""
+        if PY3:
+            for key, val in parse_qsl(querystring):
+                yield key, val
         if isinstance(querystring, unicode):
             querystring = querystring.encode(self.encoding)
         for key, val in parse_qsl(querystring):
--- a/web/test/unittest_views_staticcontrollers.py	Wed Sep 16 16:07:29 2015 +0200
+++ b/web/test/unittest_views_staticcontrollers.py	Tue Sep 22 14:18:14 2015 +0200
@@ -120,12 +120,12 @@
             yield res, req
 
     def expected_content(self, js_files):
-        content = u''
+        content = b''
         for js_file in js_files:
             dirpath, rid = self.config.locate_resource(js_file)
             if dirpath is not None: # ignore resources not found
-                with open(osp.join(dirpath, rid)) as f:
-                    content += f.read() + '\n'
+                with open(osp.join(dirpath, rid), 'rb') as f:
+                    content += f.read() + b'\n'
         return content
 
     def test_cache(self):
--- a/web/views/staticcontrollers.py	Wed Sep 16 16:07:29 2015 +0200
+++ b/web/views/staticcontrollers.py	Tue Sep 22 14:18:14 2015 +0200
@@ -167,7 +167,7 @@
                             with open(osp.join(dirpath, rid), 'rb') as source:
                                 for line in source:
                                     f.write(line)
-                            f.write('\n')
+                            f.write(b'\n')
                     f.close()
                 except:
                     os.remove(tmpfile)