diff options
author | Toshio Kuratomi <a.badger@gmail.com> | 2018-03-20 12:58:51 -0700 |
---|---|---|
committer | Toshio Kuratomi <a.badger@gmail.com> | 2018-03-29 15:10:28 -0700 |
commit | f7d14a78eb9be5668f341e55d173701abc70368d (patch) | |
tree | 992512798e5a027c9a1b16f2fa0320236b7acf7c | |
parent | ff2a4eadd8f33de80f8a9fd33b244194591e9f5a (diff) | |
download | ansible-f7d14a78eb9be5668f341e55d173701abc70368d.tar.gz |
Fix csvfile traceback on Python3 (#37625)
* Fix csvfile traceback on Python3
The csvfile lookup uses some custom iterators. These needed to be
ported to handle the python3 iterator protocol. In addition, the
csvfile module takes an iterator of byte strings in Python2 and an
iterator of text strings in Python3
Fixes #36808
(cherry picked from commit 09325b619e4f015d94b18a4840f5ff3621b1a40f)
-rw-r--r-- | lib/ansible/plugins/lookup/csvfile.py | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/ansible/plugins/lookup/csvfile.py b/lib/ansible/plugins/lookup/csvfile.py index eadca511bc..9818cb574c 100644 --- a/lib/ansible/plugins/lookup/csvfile.py +++ b/lib/ansible/plugins/lookup/csvfile.py @@ -53,6 +53,7 @@ from collections import MutableSequence from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase +from ansible.module_utils.six import PY2 from ansible.module_utils._text import to_bytes, to_native, to_text @@ -66,8 +67,10 @@ class CSVRecoder: def __iter__(self): return self - def next(self): - return self.reader.next().encode("utf-8") + def __next__(self): + return next(self.reader).encode("utf-8") + + next = __next__ # For Python 2 class CSVReader: @@ -77,13 +80,19 @@ class CSVReader: """ def __init__(self, f, dialect=csv.excel, encoding='utf-8', **kwds): - f = CSVRecoder(f, encoding) + if PY2: + f = CSVRecoder(f, encoding) + else: + f = codecs.getreader(encoding)(f) + self.reader = csv.reader(f, dialect=dialect, **kwds) - def next(self): - row = self.reader.next() + def __next__(self): + row = next(self.reader) return [to_text(s) for s in row] + next = __next__ # For Python 2 + def __iter__(self): return self @@ -93,8 +102,8 @@ class LookupModule(LookupBase): def read_csv(self, filename, key, delimiter, encoding='utf-8', dflt=None, col=1): try: - f = open(filename, 'r') - creader = CSVReader(f, delimiter=to_bytes(delimiter), encoding=encoding) + f = open(filename, 'rb') + creader = CSVReader(f, delimiter=to_native(delimiter), encoding=encoding) for row in creader: if row[0] == key: |