diff --git a/setuptools/command/setopt.py b/setuptools/command/setopt.py index 7e57cc02627..c2f68ee67eb 100644 --- a/setuptools/command/setopt.py +++ b/setuptools/command/setopt.py @@ -2,8 +2,11 @@ from distutils import log from distutils.errors import DistutilsOptionError import distutils +import io import os +from ..unicode_utils import detect_encoding +from setuptools.extern import six from setuptools.extern.six.moves import configparser from setuptools import Command @@ -40,7 +43,14 @@ def edit_config(filename, settings, dry_run=False): """ log.debug("Reading configuration from %s", filename) opts = configparser.RawConfigParser() - opts.read([filename]) + try: + with io.open(filename, 'rb') as fp: + encoding = detect_encoding(fp) + log.debug("Reading %s [%s]" % (filename, encoding or 'locale')) + reader = io.TextIOWrapper(fp, encoding=encoding) + (opts.read_file if six.PY3 else opts.readfp)(reader) + except IOError: + encoding = 'locale' for section, options in settings.items(): if options is None: log.info("Deleting section [%s] from %s", section, filename) @@ -70,6 +80,8 @@ def edit_config(filename, settings, dry_run=False): log.info("Writing %s", filename) if not dry_run: with open(filename, 'w') as f: + if encoding != 'locale': + f.write("# coding: %s\n" % encoding) opts.write(f)