diff options
author | Christoph Reiter <reiter.christoph@gmail.com> | 2018-04-07 18:12:03 +0200 |
---|---|---|
committer | Christoph Reiter <reiter.christoph@gmail.com> | 2018-04-07 18:12:03 +0200 |
commit | 2e1ade3d19657755984a6abe0e34033b631efeea (patch) | |
tree | b97774d894928f1e63a6b48fbd2f23a2fdaad3b0 | |
parent | 1f52c1ba5b48364c91d5eb99d1a5bb47b455b259 (diff) | |
download | pygobject-2e1ade3d19657755984a6abe0e34033b631efeea.tar.gz |
flake8: remove python2 builtin list
Only use the _compat aliases and hide them in _compat through eval().
This will make flake8 complain in case any py2 only builtins are used.
-rw-r--r-- | examples/demo/demos/rotatedtext.py | 17 | ||||
-rw-r--r-- | gi/_compat.py | 10 | ||||
-rw-r--r-- | gi/overrides/GObject.py | 4 | ||||
-rw-r--r-- | setup.cfg | 1 | ||||
-rw-r--r-- | tests/test_everything.py | 13 | ||||
-rw-r--r-- | tests/test_import_machinery.py | 2 | ||||
-rw-r--r-- | tests/test_properties.py | 2 |
7 files changed, 19 insertions, 30 deletions
diff --git a/examples/demo/demos/rotatedtext.py b/examples/demo/demos/rotatedtext.py index d47b1cfe..429bbdfa 100644 --- a/examples/demo/demos/rotatedtext.py +++ b/examples/demo/demos/rotatedtext.py @@ -29,20 +29,13 @@ cairo drawing operations instead of the Unicode heart character. from gi.repository import Gtk, Pango, PangoCairo, Gdk import cairo -import sys import math -# Python 2 and 3 handle UTF8 differently -if sys.version_info < (3, 0): - BYTES_TEXT = "I \xe2\x99\xa5 GTK+" - UTF8_TEXT = unicode(BYTES_TEXT, 'UTF-8') - BYTES_HEART = "\xe2\x99\xa5" - HEART = unicode(BYTES_HEART, 'UTF-8') -else: - UTF8_TEXT = "I ♥ GTK+" - BYTES_TEXT = bytes(UTF8_TEXT, 'utf-8') - HEART = "♥" - BYTES_HEART = bytes(HEART, 'utf-8') + +UTF8_TEXT = u"I ♥ GTK+" +HEART = u"♥" +BYTES_TEXT = UTF8_TEXT.encode() +BYTES_HEART = HEART.encode() class RotatedTextApp: diff --git a/gi/_compat.py b/gi/_compat.py index 57a2b8ec..b8a3506c 100644 --- a/gi/_compat.py +++ b/gi/_compat.py @@ -23,11 +23,12 @@ if sys.version_info[0] == 2: from UserList import UserList UserList - long_ = long - integer_types = (int, long) - string_types = (basestring,) + long_ = eval("long") + integer_types = eval("(int, long)") + string_types = eval("(basestring,)") + text_type = eval("unicode") - exec("reload = reload") + reload = eval("reload") exec("def reraise(tp, value, tb):\n raise tp, value, tb") else: @@ -42,6 +43,7 @@ else: long_ = int integer_types = (int,) string_types = (str,) + text_type = str from importlib import reload reload diff --git a/gi/overrides/GObject.py b/gi/overrides/GObject.py index 3f0d6c66..f758e6df 100644 --- a/gi/overrides/GObject.py +++ b/gi/overrides/GObject.py @@ -29,7 +29,7 @@ import gi.module from gi.overrides import override, deprecated_attr from gi.repository import GLib from gi import PyGIDeprecationWarning -from gi._compat import PY2 +from gi._compat import PY2, text_type from gi import _propertyhelper as propertyhelper from gi import _signalhelper as signalhelper @@ -264,7 +264,7 @@ class Value(GObjectModule.Value): if isinstance(py_value, str): py_value = str(py_value) elif PY2: - if isinstance(py_value, unicode): + if isinstance(py_value, text_type): py_value = py_value.encode('UTF-8') else: raise ValueError("Expected string or unicode but got %s%s" % @@ -1,6 +1,5 @@ [flake8] ignore=E501,E123,E124,E402,E731,E722 -builtins=long,unicode,basestring [coverage:run] branch=True diff --git a/tests/test_everything.py b/tests/test_everything.py index eab9b95d..429404a0 100644 --- a/tests/test_everything.py +++ b/tests/test_everything.py @@ -27,13 +27,6 @@ except: from .helper import capture_exceptions -if PY2: - UNICHAR = "\xe2\x99\xa5" - PY2_UNICODE_UNICHAR = unicode(UNICHAR, 'UTF-8') -else: - UNICHAR = "♥" - - const_str = b'const \xe2\x99\xa5 utf8' if PY3: const_str = const_str.decode('UTF-8') @@ -182,8 +175,10 @@ class TestEverything(unittest.TestCase): self.assertEqual("c", Everything.test_unichar("c")) if PY2: - self.assertEqual(UNICHAR, Everything.test_unichar(PY2_UNICODE_UNICHAR)) - self.assertEqual(UNICHAR, Everything.test_unichar(UNICHAR)) + self.assertEqual(b"\xe2\x99\xa5", Everything.test_unichar(u"♥")) + self.assertEqual(b"\xe2\x99\xa5", Everything.test_unichar(b"\xe2\x99\xa5")) + else: + self.assertEqual(u"♥", Everything.test_unichar(u"♥")) self.assertRaises(TypeError, Everything.test_unichar, "") self.assertRaises(TypeError, Everything.test_unichar, "morethanonechar") diff --git a/tests/test_import_machinery.py b/tests/test_import_machinery.py index 1433e59d..fc1ba7be 100644 --- a/tests/test_import_machinery.py +++ b/tests/test_import_machinery.py @@ -148,7 +148,7 @@ class TestImporter(unittest.TestCase): # Test that unicode strings work in python 2 if PY2: - gi.require_version('GLib', unicode('2.0')) + gi.require_version('GLib', u'2.0') if PY3: with self.assertRaises(ValueError): diff --git a/tests/test_properties.py b/tests/test_properties.py index 4beace00..ca902708 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -875,7 +875,7 @@ class TestProperty(unittest.TestCase): tester = GObject.Property() self.assertEqual(tester._type_from_python(int), GObject.TYPE_INT) if PY2: - self.assertEqual(tester._type_from_python(long), GObject.TYPE_LONG) + self.assertEqual(tester._type_from_python(long_), GObject.TYPE_LONG) self.assertEqual(tester._type_from_python(bool), GObject.TYPE_BOOLEAN) self.assertEqual(tester._type_from_python(float), GObject.TYPE_DOUBLE) self.assertEqual(tester._type_from_python(str), GObject.TYPE_STRING) |