Python 3.7 support - deprecation of classes in the collections module

Change the base classes in the pyabc.i file to use the
collections.abc module instead of collections due to the deprecation
of the classes in the collections module in Python 3.7.
This commit is contained in:
William S Fulton 2018-06-12 21:05:07 +01:00
commit 4079fb927b
4 changed files with 53 additions and 18 deletions

View file

@ -7,6 +7,22 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.0.0 (in progress)
===========================
2018-06-12: wsfulton
[Python] The %pythonabc feature in pyabc.i now uses base classes
collections.abc.MutableSequence
collections.abc.MutableMapping
collections.abc.MutableSet
instead of
collections.MutableSequence
collections.MutableMapping
collections.MutableSet
as the latter are deprecated in Python 3.7 and are due to be removed in Python 3.8.
The classes in collections.abc.* are available from Python 3.3 onwards. If you
require support for Python 3.2, then copy the pyabc.i file and modify by removing
the few instances of the .abc sub-module.
*** POTENTIAL INCOMPATIBILITY ***
2018-06-12: olly,wsfulton
[Python] #701 Remove support for Python versions < 2.7 and 3.0 and 3.1.

View file

@ -6426,7 +6426,8 @@ modify the buffer.
<p>
By including <tt>pyabc.i</tt> and using the <tt>-py3</tt> command
line option when calling SWIG, the proxy classes of the STL containers
will automatically gain an appropriate abstract base class. For
will automatically gain an appropriate abstract base class from the
<tt>collections.abc</tt> module. For
example, the following SWIG interface:
</p>
@ -6443,8 +6444,8 @@ namespace std {
<p>
will generate a Python proxy class <tt>Mapii</tt> inheriting from
<tt>collections.MutableMap</tt> and a proxy class <tt>IntList</tt>
inheriting from <tt>collections.MutableSequence</tt>.
<tt>collections.abc.MutableMap</tt> and a proxy class <tt>IntList</tt>
inheriting from <tt>collections.abc.MutableSequence</tt>.
</p>
<p>
@ -6453,7 +6454,7 @@ used to define an abstract base class for your own C++ class:
</p>
<div class="code"><pre>
%pythonabc(MySet, collections.MutableSet);
%pythonabc(MySet, collections.abc.MutableSet);
</pre></div>
<p>
@ -6461,6 +6462,14 @@ For details of abstract base class, please see
<a href="https://www.python.org/dev/peps/pep-3119/">PEP 3119</a>.
</p>
<p>
<b>Compatibility Note:</b> SWIG-4.0.0 changed the base classes to use the
<tt>collections.abc</tt> module instead of <tt>collections</tt> due to the deprecation
of the classes in the <tt>collections</tt> module in Python 3.7.
The <tt>collections.abc</tt> module was introduced in Python 3.3 and hence this feature
requires Python 3.3 or later.
</p>
<H3><a name="Python_nn77">38.12.4 Byte string output conversion</a></H3>

View file

@ -1,5 +1,11 @@
import sys
# collections.abc requires Python 3.3+
if sys.version_info[0:2] < (3, 3):
exit(0)
from python_abstractbase import *
from collections import *
import collections.abc
# This is expected to fail with -builtin option
# Builtin types can't inherit from pure-python abstract bases
@ -10,12 +16,16 @@ if is_python_builtin():
if not is_swig_py3:
exit(0)
assert issubclass(Mapii, MutableMapping)
assert issubclass(Multimapii, MutableMapping)
assert issubclass(IntSet, MutableSet)
assert issubclass(IntMultiset, MutableSet)
assert issubclass(IntVector, MutableSequence)
assert issubclass(IntList, MutableSequence)
def check_issubclass(derived, base):
if not issubclass(derived, base):
raise RuntimeError("{} is not a subclass of {}".format(derived, base))
check_issubclass(Mapii, collections.abc.MutableMapping)
check_issubclass(Multimapii, collections.abc.MutableMapping)
check_issubclass(IntSet, collections.abc.MutableSet)
check_issubclass(IntMultiset, collections.abc.MutableSet)
check_issubclass(IntVector, collections.abc.MutableSequence)
check_issubclass(IntList, collections.abc.MutableSequence)
mapii = Mapii()
multimapii = Multimapii()

View file

@ -1,10 +1,10 @@
%define %pythonabc(Type, Abc)
%feature("python:abc", #Abc) Type;
%enddef
%pythoncode %{import collections%}
%pythonabc(std::vector, collections.MutableSequence);
%pythonabc(std::list, collections.MutableSequence);
%pythonabc(std::map, collections.MutableMapping);
%pythonabc(std::multimap, collections.MutableMapping);
%pythonabc(std::set, collections.MutableSet);
%pythonabc(std::multiset, collections.MutableSet);
%pythoncode %{import collections.abc%}
%pythonabc(std::vector, collections.abc.MutableSequence);
%pythonabc(std::list, collections.abc.MutableSequence);
%pythonabc(std::map, collections.abc.MutableMapping);
%pythonabc(std::multimap, collections.abc.MutableMapping);
%pythonabc(std::set, collections.abc.MutableSet);
%pythonabc(std::multiset, collections.abc.MutableSet);