##// END OF EJS Templates
git: use non-unicode author extraction as it's returned as bytes from backend, and we can...
marcink -
r825:dc8df3e0 stable
parent child Browse files
Show More
@@ -39,7 +39,7 b' from dulwich.repo import Repo as Dulwich'
39 from dulwich.server import update_server_info
39 from dulwich.server import update_server_info
40
40
41 from vcsserver import exceptions, settings, subprocessio
41 from vcsserver import exceptions, settings, subprocessio
42 from vcsserver.utils import safe_str, safe_int
42 from vcsserver.utils import safe_str, safe_int, safe_unicode
43 from vcsserver.base import RepoFactory, obfuscate_qs
43 from vcsserver.base import RepoFactory, obfuscate_qs
44 from vcsserver.hgcompat import (
44 from vcsserver.hgcompat import (
45 hg_url as url_parser, httpbasicauthhandler, httpdigestauthhandler)
45 hg_url as url_parser, httpbasicauthhandler, httpdigestauthhandler)
@@ -840,7 +840,11 b' class GitRemote(RemoteBase):'
840 if author.email:
840 if author.email:
841 return u"{} <{}>".format(author.name, author.email)
841 return u"{} <{}>".format(author.name, author.email)
842
842
843 return u"{}".format(author.raw_name)
843 try:
844 return u"{}".format(author.name)
845 except Exception:
846 return u"{}".format(safe_unicode(author.raw_name))
847
844 return _author(repo_id, commit_id)
848 return _author(repo_id, commit_id)
845
849
846 @reraise_safe_exceptions
850 @reraise_safe_exceptions
@@ -37,18 +37,16 b' def safe_int(val, default=None):'
37 return val
37 return val
38
38
39
39
40 def safe_str(unicode_, to_encoding=['utf8']):
40 def safe_str(unicode_, to_encoding=None):
41 """
41 """
42 safe str function. Does few trick to turn unicode_ into string
42 safe str function. Does few trick to turn unicode_ into string
43
43
44 In case of UnicodeEncodeError, we try to return it with encoding detected
45 by chardet library if it fails fallback to string with errors replaced
46
47 :param unicode_: unicode to encode
44 :param unicode_: unicode to encode
45 :param to_encoding: encode to this type UTF8 default
48 :rtype: str
46 :rtype: str
49 :returns: str object
47 :returns: str object
50 """
48 """
51
49 to_encoding = to_encoding or ['utf8']
52 # if it's not basestr cast to str
50 # if it's not basestr cast to str
53 if not isinstance(unicode_, basestring):
51 if not isinstance(unicode_, basestring):
54 return str(unicode_)
52 return str(unicode_)
@@ -65,15 +63,38 b" def safe_str(unicode_, to_encoding=['utf"
65 except UnicodeEncodeError:
63 except UnicodeEncodeError:
66 pass
64 pass
67
65
66 return unicode_.encode(to_encoding[0], 'replace')
67
68
69 def safe_unicode(str_, from_encoding=None):
70 """
71 safe unicode function. Does few trick to turn str_ into unicode
72
73 :param str_: string to decode
74 :param from_encoding: encode from this type UTF8 default
75 :rtype: unicode
76 :returns: unicode object
77 """
78 from_encoding = from_encoding or ['utf8']
79
80 if isinstance(str_, unicode):
81 return str_
82
83 if not isinstance(from_encoding, (list, tuple)):
84 from_encoding = [from_encoding]
85
68 try:
86 try:
69 import chardet
87 return unicode(str_)
70 encoding = chardet.detect(unicode_)['encoding']
88 except UnicodeDecodeError:
71 if encoding is None:
89 pass
72 raise UnicodeEncodeError()
73
90
74 return unicode_.encode(encoding)
91 for enc in from_encoding:
75 except (ImportError, UnicodeEncodeError):
92 try:
76 return unicode_.encode(to_encoding[0], 'replace')
93 return unicode(str_, enc)
94 except UnicodeDecodeError:
95 pass
96
97 return unicode(str_, from_encoding[0], 'replace')
77
98
78
99
79 class AttributeDict(dict):
100 class AttributeDict(dict):
General Comments 0
You need to be logged in to leave comments. Login now