summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Walter Purcaro <vuolter@users.noreply.github.com> 2015-12-20 12:24:43 +0100
committerGravatar Walter Purcaro <vuolter@users.noreply.github.com> 2015-12-27 22:38:43 +0100
commit0f7b3e9d52fe6d1c81ba59c6062a755fc1c120ab (patch)
treecde20c03b1df77019eaf6984da4221d2312c12f8
parentSpare code fixes (diff)
downloadpyload-0f7b3e9d52fe6d1c81ba59c6062a755fc1c120ab.tar.xz
[misc] Fix encoding
-rw-r--r--module/plugins/internal/misc.py61
1 files changed, 30 insertions, 31 deletions
diff --git a/module/plugins/internal/misc.py b/module/plugins/internal/misc.py
index 9085106cd..692052029 100644
--- a/module/plugins/internal/misc.py
+++ b/module/plugins/internal/misc.py
@@ -218,22 +218,12 @@ def get_console_encoding(enc):
#@NOTE: Revert to `decode` in Python 3
-def decode(value, encoding=None):
+def decode(value, encoding=None, errors='strict'):
"""
- Encoded string (default to UTF-8) -> unicode string
+ Encoded string (default to own system encoding) -> unicode string
"""
if type(value) is str:
- try:
- # res = value.decode(encoding or 'utf-8')
- res = unicode(value, encoding or 'utf-8')
-
- except UnicodeDecodeError, e:
- if encoding:
- raise UnicodeDecodeError(e)
-
- encoding = get_console_encoding(sys.stdout.encoding)
- # res = value.decode(encoding)
- res = unicode(value, encoding)
+ res = unicode(value, encoding or get_console_encoding(sys.stdout.encoding), errors)
elif type(value) is unicode:
res = value
@@ -243,23 +233,31 @@ def decode(value, encoding=None):
return res
+
+def transcode(value, decoding, encoding):
+ return value.decode(decoding).encode(encoding)
+
-def encode(value, encoding=None, decoding=None):
+def encode(value, encoding='utf-8', errors='backslashreplace'):
"""
- Unicode or decoded string -> encoded string (default to UTF-8)
+ Unicode string -> encoded string (default to UTF-8)
"""
if type(value) is unicode:
- res = value.encode(encoding or "utf-8")
+ res = value.encode(encoding, errors)
- # elif type(value) is str:
- # res = encode(decode(value, decoding), encoding)
+ elif type(value) is str:
+ decoding = get_console_encoding(sys.stdin.encoding)
+ if encoding == decoding:
+ res = value
+ else:
+ res = transcode(decoding, encoding)
else:
res = str(value)
return res
-
-
+
+
def exists(path):
path = encode(path)
@@ -271,6 +269,14 @@ def exists(path):
return True
else:
return False
+
+
+def fsjoin(*args):
+ """
+ Like os.path.join, but encoding aware
+ (for safe-joining see `safejoin`)
+ """
+ return encode(os.path.join(args))
def remove_chars(value, repl):
@@ -305,14 +311,6 @@ def fixurl(url, unquote=None):
return url
-def fsjoin(*args):
- """
- Like os.path.join, but encoding aware
- (for safe-joining see `safejoin`)
- """
- return encode(os.path.join(args))
-
-
def truncate(name, length):
max_trunc = len(name) / 2
if length > max_trunc:
@@ -477,7 +475,7 @@ def which(filename):
else:
for path in os.environ['PATH'].split(os.pathsep):
- filename = os.path.join(path.strip('"'), program)
+ filename = os.path.join(path.strip('"'), filename)
if isexecutable(filename):
return filename
@@ -539,8 +537,9 @@ def replace_patterns(value, rules):
#@TODO: Remove in 0.4.10 and fix exp in CookieJar.setCookie
-def set_cookie(cj, domain, name, value, path='/', exp=time.time() + 180 * 24 * 3600):
- return cj.setCookie(encode(domain), encode(name), encode(value), encode(path), int(exp))
+def set_cookie(cj, *args, path='/', exp=time.time() + 180 * 24 * 3600):
+ args = map(encode, args) + [encode(path), int(exp)]
+ return cj.setCookie(*args)
def set_cookies(cj, cookies):