Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 10 additions & 5 deletions sphinx_execute_code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
3 changes: 2 additions & 1 deletion tests/example_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ def out(self):
""" returns Hello, world!"""
return self.msg


if __name__ == "__main__":
print Hello().out()
print(Hello().out())
2 changes: 1 addition & 1 deletion tests/hidden_filename.py
Original file line number Diff line number Diff line change
@@ -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')
2 changes: 1 addition & 1 deletion tests/roots/test-ext-execute-code/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
exclude_patterns = ['_build']
sys.path.insert(0, os.path.abspath('.'))
import sphinx_execute_code
extensions = ['sphinx_execute_code']
extensions = ['sphinx_execute_code']
14 changes: 7 additions & 7 deletions tests/roots/test-ext-execute-code/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions tests/test_execute_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('''\
Expand All @@ -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()
Expand Down