diff --git a/eppy/modeleditor.py b/eppy/modeleditor.py index 560a6f3b..b8cc9d4a 100644 --- a/eppy/modeleditor.py +++ b/eppy/modeleditor.py @@ -996,6 +996,9 @@ def save(self, filename=None, lineendings="default", encoding="latin-1"): is compatible with the EnergyPlus IDFEditor. """ + if self.idfabsname is None and isinstance(self.idfname, str): + # this happens when the user sets self.idfname to an in-memory idf + self.idfabsname = os.path.abspath(self.idfname) if filename is None: filename = self.idfabsname s = self.idfstr() @@ -1023,6 +1026,13 @@ def save(self, filename=None, lineendings="default", encoding="latin-1"): except TypeError: filename.write(s.decode(encoding)) + try: + pass + except Exception as e: + raise e + else: + pass + def saveas(self, filename, lineendings="default", encoding="latin-1"): """Save the IDF as a text file with the filename passed. diff --git a/tests/test_modeleditor.py b/tests/test_modeleditor.py index 873077af..3c16494f 100644 --- a/tests/test_modeleditor.py +++ b/tests/test_modeleditor.py @@ -5,7 +5,7 @@ # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= -"""py.test for modeleditor""" +"""Main py.test modeleditor. Other tests are in test_modeleditor.py""" from __future__ import absolute_import from __future__ import division @@ -690,3 +690,20 @@ def test_copyidf(): idf = IDF(StringIO(idftxt)) new_idf = idf.copyidf() assert idf.idfstr() == new_idf.idfstr() + + +def test_name_and_save_inmemoryidf(): + """py.test to name and save a file that is created in memory""" + # fixing and testing an error from user documentation code + # fix for issue #408 + + idftxt = "VERSION, 7.3;" + fhandle = StringIO(idftxt) + idf_notemptyfile = IDF(fhandle) + idf_notemptyfile.idfname = "notemptyfile_randomchars_laksdfjl.idf" + try: + idf_notemptyfile.save() + except Exception as e: + raise e + finally: + os.remove("notemptyfile_randomchars_laksdfjl.idf") diff --git a/tests/test_modeleditor1.py b/tests/test_modeleditor1.py index 0236b2d1..9588687f 100644 --- a/tests/test_modeleditor1.py +++ b/tests/test_modeleditor1.py @@ -4,7 +4,7 @@ # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= -"""py.test for some functions of modeleditor""" +"""py.test for some functions of modeleditor. Other tests are in test_modeleditor.py""" from __future__ import absolute_import from __future__ import division from __future__ import print_function