Skip to content

Commit 6bc9621

Browse files
JoeSchiffWyattBlue
authored andcommitted
Convert deprecated cython extension class properties to new syntax part 1
1 parent cde9950 commit 6bc9621

File tree

6 files changed

+293
-281
lines changed

6 files changed

+293
-281
lines changed

av/descriptor.pyx

+35-35
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,42 @@ cdef class Descriptor:
1818
if sentinel is not _cinit_sentinel:
1919
raise RuntimeError("Cannot construct av.Descriptor")
2020

21-
property name:
22-
def __get__(self):
23-
return self.ptr.class_name if self.ptr.class_name else None
24-
25-
property options:
26-
def __get__(self):
27-
cdef const lib.AVOption *ptr = self.ptr.option
28-
cdef const lib.AVOption *choice_ptr
29-
cdef Option option
30-
cdef OptionChoice option_choice
31-
cdef bint choice_is_default
32-
if self._options is None:
33-
options = []
34-
ptr = self.ptr.option
35-
while ptr != NULL and ptr.name != NULL:
36-
if ptr.type == lib.AV_OPT_TYPE_CONST:
37-
ptr += 1
38-
continue
39-
choices = []
40-
if ptr.unit != NULL: # option has choices (matching const options)
41-
choice_ptr = self.ptr.option
42-
while choice_ptr != NULL and choice_ptr.name != NULL:
43-
if choice_ptr.type != lib.AV_OPT_TYPE_CONST or choice_ptr.unit != ptr.unit:
44-
choice_ptr += 1
45-
continue
46-
choice_is_default = (choice_ptr.default_val.i64 == ptr.default_val.i64 or
47-
ptr.type == lib.AV_OPT_TYPE_FLAGS and
48-
choice_ptr.default_val.i64 & ptr.default_val.i64)
49-
option_choice = wrap_option_choice(choice_ptr, choice_is_default)
50-
choices.append(option_choice)
51-
choice_ptr += 1
52-
option = wrap_option(tuple(choices), ptr)
53-
options.append(option)
21+
@property
22+
def name(self):
23+
return self.ptr.class_name if self.ptr.class_name else None
24+
25+
@property
26+
def options(self):
27+
cdef const lib.AVOption *ptr = self.ptr.option
28+
cdef const lib.AVOption *choice_ptr
29+
cdef Option option
30+
cdef OptionChoice option_choice
31+
cdef bint choice_is_default
32+
if self._options is None:
33+
options = []
34+
ptr = self.ptr.option
35+
while ptr != NULL and ptr.name != NULL:
36+
if ptr.type == lib.AV_OPT_TYPE_CONST:
5437
ptr += 1
55-
self._options = tuple(options)
56-
return self._options
38+
continue
39+
choices = []
40+
if ptr.unit != NULL: # option has choices (matching const options)
41+
choice_ptr = self.ptr.option
42+
while choice_ptr != NULL and choice_ptr.name != NULL:
43+
if choice_ptr.type != lib.AV_OPT_TYPE_CONST or choice_ptr.unit != ptr.unit:
44+
choice_ptr += 1
45+
continue
46+
choice_is_default = (choice_ptr.default_val.i64 == ptr.default_val.i64 or
47+
ptr.type == lib.AV_OPT_TYPE_FLAGS and
48+
choice_ptr.default_val.i64 & ptr.default_val.i64)
49+
option_choice = wrap_option_choice(choice_ptr, choice_is_default)
50+
choices.append(option_choice)
51+
choice_ptr += 1
52+
option = wrap_option(tuple(choices), ptr)
53+
options.append(option)
54+
ptr += 1
55+
self._options = tuple(options)
56+
return self._options
5757

5858
def __repr__(self):
5959
return f"<{self.__class__.__name__} {self.name} at 0x{id(self):x}>"

av/format.pyx

+51-51
Original file line numberDiff line numberDiff line change
@@ -81,59 +81,59 @@ cdef class ContainerFormat:
8181
def __repr__(self):
8282
return f"<av.{self.__class__.__name__} {self.name!r}>"
8383

84-
property descriptor:
85-
def __get__(self):
86-
if self.iptr:
87-
return wrap_avclass(self.iptr.priv_class)
88-
else:
89-
return wrap_avclass(self.optr.priv_class)
90-
91-
property options:
92-
def __get__(self):
93-
return self.descriptor.options
94-
95-
property input:
84+
@property
85+
def descriptor(self):
86+
if self.iptr:
87+
return wrap_avclass(self.iptr.priv_class)
88+
else:
89+
return wrap_avclass(self.optr.priv_class)
90+
91+
@property
92+
def options(self):
93+
return self.descriptor.options
94+
95+
@property
96+
def input(self):
9697
"""An input-only view of this format."""
97-
def __get__(self):
98-
if self.iptr == NULL:
99-
return None
100-
elif self.optr == NULL:
101-
return self
102-
else:
103-
return build_container_format(self.iptr, NULL)
104-
105-
property output:
98+
if self.iptr == NULL:
99+
return None
100+
elif self.optr == NULL:
101+
return self
102+
else:
103+
return build_container_format(self.iptr, NULL)
104+
105+
@property
106+
def output(self):
106107
"""An output-only view of this format."""
107-
def __get__(self):
108-
if self.optr == NULL:
109-
return None
110-
elif self.iptr == NULL:
111-
return self
112-
else:
113-
return build_container_format(NULL, self.optr)
114-
115-
property is_input:
116-
def __get__(self):
117-
return self.iptr != NULL
118-
119-
property is_output:
120-
def __get__(self):
121-
return self.optr != NULL
122-
123-
property long_name:
124-
def __get__(self):
125-
# We prefer the output names since the inputs may represent
126-
# multiple formats.
127-
return self.optr.long_name if self.optr else self.iptr.long_name
128-
129-
property extensions:
130-
def __get__(self):
131-
cdef set exts = set()
132-
if self.iptr and self.iptr.extensions:
133-
exts.update(self.iptr.extensions.split(","))
134-
if self.optr and self.optr.extensions:
135-
exts.update(self.optr.extensions.split(","))
136-
return exts
108+
if self.optr == NULL:
109+
return None
110+
elif self.iptr == NULL:
111+
return self
112+
else:
113+
return build_container_format(NULL, self.optr)
114+
115+
@property
116+
def is_input(self):
117+
return self.iptr != NULL
118+
119+
@property
120+
def is_output(self):
121+
return self.optr != NULL
122+
123+
@property
124+
def long_name(self):
125+
# We prefer the output names since the inputs may represent
126+
# multiple formats.
127+
return self.optr.long_name if self.optr else self.iptr.long_name
128+
129+
@property
130+
def extensions(self):
131+
cdef set exts = set()
132+
if self.iptr and self.iptr.extensions:
133+
exts.update(self.iptr.extensions.split(","))
134+
if self.optr and self.optr.extensions:
135+
exts.update(self.optr.extensions.split(","))
136+
return exts
137137

138138
@Flags.property
139139
def flags(self):

av/frame.pyx

+42-39
Original file line numberDiff line numberDiff line change
@@ -55,79 +55,82 @@ cdef class Frame:
5555

5656
self._time_base = dst
5757

58-
property dts:
58+
@property
59+
def dts(self):
5960
"""
6061
The decoding timestamp copied from the :class:`~av.packet.Packet` that triggered returning this frame in :attr:`time_base` units.
6162
6263
(if frame threading isn't used) This is also the Presentation time of this frame calculated from only :attr:`.Packet.dts` values without pts values.
6364
6465
:type: int
6566
"""
66-
def __get__(self):
67-
if self.ptr.pkt_dts == lib.AV_NOPTS_VALUE:
68-
return None
69-
return self.ptr.pkt_dts
70-
71-
def __set__(self, value):
72-
if value is None:
73-
self.ptr.pkt_dts = lib.AV_NOPTS_VALUE
74-
else:
75-
self.ptr.pkt_dts = value
76-
77-
property pts:
67+
if self.ptr.pkt_dts == lib.AV_NOPTS_VALUE:
68+
return None
69+
return self.ptr.pkt_dts
70+
71+
@dts.setter
72+
def dts(self, value):
73+
if value is None:
74+
self.ptr.pkt_dts = lib.AV_NOPTS_VALUE
75+
else:
76+
self.ptr.pkt_dts = value
77+
78+
@property
79+
def pts(self):
7880
"""
7981
The presentation timestamp in :attr:`time_base` units for this frame.
8082
8183
This is the time at which the frame should be shown to the user.
8284
8385
:type: int
8486
"""
85-
def __get__(self):
86-
if self.ptr.pts == lib.AV_NOPTS_VALUE:
87-
return None
88-
return self.ptr.pts
89-
90-
def __set__(self, value):
91-
if value is None:
92-
self.ptr.pts = lib.AV_NOPTS_VALUE
93-
else:
94-
self.ptr.pts = value
95-
96-
property time:
87+
if self.ptr.pts == lib.AV_NOPTS_VALUE:
88+
return None
89+
return self.ptr.pts
90+
91+
@pts.setter
92+
def pts(self, value):
93+
if value is None:
94+
self.ptr.pts = lib.AV_NOPTS_VALUE
95+
else:
96+
self.ptr.pts = value
97+
98+
@property
99+
def time(self):
97100
"""
98101
The presentation time in seconds for this frame.
99102
100103
This is the time at which the frame should be shown to the user.
101104
102105
:type: float
103106
"""
104-
def __get__(self):
105-
if self.ptr.pts == lib.AV_NOPTS_VALUE:
106-
return None
107-
else:
108-
return float(self.ptr.pts) * self._time_base.num / self._time_base.den
107+
if self.ptr.pts == lib.AV_NOPTS_VALUE:
108+
return None
109+
else:
110+
return float(self.ptr.pts) * self._time_base.num / self._time_base.den
109111

110-
property time_base:
112+
@property
113+
def time_base(self):
111114
"""
112115
The unit of time (in fractional seconds) in which timestamps are expressed.
113116
114117
:type: fractions.Fraction
115118
"""
116-
def __get__(self):
117-
if self._time_base.num:
118-
return avrational_to_fraction(&self._time_base)
119+
if self._time_base.num:
120+
return avrational_to_fraction(&self._time_base)
119121

120-
def __set__(self, value):
121-
to_avrational(value, &self._time_base)
122+
@time_base.setter
123+
def time_base(self, value):
124+
to_avrational(value, &self._time_base)
122125

123-
property is_corrupt:
126+
@property
127+
def is_corrupt(self):
124128
"""
125129
Is this frame corrupt?
126130
127131
:type: bool
128132
"""
129-
def __get__(self):
130-
return self.ptr.decode_error_flags != 0 or bool(self.ptr.flags & lib.AV_FRAME_FLAG_CORRUPT)
133+
return self.ptr.decode_error_flags != 0 or bool(self.ptr.flags & lib.AV_FRAME_FLAG_CORRUPT)
131134

132135
@property
133136
def side_data(self):

0 commit comments

Comments
 (0)