From: Bertrand <bco...@us...> - 2016-06-19 09:26:26
|
Update of /cvsroot/jsbsim/JSBSim/tests In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv15729/tests Modified Files: CMakeLists.txt CheckScripts.py ExceptionManagement.h setup.py.in Added Files: fpectlmodule.h fpectlmodule.cpp Log Message: Discovered by accident that the script L410.xml is raising an FPE exception and that this was not caught by the test cases. This commit adds the Python framework to enable the FPE mechanism and catch one of its occurence through the Python exception mechanism. This work is derived from the official Python fpectl module (see the license details in tests/fpectlmodule.cpp) --- NEW FILE --- /* Python module to control floating point exceptions * * Copyright (c) 2016 Bertrand Coconnier * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, see <http://www.gnu.org/licenses/> */ #include <exception> #include <string> namespace JSBSim { class FloatingPointException: public std::exception { public: FloatingPointException(const std::string& _msg) : msg(_msg) {} const char* what() const throw() { return msg.c_str(); } ~FloatingPointException() throw() {} private: std::string msg; }; } --- NEW FILE --- /* --------------------------------------------------------------------- / Copyright (c) 1996. \ | The Regents of the University of California. | | All rights reserved. | | | | Permission to use, copy, modify, and distribute this software for | | any purpose without fee is hereby granted, provided that this en- | | tire notice is included in all copies of any software which is or | | includes a copy or modification of this software and in all | | copies of the supporting documentation for such software. | | | | This work was produced at the University of California, Lawrence | | Livermore National Laboratory under contract no. W-7405-ENG-48 | | between the U.S. Department of Energy and The Regents of the | | University of California for the operation of UC LLNL. | | | | DISCLAIMER | | | | This software was prepared as an account of work sponsored by an | | agency of the United States Government. Neither the United States | | Government nor the University of California nor any of their em- | | ployees, makes any warranty, express or implied, or assumes any | | liability or responsibility for the accuracy, completeness, or | | usefulness of any information, apparatus, product, or process | | disclosed, or represents that its use would not infringe | | privately-owned rights. Reference herein to any specific commer- | | cial products, process, or service by trade name, trademark, | | manufacturer, or otherwise, does not necessarily constitute or | | imply its endorsement, recommendation, or favoring by the United | | States Government or the University of California. The views and | | opinions of authors expressed herein do not necessarily state or | | reflect those of the United States Government or the University | | of California, and shall not be used for advertising or product | \ endorsement purposes. / --------------------------------------------------------------------- */ /* Floating point exception control module. This Python module provides bare-bones control over floating point units from several hardware manufacturers. Specifically, it allows the user to turn on the generation of SIGFPE whenever any of the three serious IEEE 754 exceptions (Division by Zero, Overflow, Invalid Operation) occurs. We currently ignore Underflow and Inexact Result exceptions, although those could certainly be added if desired. The module also establishes a signal handler for SIGFPE during initialization. This is an adaptation of the fpectl module (https://docs.python.org/2/library/fpectl.html) which code can be found in the Python distribution at Module/fpectlmodule.c The module has been adapted to modern OS APIs and simplified by the use of the C++ exception mechanism. The Python API remains the same. This module is only useful to you if it happens to include code specific for your hardware and software environment. If you can contribute OS-specific code for new platforms, or corrections for the code provided, it will be greatly appreciated. ** Version 1.0: September 20, 1996. Lee Busby, LLNL. ** JSBSim adaptation: June 18, 2016. Bertrand Coconnier */ #include "Python.h" #include <signal.h> #include "fpectlmodule.h" #if defined(_MSC_VER) # include <float.h> static unsigned int fp_flags = 0; #elif defined(__GNUC__) && !defined(sgi) # include <fenv.h> static int fp_flags = 0; #endif static PyOS_sighandler_t handler = 0; PyMODINIT_FUNC initfpectl(void); static PyObject *turnon_sigfpe(PyObject *self,PyObject *args); static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args); static PyMethodDef fpectl_methods[] = { {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, METH_VARARGS}, {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, METH_VARARGS}, {0,0} }; static void sigfpe_handler(int signo) { PyOS_setsig(SIGFPE, sigfpe_handler); throw JSBSim::FloatingPointException("Caught signal SIGFPE in JSBSim"); } static PyObject *turnon_sigfpe(PyObject *self, PyObject *args) { #if defined(_MSC_VER) _clearfp(); fp_flags = _controlfp(_controlfp(0, 0) & ~(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM); #elif defined(__GNUC__) && !defined(sgi) fp_flags = feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); #endif handler = PyOS_setsig(SIGFPE, sigfpe_handler); Py_INCREF (Py_None); return Py_None; } static PyObject *turnoff_sigfpe(PyObject *self, PyObject *args) { #if defined(_MSC_VER) _controlfp(fp_flags, _MCW_EM); #elif defined(__GNUC__) && !defined(sgi) fedisableexcept(fp_flags); #endif PyOS_setsig(SIGFPE, handler); Py_INCREF (Py_None); return Py_None; } PyMODINIT_FUNC initfpectl(void) { Py_InitModule("fpectl", fpectl_methods); } Index: CMakeLists.txt =================================================================== RCS file: /cvsroot/jsbsim/JSBSim/tests/CMakeLists.txt,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -r1.29 -r1.30 *** CMakeLists.txt 12 Jun 2016 14:39:51 -0000 1.29 --- CMakeLists.txt 19 Jun 2016 09:26:22 -0000 1.30 *************** *** 10,13 **** --- 10,18 ---- target_link_libraries(jsbsim libJSBSim) + set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py") + configure_file(setup.py.in ${SETUP_PY}) + add_custom_target(fpectl_module ALL DEPENDS ${SETUP_PY} + COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} build_ext -i) + # Replicate the Python files in the build dir. # With CMake, the build tree can be separated from the source tree. For tests *************** *** 53,58 **** # Install the JSBSim Python module if (INSTALL_PYTHON_MODULE) - set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py") - configure_file(setup.py.in ${SETUP_PY}) install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} install)") endif() --- 58,61 ---- Index: CheckScripts.py =================================================================== RCS file: /cvsroot/jsbsim/JSBSim/tests/CheckScripts.py,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -r1.5 -r1.6 *** CheckScripts.py 30 Jan 2016 14:34:44 -0000 1.5 --- CheckScripts.py 19 Jun 2016 09:26:22 -0000 1.6 *************** *** 20,23 **** --- 20,24 ---- # + import fpectl from JSBSim_utils import JSBSimTestCase, CreateFDM, RunTest *************** *** 25,28 **** --- 26,31 ---- class CheckScripts(JSBSimTestCase): def testScripts(self): + fpectl.turnon_sigfpe() + for s in self.script_list(): fdm = CreateFDM(self.sandbox) Index: ExceptionManagement.h =================================================================== RCS file: /cvsroot/jsbsim/JSBSim/tests/ExceptionManagement.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** ExceptionManagement.h 2 Jan 2016 17:06:49 -0000 1.1 --- ExceptionManagement.h 19 Jun 2016 09:26:22 -0000 1.2 *************** *** 17,23 **** */ - #include <exception> #include <string> #include "Python.h" void convertJSBSimToPyExc() --- 17,23 ---- */ #include <string> #include "Python.h" + #include "fpectlmodule.h" void convertJSBSimToPyExc() *************** *** 33,35 **** --- 33,38 ---- PyErr_SetString(PyExc_RuntimeError, msg); } + catch (const JSBSim::FloatingPointException& e) { + PyErr_SetString(PyExc_FloatingPointError, e.what()); + } } Index: setup.py.in =================================================================== RCS file: /cvsroot/jsbsim/JSBSim/tests/setup.py.in,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** setup.py.in 18 May 2014 12:43:35 -0000 1.1 --- setup.py.in 19 Jun 2016 09:26:22 -0000 1.2 *************** *** 6,9 **** --- 6,10 ---- from distutils.command.install_lib import install_lib + # Dummy class to skip the standard build process of Python distutils. This is # done separately by CMake. *************** *** 11,45 **** class NoBuild(build_ext): def build_extension(self, ext): ! pass # Don't build # The standard install_lib class installs every single file it finds in the # directory contained in self.build_dir. Here, we just want to install the ! # JSBSim module so that it can be accessed everywhere on the platform. ! # This class is therefore specializing the standard install_lib to do just that. # class InstallJSBSimModule(install_lib): def __init__(self, dist): install_lib.__init__(self, dist) ! # Append whichever platform specific extension to the jsbsim module name self.module_name = build_ext(dist).get_ext_filename('jsbsim') ! # To stay on the safe side, we are explicitly asking (again) to skip the ! # build process. Note that, according to my tests, this does not prevent ! # the Python distutils from calling 'build_ext', hence the class ! # 'NoBuild' above. self.skip_build = True def install(self): ! self.copy_file(os.path.join('tests',self.module_name), self.install_dir+self.module_name) setup( name="${PROJECT_NAME}", version="${PROJECT_VERSION}", ! cmdclass = {'build_ext' : NoBuild, 'install_lib' : InstallJSBSimModule }, ! ext_modules = [Extension('jsbsim',['jsbsim.cxx'], ! libraries=['JSBSim'], ! include_dirs=[os.path.join('${CMAKE_SOURCE_DIR}', ! 'src')], ! library_dirs=[os.path.join('${CMAKE_BINARY_DIR}', ! 'src')], ! language='c++')] ! ) --- 12,56 ---- class NoBuild(build_ext): def build_extension(self, ext): ! pass # Don't build ! # The standard install_lib class installs every single file it finds in the # directory contained in self.build_dir. Here, we just want to install the ! # JSBSim module so that it can be accessed everywhere on the platform. This ! # class is therefore specializing the standard install_lib to do just that. # class InstallJSBSimModule(install_lib): def __init__(self, dist): install_lib.__init__(self, dist) ! # Append whichever platform specific extension to the jsbsim module ! # name self.module_name = build_ext(dist).get_ext_filename('jsbsim') ! # To stay on the safe side, we are explicitly asking (again) to skip ! # the build process. Note that, according to my tests, this does not ! # prevent the Python distutils from calling 'build_ext', hence the ! # class 'NoBuild' above. self.skip_build = True + def install(self): ! self.copy_file(os.path.join('tests', self.module_name), self.install_dir+self.module_name) + # Installation process for the JSBSim Python module setup( name="${PROJECT_NAME}", version="${PROJECT_VERSION}", ! cmdclass={'build_ext': NoBuild, 'install_lib': InstallJSBSimModule}, ! ext_modules=[Extension('jsbsim', ['jsbsim.cxx'], ! libraries=['JSBSim'], ! include_dirs=[os.path.join('${CMAKE_SOURCE_DIR}', ! 'src')], ! library_dirs=[os.path.join('${CMAKE_BINARY_DIR}', ! 'src')], ! language='c++')]) ! ! # Build process for the fpectl module ! setup(ext_modules=[Extension('fpectl', ! sources=[os.path.join('${CMAKE_SOURCE_DIR}', ! 'tests', ! 'fpectlmodule.cpp')], ! language='c++')]) |