[openstack-dev] [nova] contextlib.nested and Python3 failing

Joshua Harlow harlowja at outlook.com
Sun Aug 23 21:16:24 UTC 2015


On Fri, 21 Aug 2015 10:39:34 -0500
"Kevin L. Mitchell" <kevin.mitchell at rackspace.com> wrote:

> On Wed, 2015-08-19 at 16:51 -0700, Sylvain Bauza wrote:
> > I was writing some tests so I added a contextlib.nested to a
> > checked TestCase [1]. Unfortunately, contextlib.nested is no longer
> > available in Python3 and there is no clear solution on how to
> > provide a compatible import for both python2 and python3:
> >   - either providing a python3 compatible behaviour by using 
> > contextlib.ExitStack but that class is not available in Python 2
> >   - or provide contextlib2 for python2 (and thus adding it to the 
> > requirements)
> 
> Actually, there should no longer be a need to use contextlib.nested.
> We've explicitly dropped Python 2.6 compatibility, which means we're
> expecting compatibility with Python 2.7+ only, and as of Python 2.7,
> the 'with' statement supports accepting multiple 'as' clauses.  The
> contextlib.nested tool was really only necessary to work around that
> functionality being missing in Python 2.6, and has been deprecated as
> of Python 2.7 because it's no longer necessary.

Other option:

https://contextlib2.readthedocs.org/en/latest/#contextlib2.ExitStack

Which is also in py3.x

https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack

Might be easier to read than the multiple as clauses and such, use at
your own will, but it's in the global-requirements (contextlib2 that
is); so use it where applicable imho :)

-Josh



More information about the OpenStack-dev mailing list