From b1441a2c04d07a13a0474b5243ebc30b6c8daeb5 Mon Sep 17 00:00:00 2001 From: Craig Warren Date: Wed, 29 Jun 2016 12:39:22 +0100 Subject: [PATCH] v.3.0.1 removes ListStream() class and allows any Python output that is not a command to be printed to the stdout. --- gprMax/_version.py | 2 +- gprMax/input_cmds_file.py | 35 +++++++++++++++++++++++------------ gprMax/utilities.py | 10 ---------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/gprMax/_version.py b/gprMax/_version.py index 462e9009..cfa8becc 100644 --- a/gprMax/_version.py +++ b/gprMax/_version.py @@ -1,4 +1,4 @@ # This is where the version number is set and read by setup.py and conf.py (for the docs) -__version__ = '3.0.0' +__version__ = '3.0.1' diff --git a/gprMax/input_cmds_file.py b/gprMax/input_cmds_file.py index 75bfe419..e86a011b 100644 --- a/gprMax/input_cmds_file.py +++ b/gprMax/input_cmds_file.py @@ -18,9 +18,9 @@ import os import sys +from io import StringIO from gprMax.exceptions import CmdInputError -from gprMax.utilities import ListStream def process_python_include_code(inputfile, usernamespace): @@ -46,8 +46,6 @@ def process_python_include_code(inputfile, usernamespace): # Process any Python code if(inputlines[x].startswith('#python:')): - # Save stdout location to restore later - stdout = sys.stdout # String to hold Python code to be executed pythoncode = '' @@ -60,19 +58,32 @@ def process_python_include_code(inputfile, usernamespace): raise CmdInputError('Cannot find the end of the Python code block, i.e. missing #end_python: command.') # Compile code for faster execution pythoncompiledcode = compile(pythoncode, '', 'exec') - # Redirect stdio to a ListStream - sys.stdout = codeout = ListStream() + # Redirect stdout to a text stream + sys.stdout = result = StringIO() # Execute code block & make available only usernamespace exec(pythoncompiledcode, usernamespace) + # String containing buffer of executed code + codeout = result.getvalue().split('\n') + result.close() - # Now strip out any lines that don't begin with a hash command - codeproc = [line + ('\n') for line in codeout.data if(line.startswith('#'))] - - # Add processed Python code to list - processedlines.extend(codeproc) - # Reset stdio - sys.stdout = stdout + sys.stdout = sys.__stdout__ + + # Separate commands from any other generated output + hashcmds = [] + pythonstdout = [] + for line in codeout: + if line.startswith('#'): + hashcmds.append(line + '\n') + elif line: + pythonstdout.append(line) + + # Add commands to a list + processedlines.extend(hashcmds) + + # Print any generated output that is not commands + if pythonstdout: + print('Python messages (from stdout): {}\n'.format(pythonstdout)) # Process any include commands elif(inputlines[x].startswith('#include_file:')): diff --git a/gprMax/utilities.py b/gprMax/utilities.py index e56a4123..19d23b40 100644 --- a/gprMax/utilities.py +++ b/gprMax/utilities.py @@ -20,16 +20,6 @@ import sys import decimal as d -class ListStream(object): - """A list can be streamed into. Required when temporarily redirecting stdio to capture output from users Python code blocks.""" - - def __init__(self): - self.data = [] - - def write(self, s): - self.data.append(s) - - def logo(version): """Print gprMax logo, version, and licencing/copyright information.