Commit f49d9377 authored by Andrey Golovizin's avatar Andrey Golovizin

Use __str__ instead of __unicode__

parent 2997e858
......@@ -29,8 +29,10 @@ import re
import pybtex.io
from pybtex.errors import report_error
from pybtex.exceptions import PybtexError
from pybtex import py3compat
@py3compat.python_2_unicode_compatible
class AuxDataError(PybtexError):
def __init__(self, message, context=None):
super(AuxDataError, self).__init__(message, context.filename)
......@@ -41,8 +43,8 @@ class AuxDataError(PybtexError):
marker = '^' * len(self.context.line)
return self.context.line + '\n' + marker
def __unicode__(self):
base_message = super(AuxDataError, self).__unicode__()
def __str__(self):
base_message = py3compat.__str__(super(AuxDataError, self))
lineno = self.context.lineno
location = 'in line {0}: '.format(lineno) if lineno else ''
return location + base_message
......
......@@ -27,6 +27,7 @@ import six
from pybtex.bibtex.exceptions import BibTeXError
from pybtex.py3compat import fix_unicode_literals_in_doctest
from pybtex.utils import pairwise
from pybtex import py3compat
whitespace_re = re.compile(r'(\s)')
purify_special_char_re = re.compile(r'^\\[A-Za-z]+')
......@@ -95,6 +96,7 @@ def wrap(string, width=79, subsequent_indent=' '):
return '\n'.join(line.rstrip() for line in iter_lines(string))
@py3compat.python_2_unicode_compatible
class BibTeXString(object):
def __init__(self, chars, level=0, max_level=100):
if level > max_level:
......@@ -141,7 +143,7 @@ class BibTeXString(object):
if close is not None and self.level > 0 and self.is_closed:
yield close(self)
def __unicode__(self):
def __str__(self):
return ''.join(self.traverse(open=lambda string: '{', close=lambda string: '}'))
def inner_string(self):
......
......@@ -37,7 +37,7 @@ from pybtex.utils import (
from pybtex.richtext import Text
from pybtex.bibtex.utils import split_tex_string, scan_bibtex_string
from pybtex.errors import report_error
from pybtex.py3compat import fix_unicode_literals_in_doctest
from pybtex.py3compat import fix_unicode_literals_in_doctest, python_2_unicode_compatible
class BibliographyDataError(PybtexError):
......@@ -471,6 +471,7 @@ class Entry(object):
)
@python_2_unicode_compatible
@fix_unicode_literals_in_doctest
class Person(object):
"""A person or some other person-like entity.
......@@ -716,7 +717,7 @@ class Person(object):
and self.lineage_names == other.lineage_names
)
def __unicode__(self):
def __str__(self):
# von Last, Jr, First
von_last = ' '.join(self.prelast_names + self.last_names)
jr = ' '.join(self.lineage_names)
......
......@@ -29,6 +29,20 @@ if PY3:
def fix_unicode_literals_in_doctest(obj):
obj.__doc__ = obj.__doc__.replace("u'", "'")
return obj
def python_2_unicode_compatible(obj):
return obj
def __str__(obj):
return obj.__str__()
else:
def fix_unicode_literals_in_doctest(obj):
return obj
def python_2_unicode_compatible(obj):
obj.__unicode__ = obj.__str__
del obj.__str__
return obj
def __str__(obj):
return obj.__unicode__()
......@@ -66,6 +66,7 @@ from abc import ABCMeta, abstractmethod
import six
from pybtex import textutils
from pybtex.utils import collect_iterable, deprecated
from pybtex import py3compat
# workaround for doctests in Python 2/3
......@@ -94,11 +95,12 @@ def ensure_text(value):
raise ValueError('parts must be strings or BaseText instances, not ' + bad_type)
@py3compat.python_2_unicode_compatible
class BaseText(object):
__metaclass__ = ABCMeta
@abstractmethod
def __unicode__(self):
def __str__(self):
raise NotImplementedError
@abstractmethod
......@@ -304,6 +306,7 @@ class BaseText(object):
return None, ()
@py3compat.python_2_unicode_compatible
class BaseMultipartText(BaseText):
info = ()
......@@ -339,7 +342,7 @@ class BaseMultipartText(BaseText):
self.parts = list(merged_parts)
self.length = sum(len(part) for part in self.parts)
def __unicode__(self):
def __str__(self):
return ''.join(six.text_type(part) for part in self.parts)
def __eq__(self, other):
......@@ -686,6 +689,7 @@ class BaseMultipartText(BaseText):
return self._create_similar(iter_map_with_condition())
@py3compat.python_2_unicode_compatible
class String(BaseText):
"""
A :py:class:`String` is a wrapper for a plain Python string.
......@@ -714,7 +718,7 @@ class String(BaseText):
def __repr__(self):
return str_repr(self.value)
def __unicode__(self):
def __str__(self):
return six.text_type(self.value)
def __eq__(self, other):
......@@ -952,6 +956,7 @@ class Protected(BaseMultipartText):
return backend.format_protected(text)
@py3compat.python_2_unicode_compatible
class Symbol(BaseText):
"""A special symbol. This class is rarely used and may be removed in
future versions.
......@@ -971,7 +976,7 @@ class Symbol(BaseText):
def __repr__(self):
return "Symbol(%s)" % str_repr(self.name)
def __unicode__(self):
def __str__(self):
# XXX
return u'<%s>' % self.name
......
......@@ -26,6 +26,7 @@ from __future__ import unicode_literals
import re
from pybtex.exceptions import PybtexError
from pybtex import py3compat
class Token(object):
......@@ -140,6 +141,7 @@ class Scanner(object):
return self.text[self.pos:]
@py3compat.python_2_unicode_compatible
class PybtexSyntaxError(PybtexError):
error_type = 'syntax error'
......@@ -149,8 +151,8 @@ class PybtexSyntaxError(PybtexError):
self.parser = parser
self.error_context_info = parser.get_error_context_info()
def __unicode__(self):
base_message = super(PybtexSyntaxError, self).__unicode__()
def __str__(self):
base_message = super(PybtexSyntaxError, self).__str__()
pos = u' in line {0}'.format(self.lineno) if self.lineno is not None else ''
return u'{error_type}{pos}: {message}'.format(
error_type=self.error_type,
......
......@@ -20,7 +20,7 @@ class TextTestMixin(object):
raise NotImplementedError
@abstractmethod
def test__unicode__(self):
def test__str__(self):
raise NotImplementedError
@abstractmethod
......@@ -124,7 +124,7 @@ class TestText(TextTestMixin, TestCase):
assert len(Text('Never', ' ', Tag('em', 'Knows', ' '), 'Best')) == len('Never Knows Best')
assert len(Text('Never', ' ', Tag('em', HRef('/', 'Knows'), ' '), 'Best')) == len('Never Knows Best')
def test__unicode__(self):
def test__str__(self):
assert six.text_type(Text()) == ''
assert six.text_type(Text(u'Чудаки украшают мир')) == u'Чудаки украшают мир'
......@@ -312,7 +312,7 @@ class TestString(TextTestMixin, TestCase):
val = 'test string'
assert len(String(val)) == len(val)
def test__unicode__(self):
def test__str__(self):
val = u'Detektivbyrån'
assert six.text_type(String(val)) == val
......@@ -445,7 +445,7 @@ class TestTag(TextTestMixin, TestCase):
val = 'Tomato apple!'
assert len(Tag('em', val)) == len(val)
def test__unicode__(self):
def test__str__(self):
empty = Tag('em')
assert six.text_type(empty.lower()) == ''
assert six.text_type(empty.capitalize()) == ''
......@@ -675,7 +675,7 @@ class TestHRef(TextTestMixin, TestCase):
assert empty.url == '/'
assert empty.parts == []
def test__unicode__(self):
def test__str__(self):
empty = HRef('/')
assert six.text_type(empty) == ''
......@@ -988,7 +988,7 @@ class TestProtected(TextTestMixin, TestCase):
assert len(Protected('Never', ' ', Tag('em', 'Knows', ' '), 'Best')) == len('Never Knows Best')
assert len(Protected('Never', ' ', Tag('em', HRef('/', 'Knows'), ' '), 'Best')) == len('Never Knows Best')
def test__unicode__(self):
def test__str__(self):
assert six.text_type(Protected()) == ''
assert six.text_type(Protected(u'Чудаки украшают мир')) == u'Чудаки украшают мир'
......@@ -1164,7 +1164,7 @@ class TestSymbol(TextTestMixin, TestCase):
assert Text(nbsp, nbsp) == Text(Symbol('nbsp'), Symbol('nbsp'))
def test__unicode__(self):
def test__str__(self):
assert six.text_type(nbsp) == '<nbsp>'
def test__len__(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment