# HG changeset patch # User Franz Glasner # Date 1639086670 -3600 # Node ID b96f49c9c76b7e1dbb2ee99a271a53def2f497a6 # Parent 49cadb78c45ac8e253dacf201574ebe70fb3f6ce Proper "repr()" for a jailed configuration: put the root path into the output diff -r 49cadb78c45a -r b96f49c9c76b configmix/compat.py --- a/configmix/compat.py Thu Dec 09 22:50:26 2021 +0100 +++ b/configmix/compat.py Thu Dec 09 22:51:10 2021 +0100 @@ -15,7 +15,8 @@ "native_os_str_to_text", "u", "u2fs", - "uchr"] + "uchr", + "n"] import sys @@ -71,6 +72,12 @@ def uchr(n): return unichr(n) # noqa: F821 + def n(s, encoding="utf-8"): + if isinstance(s, str): + return s + else: + return s.encode(encoding) + else: def text_to_native_os_str(s, encoding=None): @@ -103,3 +110,9 @@ def uchr(n): return chr(n) + + def n(s, encoding="utf-8"): + if isinstance(s, str): + return s + else: + return s.decode(encoding) diff -r 49cadb78c45a -r b96f49c9c76b configmix/config.py --- a/configmix/config.py Thu Dec 09 22:50:26 2021 +0100 +++ b/configmix/config.py Thu Dec 09 22:51:10 2021 +0100 @@ -28,7 +28,7 @@ from urlparse import urlsplit from .variables import lookup_varns, lookup_filter -from .compat import u, uchr +from .compat import u, uchr, n from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER @@ -894,3 +894,7 @@ if bind_root: sjc.rebind(self._base) return sjc + + def __repr__(self): + r = "_JailedConfiguration(rootpath=%s)" % n(repr(self._path)) + return r diff -r 49cadb78c45a -r b96f49c9c76b tests/test.py --- a/tests/test.py Thu Dec 09 22:50:26 2021 +0100 +++ b/tests/test.py Thu Dec 09 22:51:10 2021 +0100 @@ -17,7 +17,7 @@ import configmix.json import configmix.py import configmix.toml -from configmix.compat import u +from configmix.compat import u, PY2 TESTDATADIR = os.path.join( @@ -1535,6 +1535,27 @@ set([u"key1", u"key2", u"tree1"]), set(jcfg.getkeys(u""))) + def test_repr_empty_rootpath(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf10.py")) + jcfg = cfg.jailed(rootpath=tuple()) + + self.assertEqual( + r"_JailedConfiguration(rootpath=())", + repr(jcfg)) + + def test_repr_non_empty_rootpath(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf10.py")) + jcfg = cfg.jailed(rootpath=(u"tree1", u"tree2")) + + if PY2: + self.assertEqual( + r"_JailedConfiguration(rootpath=(u'tree1', u'tree2'))", + repr(jcfg)) + else: + self.assertEqual( + r"_JailedConfiguration(rootpath=('tree1', 'tree2'))", + repr(jcfg)) + if __name__ == "__main__": unittest.main()