你已经派生过 gprMax
镜像自地址
https://gitee.com/sunhf/gprMax.git
已同步 2025-08-06 20:46:52 +08:00
v.3.0.1 removes ListStream() class and allows any Python output that is not a command to be printed to the stdout.
这个提交包含在:
@@ -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'
|
||||
|
||||
|
@@ -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, '<string>', '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:')):
|
||||
|
@@ -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.
|
||||
|
||||
|
在新工单中引用
屏蔽一个用户