[Piglit] [PATCH] python: function with six version 1.5.2
Brian Paul
brianp at vmware.com
Thu Feb 11 14:07:16 UTC 2016
On 02/10/2016 07:07 PM, Dylan Baker wrote:
> CMake actually marks that we require six 1.4.0, however, I can't find
> any packages anywhere for 1.4.0, and the lowest version I've seen
> requested is 1.5.2.
>
> This fixes requirements for working with six 1.5.2, and sets tox to use
> 1.5.2 (and a suitable version of mock). Primarily there are a few things
> we're using that are not available: six.moves.getcwd, six.viewvalues,
> six.python_2_unicode_compatible.
>
> cc: Brian Paul <brianp at vmware.com>
> Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
> ---
>
> Brian, This gets at least all of the unit tests running with the version
> of six you have. I can adjust more things if you continue to have
> problems.
>
> Ideally if you can upgrade six that would be better, but I understand
> that isn't always possible, so if you can't upgrade six we'll just have
> to merge this.
>
> CMakeLists.txt | 2 +-
> framework/backends/compression.py | 4 ++--
> framework/backends/json.py | 4 ++--
> framework/compat.py | 36 ++++++++++++++++++++++++++++++++++--
> framework/exceptions.py | 7 ++++---
> framework/status.py | 6 +++---
> tox.ini | 4 ++--
> unittests/core_tests.py | 11 ++++++++++-
> unittests/summary_html_tests.py | 11 ++++++++++-
> unittests/utils.py | 16 ++++++++++++----
> 10 files changed, 80 insertions(+), 21 deletions(-)
>
> diff --git a/CMakeLists.txt b/CMakeLists.txt
> index b822934..d136edc 100644
> --- a/CMakeLists.txt
> +++ b/CMakeLists.txt
> @@ -206,7 +206,7 @@ set(Python_ADDITIONAL_VERSIONS
> find_package(PythonInterp REQUIRED)
> find_package(PythonNumpy 1.6.2 REQUIRED)
> find_package(PythonMako 0.8.0 REQUIRED)
> -find_package(PythonSix 1.4.0 REQUIRED)
> +find_package(PythonSix 1.5.2 REQUIRED)
>
> # Default to compiling with debug information (`gcc -g`):
> if(NOT CMAKE_BUILD_TYPE)
> diff --git a/framework/backends/compression.py b/framework/backends/compression.py
> index 863693e..96565ec 100644
> --- a/framework/backends/compression.py
> +++ b/framework/backends/compression.py
> @@ -51,7 +51,7 @@ import contextlib
> import six
> from six.moves import cStringIO as StringIO
>
> -from framework import exceptions
> +from framework import exceptions, compat
> from framework.core import PIGLIT_CONFIG
>
> __all__ = [
> @@ -62,7 +62,7 @@ __all__ = [
> ]
>
>
> - at six.python_2_unicode_compatible
> + at compat.python_2_unicode_compatible
> class UnsupportedCompressor(exceptions.PiglitInternalError):
> def __init__(self, method, *args, **kwargs):
> super(UnsupportedCompressor, self).__init__(*args, **kwargs)
> diff --git a/framework/backends/json.py b/framework/backends/json.py
> index c4074fd..1219099 100644
> --- a/framework/backends/json.py
> +++ b/framework/backends/json.py
> @@ -35,7 +35,7 @@ except ImportError:
>
> import six
>
> -from framework import status, results, exceptions
> +from framework import status, results, exceptions, compat
> from .abstract import FileBackend, write_compressed
> from .register import Registry
> from . import compression
> @@ -582,7 +582,7 @@ def _update_seven_to_eight(result):
> This value is used for both TestResult.time and TestrunResult.time_elapsed.
>
> """
> - for test in six.viewvalues(result.tests):
> + for test in compat.viewvalues(result.tests):
> test.time = results.TimeAttribute(end=test.time)
>
> result.time_elapsed = results.TimeAttribute(end=result.time_elapsed)
> diff --git a/framework/compat.py b/framework/compat.py
> index 3c8e490..114ca36 100644
> --- a/framework/compat.py
> +++ b/framework/compat.py
> @@ -18,7 +18,7 @@
> # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> # SOFTWARE.
>
> -"""A small library that adds a single compatability decorator for python 2/3.
> +"""A small library that contains libraries equivalent to six
>
> This function is pending upstreaming in six.
>
> @@ -30,7 +30,6 @@ from __future__ import (
>
> import six
>
> -
> def python_2_bool_compatible(class_):
> """A decorator to fix __bool__/__nonzero__ name changes."""
> if six.PY2:
> @@ -41,3 +40,36 @@ def python_2_bool_compatible(class_):
> "@python_2_bool_compatible cannot be applied to {} because "
> "it doesn't define __bool__().".format(class_.__name__))
> return class_
> +
> +
> +# Some version of six don't have this function
> +try:
> + from six import python_2_unicode_compatible
> +except ImportError:
> + def python_2_unicode_compatible(class_):
> + """A decorator to fix __str/__bytes__/__unicode__ name changes."""
> + if six.PY2:
> + failed = False
> + try:
> + class_.__unicode__ = class_.__str__
> + except AttributeError:
> + failed = True
> +
> + try:
> + class_.__str__ = class_.__bytes__
> + except AttributeError:
> + if failed:
> + raise ValueError(
> + "@python_2_unicode_compatible cannot be applied to {} "
> + "because it doesn't define __str__() "
> + "or __bytes__().".format(class_.__name__))
> + return class_
> +
> +
> +try:
> + from six import viewvalues
> +except ImportError:
> + if six.PY2:
> + viewvalues = lambda d: d.viewvalues()
> + elif six.PY3:
> + viewvalues = lambda d: d.values()
> diff --git a/framework/exceptions.py b/framework/exceptions.py
> index 566372f..6e87bee 100644
> --- a/framework/exceptions.py
> +++ b/framework/exceptions.py
> @@ -24,7 +24,7 @@ from __future__ import print_function, absolute_import, division
> import sys
> import functools
>
> -import six
> +from framework import compat
>
> __all__ = [
> 'PiglitInternalError',
> @@ -34,6 +34,7 @@ __all__ = [
> ]
>
>
> +
> def handler(func):
> """Decorator function for handling errors in an entry point.
>
> @@ -54,7 +55,7 @@ def handler(func):
> return _inner
>
>
> - at six.python_2_unicode_compatible
> + at compat.python_2_unicode_compatible
> class PiglitException(Exception):
> """Class for non-error exceptions.
>
> @@ -67,7 +68,7 @@ class PiglitException(Exception):
> '\n{}'.format(super(PiglitException, self).__str__()))
>
>
> - at six.python_2_unicode_compatible
> + at compat.python_2_unicode_compatible
> class PiglitInternalError(Exception):
> """Class for errors in piglit.
>
> diff --git a/framework/status.py b/framework/status.py
> index 7fca856..6a1b85e 100644
> --- a/framework/status.py
> +++ b/framework/status.py
> @@ -62,7 +62,7 @@ from __future__ import (
>
> import six
>
> -from framework import exceptions
> +from framework import exceptions, compat
>
> __all__ = ['NOTRUN',
> 'PASS',
> @@ -97,7 +97,7 @@ def status_lookup(status):
> raise StatusException(status)
>
>
> - at six.python_2_unicode_compatible
> + at compat.python_2_unicode_compatible
> class StatusException(exceptions.PiglitInternalError):
> """ Raise this exception when a string is passed to status_lookup that
> doesn't exists
> @@ -116,7 +116,7 @@ class StatusException(exceptions.PiglitInternalError):
> return u'Unknown status "{}"'.format(self.__status)
>
>
> - at six.python_2_unicode_compatible
> + at compat.python_2_unicode_compatible
> class Status(object):
> """ A simple class for representing the output values of tests.
>
> diff --git a/tox.ini b/tox.ini
> index 7cb1722..bc65672 100644
> --- a/tox.ini
> +++ b/tox.ini
> @@ -9,11 +9,11 @@ deps =
> mako
> nose
> coverage
> - six
> + six==1.5.2
> accel: simplejson
> accel: lxml
> py27-accel,py{33,34,35}: psutil
> - py27-{accel,noaccel}: mock
> + py27-{accel,noaccel}: mock==1.0.1
> py27-accel: backports.lzma
> py27-accel: subprocess32
> generator: numpy
> diff --git a/unittests/core_tests.py b/unittests/core_tests.py
> index 9109be2..df7f48b 100644
> --- a/unittests/core_tests.py
> +++ b/unittests/core_tests.py
> @@ -30,7 +30,16 @@ import shutil
> import textwrap
>
> import nose.tools as nt
> -from six.moves import getcwd
> +import six
> +try:
> + from six.moves import getcwd
> +except ImportError:
> + # pylint: disable=no-member
> + if six.PY2:
> + getcwd = os.getcwdu
> + elif six.PY3:
> + getcwd = os.getcwd
> + # pylint: enable=no-member
>
> from framework import core, exceptions
> from . import utils
> diff --git a/unittests/summary_html_tests.py b/unittests/summary_html_tests.py
> index 60de10f..d3c28fe 100644
> --- a/unittests/summary_html_tests.py
> +++ b/unittests/summary_html_tests.py
> @@ -28,7 +28,16 @@ from __future__ import (
> import os
>
> import nose.tools as nt
> -from six.moves import getcwd
> +import six
> +try:
> + from six.moves import getcwd
> +except ImportError:
> + # pylint: disable=no-member
> + if six.PY2:
> + getcwd = os.getcwdu
> + elif six.PY3:
> + getcwd = os.getcwd
> + # pylint: enable=no-member
>
> from framework.summary import html_
> from . import utils
> diff --git a/unittests/utils.py b/unittests/utils.py
> index 8e05fdc..178d24b 100644
> --- a/unittests/utils.py
> +++ b/unittests/utils.py
> @@ -45,9 +45,17 @@ except ImportError:
> import json
> from nose.plugins.skip import SkipTest
> import six
> -from six.moves import getcwdb
> +try:
> + from six.moves import getcwd
> +except ImportError:
> + # pylint: disable=no-member
> + if six.PY2:
> + getcwd = os.getcwdu
> + elif six.PY3:
> + getcwd = os.getcwd
> + # pylint: enable=no-member
>
> -from framework import test, backends, core, results
> +from framework import test, backends, core, results, compat
>
> __all__ = [
> 'tempfile',
> @@ -93,7 +101,7 @@ JSON_DATA = {
> _SAVED_COMPRESSION = os.environ.get('PIGLIT_COMPRESSION')
>
>
> - at six.python_2_unicode_compatible
> + at compat.python_2_unicode_compatible
> class TestFailure(AssertionError):
> """An exception to be raised when a test fails.
>
> @@ -349,7 +357,7 @@ def test_in_tempdir(func):
> returns to the original directory after the test completes.
>
> """
> - original_dir = getcwdb()
> + original_dir = getcwd()
>
> @functools.wraps(func)
> def wrapper(*args, **kwargs):
>
That fixes things for me. Thanks, Dylan!
Tested-by: Brian Paul <brianp at vmware.com>
More information about the Piglit
mailing list