Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/ini.py @ 166:b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 14 Mar 2019 01:35:16 +0100 |
| parents | e2e8d21b4122 |
| children | b3b5ed34d180 |
comparison
equal
deleted
inserted
replaced
| 165:6ca90e80f4f4 | 166:b5ce9a8461bf |
|---|---|
| 30 try: | 30 try: |
| 31 from ordereddict import OrderedDict as DictImpl | 31 from ordereddict import OrderedDict as DictImpl |
| 32 except ImportError: | 32 except ImportError: |
| 33 DictImpl = dict | 33 DictImpl = dict |
| 34 | 34 |
| 35 from .compat import PY2, u | 35 from .compat import PY2, u, u2fs |
| 36 | 36 |
| 37 | 37 |
| 38 __all__ = ["INIConfigParser", "NoSectionError", "NoOptionError", | 38 __all__ = ["INIConfigParser", "NoSectionError", "NoOptionError", |
| 39 "load"] | 39 "load"] |
| 40 | 40 |
| 48 | 48 |
| 49 def __init__(self, filename, executable=None, encoding=None): | 49 def __init__(self, filename, executable=None, encoding=None): |
| 50 _ConfigParserBase.__init__(self) | 50 _ConfigParserBase.__init__(self) |
| 51 if executable is None: | 51 if executable is None: |
| 52 executable = sys.argv[0] | 52 executable = sys.argv[0] |
| 53 if PY2: | 53 filename = u(filename, locale.getpreferredencoding()) |
| 54 if isinstance(filename, str): | 54 executable = u(executable, locale.getpreferredencoding()) |
| 55 filename = filename.decode(locale.getpreferredencoding()) | |
| 56 if isinstance(executable, str): | |
| 57 executable = executable.decode(locale.getpreferredencoding()) | |
| 58 self.executable = os.path.normpath(os.path.abspath(executable)) | 55 self.executable = os.path.normpath(os.path.abspath(executable)) |
| 59 if encoding is None: | 56 if encoding is None: |
| 60 encoding = locale.getpreferredencoding() | 57 encoding = locale.getpreferredencoding() |
| 61 self.encoding = encoding | 58 self.encoding = encoding |
| 62 with io.open(filename, mode="rt", encoding=self.encoding) as cf: | 59 with io.open(u2fs(filename), mode="rt", encoding=self.encoding) as cf: |
| 63 self.read_file(cf, filename) | 60 self.read_file(cf, filename) |
| 64 | 61 |
| 65 def optionxform(self, option): | 62 def optionxform(self, option): |
| 66 return option | 63 return option |
| 67 | 64 |
| 89 | 86 |
| 90 """ | 87 """ |
| 91 if hasattr(self, "filename"): | 88 if hasattr(self, "filename"): |
| 92 raise RuntimeError("already initialized") | 89 raise RuntimeError("already initialized") |
| 93 filename = os.path.normpath(os.path.abspath(filename)) | 90 filename = os.path.normpath(os.path.abspath(filename)) |
| 94 if PY2: | 91 filename = u(filename, locale.getpreferredencoding()) |
| 95 if isinstance(filename, str): | |
| 96 filename = filename.decode(locale.getpreferredencoding()) | |
| 97 self.set(None, u("self"), filename) | 92 self.set(None, u("self"), filename) |
| 98 self.set(None, u("here"), os.path.dirname(filename)) | 93 self.set(None, u("here"), os.path.dirname(filename)) |
| 99 self.set(None, u("root"), os.path.dirname(self.executable)) | 94 self.set(None, u("root"), os.path.dirname(self.executable)) |
| 100 if hasattr(_ConfigParserBase, "read_file"): | 95 if hasattr(_ConfigParserBase, "read_file"): |
| 101 _ConfigParserBase.read_file(self, fp, source=filename) | 96 _ConfigParserBase.read_file(self, fp, source=filename) |
