[Piglit] [PATCH 36/44] framework/test/glsl_parser_test.py: stop supporting bytes

baker.dylan.c at gmail.com baker.dylan.c at gmail.com
Wed Jan 27 16:06:44 PST 2016


From: Dylan Baker <baker.dylan.c at gmail.com>

This makes the GLSLParserTest class only support str (unicode in python
2), and not bytes (str in python 2).

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/test/glsl_parser_test.py  | 27 +++++++++++++++++++--------
 framework/test/shader_test.py       |  9 ++++++---
 unittests/glsl_parser_test_tests.py |  6 +++---
 unittests/shader_test_tests.py      | 10 +++++-----
 4 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/framework/test/glsl_parser_test.py b/framework/test/glsl_parser_test.py
index 73224c6..ce043a7 100644
--- a/framework/test/glsl_parser_test.py
+++ b/framework/test/glsl_parser_test.py
@@ -27,6 +27,8 @@ from __future__ import (
 import os
 import re
 
+import six
+
 from framework import exceptions
 from .base import TestIsSkip
 from .opengl import FastSkipMixin
@@ -48,7 +50,10 @@ _FORCE_DESKTOP_VERSION = os.environ.get('PIGLIT_FORCE_GLSLPARSER_DESKTOP', False
 
 def _is_gles_version(version):
     """Return True if version is es, otherwsie false."""
-    if isinstance(version, basestring):
+    assert not isinstance(version, six.binary_type), \
+        '{}({})'.format(version, type(version))
+
+    if isinstance(version, six.text_type):
         # GLES 3+ versions should have "es" appended, even though
         # glslparsertest doesn't require them. If the version ends in "es" then
         # it is a GLES test for sure.
@@ -92,13 +97,19 @@ class GLSLParserTest(FastSkipMixin, PiglitBaseTest):
 
         # Parse the config file and get the config section, then write this
         # section to a StringIO and pass that to ConfigParser
-        with open(filepath, 'r') as testfile:
-            try:
+        try:
+            with open(filepath, 'r') as testfile:
+                # Python 2 returns a bytes instance, but python 3 returns str
+                # (unicode) instance.
+                if six.PY2:
+                    testfile = testfile.read().decode('utf-8')
+                elif six.PY3:
+                    testfile = testfile.read()
                 config = self.__parser(testfile, filepath)
-                command = self.__get_command(config, filepath)
-            except GLSLParserInternalError as e:
-                raise exceptions.PiglitFatalError(
-                    'In file "{}":\n{}'.format(filepath, str(e)))
+            command = self.__get_command(config, filepath)
+        except GLSLParserInternalError as e:
+            raise exceptions.PiglitFatalError(
+                'In file "{}":\n{}'.format(filepath, six.text_type(e)))
 
         super(GLSLParserTest, self).__init__(command, run_concurrent=True)
 
@@ -200,7 +211,7 @@ class GLSLParserTest(FastSkipMixin, PiglitBaseTest):
         # This allows us to run the loop until we find the header, stop and
         # then run again looking for the config sections.
         # This reduces the need for if statements substantially
-        lines = (l.strip() for l in testfile)
+        lines = (l.strip() for l in testfile.split('\n'))
 
         is_header = re.compile(r'(//|/\*|\*)\s*\[config\]')
         for line in lines:
diff --git a/framework/test/shader_test.py b/framework/test/shader_test.py
index f5c26ba..4fb33a0 100644
--- a/framework/test/shader_test.py
+++ b/framework/test/shader_test.py
@@ -61,10 +61,13 @@ class ShaderTest(FastSkipMixin, PiglitBaseTest):
         # an exception. The second looks for the GL version or raises an
         # exception
         with open(filename, 'r') as shader_file:
+            # The mock in python 3.3 doesn't support readlines(), so use
+            # read().split() as a workaround
             if six.PY3:
-                lines = (l for l in shader_file.readlines())
+                lines = (l for l in shader_file.read().split('\n'))
             elif six.PY2:
-                lines = (l.decode('utf-8') for l in shader_file.readlines())
+                lines = (l.decode('utf-8') for l in
+                         shader_file.read().split(b'\n'))
 
             # Find the config section
             for line in lines:
@@ -72,7 +75,7 @@ class ShaderTest(FastSkipMixin, PiglitBaseTest):
                 # soon as we do then we can move on to geting the
                 # configuration. The first line needs to be parsed by the next
                 # block.
-                if line.lstrip().startswith('[require]'):
+                if line.startswith('[require]'):
                     break
             else:
                 raise exceptions.PiglitFatalError(
diff --git a/unittests/glsl_parser_test_tests.py b/unittests/glsl_parser_test_tests.py
index de00d11..c732bec 100644
--- a/unittests/glsl_parser_test_tests.py
+++ b/unittests/glsl_parser_test_tests.py
@@ -417,7 +417,7 @@ def test_set_glsl_version():
                                '_GLSLParserTest__get_command',
                                return_value=['foo']):
             with mock.patch('framework.test.glsl_parser_test.open',
-                            mock.mock_open()):
+                            mock.mock_open(), create=True):
                 with mock.patch('framework.test.glsl_parser_test.os.stat',
                                 mock.mock_open()):
                     test = glsl.GLSLParserTest('foo')
@@ -433,7 +433,7 @@ def test_set_glsl_es_version():
                                '_GLSLParserTest__get_command',
                                return_value=['foo']):
             with mock.patch('framework.test.glsl_parser_test.open',
-                            mock.mock_open()):
+                            mock.mock_open(), create=True):
                 with mock.patch('framework.test.glsl_parser_test.os.stat',
                                 mock.mock_open()):
                     test = glsl.GLSLParserTest('foo')
@@ -449,7 +449,7 @@ def test_set_gl_required():
                                '_GLSLParserTest__get_command',
                                return_value=['foo']):
             with mock.patch('framework.test.glsl_parser_test.open',
-                            mock.mock_open()):
+                            mock.mock_open(), create=True):
                 with mock.patch('framework.test.glsl_parser_test.os.stat',
                                 mock.mock_open()):
                     test = glsl.GLSLParserTest('foo')
diff --git a/unittests/shader_test_tests.py b/unittests/shader_test_tests.py
index 8d67511..b91cde6 100644
--- a/unittests/shader_test_tests.py
+++ b/unittests/shader_test_tests.py
@@ -132,7 +132,7 @@ def test_find_requirements_gl_version():
             'GL_ARB_ham_sandwhich\n')
 
     with mock.patch('framework.test.shader_test.open',
-                    mock.mock_open(read_data=data)):
+                    mock.mock_open(read_data=data), create=True):
         test = testm.ShaderTest('null')
     nt.eq_(test.gl_version, 2.0)
 
@@ -144,7 +144,7 @@ def test_find_requirements_gles_version():
             'GL_ARB_ham_sandwhich\n')
 
     with mock.patch('framework.test.shader_test.open',
-                    mock.mock_open(read_data=data)):
+                    mock.mock_open(read_data=data), create=True):
         test = testm.ShaderTest('null')
     nt.eq_(test.gles_version, 2.0)
 
@@ -157,7 +157,7 @@ def test_find_requirements_glsl_version():
             'GL_ARB_ham_sandwhich\n')
 
     with mock.patch('framework.test.shader_test.open',
-                    mock.mock_open(read_data=data)):
+                    mock.mock_open(read_data=data), create=True):
         test = testm.ShaderTest('null')
     nt.eq_(test.glsl_version, 1.0)
 
@@ -170,7 +170,7 @@ def test_find_requirements_glsl_es_version():
             'GL_ARB_ham_sandwhich\n')
 
     with mock.patch('framework.test.shader_test.open',
-                    mock.mock_open(read_data=data)):
+                    mock.mock_open(read_data=data), create=True):
         test = testm.ShaderTest('null')
     nt.eq_(test.glsl_es_version, 2.0)
 
@@ -187,7 +187,7 @@ def test_ignore_shader_runner_directives():
 
     def test(config):
         with mock.patch('framework.test.shader_test.open',
-                        mock.mock_open(read_data=config)):
+                        mock.mock_open(read_data=config), create=True):
             test = testm.ShaderTest('null')
         nt.eq_(test.gl_required, {'GL_foobar'})
 
-- 
2.7.0



More information about the Piglit mailing list