|
2 | 2 | from distutils import log
|
3 | 3 | from distutils.errors import DistutilsOptionError
|
4 | 4 | import distutils
|
| 5 | +import io |
5 | 6 | import os
|
| 7 | +from ..unicode_utils import detect_encoding |
6 | 8 |
|
| 9 | +from setuptools.extern import six |
7 | 10 | from setuptools.extern.six.moves import configparser
|
8 | 11 |
|
9 | 12 | from setuptools import Command
|
@@ -40,7 +43,11 @@ def edit_config(filename, settings, dry_run=False):
|
40 | 43 | """
|
41 | 44 | log.debug("Reading configuration from %s", filename)
|
42 | 45 | opts = configparser.RawConfigParser()
|
43 |
| - opts.read([filename]) |
| 46 | + with io.open(filename, 'rb') as fp: |
| 47 | + encoding = detect_encoding(fp) |
| 48 | + log.debug("Reading %s [%s]" % (filename, encoding or 'locale')) |
| 49 | + reader = io.TextIOWrapper(fp, encoding=encoding) |
| 50 | + (opts.read_file if six.PY3 else opts.readfp)(reader) |
44 | 51 | for section, options in settings.items():
|
45 | 52 | if options is None:
|
46 | 53 | log.info("Deleting section [%s] from %s", section, filename)
|
@@ -70,6 +77,8 @@ def edit_config(filename, settings, dry_run=False):
|
70 | 77 | log.info("Writing %s", filename)
|
71 | 78 | if not dry_run:
|
72 | 79 | with open(filename, 'w') as f:
|
| 80 | + if encoding != 'locale': |
| 81 | + f.write("# coding: %s\n" % encoding) |
73 | 82 | opts.write(f)
|
74 | 83 |
|
75 | 84 |
|
|
0 commit comments