mirror of
https://github.com/golang/go
synced 2024-11-24 15:30:13 -07:00
codereview: more ascii vs unicode nonsense
R=r CC=golang-dev https://golang.org/cl/4124047
This commit is contained in:
parent
05b9050bda
commit
dc9a02fa66
@ -139,6 +139,32 @@ def typecheck(s, t):
|
|||||||
if type(s) != t:
|
if type(s) != t:
|
||||||
raise util.Abort("type check failed: %s has type %s != %s" % (repr(s), type(s), t))
|
raise util.Abort("type check failed: %s has type %s != %s" % (repr(s), type(s), t))
|
||||||
|
|
||||||
|
# If we have to pass unicode instead of str, ustr does that conversion clearly.
|
||||||
|
def ustr(s):
|
||||||
|
typecheck(s, str)
|
||||||
|
return s.decode("utf-8")
|
||||||
|
|
||||||
|
# Even with those, Mercurial still sometimes turns unicode into str
|
||||||
|
# and then tries to use it as ascii. Change Mercurial's default.
|
||||||
|
def set_mercurial_encoding_to_utf8():
|
||||||
|
from mercurial import encoding
|
||||||
|
encoding.encoding = 'utf-8'
|
||||||
|
|
||||||
|
set_mercurial_encoding_to_utf8()
|
||||||
|
|
||||||
|
# Even with those we still run into problems.
|
||||||
|
# I tried to do things by the book but could not convince
|
||||||
|
# Mercurial to let me check in a change with UTF-8 in the
|
||||||
|
# CL description or author field, no matter how many conversions
|
||||||
|
# between str and unicode I inserted and despite changing the
|
||||||
|
# default encoding. I'm tired of this game, so set the default
|
||||||
|
# encoding for all of Python to 'utf-8', not 'ascii'.
|
||||||
|
def default_to_utf8():
|
||||||
|
import sys
|
||||||
|
reload(sys) # site.py deleted setdefaultencoding; get it back
|
||||||
|
sys.setdefaultencoding('utf-8')
|
||||||
|
|
||||||
|
default_to_utf8()
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
# Change list parsing.
|
# Change list parsing.
|
||||||
@ -1319,6 +1345,9 @@ def submit(ui, repo, *pats, **opts):
|
|||||||
if missing_codereview:
|
if missing_codereview:
|
||||||
return missing_codereview
|
return missing_codereview
|
||||||
|
|
||||||
|
# We already called this on startup but sometimes Mercurial forgets.
|
||||||
|
set_mercurial_encoding_to_utf8()
|
||||||
|
|
||||||
repo.ui.quiet = True
|
repo.ui.quiet = True
|
||||||
if not opts["no_incoming"] and Incoming(ui, repo, opts):
|
if not opts["no_incoming"] and Incoming(ui, repo, opts):
|
||||||
return "local repository out of date; must sync before submit"
|
return "local repository out of date; must sync before submit"
|
||||||
@ -1331,6 +1360,7 @@ def submit(ui, repo, *pats, **opts):
|
|||||||
if cl.copied_from:
|
if cl.copied_from:
|
||||||
user = cl.copied_from
|
user = cl.copied_from
|
||||||
userline = CheckContributor(ui, repo, user)
|
userline = CheckContributor(ui, repo, user)
|
||||||
|
typecheck(userline, str)
|
||||||
|
|
||||||
about = ""
|
about = ""
|
||||||
if cl.reviewer:
|
if cl.reviewer:
|
||||||
@ -1360,6 +1390,7 @@ def submit(ui, repo, *pats, **opts):
|
|||||||
|
|
||||||
if cl.copied_from:
|
if cl.copied_from:
|
||||||
about += "\nCommitter: " + CheckContributor(ui, repo, None) + "\n"
|
about += "\nCommitter: " + CheckContributor(ui, repo, None) + "\n"
|
||||||
|
typecheck(about, str)
|
||||||
|
|
||||||
if not cl.mailed and not cl.copied_from: # in case this is TBR
|
if not cl.mailed and not cl.copied_from: # in case this is TBR
|
||||||
cl.Mail(ui, repo)
|
cl.Mail(ui, repo)
|
||||||
@ -1368,7 +1399,9 @@ def submit(ui, repo, *pats, **opts):
|
|||||||
date = opts.get('date')
|
date = opts.get('date')
|
||||||
if date:
|
if date:
|
||||||
opts['date'] = util.parsedate(date)
|
opts['date'] = util.parsedate(date)
|
||||||
|
typecheck(opts['date'], str)
|
||||||
opts['message'] = cl.desc.rstrip() + "\n\n" + about
|
opts['message'] = cl.desc.rstrip() + "\n\n" + about
|
||||||
|
typecheck(opts['message'], str)
|
||||||
|
|
||||||
if opts['dryrun']:
|
if opts['dryrun']:
|
||||||
print "NOT SUBMITTING:"
|
print "NOT SUBMITTING:"
|
||||||
@ -1380,7 +1413,7 @@ def submit(ui, repo, *pats, **opts):
|
|||||||
return "dry run; not submitted"
|
return "dry run; not submitted"
|
||||||
|
|
||||||
m = match.exact(repo.root, repo.getcwd(), cl.files)
|
m = match.exact(repo.root, repo.getcwd(), cl.files)
|
||||||
node = repo.commit(opts['message'], userline, opts.get('date'), m)
|
node = repo.commit(ustr(opts['message']), ustr(userline), opts.get('date'), m)
|
||||||
if not node:
|
if not node:
|
||||||
return "nothing changed"
|
return "nothing changed"
|
||||||
|
|
||||||
@ -1821,7 +1854,7 @@ def MySend1(request_path, payload=None,
|
|||||||
|
|
||||||
def GetForm(url):
|
def GetForm(url):
|
||||||
f = FormParser()
|
f = FormParser()
|
||||||
f.feed(MySend(url).decode("utf-8")) # f.feed wants unicode
|
f.feed(ustr(MySend(url))) # f.feed wants unicode
|
||||||
f.close()
|
f.close()
|
||||||
# convert back to utf-8 to restore sanity
|
# convert back to utf-8 to restore sanity
|
||||||
m = {}
|
m = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user