Skip to content

Commit 74534ec

Browse files
lgeigerWyattBlue
authored andcommitted
Break reference cycle StreamContainer.get()
1 parent 37a6ece commit 74534ec

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

av/container/streams.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Iterator
2+
13
import cython
24
import cython.cimports.libav as lib
35
from cython.cimports.av.stream import Stream
@@ -95,6 +97,27 @@ def data(self):
9597
def attachments(self):
9698
return tuple(s for s in self._streams if s.type == "attachment")
9799

100+
def _get_streams(self, x) -> Iterator[Stream]:
101+
if x is None:
102+
pass
103+
elif isinstance(x, Stream):
104+
yield x
105+
elif isinstance(x, int):
106+
yield self._streams[x]
107+
elif isinstance(x, (tuple, list)):
108+
for item in x:
109+
yield from self._get_streams(item)
110+
elif isinstance(x, dict):
111+
for type_, indices in x.items():
112+
# For compatibility with the pseudo signature
113+
streams = self._streams if type_ == "streams" else getattr(self, type_)
114+
if not isinstance(indices, (tuple, list)):
115+
indices = [indices]
116+
for i in indices:
117+
yield streams[i]
118+
else:
119+
raise TypeError("Argument must be Stream or int.", type(x))
120+
98121
def get(self, *args, **kwargs):
99122
"""get(streams=None, video=None, audio=None, subtitles=None, data=None)
100123
@@ -122,35 +145,9 @@ def get(self, *args, **kwargs):
122145
If nothing is selected, then all streams are returned.
123146
124147
"""
125-
selection: list = []
126-
127-
def process(x):
128-
if x is None:
129-
pass
130-
elif isinstance(x, Stream):
131-
selection.append(x)
132-
elif isinstance(x, int):
133-
selection.append(self._streams[x])
134-
elif isinstance(x, (tuple, list)):
135-
for item in x:
136-
process(item)
137-
elif isinstance(x, dict):
138-
for type_, indices in x.items():
139-
# For compatibility with the pseudo signature
140-
streams = (
141-
self._streams if type_ == "streams" else getattr(self, type_)
142-
)
143-
if not isinstance(indices, (tuple, list)):
144-
indices = [indices]
145-
for i in indices:
146-
selection.append(streams[i])
147-
else:
148-
raise TypeError("Argument must be Stream or int.", type(x))
149-
150-
for arg in args:
151-
process(arg)
148+
selection = list(self._get_streams(args))
152149
if kwargs:
153-
process(kwargs)
150+
selection.extend(self._get_streams(kwargs))
154151

155152
return selection or self._streams[:]
156153

0 commit comments

Comments
 (0)