Dee*_*sha 6 python future new-style-class python-2.7 python-3.x
我正在newstyle使用python2将代码从python2转换为python3 future.我的项目是Django 1.11
我在forms.py中有一个类:
class Address:
...rest of code...
class AddressForm(Address, forms.ModelForm):
...rest of code...
Run Code Online (Sandbox Code Playgroud)
在Python 2中
转换为:
from buitlins import object
class Address(object):
...rest of code...
class AddressForm(Address, forms.ModelForm):
...rest of code...
Run Code Online (Sandbox Code Playgroud)
在Python 3中
我有一个selenium测试,在转换为Python3后调用此Form时失败,并出现以下错误:
File "<path_to_venv>/local/lib/python2.7/site-packages/django/utils/six.py", line 842, in <lambda>
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
File "<path_to_venv>/local/lib/python2.7/site-packages/future/types/newobject.py", line 78, in __unicode__
s = type(self).__str__(self)
RuntimeError: maximum recursion depth exceeded
Run Code Online (Sandbox Code Playgroud)
但是,当我删除导入时from buitlins import object,测试通过.
但是,由于我添加了一个未来的检查,我得到了一个未来的差异错误,因此每个班级都必须转换为newstyle.我希望它可以在Python2和Python3中工作.
有没有办法这个模块builtins模块导入只能影响一个类而不影响forms.py文件中的其他类.还是有其他方法来处理这个?
您遇到的问题似乎来自两个不同的 Python 2 现代化工具的斗争。你似乎正在使用python_2_unicode_compatible装饰器django.utils.six
def python_2_unicode_compatible(klass):
"""
A decorator that defines __unicode__ and __str__ methods under Python 2.
Under Python 3 it does nothing.
To support Python 2 and 3 with a single code base, define a __str__ method
returning text and apply this decorator to the class.
"""
if PY2:
if '__str__' not in klass.__dict__:
raise ValueError("@python_2_unicode_compatible cannot be applied "
"to %s because it doesn't define __str__()." %
klass.__name__)
klass.__unicode__ = klass.__str__
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
return klass
Run Code Online (Sandbox Code Playgroud)
并继承自newobject,具有此__unicode__方法
def __unicode__(self):
# All subclasses of the builtin object should have __str__ defined.
# Note that old-style classes do not have __str__ defined.
if hasattr(self, '__str__'):
s = type(self).__str__(self)
else:
s = str(self)
if isinstance(s, unicode):
return s
else:
return s.decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
并且因为两者提供两者__unicode__和__str__方法的策略略有不同,所以它们会无限地相互调用,这会导致您的递归错误。
提供 builtins.object 的模块提供了自己的python_2_unicode_compatible装饰器。你有没有试过在 from 上使用它django.utils.six?
| 归档时间: |
|
| 查看次数: |
192 次 |
| 最近记录: |