diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-08-11 16:41:25 +0200 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-08-12 14:44:40 +0200 |
| commit | 79d62a71751140315227891bbe175630f9d3edc3 (patch) | |
| tree | 94d71669f89666dce137420daa61e2f2ecec906e /django/utils/2to3_fixes | |
| parent | a0a0203a392f67832ba7a8a2f099e70d7db2d19e (diff) | |
[py3] Added fixer for python_2_unicode_compatible.
This doesn't deal with classes that define both __unicode__ and
__str__; the definition of __str__ should be removed first. It
doesn't guarantee that __str__ will return a str (rather than bytes)
under Python 3 either.
Diffstat (limited to 'django/utils/2to3_fixes')
| -rw-r--r-- | django/utils/2to3_fixes/__init__.py | 0 | ||||
| -rw-r--r-- | django/utils/2to3_fixes/fix_unicode.py | 36 |
2 files changed, 36 insertions, 0 deletions
diff --git a/django/utils/2to3_fixes/__init__.py b/django/utils/2to3_fixes/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/django/utils/2to3_fixes/__init__.py diff --git a/django/utils/2to3_fixes/fix_unicode.py b/django/utils/2to3_fixes/fix_unicode.py new file mode 100644 index 0000000000..613734ca86 --- /dev/null +++ b/django/utils/2to3_fixes/fix_unicode.py @@ -0,0 +1,36 @@ +"""Fixer for __unicode__ methods. + +Uses the django.utils.encoding.python_2_unicode_compatible decorator. +""" + +from __future__ import unicode_literals + +from lib2to3 import fixer_base +from lib2to3.fixer_util import find_indentation, Name, syms, touch_import +from lib2to3.pgen2 import token +from lib2to3.pytree import Leaf, Node + + +class FixUnicode(fixer_base.BaseFix): + + BM_compatible = True + PATTERN = """ + classdef< 'class' any+ ':' + suite< any* + funcdef< 'def' unifunc='__unicode__' + parameters< '(' NAME ')' > any+ > + any* > > + """ + + def transform(self, node, results): + unifunc = results["unifunc"] + strfunc = Name("__str__", prefix=unifunc.prefix) + unifunc.replace(strfunc) + + klass = node.clone() + klass.prefix = '\n' + find_indentation(node) + decorator = Node(syms.decorator, [Leaf(token.AT, "@"), Name('python_2_unicode_compatible')]) + decorated = Node(syms.decorated, [decorator, klass], prefix=node.prefix) + node.replace(decorated) + + touch_import('django.utils.encoding', 'python_2_unicode_compatible', decorated) |
