From 0aaa0f1f4ab049a157d7d26e81b244db708a70be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Minier?= Date: Mon, 13 May 2013 13:58:15 +0200 Subject: [PATCH 1/3] Decode name from environment or from gecos with the locale's encoding and add corresponding regression test; fixes handling of non-ascii developer name like mine with e.g. syncpackage using it with dpkg-genchanges -e. --- ubuntutools/config.py | 6 ++++++ ubuntutools/test/test_config.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/ubuntutools/config.py b/ubuntutools/config.py index 899750f..bcbecad 100644 --- a/ubuntutools/config.py +++ b/ubuntutools/config.py @@ -21,6 +21,7 @@ import re import shlex import socket import sys +import locale from ubuntutools.logger import Logger @@ -173,4 +174,9 @@ def ubu_email(name=None, email=None, export=True): if export: os.environ['DEBFULLNAME'] = name os.environ['DEBEMAIL'] = email + + # decode env var or gecos raw string with the current locale's encoding + encoding = locale.getdefaultlocale()[1] + if encoding: + name = name.decode(encoding) return name, email diff --git a/ubuntutools/test/test_config.py b/ubuntutools/test/test_config.py index 8696514..3e1fa0c 100644 --- a/ubuntutools/test/test_config.py +++ b/ubuntutools/test/test_config.py @@ -1,4 +1,5 @@ # test_config.py - Test suite for ubuntutools.config +# -*- coding: utf-8 -*- # # Copyright (C) 2010, Stefano Rivera # @@ -210,3 +211,8 @@ class UbuEmailTestCase(unittest.TestCase): os.environ['DEBEMAIL'] = orig = '%s <%s>' % (name, email) self.assertEqual(ubu_email(), (name, email)) self.assertEqual(os.environ['DEBEMAIL'], orig) + + def test_unicode_name(self): + os.environ['DEBFULLNAME'] = name = 'Jöe Déveloper' + os.environ['DEBEMAIL'] = email = 'joe@example.net' + self.assertEqual(ubu_email(), (name.decode('utf-8'), email)) From a41422a7f41fe353481249d43f406421eec1412e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Minier?= Date: Wed, 15 May 2013 02:10:04 +0200 Subject: [PATCH 2/3] Default to UTF-8 encoding when locale doesn't specify one (as Python defaults to ascii); thanks Stefano Rivera. --- ubuntutools/config.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ubuntutools/config.py b/ubuntutools/config.py index bcbecad..911f7d0 100644 --- a/ubuntutools/config.py +++ b/ubuntutools/config.py @@ -177,6 +177,7 @@ def ubu_email(name=None, email=None, export=True): # decode env var or gecos raw string with the current locale's encoding encoding = locale.getdefaultlocale()[1] - if encoding: - name = name.decode(encoding) + if not encoding: + encoding = 'utf-8' + name = name.decode(encoding) return name, email From 658946c2141649261cf47978b40892f3c3e2a76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Minier?= Date: Wed, 15 May 2013 02:18:50 +0200 Subject: [PATCH 3/3] Encode the developer name in the current locale to run the test; thanks Stefano Rivera. --- ubuntutools/test/test_config.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ubuntutools/test/test_config.py b/ubuntutools/test/test_config.py index 3e1fa0c..e32d5dc 100644 --- a/ubuntutools/test/test_config.py +++ b/ubuntutools/test/test_config.py @@ -18,6 +18,7 @@ import __builtin__ import os import sys +import locale from StringIO import StringIO import mox @@ -213,6 +214,10 @@ class UbuEmailTestCase(unittest.TestCase): self.assertEqual(os.environ['DEBEMAIL'], orig) def test_unicode_name(self): - os.environ['DEBFULLNAME'] = name = 'Jöe Déveloper' + encoding = locale.getdefaultlocale()[1] + if not encoding: + encoding = 'utf-8' + name = 'Jöe Déveloper'.decode('utf-8') + os.environ['DEBFULLNAME'] = name.encode(encoding) os.environ['DEBEMAIL'] = email = 'joe@example.net' - self.assertEqual(ubu_email(), (name.decode('utf-8'), email)) + self.assertEqual(ubu_email(), (name, email))