diff --git a/setup.py b/setup.py index 70ae54a..e1a31ce 100644 --- a/setup.py +++ b/setup.py @@ -2,10 +2,13 @@ """ Distutils setup file """ from setuptools import setup + def readme(): """ Returns Readme.rst as loaded RST for documentation """ with open('Readme.rst', 'r') as filename: return filename.read() + + setup( name='sphinx_execute_code', version='0.2a2', diff --git a/sphinx_execute_code/__init__.py b/sphinx_execute_code/__init__.py index 82cd231..51c668d 100644 --- a/sphinx_execute_code/__init__.py +++ b/sphinx_execute_code/__init__.py @@ -23,11 +23,15 @@ See Readme.rst for documentation details """ import sys -import StringIO import os from docutils.parsers.rst import Directive, directives from docutils import nodes +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + # execute_code function thanks to Stackoverflow code post from hekevintran # https://stackoverflow.com/questions/701802/how-do-i-execute-a-string-containing-python-code-in-python @@ -66,19 +70,19 @@ def execute_code(cls, code): ExecutionError when supplied python is incorrect Examples: - >>> execute_code('print "foobar"') + >>> execute_code('print("foobar")') 'foobar' """ - output = StringIO.StringIO() - err = StringIO.StringIO() + output = StringIO() + err = StringIO() sys.stdout = output sys.stderr = err try: # pylint: disable=exec-used - exec code + exec(code) # If the code is invalid, just skip the block - any actual code errors # will be raised properly except TypeError: @@ -143,6 +147,7 @@ def run(self): output.append(code_results) return output + def setup(app): """ Register sphinx_execute_code directive with Sphinx """ app.add_directive('execute_code', ExecuteCode) diff --git a/tests/conftest.py b/tests/conftest.py index 258001f..0996f6f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,7 @@ # pylint: disable=invalid-name pytest_plugins = 'sphinx.testing.fixtures' + @pytest.fixture(scope='session') def rootdir(): """ Sets root folder for sphinx unit tests """ diff --git a/tests/example_class.py b/tests/example_class.py index fafa837..009632b 100644 --- a/tests/example_class.py +++ b/tests/example_class.py @@ -12,5 +12,6 @@ def out(self): """ returns Hello, world!""" return self.msg + if __name__ == "__main__": - print Hello().out() + print(Hello().out()) diff --git a/tests/hidden_filename.py b/tests/hidden_filename.py index 982cffd..569da40 100644 --- a/tests/hidden_filename.py +++ b/tests/hidden_filename.py @@ -1,4 +1,4 @@ #!/usr/bin/env/python """ Used for unit testing """ -print 'execute_code_hide_filename:' + 'sample_9' +print('execute_code_hide_filename:' + 'sample_9') diff --git a/tests/roots/test-ext-execute-code/conf.py b/tests/roots/test-ext-execute-code/conf.py index c55356d..bdc31eb 100644 --- a/tests/roots/test-ext-execute-code/conf.py +++ b/tests/roots/test-ext-execute-code/conf.py @@ -5,4 +5,4 @@ exclude_patterns = ['_build'] sys.path.insert(0, os.path.abspath('.')) import sphinx_execute_code -extensions = ['sphinx_execute_code'] \ No newline at end of file +extensions = ['sphinx_execute_code'] diff --git a/tests/roots/test-ext-execute-code/index.rst b/tests/roots/test-ext-execute-code/index.rst index a4d7973..935e720 100644 --- a/tests/roots/test-ext-execute-code/index.rst +++ b/tests/roots/test-ext-execute-code/index.rst @@ -5,19 +5,19 @@ This is a test .. execute_code:: - print 'execute_code:' + 'sample_1' + print('execute_code:' + 'sample_1') .. execute_code:: :linenos: - print 'execute_code_linenos:' + 'sample_2' + print('execute_code_linenos:' + 'sample_2') .. execute_code:: :output_language: javascript data = {'execute_code': 'sample_3', 'output_language': 'javascript', 'sample3': True} import json - print json.dumps(data) + print(json.dumps(data)) .. sample_4: @@ -27,24 +27,24 @@ This is a test .. execute_code:: :filename: tests/example_class.py - print 'execute_code_should_not_run:' + 'sample_5' + print('execute_code_should_not_run:' + 'sample_5') #execute_code_sample_5_comment_is_hidden .. execute_code:: :hide_code: - print 'execute_code_hide_code:sample_6' + print('execute_code_hide_code:sample_6') # This comment is hidden .. execute_code:: - print 'execute_code_show_header:' + 'sample_7' + print('execute_code_show_header:' + 'sample_7') .. execute_code:: :hide_headers: - print 'execute_code_hide_header:' + 'sample_8' + print('execute_code_hide_header:' + 'sample_8') .. execute_code:: :filename: tests/hidden_filename.py diff --git a/tests/test_execute_code.py b/tests/test_execute_code.py index 8444f01..f333481 100644 --- a/tests/test_execute_code.py +++ b/tests/test_execute_code.py @@ -16,13 +16,11 @@ from tests.example_class import Hello - - def test_execute_code_function(): """ Ensure simple code functions execute """ code = dedent(''' - print "foo" - print "bar" + print("foo") + print("bar") ''') expected_output = dedent('''\ @@ -33,23 +31,26 @@ def test_execute_code_function(): assert expected_output == results + def test_execute_and_import(): """ Import a generic module, make sure we do not get any type errors """ code = dedent(''' import os - print os.path + print(os.path) ''') results = ExecuteCode.execute_code(code) - assert results != None + assert results is not None assert results != '' + def test_execute_empty(): """ execute_code function should be able to take empty content """ code = '' results = ExecuteCode.execute_code(code) assert results == '' + def test_execute_code_sample(): """ Just makes sure the sample code output of this sample works as expected """ hello = Hello()