Anonymous avatar Anonymous committed 68c2599

autodoc uses the module charset now

Comments (0)

Files changed (1)


     :license: BSD.
+import re
 import types
 import inspect
 import textwrap
+import linecache
 from docutils import nodes
 from docutils.parsers.rst import directives
 except NameError:
     base_exception = Exception
+_charset_re = re.compile(r'coding[:=]\s*([-\w.]+)')
+_module_charsets = {}
 def prepare_docstring(s):
     return [firstline] + otherlines.splitlines() + ['']
+def get_module_charset(module):
+    """Return the charset of the given module."""
+    if module in _module_charsets:
+        return _module_charsets[module]
+    filename = __import__(module, None, None, ['']).__file__
+    if filename[-4:] in ('.pyc', '.pyo'):
+        filename = filename[:-1]
+    for line in [linecache.getline(filename, x) for x in (1, 2)]:
+        match =
+        if match is not None:
+            charset =
+            break
+    else:
+        charset = 'ascii'
+    _module_charsets[module] = charset
+    return charset
 def generate_rst(what, name, members, undoc, add_content,
                  document, lineno, indent=''):
     env = document.settings.env
     if what == 'module' and env.config.automodule_skip_lines:
         docstring = '\n'.join(docstring.splitlines()
+    # get the encoding of the docstring
+    module = getattr(todoc, '__module__', None)
+    if module is not None:
+        docstring = docstring.decode(get_module_charset(module))
     docstring = prepare_docstring(docstring)
     for i, line in enumerate(docstring):
         result.append(indent + line, '<docstring of %s>' % name, i)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.