From 76c7a402ebd7c779d332e230b3109260008c1de0 Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Fri, 5 Aug 2022 07:35:34 +0100 Subject: [PATCH] PSSH: Optimize how overwrite_key_ids works with the repeated field We can clear a repeated field with `del field[:]` and overwrite an entire field with `field[:] = [b"123", b"456"]`. So we can reduce this down to a single call operation. --- pywidevine/pssh.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pywidevine/pssh.py b/pywidevine/pssh.py index 8c3d0a5..229c8c8 100644 --- a/pywidevine/pssh.py +++ b/pywidevine/pssh.py @@ -270,13 +270,10 @@ class PSSH: init = WidevinePsshData() init.ParseFromString(box.init_data) - # TODO: Is there a better way to clear the Key IDs? - for _ in range(len(init.key_ids or [])): - init.key_ids.pop(0) - - # TODO: Is there a .extend or a way to add all without a loop? - for key_id in key_ids: - init.key_ids.append(key_id.bytes) + init.key_ids[:] = [ + key_id.bytes + for key_id in key_ids + ] box.init_data = init.SerializeToString()