aboutsummaryrefslogtreecommitdiff
path: root/yt_dlp/utils.py
diff options
context:
space:
mode:
authorSimon Sawicki <contact@grub4k.xyz>2023-02-10 03:56:26 +0530
committerpukkandan <pukkandan.ytdlp@gmail.com>2023-02-10 19:36:55 +0530
commit6839ae1f6dde4c0442619e351b3f0442312ab4f9 (patch)
treebb41a1aebf34e2b28f1301dbbe584c40e4ecf8fa /yt_dlp/utils.py
parentc0cd13fb1c71b842c3d272d0273c03542b467766 (diff)
[utils] `traverse_obj`: Fix more bugs
and cleanup uses of `default=[]` Continued from b1bde57bef878478e3503ab07190fd207914ade9
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r--yt_dlp/utils.py15
1 files changed, 7 insertions, 8 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py
index 878b2b6a8..7cf151e3a 100644
--- a/yt_dlp/utils.py
+++ b/yt_dlp/utils.py
@@ -5420,7 +5420,7 @@ def traverse_obj(
Each of the provided `paths` is tested and the first producing a valid result will be returned.
The next path will also be tested if the path branched but no results could be found.
Supported values for traversal are `Mapping`, `Sequence` and `re.Match`.
- Unhelpful values (`[]`, `{}`, `None`) are treated as the absence of a value and discarded.
+ Unhelpful values (`{}`, `None`) are treated as the absence of a value and discarded.
The paths will be wrapped in `variadic`, so that `'key'` is conveniently the same as `('key', )`.
@@ -5484,7 +5484,7 @@ def traverse_obj(
branching = False
result = None
- if obj is None:
+ if obj is None and traverse_string:
pass
elif key is None:
@@ -5558,14 +5558,13 @@ def traverse_obj(
result = next((v for k, v in obj.groupdict().items() if casefold(k) == key), None)
elif isinstance(key, (int, slice)):
- if not is_sequence(obj):
- if traverse_string:
- with contextlib.suppress(IndexError):
- result = str(obj)[key]
- else:
+ if is_sequence(obj):
branching = isinstance(key, slice)
with contextlib.suppress(IndexError):
result = obj[key]
+ elif traverse_string:
+ with contextlib.suppress(IndexError):
+ result = str(obj)[key]
return branching, result if branching else (result,)
@@ -5617,7 +5616,7 @@ def traverse_obj(
def _traverse_obj(obj, path, allow_empty, test_type):
results, has_branched, is_dict = apply_path(obj, path, test_type)
- results = LazyList(item for item in results if item not in (None, [], {}))
+ results = LazyList(item for item in results if item not in (None, {}))
if get_all and has_branched:
if results:
return results.exhaust()