Skip to content

Commit

Permalink
Merge pull request #264 from jamiebull1/i245_py3_parse_error
Browse files Browse the repository at this point in the history
I245 py3 parse error
  • Loading branch information
santoshphilip authored Sep 14, 2019
2 parents deca838 + bdb6db5 commit 5e4f47a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
14 changes: 10 additions & 4 deletions eppy/runner/run_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import sys
import tempfile

from six import StringIO

try:
import multiprocessing as mp
except ImportError:
Expand Down Expand Up @@ -346,28 +348,32 @@ def run(
cmd.extend([args[arg]])
cmd.extend([idf_path])

# send stdout to tmp filehandle to avoid issue #245
tmp_err = StringIO()
sys.stderr = tmp_err
try:
if verbose == "v":
print("\r\n" + " ".join(cmd) + "\r\n")
check_call(cmd)
elif verbose == "q":
check_call(cmd, stdout=open(os.devnull, "w"))
except CalledProcessError:
message = parse_error(output_dir)
message = parse_error(tmp_err, output_dir)
raise EnergyPlusRunError(message)
finally:
sys.stderr = sys.__stderr__
os.chdir(cwd)
return "OK"


def parse_error(output_dir):
def parse_error(tmp_err, output_dir):
"""Add contents of stderr and eplusout.err and put it in the exception message.
:param tmp_err: file-like
:param output_dir: str
:return: str
"""
sys.stderr.seek(0)
std_err = sys.stderr.read().decode("utf-8")
std_err = tmp_err.getvalue()
err_file = os.path.join(output_dir, "eplusout.err")
if os.path.isfile(err_file):
with open(err_file, "r") as f:
Expand Down
34 changes: 34 additions & 0 deletions eppy/tests/test_parse_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import shutil
import sys

from six import StringIO

from eppy.runner.run_functions import parse_error, EnergyPlusRunError


def test_capture_stderr():
tmp_out = StringIO()
sys.stderr = tmp_out
sys.stderr.write("I am in stderr")
msg = parse_error(tmp_out, "C:/notafile")
assert "<File not found>" in msg
assert "I am in stderr" in msg
sys.stderr = sys.__stderr__


def test_capture_real_error(test_idf):
test_idf.newidfobject(
"HVACTemplate:Thermostat",
Name="thermostat VRF",
Heating_Setpoint_Schedule_Name=15,
Constant_Cooling_Setpoint=25,
)
rundir = "test_capture_real_error"
os.mkdir(rundir)
try:
test_idf.run(output_directory=rundir)
except EnergyPlusRunError as e:
assert "invalid Heating Setpoint Temperature Schedule" in str(e)
finally:
shutil.rmtree(rundir)

0 comments on commit 5e4f47a

Please sign in to comment.