diff --git a/sphinx_gallery/gen_gallery.py b/sphinx_gallery/gen_gallery.py index f19cdb98a..f1e78e2f7 100644 --- a/sphinx_gallery/gen_gallery.py +++ b/sphinx_gallery/gen_gallery.py @@ -11,6 +11,7 @@ from __future__ import division, print_function, absolute_import +import codecs import copy import re import os @@ -188,24 +189,24 @@ def generate_gallery_rst(app): computation_times += this_computation_times # we create an index.rst with all examples - fhindex = open(os.path.join(gallery_dir, 'index.rst'), 'w') - # :orphan: to suppress "not included in TOCTREE" sphinx warnings - fhindex.write(":orphan:\n\n" + this_fhindex) - for directory in sorted(os.listdir(examples_dir)): - if os.path.isdir(os.path.join(examples_dir, directory)): - src_dir = os.path.join(examples_dir, directory) - target_dir = os.path.join(gallery_dir, directory) - this_fhindex, this_computation_times = generate_dir_rst(src_dir, target_dir, gallery_conf, - seen_backrefs) - fhindex.write(this_fhindex) - computation_times += this_computation_times - - if gallery_conf['download_all_examples']: - download_fhindex = generate_zipfiles(gallery_dir) - fhindex.write(download_fhindex) - - fhindex.write(SPHX_GLR_SIG) - fhindex.flush() + with codecs.open(os.path.join(gallery_dir, 'index.rst'), 'w', + encoding='utf-8') as fhindex: + # :orphan: to suppress "not included in TOCTREE" sphinx warnings + fhindex.write((u":orphan:\n\n" + this_fhindex)) + for directory in sorted(os.listdir(examples_dir)): + if os.path.isdir(os.path.join(examples_dir, directory)): + src_dir = os.path.join(examples_dir, directory) + target_dir = os.path.join(gallery_dir, directory) + this_fhindex, this_computation_times = generate_dir_rst(src_dir, target_dir, gallery_conf, + seen_backrefs) + fhindex.write(this_fhindex) + computation_times += this_computation_times + + if gallery_conf['download_all_examples']: + download_fhindex = generate_zipfiles(gallery_dir) + fhindex.write(download_fhindex) + + fhindex.write(SPHX_GLR_SIG) if gallery_conf['plot_gallery']: logger.info("Computation time summary:", color='white') diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py index b60c691c2..0db899da0 100644 --- a/sphinx_gallery/gen_rst.py +++ b/sphinx_gallery/gen_rst.py @@ -378,7 +378,8 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): location=src_dir) return "", [] # because string is an expected return type - with open(os.path.join(src_dir, 'README.txt')) as fid: + with codecs.open(os.path.join(src_dir, 'README.txt'), 'r', + encoding='utf-8') as fid: fhindex = fid.read() # Add empty lines to avoid bug in issue #165 fhindex += "\n\n" diff --git a/sphinx_gallery/tests/conftest.py b/sphinx_gallery/tests/conftest.py index 52ead110c..4deeacf6a 100644 --- a/sphinx_gallery/tests/conftest.py +++ b/sphinx_gallery/tests/conftest.py @@ -24,7 +24,8 @@ def __init__(self): def status_iterator(self, *args, **kwargs): self.calls['status_iterator'].append(Params(args, kwargs)) - yield + for it in args[0]: + yield it def warning(self, *args, **kwargs): self.calls['warning'].append(Params(args, kwargs)) diff --git a/sphinx_gallery/tests/test_gen_rst.py b/sphinx_gallery/tests/test_gen_rst.py index 18f2e9bfb..44ff954ce 100644 --- a/sphinx_gallery/tests/test_gen_rst.py +++ b/sphinx_gallery/tests/test_gen_rst.py @@ -9,17 +9,18 @@ import ast import codecs import copy +import logging import tempfile import re import os import shutil -import warnings import zipfile import pytest import sphinx_gallery.gen_rst as sg -from sphinx_gallery import gen_gallery -from sphinx_gallery import downloads +from sphinx_gallery import gen_gallery, downloads +from sphinx_gallery.gen_gallery import generate_dir_rst +from sphinx_gallery import sphinx_compatibility # Need to import gen_rst before matplotlib.pyplot to set backend to 'Agg' import matplotlib.pyplot as plt @@ -111,7 +112,7 @@ def test_extract_intro(): finally: os.remove(f.name) assert 'Docstring' not in result - assert result == 'This is the description of the example which goes on and on, Óscar' + assert result == 'This is the description of the example which goes on and on, Óscar' # noqa assert 'second paragraph' not in result @@ -177,6 +178,21 @@ def test_fail_example(log_collector): raise ValueError('Did not stop executing script after error') +def test_gen_dir_rst(fakesphinxapp): + """Test gen_dir_rst.""" + gallery_conf = build_test_configuration() + print(os.listdir(gallery_conf['examples_dir'])) + args = (gallery_conf['src_dir'], gallery_conf['gallery_dir'], + gallery_conf, []) + out = generate_dir_rst(*args) + assert out[0] == "" + fname_readme = os.path.join(gallery_conf['src_dir'], 'README.txt') + with open(fname_readme, 'wb') as fid: + fid.write(u"Testing\n=======\n\nÓscar here.".encode('utf-8')) + out = generate_dir_rst(*args) + assert u"Óscar here" in out[0] + + def test_pattern_matching(log_collector): """Test if only examples matching pattern are executed"""