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

Skip to content

Commit e05b200

Browse files
committed
FIX: Minor fixes
1 parent 7118a09 commit e05b200

File tree

2 files changed

+53
-36
lines changed

2 files changed

+53
-36
lines changed

sphinx_gallery/gen_rst.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
Files that generate images should start with 'plot'
1313
1414
"""
15-
from __future__ import division, print_function, absolute_import
15+
from __future__ import (division, print_function, absolute_import,
16+
unicode_literals)
1617
from time import time
1718
import ast
19+
import codecs
1820
import hashlib
1921
import os
2022
import re
@@ -49,9 +51,9 @@ def prefixed_lines():
4951
return ''.join(prefixed_lines())
5052

5153
try:
52-
from StringIO import StringIO
54+
from BytesIO import BytesIO
5355
except ImportError:
54-
from io import StringIO
56+
from io import BytesIO
5557

5658
try:
5759
# make sure that the Agg backend is set before importing any
@@ -87,13 +89,25 @@ def __init__(self, file1, file2):
8789

8890
def write(self, data):
8991
self.file1.write(data)
90-
self.file2.write(unicode(data)) # this is the StringIO
92+
self.file2.write(data)
9193

9294
def flush(self):
9395
self.file1.flush()
9496
self.file2.flush()
9597

9698

99+
class MyBytesIO(BytesIO):
100+
"""Helper to deal with unicode not having buffer interface on Py2.7"""
101+
def write(self, data):
102+
# "unicode" objects don't have a buffer interface on Py2.7,
103+
# so we need to manually convert
104+
if isinstance(data, unicode):
105+
data = data.encode('utf-8')
106+
super(MyBytesIO, self).write(data)
107+
108+
def getvalue(self):
109+
return super(MyBytesIO, self).getvalue().decode('utf-8')
110+
97111
###############################################################################
98112
CODE_DOWNLOAD = """**Total running time of the script:**
99113
({0:.0f} minutes {1:.3f} seconds)\n\n
@@ -125,7 +139,7 @@ def flush(self):
125139
"""
126140

127141

128-
CODE_OUTPUT = u""".. rst-class:: sphx-glr-script-out
142+
CODE_OUTPUT = """.. rst-class:: sphx-glr-script-out
129143
130144
Out::
131145
@@ -144,6 +158,9 @@ def get_docstring_and_rest(filename):
144158
rest: str
145159
`filename` content without the docstring
146160
"""
161+
# can't use codecs.open(filename, 'r', 'utf-8') here b/c ast doesn't
162+
# seem to work with unicode strings in Python2.7
163+
# "SyntaxError: encoding declaration in Unicode string"
147164
with open(filename, 'rb') as f:
148165
content = f.read()
149166

@@ -206,8 +223,8 @@ def split_code_and_text_blocks(source_file):
206223

207224
def codestr2rst(codestr, lang='python'):
208225
"""Return reStructuredText code block from code string"""
209-
code_directive = u"\n.. code-block:: {0}\n\n".format(lang)
210-
indented_block = indent(codestr, u' ' * 4)
226+
code_directive = "\n.. code-block:: {0}\n\n".format(lang)
227+
indented_block = indent(codestr, ' ' * 4)
211228
return code_directive + indented_block
212229

213230

@@ -464,7 +481,7 @@ def execute_script(code_block, example_globals, image_path, fig_count,
464481
# First cd in the original example dir, so that any file
465482
# created by the example get created in this directory
466483
os.chdir(os.path.dirname(src_file))
467-
my_buffer = StringIO()
484+
my_buffer = MyBytesIO()
468485
my_stdout = Tee(sys.stdout, my_buffer)
469486
sys.stdout = my_stdout
470487

@@ -476,7 +493,7 @@ def execute_script(code_block, example_globals, image_path, fig_count,
476493

477494
my_stdout = my_buffer.getvalue().strip().expandtabs()
478495
if my_stdout:
479-
stdout = CODE_OUTPUT.format(indent(my_stdout, u' ' * 4))
496+
stdout = CODE_OUTPUT.format(indent(my_stdout, ' ' * 4))
480497
os.chdir(cwd)
481498
figure_list = save_figures(image_path, fig_count, gallery_conf)
482499

@@ -518,7 +535,7 @@ def execute_script(code_block, example_globals, image_path, fig_count,
518535
os.chdir(cwd)
519536

520537
print(" - time elapsed : %.2g sec" % time_elapsed)
521-
code_output = u"\n{0}\n\n{1}\n\n".format(image_list, stdout)
538+
code_output = "\n{0}\n\n{1}\n\n".format(image_list, stdout)
522539

523540
return code_output, time_elapsed, fig_count + len(figure_list)
524541

@@ -554,7 +571,7 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
554571
time_elapsed = 0
555572

556573
ref_fname = example_file.replace(os.path.sep, '_')
557-
example_rst = u"""\n\n.. _sphx_glr_{0}:\n\n""".format(ref_fname)
574+
example_rst = """\n\n.. _sphx_glr_{0}:\n\n""".format(ref_fname)
558575
example_nb = Notebook(fname, target_dir)
559576

560577
filename_pattern = gallery_conf.get('filename_pattern')
@@ -613,9 +630,10 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
613630

614631
time_m, time_s = divmod(time_elapsed, 60)
615632
example_nb.save_file()
616-
with open(os.path.join(target_dir, base_image_name + '.rst'), 'wb') as f:
633+
with codecs.open(os.path.join(target_dir, base_image_name + '.rst'),
634+
'w', 'utf-8') as f:
617635
example_rst += CODE_DOWNLOAD.format(time_m, time_s, fname,
618636
example_nb.file_name)
619-
f.write(example_rst.encode('utf-8'))
637+
f.write(example_rst)
620638

621639
return amount_of_code, time_elapsed

sphinx_gallery/tests/test_gen_rst.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
"""
55
Testing the rst files generator
66
"""
7-
from __future__ import division, absolute_import, print_function
7+
from __future__ import (division, absolute_import, print_function,
8+
unicode_literals)
89
import ast
10+
import codecs
911
import json
1012
import tempfile
1113
import re
@@ -20,7 +22,7 @@
2022
'================',
2123
'',
2224
'This is the description of the example',
23-
u'which goes on and on, Óscar',
25+
'which goes on and on, Óscar',
2426
'',
2527
'',
2628
'And this is a second paragraph',
@@ -30,13 +32,13 @@
3032
'import logging',
3133
'import sys',
3234
'x, y = 1, 2',
33-
'print(u"' u"Óscar output" '") # need some code output',
35+
'print(u"Óscar output") # need some code output',
3436
'logger = logging.getLogger()',
3537
'logger.setLevel(logging.INFO)',
3638
'lh = logging.StreamHandler(sys.stdout)',
3739
'lh.setFormatter(logging.Formatter("log:%(message)s"))',
3840
'logger.addHandler(lh)',
39-
'logger.info(u"' u"Óscar" '")',
41+
'logger.info(u"Óscar")',
4042
]
4143

4244

@@ -70,7 +72,6 @@ def test_direct_comment_after_docstring():
7072
'x, y = 1, 2',
7173
'']))
7274
f.flush()
73-
7475
result = sg.split_code_and_text_blocks(f.name)
7576

7677
expected_result = [
@@ -94,15 +95,13 @@ def test_codestr2rst():
9495

9596
def test_extract_intro():
9697
with tempfile.NamedTemporaryFile('wb') as f:
97-
f.write(u'\n'.join(CONTENT).encode('utf-8'))
98-
98+
f.write('\n'.join(CONTENT).encode('utf-8'))
9999
f.flush()
100-
101100
result = sg.extract_intro(f.name)
102101
assert_false('Docstring' in result)
103102
assert_equal(
104103
result,
105-
u'This is the description of the example which goes on and on, Óscar')
104+
'This is the description of the example which goes on and on, Óscar')
106105
assert_false('second paragraph' in result)
107106

108107

@@ -138,27 +137,27 @@ def test_pattern_matching():
138137
'reference_url': {},
139138
}
140139

141-
code_output = u'\n Out::\n\n Óscar output\n log:Óscar\n\n'
140+
code_output = '\n Out::\n\n Óscar output\n log:Óscar\n\n'
142141
# create three files in tempdir (only one matches the pattern)
143142
fnames = ['plot_0.py', 'plot_1.py', 'plot_2.py']
144143
for fname in fnames:
145-
with open(os.path.join(examples_dir, fname), 'wb') as f:
146-
f.write('\n'.join(CONTENT).encode('utf-8'))
147-
f.flush()
144+
with codecs.open(os.path.join(examples_dir, fname), 'w', 'utf-8') as f:
145+
f.write('\n'.join(CONTENT))
148146
# generate rst file
149147
sg.generate_file_rst(fname, gallery_dir, examples_dir, gallery_conf)
150148
# read rst file and check if it contains code output
151149
rst_fname = os.path.splitext(fname)[0] + '.rst'
152-
with open(os.path.join(gallery_dir, rst_fname), 'rb') as f:
153-
rst = f.read().decode('utf-8')
154-
if re.search(gallery_conf['filename_pattern'],
155-
os.path.join(gallery_dir, rst_fname)):
156-
print(code_output)
157-
print('')
158-
print(rst)
159-
assert_true(code_output in rst)
160-
else:
161-
assert_false(code_output in rst)
150+
with codecs.open(os.path.join(gallery_dir, rst_fname),
151+
'r', 'utf-8') as f:
152+
rst = f.read()
153+
if re.search(gallery_conf['filename_pattern'],
154+
os.path.join(gallery_dir, rst_fname)):
155+
print(code_output)
156+
print('')
157+
print(rst)
158+
assert_true(code_output in rst)
159+
else:
160+
assert_false(code_output in rst)
162161

163162

164163
def test_ipy_notebook():

0 commit comments

Comments
 (0)