From 311cd617f7284e3f24adebe21888eb7d004ba015 Mon Sep 17 00:00:00 2001 From: Pavel Shramov Date: Thu, 19 Dec 2024 15:09:05 +0300 Subject: [PATCH] python: Incorrect Config.get when subtree is present --- python/test/test_config.py | 2 +- python/tll/config.pyx | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/python/test/test_config.py b/python/test/test_config.py index fd6d68cb..9245683b 100644 --- a/python/test/test_config.py +++ b/python/test/test_config.py @@ -233,7 +233,7 @@ def test_double_import(): cfg.process_imports('include') assert cfg.get('include.inline') == 'yamls://{link: !link /dest}' - assert cfg.get('link') == None + assert cfg.get('link', None) == None cfg['dest'] = 'value' assert cfg.get('link') == 'value' diff --git a/python/tll/config.pyx b/python/tll/config.pyx index bab7b62b..ea14014c 100644 --- a/python/tll/config.pyx +++ b/python/tll/config.pyx @@ -197,10 +197,13 @@ cdef class Config: raise TLLError(f"Failed to set callback at {key}: {value}", r) Py_INCREF(cb) - def _get(self, decode=True): - if tll_config_value(self._ptr) == 0: return None + def _get(self, key=None, decode=True): cdef int len = 0; - cdef char * buf = tll_config_get_copy(self._ptr, NULL, 0, &len) + cdef const char * ckey = NULL + if key is not None: + key = s2b(key) + ckey = key + cdef char * buf = tll_config_get_copy(self._ptr, ckey, -1, &len) if buf == NULL: return None try: @@ -212,14 +215,12 @@ cdef class Config: tll_config_value_free(buf) def get(self, key=None, default=DEFAULT_TAG, decode=True): - if key is None: return self._get(decode=decode) - k = s2b(key) - cdef tll_config_t * cfg = tll_config_sub(self._ptr, k, len(k), 0) - if cfg == NULL: + r = self._get(key, decode=decode) + if r is None: if default == DEFAULT_TAG: raise KeyError("Key {} not found".format(key)) return default - return Config.wrap(cfg).get(decode=decode) + return r def get_url(self, key=None, default=DEFAULT_TAG): cdef Config sub = self @@ -230,7 +231,7 @@ cdef class Config: raise KeyError(f"Key {key} not found") return default r = Config.wrap(tll_config_get_url(sub._ptr, NULL, 0)) - error = r.get() + error = r.get(default=None) if error is not None: raise ValueError(f"Invalid url at '{key}': {error}") return Url(r)