use UnicodeMixin for __str__, __unicode__ to support py2/py3 in one source. refs #1350.

This commit is contained in:
Takayuki Shimizukawa 2014-04-30 00:31:09 +09:00
parent 00eff0b7f6
commit 9dbb6bf092
3 changed files with 20 additions and 24 deletions

View File

@ -22,6 +22,7 @@ from sphinx.domains import Domain, ObjType
from sphinx.directives import ObjectDescription
from sphinx.util.nodes import make_refnode
from sphinx.util.compat import Directive
from sphinx.util.pycompat import UnicodeMixin
from sphinx.util.docfields import Field, GroupedField
@ -104,19 +105,16 @@ _id_shortwords = {
}
class DefinitionError(Exception):
class DefinitionError(UnicodeMixin, Exception):
def __init__(self, description):
self.description = description
def __str__(self):
return text_type(self).encode('utf-8')
def __unicode__(self):
return self.description
class DefExpr(object):
class DefExpr(UnicodeMixin):
def __eq__(self, other):
if type(self) is not type(other):
@ -162,9 +160,6 @@ class DefExpr(object):
"""Prefix a name node (a node returned by :meth:`get_name`)."""
raise NotImplementedError()
def __str__(self):
return text_type(self).encode('utf-8')
def __unicode__(self):
raise NotImplementedError()
@ -225,7 +220,7 @@ class PathDefExpr(PrimaryDefExpr):
return u'::'.join(map(text_type, self.path))
class ArrayTypeSuffixDefExpr(object):
class ArrayTypeSuffixDefExpr(UnicodeMixin):
def __init__(self, size_hint=None):
self.size_hint = size_hint

View File

@ -19,6 +19,7 @@ import six
from six.moves import range
from sphinx.ext.napoleon.iterators import modify_iter
from sphinx.util.pycompat import UnicodeMixin
_directive_regex = re.compile(r'\.\. \S+::')
@ -26,7 +27,7 @@ _google_untyped_arg_regex = re.compile(r'\s*(\w+)\s*:\s*(.*)')
_google_typed_arg_regex = re.compile(r'\s*(\w+)\s*\(\s*(.+?)\s*\)\s*:\s*(.*)')
class GoogleDocstring(object):
class GoogleDocstring(UnicodeMixin):
"""Parse Google style docstrings.
Convert Google style docstrings to reStructuredText.
@ -149,20 +150,6 @@ class GoogleDocstring(object):
}
self._parse()
def __str__(self):
"""Return the parsed docstring in reStructuredText format.
Returns
-------
str
UTF-8 encoded version of the docstring.
"""
if six.PY3:
return self.__unicode__()
else:
return self.__unicode__().encode('utf8')
def __unicode__(self):
"""Return the parsed docstring in reStructuredText format.

View File

@ -48,6 +48,13 @@ if six.PY3:
return six.text_type(tree)
from html import escape as htmlescape # >= Python 3.2
class UnicodeMixin:
"""Mixin class to handle defining the proper __str__/__unicode__
methods in Python 2 or 3."""
def __str__(self):
return self.__unicode__()
else:
# Python 2
b = str
@ -65,6 +72,13 @@ else:
# use Python 3 name
from cgi import escape as htmlescape # 2.6, 2.7
class UnicodeMixin(object):
"""Mixin class to handle defining the proper __str__/__unicode__
methods in Python 2 or 3."""
def __str__(self):
return self.__unicode__().encode('utf8')
def execfile_(filepath, _globals):
from sphinx.util.osutil import fs_encoding