Skip to content

Commit

Permalink
Intercepts file not found errors for proper errors recovery.
Browse files Browse the repository at this point in the history
When calling load_ic() and load_script() with incorrect file names, the 2 C++ methods can either return false or call exit().

In the former case, if the user does not check the returned value and continue calling methods on the FGFDMExec instance this can set the Python interpreter in a state of confusion that can only be recovered by quitting the interpreter.

In the latter case, an error in the file name simply terminates the Python session (i.e. exit() is called) which is not an appropriate way to raise an error in an interpreter.

This commit intercepts the file name errors for load_ic() and load_script() and raises a proper Python exception allowing the user to take the appropriate corrections within a Python interpreter.
  • Loading branch information
bcoconni committed Jul 10, 2021
1 parent 01d5e90 commit f018b70
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion python/jsbsim.pyx.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,20 @@

@DoxMainPage"""

import os, platform, numpy
import errno
import os
import platform

import numpy

__version__='${PROJECT_VERSION}'

def append_xml(name):
if len(name) < 4 or name[-4:] != '.xml':
return name+'.xml'
return name


cdef convertToNumpyMat(const c_FGMatrix33& m):
return numpy.mat([[m.Entry(1, 1), m.Entry(1, 2), m.Entry(1, 3)],
[m.Entry(2, 1), m.Entry(2, 2), m.Entry(2, 3)],
Expand Down Expand Up @@ -346,6 +356,10 @@ cdef class FGFDMExec(FGJSBBase):

def load_script(self, script, delta_t=0.0, initfile=""):
"""@Dox(JSBSim::FGFDMExec::LoadScript) """
scriptfile = os.path.join(self.get_root_dir(), script)
if not os.path.exists(scriptfile):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),
scriptfile)
return self.thisptr.LoadScript(c_SGPath(script.encode(), NULL), delta_t,
c_SGPath(initfile.encode(),NULL))

Expand Down Expand Up @@ -535,6 +549,12 @@ cdef class FGFDMExec(FGJSBBase):
return self.thisptr.GetDebugLevel()

def load_ic(self, rstfile, useStoredPath):
reset_file = append_xml(rstfile)
if useStoredPath and not os.path.isabs(reset_file):
reset_file = os.path.join(self.get_full_aircraft_path(), reset_file)
if not os.path.exists(reset_file):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),
reset_file)
rstfile = rstfile.encode()
return self.thisptr.GetIC().Load(c_SGPath(rstfile, NULL), useStoredPath)

Expand Down

0 comments on commit f018b70

Please # to comment.