Skip to content

Commit 625ea1c

Browse files
committed
bind: fix py3.9+ deprecation of collections members
This fix is part of a larger error handling cleanup. Py3 generates deprecation warnings of access to containers.Iterator (etc) and states they will break in py3.9. Commit prefers to access members via collections.abs if py3, with a fallback for older py2 location. Refs go-python/gopy #254
1 parent d0cfd28 commit 625ea1c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

bind/gen.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ mod.add_function('NumHandles', retval('int'), [])
226226
227227
# the following is required to enable dlopen to open the _go.so file
228228
import os,sys,inspect,collections
229+
try:
230+
import collections.abc as _collections_abc
231+
except ImportError:
232+
_collections_abc = collections
233+
229234
cwd = os.getcwd()
230235
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
231236
os.chdir(currentdir)
@@ -250,6 +255,10 @@ os.chdir(cwd)
250255
# %[2]s
251256
252257
import collections
258+
try:
259+
import collections.abc as _collections_abc
260+
except ImportError:
261+
_collections_abc = collections
253262
%[6]s
254263
255264
# to use this code in your end-user python file, import it as follows:
@@ -263,6 +272,10 @@ import collections
263272

264273
GoPkgDefs = `
265274
import collections
275+
try:
276+
import collections.abc as _collections_abc
277+
except ImportError:
278+
_collections_abc = collections
266279
267280
class GoClass(object):
268281
"""GoClass is the base class for all GoPy wrapper classes"""

bind/gen_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ otherwise parameter is a python list that we copy from
112112
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
113113
g.pywrap.Printf("if len(args) > 0:\n")
114114
g.pywrap.Indent()
115-
g.pywrap.Printf("if not isinstance(args[0], collections.Mapping):\n")
115+
g.pywrap.Printf("if not isinstance(args[0], _collections_abc.Mapping):\n")
116116
g.pywrap.Indent()
117117
g.pywrap.Printf("raise TypeError('%s.__init__ takes a mapping as argument')\n", slNm)
118118
g.pywrap.Outdent()

bind/gen_slice.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ otherwise parameter is a python list that we copy from
106106
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
107107
g.pywrap.Printf("if len(args) > 0:\n")
108108
g.pywrap.Indent()
109-
g.pywrap.Printf("if not isinstance(args[0], collections.Iterable):\n")
109+
g.pywrap.Printf("if not isinstance(args[0], _collections_abc.Iterable):\n")
110110
g.pywrap.Indent()
111111
g.pywrap.Printf("raise TypeError('%s.__init__ takes a sequence as argument')\n", slNm)
112112
g.pywrap.Outdent()
@@ -220,7 +220,7 @@ otherwise parameter is a python list that we copy from
220220
if slc.isSlice() {
221221
g.pywrap.Printf("def __iadd__(self, value):\n")
222222
g.pywrap.Indent()
223-
g.pywrap.Printf("if not isinstance(value, collections.Iterable):\n")
223+
g.pywrap.Printf("if not isinstance(value, _collections_abc.Iterable):\n")
224224
g.pywrap.Indent()
225225
g.pywrap.Printf("raise TypeError('%s.__iadd__ takes a sequence as argument')\n", slNm)
226226
g.pywrap.Outdent()

0 commit comments

Comments
 (0)