]> granicus.if.org Git - python/commitdiff
Issue #12112: fix the encoding of setup.py in the packaging module
authorVictor Stinner <victor.stinner@haypocalc.com>
Thu, 19 May 2011 19:42:47 +0000 (21:42 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Thu, 19 May 2011 19:42:47 +0000 (21:42 +0200)
 * read: use tokenize.detect_encoding()
 * write: use 'utf-8'

Lib/packaging/create.py
Lib/packaging/util.py

index ca827732d3704abb2c4c323ff075188306390121..5432ffcf2ca30bffc3e4c9aa48b9a1455df2e132 100644 (file)
@@ -32,6 +32,7 @@ import glob
 import re
 import shutil
 import sysconfig
+import tokenize
 from configparser import RawConfigParser
 from textwrap import dedent
 from hashlib import md5
@@ -116,7 +117,9 @@ def load_setup():
     This function load the setup file in all cases (even if it have already
     been loaded before, because we are monkey patching its setup function with
     a particular one"""
-    with open("setup.py") as f:
+    with open("setup.py", "rb") as f:
+        encoding, lines = tokenize.detect_encoding(f.readline)
+    with open("setup.py", encoding=encoding) as f:
         imp.load_module("setup", f, "setup.py", (".py", "r", imp.PY_SOURCE))
 
 
index 058f13e334e02ff399fbd37b44a14d47cdc41077..71ce819af015049f324f877c1499c508f8f88eeb 100644 (file)
@@ -346,9 +346,9 @@ def byte_compile(py_files, optimize=0, force=False, prefix=None,
         logger.info("writing byte-compilation script '%s'", script_name)
         if not dry_run:
             if script_fd is not None:
-                script = os.fdopen(script_fd, "w")
+                script = os.fdopen(script_fd, "w", encoding='utf-8')
             else:
-                script = open(script_name, "w")
+                script = open(script_name, "w", encoding='utf-8')
 
             with script:
                 script.write("""\
@@ -1087,7 +1087,7 @@ def generate_setup_py():
     if os.path.exists("setup.py"):
         raise PackagingFileError("a setup.py file alreadyexists")
 
-    with open("setup.py", "w") as fp:
+    with open("setup.py", "w", encoding='utf-8') as fp:
         fp.write(_SETUP_TMPL % {'func': getsource(cfg_to_args)})