Commits

Fredrik Håård committed 84b8587

UTF-8 fix for Python 2

  • Participants
  • Parent commits ace416f

Comments (0)

Files changed (2)

File hgapi/hgapi.py

 # -*- coding: utf-8 -*-
 from __future__ import print_function, unicode_literals, with_statement
-from subprocess import Popen, STDOUT, PIPE
+from subprocess import Popen, PIPE
 
 try:
     from urllib import unquote
 import re
 import os.path
 import os
+import sys
 
 try:
     import json  # for reading logs
         rev = json.loads(json_log)
 
         for key in rev.keys():
-            self.__setattr__(key, unquote(rev[key]))
+            if sys.version_info.major < 3:
+                _value = unquote(rev[key].encode("ascii")).decode("utf-8")
+            else:
+                _value = unquote(rev[key])
+            self.__setattr__(key, _value)
         self.rev = int(self.rev)
         if not self.branch:
             self.branch = 'default'

File hgapi/testhgapi.py

-from __future__ import with_statement
+#  -*- encoding: utf-8 -*-
+from __future__ import with_statement, unicode_literals
 
 import unittest
 import doctest
-
 import os
-
 import shutil
-
 import hgapi
 
 
     def test_220_LogWithBranch(self):
         default = self.repo.hg_log(branch='default')
         branch = self.repo.hg_log(branch='test_branch')
-
-        print(default)
-        print(branch)
-
         self.assertTrue("commit test_branch" in branch)
         self.assertFalse("commit test_branch" in default)
 
         self.assertTrue("default" in paths)
         self.assertTrue(paths['default'].endswith('test'))
 
+    def test_420_CommitWithNonAsciiCharacters(self):
+        with open("test/file3.txt", "w") as out:
+            out.write("enjoy a new file")
+        self.repo.hg_add("file3.txt")
+
+        self.repo.hg_commit("éàô".encode('utf-8'),
+                            user="F. Håård",
+                            date="10/10/11 UTC")
+
+        rev = self.repo["tip"]
+
+        self.assertEquals(rev.desc, "éàô")
+        self.assertEquals(rev.author, "F. Håård")
+
+
 def test_doc():
     # prepare for doctest
     os.mkdir("./test_hgapi")
     with open("test_hgapi/file.txt", "w") as target:
-        w = target.write("stuff")
+        target.write("stuff")
     try:
         # run doctest
-        res = doctest.testfile("../README.rst")
+        doctest.testfile("../README.rst")
     finally:
         # cleanup
         shutil.rmtree("test_hgapi")