From c1bee55192516ddaac39b948fcea37745c6ca752 Mon Sep 17 00:00:00 2001 From: "Ivan A. Melnikov" Date: Thu, 10 Oct 2013 15:06:44 +0400 Subject: [PATCH] Fix several python3 incompatibilities Change-Id: Ibf5dd6c0b6bcd161364daf35d618641f6079acf5 --- taskflow/persistence/backends/__init__.py | 4 ++-- taskflow/persistence/backends/impl_dir.py | 7 +++++-- taskflow/storage.py | 9 ++++----- taskflow/tests/unit/test_utils_failure.py | 2 +- taskflow/utils/lock_utils.py | 2 +- taskflow/utils/misc.py | 10 +++++----- taskflow/utils/threading_utils.py | 3 ++- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/taskflow/persistence/backends/__init__.py b/taskflow/persistence/backends/__init__.py index 18ba2098..5b1b7238 100644 --- a/taskflow/persistence/backends/__init__.py +++ b/taskflow/persistence/backends/__init__.py @@ -17,12 +17,12 @@ # under the License. import logging -import urlparse - +from six.moves import urllib_parse as urlparse # noqa from stevedore import driver from taskflow import exceptions as exc + # NOTE(harlowja): this is the entrypoint namespace, not the module namespace. BACKEND_NAMESPACE = 'taskflow.persistence' diff --git a/taskflow/persistence/backends/impl_dir.py b/taskflow/persistence/backends/impl_dir.py index 8060b66f..0c5d74d9 100644 --- a/taskflow/persistence/backends/impl_dir.py +++ b/taskflow/persistence/backends/impl_dir.py @@ -21,6 +21,7 @@ import errno import logging import os import shutil +import six import threading import weakref @@ -103,11 +104,13 @@ class Connection(base.Connection): cache_info = self._file_cache.setdefault(filename, {}) if not cache_info or mtime > cache_info.get('mtime', 0): with open(filename, 'rb') as fp: - cache_info['data'] = fp.read() + cache_info['data'] = fp.read().decode('utf-8') cache_info['mtime'] = mtime return cache_info['data'] def _write_to(self, filename, contents): + if isinstance(contents, six.text_type): + contents = contents.encode('utf-8') with open(filename, 'wb') as fp: fp.write(contents) self._file_cache.pop(filename, None) @@ -405,7 +408,7 @@ def _str_2_datetime(text): """Converts an iso8601 string/text into a datetime object (or none)""" if text is None: return None - if not isinstance(text, basestring): + if not isinstance(text, six.string_types): raise ValueError("Can only convert strings into a datetime object and" " not %r" % (text)) if not len(text): diff --git a/taskflow/storage.py b/taskflow/storage.py index 88f06901..e7d3d871 100644 --- a/taskflow/storage.py +++ b/taskflow/storage.py @@ -18,7 +18,6 @@ import contextlib import logging - import six from taskflow import exceptions @@ -171,7 +170,7 @@ class Storage(object): result_mapping = self._result_mappings.get(uuid, None) if result_mapping is None: return - for name, index in result_mapping.items(): + for name, index in six.iteritems(result_mapping): try: _item_from_result(data, index, name) except exceptions.NotFound: @@ -220,7 +219,7 @@ class Storage(object): self.save(injector_uuid, pairs) self.set_result_mapping(injector_uuid, dict((name, name) - for name in pairs.iterkeys())) + for name in six.iterkeys(pairs))) def set_result_mapping(self, uuid, mapping): """Set mapping for naming task results @@ -233,7 +232,7 @@ class Storage(object): if not mapping: return self._result_mappings[uuid] = mapping - for name, index in mapping.iteritems(): + for name, index in six.iteritems(mapping): entries = self._reverse_mapping.setdefault(name, []) entries.append((uuid, index)) if len(entries) > 1: @@ -271,7 +270,7 @@ class Storage(object): def fetch_mapped_args(self, args_mapping): """Fetch arguments for the task using arguments mapping""" return dict((key, self.fetch(name)) - for key, name in args_mapping.iteritems()) + for key, name in six.iteritems(args_mapping)) def set_flow_state(self, state): """Set flowdetails state and save it""" diff --git a/taskflow/tests/unit/test_utils_failure.py b/taskflow/tests/unit/test_utils_failure.py index fc076ba3..436f3b28 100644 --- a/taskflow/tests/unit/test_utils_failure.py +++ b/taskflow/tests/unit/test_utils_failure.py @@ -116,7 +116,7 @@ class FailureObjectTestCase(test.TestCase): traceback_str=None, exc_type_names=['Exception'], hi='hi there') - expected = "Failure.__init__ got unexpected keyword argument: 'hi'" + expected = "Failure.__init__ got unexpected keyword argument(s): hi" self.assertEquals(str(ctx.exception), expected) def test_empty_does_not_reraise(self): diff --git a/taskflow/utils/lock_utils.py b/taskflow/utils/lock_utils.py index 3344ceb0..36217afc 100644 --- a/taskflow/utils/lock_utils.py +++ b/taskflow/utils/lock_utils.py @@ -85,7 +85,7 @@ class MultiLock(object): return lock.locked() return False - for i in xrange(0, len(self._locked)): + for i in range(0, len(self._locked)): if self._locked[i] or is_locked(self._locks[i]): raise threading.ThreadError("Lock %s not previously released" % (i + 1)) diff --git a/taskflow/utils/misc.py b/taskflow/utils/misc.py index 4e6b5978..f39c2dec 100644 --- a/taskflow/utils/misc.py +++ b/taskflow/utils/misc.py @@ -21,16 +21,15 @@ import collections import copy import errno import functools -import itertools import logging import os +import six import sys import traceback from taskflow import exceptions from taskflow.utils import reflection -import six LOG = logging.getLogger(__name__) @@ -66,7 +65,7 @@ def get_version_string(obj): def get_duplicate_keys(iterable, key=None): if key is not None: - iterable = itertools.imap(key, iterable) + iterable = six.moves.map(key, iterable) keys = set() duplicates = set() for item in iterable: @@ -254,8 +253,9 @@ class Failure(object): self._exc_type_names = kwargs.pop('exc_type_names', []) self._traceback_str = kwargs.pop('traceback_str', None) if kwargs: - raise TypeError('Failure.__init__ got unexpected keyword ' - 'argument: %r' % kwargs.keys()[0]) + raise TypeError( + 'Failure.__init__ got unexpected keyword argument(s): %s' + % ', '.join(six.iterkeys(kwargs))) def _matches(self, other): if self is other: diff --git a/taskflow/utils/threading_utils.py b/taskflow/utils/threading_utils.py index dc243534..692781c6 100644 --- a/taskflow/utils/threading_utils.py +++ b/taskflow/utils/threading_utils.py @@ -18,6 +18,7 @@ import logging import multiprocessing +import six import threading import time import types @@ -98,7 +99,7 @@ class ThreadSafeMeta(type): """Metaclass that adds locking to all pubic methods of a class""" def __new__(cls, name, bases, attrs): - for attr_name, attr_value in attrs.iteritems(): + for attr_name, attr_value in six.iteritems(attrs): if isinstance(attr_value, types.FunctionType): if attr_name[0] != '_': attrs[attr_name] = lock_utils.locked(attr_value)