summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakayuki Shimizukawa <shimizukawa@gmail.com>2014-04-30 00:31:09 +0900
committerTakayuki Shimizukawa <shimizukawa@gmail.com>2014-04-30 00:31:09 +0900
commit9dbb6bf0927b4b04cc95b0ec27ece2325c5c6053 (patch)
tree1ee33c535070f1a21597d68f11db37f02d8d1d62
parent00eff0b7f656b5212b6f4d2c567f6828ee81ee82 (diff)
downloadsphinx-git-9dbb6bf0927b4b04cc95b0ec27ece2325c5c6053.tar.gz
use UnicodeMixin for __str__, __unicode__ to support py2/py3 in one source. refs #1350.
-rw-r--r--sphinx/domains/cpp.py13
-rw-r--r--sphinx/ext/napoleon/docstring.py17
-rw-r--r--sphinx/util/pycompat.py14
3 files changed, 20 insertions, 24 deletions
diff --git a/sphinx/domains/cpp.py b/sphinx/domains/cpp.py
index 0b9d3b023..11fe5ad1b 100644
--- a/sphinx/domains/cpp.py
+++ b/sphinx/domains/cpp.py
@@ -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
diff --git a/sphinx/ext/napoleon/docstring.py b/sphinx/ext/napoleon/docstring.py
index 3993dfeaa..6a6650389 100644
--- a/sphinx/ext/napoleon/docstring.py
+++ b/sphinx/ext/napoleon/docstring.py
@@ -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.
diff --git a/sphinx/util/pycompat.py b/sphinx/util/pycompat.py
index 519afe5fe..b3e50474a 100644
--- a/sphinx/util/pycompat.py
+++ b/sphinx/util/pycompat.py
@@ -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