Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea0444b

Browse files
authored
Merge pull request astropy#1 from saimn/automodapi-fix-encoding
Fix encoding error with automodapi_writereprocessed and py2
2 parents 1c9c17f + de3002d commit ea0444b

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

sphinx_automodapi/automodapi.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class are included in the generated documentation. Defaults to ``False``.
8383
# actually built.
8484

8585
import inspect
86+
import io
8687
import os
8788
import re
8889
import sys
@@ -322,8 +323,9 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None,
322323
ustr = newsourcestr.decode(app.config.source_encoding)
323324

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

335-
with open(os.path.join(app.srcdir, filename), 'w') as f:
337+
with io.open(os.path.join(app.srcdir, filename), 'w',
338+
encoding='utf8') as f:
336339
f.write(ustr)
337340

338341
return newsourcestr

sphinx_automodapi/tests/test_automodapi.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
# Licensed under a 3-clause BSD style license - see LICENSE.rst
23

34
import pytest
@@ -97,6 +98,31 @@ def test_am_replacer_basic():
9798

9899
assert result == am_replacer_basic_expected
99100

101+
102+
am_replacer_repr_str = u"""
103+
This comes before with spéciàl çhars
104+
105+
.. automodapi:: sphinx_automodapi.tests.test_automodapi
106+
{options}
107+
108+
This comes after
109+
"""
110+
111+
112+
def test_am_replacer_writereprocessed(tmpdir):
113+
"""
114+
Tests the automodapi_writereprocessed option
115+
"""
116+
from ..automodapi import automodapi_replace
117+
118+
fakeapp = FakeApp()
119+
fakeapp.srcdir = str(tmpdir)
120+
fakeapp.config.automodapi_writereprocessed = True
121+
automodapi_replace(am_replacer_repr_str.format(options=''), fakeapp)
122+
123+
assert tmpdir.join('unknown.automodapi').isfile()
124+
125+
100126
am_replacer_noinh_expected = """
101127
This comes before
102128

0 commit comments

Comments
 (0)