diff options
Diffstat (limited to 'sphinx/domains/cpp.py')
| -rw-r--r-- | sphinx/domains/cpp.py | 76 |
1 files changed, 36 insertions, 40 deletions
diff --git a/sphinx/domains/cpp.py b/sphinx/domains/cpp.py index 71cc104a..c8505dc8 100644 --- a/sphinx/domains/cpp.py +++ b/sphinx/domains/cpp.py @@ -12,6 +12,7 @@ import re from copy import deepcopy +from six import iteritems, text_type from docutils import nodes from sphinx import addnodes @@ -21,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 @@ -103,25 +105,22 @@ _id_shortwords = { } -class DefinitionError(Exception): +class DefinitionError(UnicodeMixin, Exception): def __init__(self, description): self.description = description - def __str__(self): - return unicode(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): return False try: - for key, value in self.__dict__.iteritems(): + for key, value in iteritems(self.__dict__): if value != getattr(other, key): return False except AttributeError: @@ -161,9 +160,6 @@ class DefExpr(object): """Prefix a name node (a node returned by :meth:`get_name`).""" raise NotImplementedError() - def __str__(self): - return unicode(self).encode('utf-8') - def __unicode__(self): raise NotImplementedError() @@ -196,7 +192,7 @@ class NameDefExpr(PrimaryDefExpr): return self.name.replace(u' ', u'-') def __unicode__(self): - return unicode(self.name) + return text_type(self.name) class PathDefExpr(PrimaryDefExpr): @@ -221,10 +217,10 @@ class PathDefExpr(PrimaryDefExpr): return PathDefExpr([prefix] + self.path) def __unicode__(self): - return u'::'.join(map(unicode, self.path)) + 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 @@ -234,7 +230,7 @@ class ArrayTypeSuffixDefExpr(object): def __unicode__(self): return u'[%s]' % ( - self.size_hint is not None and unicode(self.size_hint) or u'', + self.size_hint is not None and text_type(self.size_hint) or u'', ) @@ -253,7 +249,7 @@ class TemplateDefExpr(PrimaryDefExpr): u'.'.join(x.get_id() for x in self.args)) def __unicode__(self): - return u'%s<%s>' % (self.typename, u', '.join(map(unicode, self.args))) + return u'%s<%s>' % (self.typename, u', '.join(map(text_type, self.args))) class ConstantTemplateArgExpr(PrimaryDefExpr): @@ -265,7 +261,7 @@ class ConstantTemplateArgExpr(PrimaryDefExpr): return self.arg.replace(u' ', u'-') def __unicode__(self): - return unicode(self.arg) + return text_type(self.arg) class WrappingDefExpr(DefExpr): @@ -284,13 +280,13 @@ class ModifierDefExpr(WrappingDefExpr): self.modifiers = modifiers def get_id(self): - pieces = [_id_shortwords.get(unicode(x), unicode(x)) + pieces = [_id_shortwords.get(text_type(x), text_type(x)) for x in self.modifiers] pieces.append(self.typename.get_id()) return u'-'.join(pieces) def __unicode__(self): - return u' '.join(map(unicode, list(self.modifiers) + [self.typename])) + return u' '.join(map(text_type, list(self.modifiers) + [self.typename])) class PtrDefExpr(WrappingDefExpr): @@ -373,7 +369,7 @@ class ArgumentDefExpr(DefExpr): if self.default is not None: buf.append('=%s' % self.default) for suffix in self.type_suffixes: - buf.append(unicode(suffix)) + buf.append(text_type(suffix)) return u''.join(buf) @@ -415,12 +411,12 @@ class TypeObjDefExpr(NamedDefExpr): def __unicode__(self): buf = self.get_modifiers() if self.typename is None: - buf.append(unicode(self.name)) + buf.append(text_type(self.name)) else: - buf.extend(map(unicode, (self.typename, self.name))) + buf.extend(map(text_type, (self.typename, self.name))) buf = [u' '.join(buf)] for suffix in self.type_suffixes: - buf.append(unicode(suffix)) + buf.append(text_type(suffix)) return u''.join(buf) @@ -441,10 +437,10 @@ class MemberObjDefExpr(NamedDefExpr): def __unicode__(self): buf = self.get_modifiers() - buf.extend((unicode(self.typename), unicode(self.name))) + buf.extend((text_type(self.typename), text_type(self.name))) buf = [u' '.join(buf)] for suffix in self.type_suffixes: - buf.append(unicode(suffix)) + buf.append(text_type(suffix)) if self.value is not None: buf.append(u' = %s' % self.value) return u''.join(buf) @@ -485,9 +481,9 @@ class FuncDefExpr(NamedDefExpr): if self.constexpr: buf.append(u'constexpr') if self.rv is not None: - buf.append(unicode(self.rv)) + buf.append(text_type(self.rv)) buf.append(u'%s(%s)' % (self.name, u', '.join( - map(unicode, self.signature)))) + map(text_type, self.signature)))) if self.const: buf.append(u'const') if self.volatile: @@ -520,7 +516,7 @@ class ClassDefExpr(NamedDefExpr): def _tostring(self, visibility='public'): buf = self.get_modifiers(visibility) - buf.append(unicode(self.name)) + buf.append(text_type(self.name)) if self.bases: buf.append(u':') buf.append(u', '.join(base._tostring('private') @@ -1001,19 +997,19 @@ class CPPObject(ObjectDescription): def attach_name(self, node, name): owner, name = name.split_owner() - varname = unicode(name) + varname = text_type(name) if owner is not None: - owner = unicode(owner) + '::' + owner = text_type(owner) + '::' node += addnodes.desc_addname(owner, owner) node += addnodes.desc_name(varname, varname) def attach_type_suffixes(self, node, suffixes): for suffix in suffixes: - node += nodes.Text(unicode(suffix)) + node += nodes.Text(text_type(suffix)) def attach_type(self, node, type): # XXX: link to c? - text = unicode(type) + text = text_type(type) pnode = addnodes.pending_xref( '', refdomain='cpp', reftype='type', reftarget=text, modname=None, classname=None) @@ -1035,7 +1031,7 @@ class CPPObject(ObjectDescription): def add_target_and_index(self, sigobj, sig, signode): theid = sigobj.get_id() - name = unicode(sigobj.name) + name = text_type(sigobj.name) if theid not in self.state.document.ids: signode['names'].append(theid) signode['ids'].append(theid) @@ -1101,8 +1097,8 @@ class CPPClassObject(CPPObject): signode += nodes.Text(' : ') for base in cls.bases: self.attach_modifiers(signode, base, 'private') - signode += nodes.emphasis(unicode(base.name), - unicode(base.name)) + signode += nodes.emphasis(text_type(base.name), + text_type(base.name)) signode += nodes.Text(', ') signode.pop() # remove the trailing comma @@ -1152,7 +1148,7 @@ class CPPFunctionObject(CPPObject): def attach_function(self, node, func): owner, name = func.name.split_owner() if owner is not None: - owner = unicode(owner) + '::' + owner = text_type(owner) + '::' node += addnodes.desc_addname(owner, owner) # cast operator is special. in this case the return value @@ -1162,7 +1158,7 @@ class CPPFunctionObject(CPPObject): node += nodes.Text(u' ') self.attach_type(node, name.typename) else: - funcname = unicode(name) + funcname = text_type(name) node += addnodes.desc_name(funcname, funcname) paramlist = addnodes.desc_parameterlist() @@ -1171,10 +1167,10 @@ class CPPFunctionObject(CPPObject): if arg.type is not None: self.attach_type(param, arg.type) param += nodes.Text(u' ') - param += nodes.emphasis(unicode(arg.name), unicode(arg.name)) + param += nodes.emphasis(text_type(arg.name), text_type(arg.name)) self.attach_type_suffixes(param, arg.type_suffixes) if arg.default is not None: - def_ = u'=' + unicode(arg.default) + def_ = u'=' + text_type(arg.default) param += nodes.emphasis(def_, def_) paramlist += param @@ -1280,14 +1276,14 @@ class CPPDomain(Domain): } def clear_doc(self, docname): - for fullname, (fn, _, _) in self.data['objects'].items(): + for fullname, (fn, _, _) in list(self.data['objects'].items()): if fn == docname: del self.data['objects'][fullname] def resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode): def _create_refnode(expr): - name = unicode(expr) + name = text_type(expr) if name not in self.data['objects']: return None obj = self.data['objects'][name] @@ -1321,5 +1317,5 @@ class CPPDomain(Domain): return _create_refnode(expr.prefix(parent)) def get_objects(self): - for refname, (docname, type, theid) in self.data['objects'].iteritems(): + for refname, (docname, type, theid) in iteritems(self.data['objects']): yield (refname, refname, type, docname, refname, 1) |
