Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 deprecation fixes (including Preparing for Python 3.9) #820

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
import sys
import numbers
import copy
import collections
try:
# Python 3.3+
import collections.abc as abc
except ImportError:
import collections as abc
import fractions
import opentimelineio as otio

Expand Down Expand Up @@ -552,7 +556,7 @@ def _transcribe(item, parents, edit_rate, indent=0):
# elif isinstance(item, pyaaf.AxProperty):
# self.properties['Value'] = str(item.GetValue())

elif isinstance(item, collections.Iterable):
elif isinstance(item, abc.Iterable):
msg = "Creating SerializableCollection for Iterable for {}".format(
_encoded_name(item))
_transcribe_log(msg, indent)
Expand Down
3 changes: 2 additions & 1 deletion src/py-opentimelineio/opentimelineio-bindings/otio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ struct MutableSequencePyAPI : public V {

pybind11::class_<This::Iterator>(m, (name + "Iterator").c_str())
.def("__iter__", &This::Iterator::iter)
.def("next", &This::Iterator::next);
.def("next", &This::Iterator::next)
.def("__next__", &This::Iterator::next);

pybind11::class_<This>(m, name.c_str())
.def(pybind11::init<>())
Expand Down
9 changes: 8 additions & 1 deletion src/py-opentimelineio/opentimelineio/adapters/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
)


try:
# Python 3.0+
getargspec = inspect.getfullargspec
except AttributeError:
getargspec = inspect.getargspec
reinecke marked this conversation as resolved.
Show resolved Hide resolved


@core.register_type
class Adapter(plugins.PythonPlugin):
"""Adapters convert between OTIO and other formats.
Expand Down Expand Up @@ -307,7 +314,7 @@ def plugin_info_map(self):
for fn_name in _FEATURE_MAP[feature]:
if hasattr(self.module(), fn_name):
fn = getattr(self.module(), fn_name)
args = inspect.getargspec(fn)
args = getargspec(fn)
docs = inspect.getdoc(fn)
break

Expand Down
26 changes: 15 additions & 11 deletions src/py-opentimelineio/opentimelineio/core/_core_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import types
import collections
try:
# Python 3.3+
import collections.abc as abc
except ImportError:
import collections as abc
import copy
import sys

Expand Down Expand Up @@ -46,7 +50,7 @@ def _xrange(*args):


def _is_nonstring_sequence(v):
return isinstance(v, collections.Sequence) and not _is_str(v)
return isinstance(v, abc.Sequence) and not _is_str(v)


def _value_to_any(value, ids=None):
Expand All @@ -55,7 +59,7 @@ def _value_to_any(value, ids=None):

if isinstance(value, SerializableObject):
return PyAny(value)
if isinstance(value, collections.Mapping):
if isinstance(value, abc.Mapping):
if ids is None:
ids = set()
d = AnyDictionary()
Expand Down Expand Up @@ -88,7 +92,7 @@ def _value_to_any(value, ids=None):


def _value_to_so_vector(value, ids=None):
if not isinstance(value, collections.Sequence) or _is_str(value):
if not isinstance(value, abc.Sequence) or _is_str(value):
raise TypeError(
"Expected list/sequence of SerializableObjects;"
" found type '{}'".format(type(value))
Expand Down Expand Up @@ -150,13 +154,13 @@ def __deepcopy__(self, memo):
)
return m

collections.MutableMapping.register(mapClass)
abc.MutableMapping.register(mapClass)
mapClass.__setitem__ = __setitem__
mapClass.__str__ = __str__
mapClass.__repr__ = __repr__

seen = set()
for klass in (collections.MutableMapping, collections.Mapping):
for klass in (abc.MutableMapping, abc.Mapping):
for name in klass.__dict__.keys():
if name in seen:
continue
Expand Down Expand Up @@ -217,15 +221,15 @@ def __setitem__(self, index, item):
if not isinstance(index, slice):
self.__internal_setitem__(index, conversion_func(item))
else:
if not isinstance(item, collections.Iterable):
if not isinstance(item, abc.Iterable):
raise TypeError("can only assign an iterable")

indices = range(*index.indices(len(self)))

if index.step in (1, None):
if (
not side_effecting_insertions
and isinstance(item, collections.MutableSequence)
and isinstance(item, abc.MutableSequence)
and len(item) == len(indices)
):
for i0, i in enumerate(indices):
Expand Down Expand Up @@ -254,7 +258,7 @@ def __setitem__(self, index, item):
self.extend(cached_items)
raise e
else:
if not isinstance(item, collections.Sequence):
if not isinstance(item, abc.Sequence):
raise TypeError("can only assign a sequence")
if len(item) != len(indices):
raise ValueError(
Expand Down Expand Up @@ -292,7 +296,7 @@ def insert(self, index, item):
if conversion_func else item
)

collections.MutableSequence.register(sequenceClass)
abc.MutableSequence.register(sequenceClass)
sequenceClass.__radd__ = __radd__
sequenceClass.__add__ = __add__
sequenceClass.__getitem__ = __getitem__
Expand All @@ -303,7 +307,7 @@ def insert(self, index, item):
sequenceClass.__repr__ = __repr__

seen = set()
for klass in (collections.MutableSequence, collections.Sequence):
for klass in (abc.MutableSequence, abc.Sequence):
for name in klass.__dict__.keys():
if name not in seen:
seen.add(name)
Expand Down