pyblish / pyblish-base

Pyblish base library - see https://github.com/pyblish/pyblish for details.
Other
127 stars 59 forks source link

Slicing Context raises KeyError in Python 3.6 #336

Open BigRoy opened 6 years ago

BigRoy commented 6 years ago

Issue

In our plug-ins we've been doing context[:] to get a copy of the instances list from the context, though in Python 3.6 this is giving us the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\pyblish-base\pyblish\plugin.py", line 771, in __getitem__
    raise KeyError("%s not in list" % item)
KeyError: 'slice(None, None, None) not in list'

Confirmed and reproducable with:

import pyblish.api as api

c = api.Context()
print(c[:])

This works in Python 2.7 - but raises the error above in Python 3.6.

Workaround

It does however work when doing list(context) to get a copy of the list.

import pyblish.api as api

c = api.Context()
print(list(c))

Shouldn't the first one also work if it "should behave" like a regular list?

BigRoy commented 6 years ago

It seems to be coming from this line.

Refactoring it to:

        if isinstance(item, (int, slice)):

Allows the slicing to function as in a regular Python list in both Python 2.7 and Python 3.6.


Why it's working without this in Python 2.7 on the other hand makes me confused now. It seems in Python 2.7 that with slicing it never enters the __getitem__ function. :)

More info: