Skip to content
Permalink
Browse files

fix a few issues for python 2.7

  • Loading branch information...
sdpython committed Sep 8, 2015
1 parent 8ef08ff commit ed365c32fffafda8f5f554c4ece0035f6153ccdd
@@ -50,7 +50,7 @@ def test_file_head(self):
temp = get_temp_folder(__file__, "temp_" + self._testMethodName)
out = os.path.join(temp, "out.5.py")
assert not os.path.exists(out)
head = file_head(__file__.replace(".pyc", "py"), out=out, head=5)
head = file_head(__file__.replace(".pyc", ".py"), out=out, head=5)
assert os.path.exists(head)

def test_file_grep(self):
@@ -7,6 +7,7 @@
import os
import unittest
import sphinx
import warnings
from docutils.parsers.rst import directives

try:
@@ -168,6 +169,11 @@ def test_newdirective_with_rst2html(self):
self._testMethodName,
OutputPrint=__name__ == "__main__")

if sys.version_info[0] == 2:
warnings.warn(
"test_newdirective_with_rst2html not run on Python 2.7")
return

from docutils import nodes

class runpythonthis_node(nodes.Structural, nodes.Element):
@@ -188,8 +194,10 @@ def depart_node(self, node):
.. runpythonthis::
print("this code shoud appear" + "___")
print(u"this code shoud appear" + u"___")
""".replace(" ", "")
if sys.version_info[0] >= 3:
content = content.replace('u"', '"')

tives = [("runpythonthis", RunPythonThisDirective, runpythonthis_node,
visit_node, depart_node)]
@@ -94,6 +94,8 @@ def depart_node(self, node):
writer="custom", keep_warnings=True,
directives=tives)
Unfortunately, this functionality is only tested on Python 3.
It might only work on Python 2.7.
@endexample
@@ -236,12 +236,18 @@ def __init__(self, srcdir, confdir, outdir, doctreedir, buildername,

# now that we know all config values, collect them from conf.py
if sys.version_info[0] == 2:
self.config.init_values()
try:
self.config.init_values()
except TypeError as e:
if "takes exactly 2 arguments" in str(e):
self.config.init_values(self.warn)
else:
raise e
else:
self.config.init_values(self.warn)

# check extension versions if requested
if self.config.needs_extensions:
if sys.version_info[0] >= 3 and self.config.needs_extensions:
for extname, needs_ver in self.config.needs_extensions.items():
if extname not in self._extensions:
self.warn('needs_extensions config value specifies a '
@@ -258,7 +264,8 @@ def __init__(self, srcdir, confdir, outdir, doctreedir, buildername,
# set up translation infrastructure
self._init_i18n()
# check all configuration values for permissible types
self.config.check_types(self.warn)
if sys.version_info[0] >= 3:
self.config.check_types(self.warn)
# set up the build environment
self._init_env(freshenv)
# set up the builder
@@ -430,7 +430,7 @@ def post_process_latex(st, doall, info=None):
@todo Check latex is properly converted in HTML files
"""
fLOG(" ** enter post_process_latex", doall, "%post_process_latex" in st)
weird_character = set(chr(i) for i in range(1,9))
weird_character = set(chr(i) for i in range(1, 9))

def clean_unicode(c):
if ord(c) >= 255 or c in weird_character:
@@ -60,19 +60,22 @@ def run_python_script(script, params={}, comment=None):
sys.stdout = sout
sys.stderr = serr

gout = sout.getvalue()
gerr = serr.getvalue()

try:
exec(obj, globals(), loc)
except Exception as ee:
if comment is None:
comment = ""
message = "SCRIPT:\n{0}\nPARAMS\n{1}\nCOMMENT\n{1}".format(
script, params, comment)
message = "SCRIPT:\n{0}\nPARAMS\n{1}\nCOMMENT\n{2}\nERR\n{3}\nOUT\n{4}".format(
script, params, comment, gout, gerr)
raise RunPythonExecutionError(message) from ee

sys.stdout = kout
sys.stderr = kerr

return sout.getvalue(), serr.getvalue()
return gout, gerr


class runpython_node(nodes.Structural, nodes.Element):

0 comments on commit ed365c3

Please sign in to comment.
You can’t perform that action at this time.