Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoding error with automodapi_writereprocessed and py2 #1

Merged
merged 1 commit into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions sphinx_automodapi/automodapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class are included in the generated documentation. Defaults to ``False``.
# actually built.

import inspect
import io
import os
import re
import sys
Expand Down Expand Up @@ -322,8 +323,9 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None,
ustr = newsourcestr.decode(app.config.source_encoding)

if docname is None:
with open(os.path.join(app.srcdir, 'unknown.automodapi'), 'a') as f:
f.write('\n**NEW DOC**\n\n')
with io.open(os.path.join(app.srcdir, 'unknown.automodapi'),
'a', encoding='utf8') as f:
f.write(u'\n**NEW DOC**\n\n')
f.write(ustr)
else:
env = app.builder.env
Expand All @@ -332,7 +334,8 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None,
filename = docname + os.path.splitext(env.doc2path(docname))[1]
filename += '.automodapi'

with open(os.path.join(app.srcdir, filename), 'w') as f:
with io.open(os.path.join(app.srcdir, filename), 'w',
encoding='utf8') as f:
f.write(ustr)

return newsourcestr
Expand Down
26 changes: 26 additions & 0 deletions sphinx_automodapi/tests/test_automodapi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import pytest
Expand Down Expand Up @@ -97,6 +98,31 @@ def test_am_replacer_basic():

assert result == am_replacer_basic_expected


am_replacer_repr_str = u"""
This comes before with spéciàl çhars

.. automodapi:: sphinx_automodapi.tests.test_automodapi
{options}

This comes after
"""


def test_am_replacer_writereprocessed(tmpdir):
"""
Tests the automodapi_writereprocessed option
"""
from ..automodapi import automodapi_replace

fakeapp = FakeApp()
fakeapp.srcdir = str(tmpdir)
fakeapp.config.automodapi_writereprocessed = True
automodapi_replace(am_replacer_repr_str.format(options=''), fakeapp)

assert tmpdir.join('unknown.automodapi').isfile()


am_replacer_noinh_expected = """
This comes before

Expand Down