Skip to content

Commit

Permalink
use UnicodeMixin for __str__, __unicode__ to support py2/py3 in one s…
Browse files Browse the repository at this point in the history
…ource. refs #1350.
  • Loading branch information
shimizukawa committed Apr 29, 2014
1 parent 00eff0b commit 9dbb6bf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
13 changes: 4 additions & 9 deletions sphinx/domains/cpp.py
Expand Up @@ -22,6 +22,7 @@
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


Expand Down Expand Up @@ -104,19 +105,16 @@
}


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):
Expand Down Expand Up @@ -162,9 +160,6 @@ def prefix(self, prefix):
"""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()

Expand Down Expand Up @@ -225,7 +220,7 @@ def __unicode__(self):
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
Expand Down
17 changes: 2 additions & 15 deletions sphinx/ext/napoleon/docstring.py
Expand Up @@ -19,14 +19,15 @@
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+::')
_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.
Expand Down Expand Up @@ -149,20 +150,6 @@ def __init__(self, docstring, config=None, app=None, what='', name='',
}
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.
Expand Down
14 changes: 14 additions & 0 deletions sphinx/util/pycompat.py
Expand Up @@ -48,6 +48,13 @@ def convert_with_2to3(filepath):
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
Expand All @@ -65,6 +72,13 @@ def terminal_safe(s):
# 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
Expand Down

0 comments on commit 9dbb6bf

Please sign in to comment.