[PATCH 3 of 3] mail: fix handling of email addresses with Unicode domains (IDNA)

Marti Raudsepp marti at juffo.org
Wed Nov 4 11:53:26 CST 2009


# HG changeset patch
# User Marti Raudsepp <marti at juffo.org>
# Date 1257356232 -7200
# Node ID f20bad63a3d374b38c1c1fdde05988e09bde8a00
# Parent  e54459b6f1e21ebf847bbfb5d468eb25b179ee53
mail: fix handling of email addresses with Unicode domains (IDNA)

dom.encode('idna') requires dom to be a Unicode string.

This changeset fixes the following error:
  [...]
  File "./mercurial/mail.py", line 197, in addrlistencode
    result.append(_addressencode(ui, name, addr, charsets))
  File "./mercurial/mail.py", line 168, in _addressencode
    dom = dom.encode('idna')
  File "/usr/lib/python2.6/encodings/idna.py", line 164, in encode
    result.append(ToASCII(label))
  File "/usr/lib/python2.6/encodings/idna.py", line 76, in ToASCII
    label = nameprep(label)
  File "/usr/lib/python2.6/encodings/idna.py", line 21, in nameprep
    newlabel.append(stringprep.map_table_b2(c))
  File "/usr/lib/python2.6/stringprep.py", line 197, in map_table_b2
    b = unicodedata.normalize("NFKC", al)
TypeError: normalize() argument 2 must be unicode, not str

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -165,7 +165,7 @@
     try:
         acc, dom = addr.split('@')
         acc = acc.encode('ascii')
-        dom = dom.encode('idna')
+        dom = dom.decode(encoding.encoding).encode('idna')
         addr = '%s@%s' % (acc, dom)
     except UnicodeDecodeError:
         raise util.Abort(_('invalid email address: %s') % addr)
diff --git a/tests/test-patchbomb b/tests/test-patchbomb
--- a/tests/test-patchbomb
+++ b/tests/test-patchbomb
@@ -172,8 +172,10 @@
  -c bar -s test -r 0:1 | fixheaders
 
 echo "% test address parsing"
+UUML=`echo -en '\xfc'`
+export HGENCODING=iso-8859-1
 hg email --date '1980-1-1 0:1' -m tmp.mbox -f quux -t 'spam<spam><eggs>' \
- -t toast -c 'foo,bar at example.com' -c '"A, B <>" <a at example.com>' -s test -r 0 \
- --config email.bcc='"Quux, A." <quux>'
+ -t toast -c "foo,bar@${UUML}nicode.com" -c '"A, B <>" <a at example.com>' \
+ -s test -r 0 --config email.bcc='"Quux, A." <quux>'
 cat tmp.mbox | fixheaders
 
diff --git a/tests/test-patchbomb.out b/tests/test-patchbomb.out
--- a/tests/test-patchbomb.out
+++ b/tests/test-patchbomb.out
@@ -1485,7 +1485,7 @@
 Date: Tue, 01 Jan 1980 00:01:00 +0000
 From: quux
 To: spam <spam>, eggs, toast
-Cc: foo, bar at example.com, "A, B <>" <a at example.com>
+Cc: foo, bar at xn--nicode-2ya.com, "A, B <>" <a at example.com>
 Bcc: "Quux, A." <quux>
 
 # HG changeset patch


More information about the Mercurial-devel mailing list