summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-02-12 18:37:00 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-02-24 10:39:04 +0100
commitfc2e9544c472d6ab49982a875bd775d1493db64f (patch)
treef84523d06b4c147f7dedf6a147ed4fa8e56dcb1a
parent20f8448045635e2131e7a7480688e87b43b2136e (diff)
downloadurlgrabber-fc2e9544c472d6ab49982a875bd775d1493db64f.tar.gz
Do encoding/decoding in the subprocess calls
s.endswith('x') works properly with bytes. s[-1] == 'x' doesn't, because s[-1] returns an integer. But .endswith() is clearer anyway, so that's OK.
-rw-r--r--urlgrabber/grabber.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/urlgrabber/grabber.py b/urlgrabber/grabber.py
index c0a0572..b696472 100644
--- a/urlgrabber/grabber.py
+++ b/urlgrabber/grabber.py
@@ -2041,9 +2041,14 @@ def _dumps(v):
if v is False: return 'False'
if isinstance(v, numbers.Number):
return str(v)
- if isinstance(v, text_type):
- v = v.encode('UTF8')
- if isinstance(v, str):
+ if isinstance(v, (str, text_type, bytes)):
+ # standarize to str on both py2 to py3
+ if sys.version_info < (3,):
+ if isinstance(v, text_type):
+ v = v.encode('utf8')
+ else:
+ if isinstance(v, bytes):
+ v = v.decode('utf8')
return "'%s'" % ''.join(map(_quoter, v))
if isinstance(v, tuple):
return "(%s)" % ','.join(map(_dumps, v))
@@ -2103,9 +2108,9 @@ def _readlines(fd):
buf = os.read(fd, 4096)
if not buf: return None
# whole lines only, no buffering
- while buf[-1] != '\n':
+ while not buf.endswith(b'\n'):
buf += os.read(fd, 4096)
- return buf[:-1].split('\n')
+ return buf[:-1].split(b'\n')
import subprocess
@@ -2153,7 +2158,7 @@ class _ExternalDownloader:
self.cnt += 1
self.running[self.cnt] = opts
- os.write(self.stdin, arg +'\n')
+ os.write(self.stdin, (arg +'\n').encode('utf8'))
def perform(self):
ret = []