Skip to content

Commit

Permalink
python: Incorrect Config.get when subtree is present
Browse files Browse the repository at this point in the history
  • Loading branch information
shramov committed Dec 19, 2024
1 parent e7ed6cd commit 311cd61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion python/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
19 changes: 10 additions & 9 deletions python/tll/config.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 311cd61

Please sign in to comment.