summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2013-12-14 22:46:14 -0500
committerTimothy Crosley <timothy.crosley@gmail.com>2013-12-14 22:46:14 -0500
commit832caa64c76bb66e9fa45cb96d7ef4a6a9b080dc (patch)
tree115fddf5c1dae2548d2ea8afd87525127337686c
parent185c87d0b5073cbb684fe38d5143515a5b0d07c5 (diff)
downloadpies-832caa64c76bb66e9fa45cb96d7ef4a6a9b080dc.tar.gz
Add magical unicode __str__ support to Python2
-rw-r--r--README.md1
-rw-r--r--pies/overrides.py46
-rw-r--r--pies/unittest.py4
3 files changed, 22 insertions, 29 deletions
diff --git a/README.md b/README.md
index 2f4dcc5..c694cea 100644
--- a/README.md
+++ b/README.md
@@ -64,6 +64,7 @@ Functions:
Types:
+- object (__str__ automatically has correct behavior on all versions of Python)
- chr (creates a unichr object in Python2)
- str (creates a unicode object in Python2)
- dict (creating a dict using dict() will give you all the special Python3 itemview results, but using {} will not)
diff --git a/pies/overrides.py b/pies/overrides.py
index e8376f5..02ad52f 100644
--- a/pies/overrides.py
+++ b/pies/overrides.py
@@ -24,6 +24,7 @@ from __future__ import absolute_import
import functools
from numbers import Integral
+from ._utils import unmodified_isinstance, with_metaclass
from .version_info import PY2, PY3, VERSION
__version__ = "2.0.1"
@@ -38,10 +39,11 @@ native_str = str
native_chr = chr
native_input = input
native_next = next
+native_object = object
common = ['native_dict', 'native_round', 'native_filter', 'native_map', 'native_range', 'native_str', 'native_chr',
'native_input', 'PY2', 'PY3', 'u', 'itemsview', 'valuesview', 'keysview', 'execute', 'integer_types',
- 'native_next', 'with_metaclass']
+ 'native_next', 'native_object', 'with_metaclass']
if PY3:
import urllib
@@ -161,7 +163,7 @@ else:
def keysview(collection):
return dict_keys(collection)
- class dict(native_dict):
+ class dict(unmodified_isinstance(native_dict)):
def has_key(self, *args, **kwargs):
return AttributeError("'dict' object has no attribute 'has_key'")
@@ -198,30 +200,18 @@ else:
except Exception:
native_next(iterator)
+ class FixStr(type):
+ def __new__(cls, name, bases, dct):
+ if '__str__' in dct:
+ dct['__unicode__'] = dct['__str__']
+ dct['__str__'] = lambda self: self.__unicode__().encode('utf-8')
+ return type.__new__(cls, name, bases, dct)
+
+ def __instancecheck__(cls, instance):
+ return isinstance(instance, native_object)
+
+ class object(with_metaclass(FixStr, object)):
+ pass
+
__all__ = common + ['round', 'dict', 'apply', 'cmp', 'coerce', 'execfile', 'raw_input', 'unpacks', 'str', 'chr',
- 'input', 'range', 'filter', 'map', 'zip']
-
-def with_metaclass(meta, *bases):
- """
- Enables use of meta classes across Python Versions.
- taken from jinja2/_compat.py
-
- Use it like this::
-
- class BaseForm(object):
- pass
-
- class FormType(type):
- pass
-
- class Form(with_metaclass(FormType, BaseForm)):
- pass
- """
- class metaclass(meta):
- __call__ = type.__call__
- __init__ = type.__init__
- def __new__(cls, name, this_bases, d):
- if this_bases is None:
- return type.__new__(cls, name, (), d)
- return meta(name, bases, d)
- return metaclass('temporary_class', None, {})
+ 'input', 'range', 'filter', 'map', 'zip', 'object']
diff --git a/pies/unittest.py b/pies/unittest.py
index 328e554..a5d0960 100644
--- a/pies/unittest.py
+++ b/pies/unittest.py
@@ -3,13 +3,15 @@ from __future__ import absolute_import
import sys
from unittest import *
+from ._utils import unmodified_isinstance
+
NativeTestCase = TestCase
if sys.version_info < (2, 7):
skip = lambda why: (lambda func: 'skip')
skipIf = lambda cond, why: (skip(why) if cond else lambda func: func)
- class TestCase(TestCase):
+ class TestCase(unmodified_isinstance(TestCase)):
def assertIs(self, expr1, expr2, msg=None):
if expr1 is not expr2:
self.fail(msg or '%r is not %r' % (expr1, expr2))