KeepLastStream instance has no attribute 'close'

1 post / 0 new
mazhenir
Offline
Joined: 2017-10-19
KeepLastStream instance has no attribute 'close'

Version of JModelica.org - 2.0

Operating system:  Ubuntu 17.04, 64bit

------------

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/opt/CBMX/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/home/mazhenir/.cbmx/libraries/jmodelica/JModelica/Python/pymodelica/compiler_logging.py", line 222, in run
    xml.sax.parse(self.stream, LogErrorParser(self.result))
  File "/opt/CBMX/lib/python2.7/xml/sax/__init__.py", line 33, in parse
    parser.parse(source)
  File "/opt/CBMX/lib/python2.7/xml/sax/expatreader.py", line 116, in parse
    self._close_source()
  File "/opt/CBMX/lib/python2.7/xml/sax/expatreader.py", line 235, in _close_source
    file.close()
AttributeError: KeepLastStream instance has no attribute 'close'

-------------

The root cause:

File (the latest version) expatreader.py has method "_close_source()" to close source stream, but KeepLastStream doesn't have it.

 The quick fix - just to add method close() to  KeepLastStream .

E.g.:

class KeepLastStream():
    """
    Internal class that records the last contents sent to the SAX parser.
    It is necessary so that we can recover from SAX parser errors that is
    thrown when the compiler fail to start properly. For example when the
    JVM is unable to allocate enough memory.
    """
   
def __init__(self, stream):
        self.stream = stream
        self.last = None
        self.secondLastLine = None
        self.lastLine = ''
       
self.line = 1 # Number of lines that has been read read
       
self.errorMessage = None

    def close(self):
        self.stream.close()

...

------------

Example to reproduce:

--------

file "FirstOrder.mo"

model FirstOrder
  Real x;
equation
  der(x) = 1-x;
end FirstOrder;

--------

file test.py

from jmodelica import pyfmi, pymodelica
from pymodelica import compile_fmu
from pyfmi import load_fmu

mo_file = "FirstOrder.mo"
class_name = 'FirstOrder'
my_fmu = compile_fmu(class_name, mo_file)

first_order = load_fmu(my_fmu)
res = first_order.simulate(final_time=30)