diff options
author | Simon Sawicki <contact@grub4k.xyz> | 2023-12-24 22:09:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-24 22:09:01 +0100 |
commit | 00cdda4f6fe18712ced13dbc64b7ea10f323e268 (patch) | |
tree | 0102d5f521666e37bd607cf62a68afaa9bf1b0b1 /yt_dlp/YoutubeDL.py | |
parent | 116c268438ea4d3738f6fa502c169081ca8f0ee7 (diff) |
[core] Fix format selection parse error for CPython 3.12 (#8797)
Authored by: Grub4K
Diffstat (limited to 'yt_dlp/YoutubeDL.py')
-rw-r--r-- | yt_dlp/YoutubeDL.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 0c07866e4..5e28fd0e2 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -2465,9 +2465,16 @@ class YoutubeDL: return selector_function(ctx_copy) return final_selector - stream = io.BytesIO(format_spec.encode()) + # HACK: Python 3.12 changed the underlying parser, rendering '7_a' invalid + # Prefix numbers with random letters to avoid it being classified as a number + # See: https://github.com/yt-dlp/yt-dlp/pulls/8797 + # TODO: Implement parser not reliant on tokenize.tokenize + prefix = ''.join(random.choices(string.ascii_letters, k=32)) + stream = io.BytesIO(re.sub(r'\d[_\d]*', rf'{prefix}\g<0>', format_spec).encode()) try: - tokens = list(_remove_unused_ops(tokenize.tokenize(stream.readline))) + tokens = list(_remove_unused_ops( + token._replace(string=token.string.replace(prefix, '')) + for token in tokenize.tokenize(stream.readline))) except tokenize.TokenError: raise syntax_error('Missing closing/opening brackets or parenthesis', (0, len(format_spec))) |