Fixed error parsing files with unicode docstrings

pull/161/head
Ashley Whetter 6 years ago committed by Ashley Whetter
parent 66cf2ed4d0
commit aac53178eb

@ -4,6 +4,7 @@ Sphinx Auto-API Top-level Extension.
This extension allows you to automagically generate API documentation from your project.
"""
import codecs
import os
import shutil
@ -179,7 +180,14 @@ def viewcode_find(app, modname):
children = getattr(obj, 'children', ())
stack.extend((full_name + '.', gchild) for gchild in children)
result = (open(module.obj['file_path']).read(), locations)
if module.obj['encoding']:
source = codecs.open(
module.obj['file_path'], encoding=module.obj['encoding']
).read()
else:
source = open(module.obj['file_path']).read()
result = (source, locations)
_viewcode_cache[modname] = result
return result

@ -10,6 +10,11 @@ from .base import PythonMapperBase, SphinxMapperBase
from . import astroid_utils
from ..utils import slugify
try:
_TEXT_TYPE = unicode
except NameError:
_TEXT_TYPE = str
class PythonSphinxMapper(SphinxMapperBase):
@ -418,10 +423,17 @@ class PythonException(PythonClass):
class Parser(object):
def __init__(self):
self._name_stack = []
self._encoding = None
def _get_full_name(self, name):
return '.'.join(self._name_stack + [name])
def _encode(self, to_encode):
if self._encoding:
return _TEXT_TYPE(to_encode, self._encoding)
return to_encode
def parse_file(self, file_path):
directory, filename = os.path.split(file_path)
module_parts = []
@ -459,7 +471,7 @@ class Parser(object):
'type': type_,
'name': target,
'full_name': self._get_full_name(target),
'doc': doc,
'doc': self._encode(doc),
'value': value,
'from_line_no': node.fromlineno,
'to_line_no': node.tolineno,
@ -489,7 +501,7 @@ class Parser(object):
'full_name': self._get_full_name(node.name),
'args': args,
'bases': basenames,
'doc': node.doc or '',
'doc': self._encode(node.doc or ''),
'from_line_no': node.fromlineno,
'to_line_no': node.tolineno,
'children': [],
@ -509,7 +521,7 @@ class Parser(object):
'type': 'attribute',
'name': node.name,
'full_name': self._get_full_name(node.name),
'doc': node.doc or '',
'doc': self._encode(node.doc or ''),
'from_line_no': node.fromlineno,
'to_line_no': node.tolineno,
}
@ -529,7 +541,7 @@ class Parser(object):
'name': node.name,
'full_name': self._get_full_name(node.name),
'args': node.args.as_string(),
'doc': node.doc or '',
'doc': self._encode(node.doc or ''),
'from_line_no': node.fromlineno,
'to_line_no': node.tolineno,
}
@ -572,17 +584,20 @@ class Parser(object):
if node.package:
type_ = 'package'
self._name_stack = [node.name]
self._encoding = node.file_encoding
data = {
'type': type_,
'name': node.name,
'full_name': node.name,
'doc': node.doc or '',
'doc': self._encode(node.doc or ''),
'children': [],
'file_path': path,
'encoding': node.file_encoding,
'all': astroid_utils.get_module_all(node),
}
self._name_stack = [node.name]
top_name = node.name.split('.', 1)[0]
for child in node.get_children():
if node.package and astroid_utils.is_local_import_from(child, top_name):

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""Example module
This is a description
@ -77,3 +78,19 @@ class Foo(object):
int: The sum of foo and bar.
"""
return foo + bar
def method_sphinx_unicode(self):
"""This docstring uses unicodé.
:returns: A string.
:rtype: str
"""
return "sphinx"
def method_google_unicode(self):
"""This docstring uses unicodé.
Returns:
str: A string.
"""
return "google"

Loading…
Cancel
Save