annotate configmix/compat.py @ 428:090a25f36a3d

FIX: Allow jailed configurations to use correctly use base configurations that use a different "default" marker object. Jailed configurations assumed that their "default" marker object is identical to the "default" marker object in the unjailed base configuration. This is not always true, especially if "_JailedConfiguration.rebind()" is used. Removed the explicit "default" keyword argument and passed the complete keywords argument dictionary to the base instead. This triggers correct default handling in the base.
author Franz Glasner <f.glasner@feldmann-mg.com>
date Thu, 09 Dec 2021 13:02:17 +0100
parents ce2a8f5a2fb2
children b96f49c9c76b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
208
bbe8513ea649 Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents: 207
diff changeset
2 # :-
359
ce2a8f5a2fb2 Extend copyright year in compat.py to 2021
Franz Glasner <fzglas.hg@dom66.de>
parents: 348
diff changeset
3 # :Copyright: (c) 2015-2021, Franz Glasner. All rights reserved.
296
eed16a1ec8f3 Use SPDX license identifiers (either full or short) all over the package
Franz Glasner <fzglas.hg@dom66.de>
parents: 250
diff changeset
4 # :License: BSD-3-Clause. See LICENSE.txt for details.
208
bbe8513ea649 Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents: 207
diff changeset
5 # :-
82
218807d7d883 Remove header markup from the Python files and put them into the doc .rst files
Franz Glasner <hg@dom66.de>
parents: 79
diff changeset
6 """Some minimal compatibility shim between Python2 and Python3
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
7
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
8 """
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
9
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
10 from __future__ import division, absolute_import, print_function
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
11
250
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
12
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
13 __all__ = ["PY2",
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
14 "text_to_native_os_str",
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
15 "native_os_str_to_text",
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
16 "u",
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
17 "u2fs",
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
18 "uchr"]
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
19
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
20
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
21 import sys
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
22 import os
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
23 import locale
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
24
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
25
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
26 PY2 = sys.version_info[0] <= 2
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
27
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
28
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
29 if PY2:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
30
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
31 _OS_ENCODING = locale.getpreferredencoding()
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
32
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
33 _FS_ENCODING = sys.getfilesystemencoding() or _OS_ENCODING
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
34
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
35
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
36 def text_to_native_os_str(s, encoding=None):
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
37 if isinstance(s, unicode): # noqa: F821
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
38 return s.encode(encoding or _OS_ENCODING)
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
39 else:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
40 return s
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
41
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
42
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
43 def native_os_str_to_text(s, encoding=None):
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
44 return s.decode(encoding or _OS_ENCODING)
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
45
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
46
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
47 def u(s, encoding="utf-8"):
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
48 if isinstance(s, unicode): # noqa: F821
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
49 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
50 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
51 return s.decode(encoding)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
52
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
53
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
54 def u2fs(s, force=False):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
55 """Convert a text (Unicode) string to the filesystem encoding.
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
56
209
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
57 .. note:: The filesystem encoding on Python 3 is a Unicode text
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
58 string. The function is a noop when called on Python 3.
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
59
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
60 .. note:: If `s` is already a byte string be permissive and
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
61 return `s` unchanged.
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
62
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
63 """
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
64 if isinstance(s, str):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
65 return s
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
66 if not force and os.name in ("nt", "ce"):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
67 # WinNT and CE have native Unicode support: nothing to convert
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
68 return s
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
69 return s.encode(_FS_ENCODING)
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
70
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
71 def uchr(n):
348
396d8d9aaead Mark the use of "unichr()" with "noqa: F821" because of flake8 complaints when running it with Python3
Franz Glasner <fzglas.hg@dom66.de>
parents: 320
diff changeset
72 return unichr(n) # noqa: F821
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
73
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
74 else:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
75
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
76 def text_to_native_os_str(s, encoding=None):
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
77 return s
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
78
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
79
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
80 def native_os_str_to_text(s, encoding=None):
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
81 return s
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
82
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
83
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
84 def u(s, encoding="utf-8"):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
85 if isinstance(s, str):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
86 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
87 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
88 return s.decode(encoding)
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
89
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
90
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
91 def u2fs(s, force=False):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
92 """Convert a text (Unicode) string to the filesystem encoding.
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
93
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
94 .. note:: The filesystem encoding on Python 3 is a Unicode text
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
95 string. The function is a noop when called on Python 3.
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
96
209
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
97 .. note:: If `s` is already a byte string be permissive and
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
98 return `s` unchanged.
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
99
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
100 """
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
101 assert isinstance(s, str)
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
102 return s
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
103
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
104 def uchr(n):
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
105 return chr(n)