Skip to content

Commit

Permalink
Merge pull request #23 from ezietsman/master
Browse files Browse the repository at this point in the history
Update cython bindings for python 3.6 and 2.7.
Updated Tests.
Fixed the code for Mac OSX
  • Loading branch information
bcoconni committed May 9, 2018
2 parents c2c3669 + 1932e9b commit 67f2541
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 52 deletions.
6 changes: 3 additions & 3 deletions tests/CheckFGBug1503.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ def test_actuator_rate_is_linear(self):
hysteresis_element = actuator_element.find('hysteresis')
actuator_element.remove(hysteresis_element)
input_element = actuator_element.find('input')
input_prop = string.split(actuator_element.attrib['name'], '-')
input_prop = actuator_element.attrib['name'].split('-')
input_prop[-1] = 'input'
input_prop = string.join(input_prop, '-')
input_prop = '-'.join(input_prop)
input_element.text = input_prop
output_element = actuator_element.find('output')
output_prop = string.strip(output_element.text)
output_prop = output_element.text.strip()

# Add the new properties to <flight_control> so that we can make
# reference to them without JSBSim complaining
Expand Down
14 changes: 7 additions & 7 deletions tests/CheckOutputRate.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setUp(self):
for event in root.findall('run/event'):
if event.attrib['name'] == 'Trim':
cond_tag = event.find('condition')
self.trim_date = float(string.split(cond_tag.text)[-1])
self.trim_date = float(cond_tag.text.split()[-1])
break

# Read the output rate and the output file from the aircraft file
Expand Down Expand Up @@ -74,7 +74,7 @@ def testOutputRate(self):

self.fdm.run_ic()

for i in xrange(self.rate):
for i in range(self.rate):
self.fdm.run()

output = pd.read_csv(self.output_file)
Expand All @@ -96,7 +96,7 @@ def testDisablingOutput(self):
self.fdm.run_ic()
self.fdm["simulation/output/enabled"] = 1.0

for i in xrange(self.rate):
for i in range(self.rate):
self.fdm.run()

output = pd.read_csv(self.output_file)
Expand Down Expand Up @@ -125,15 +125,15 @@ def testTrimRestoresOutputSettings(self):
self.fdm["simulation/output/enabled"] = 1.0
frame = int(self.fdm["simulation/frame"])

for i in xrange(self.rate):
for i in range(self.rate):
self.fdm.run()

output = pd.read_csv(self.output_file)

# The frame at which the data is logged must be the next multiple of
# the output rate
self.assertEqual(int(output['Time'].iloc[0]/self.dt),
(1 + frame/self.rate)*self.rate)
self.assertEqual(int(output['Time'].iloc[0] / self.dt),
(1 + frame // self.rate)*self.rate)

def testDisablingOutputInScript(self):
property = et.SubElement(self.run_tag, 'property')
Expand All @@ -149,7 +149,7 @@ def testDisablingOutputInScript(self):
self.fdm.run_ic()
self.fdm["simulation/output/enabled"] = 1.0

for i in xrange(self.rate):
for i in range(self.rate):
self.fdm.run()

output = pd.read_csv(self.output_file)
Expand Down
2 changes: 1 addition & 1 deletion tests/CheckScripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>
#

import fpectl

from JSBSim_utils import JSBSimTestCase, CreateFDM, RunTest, ExecuteUntil


Expand Down
6 changes: 3 additions & 3 deletions tests/JSBSim_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>

import os, sys, string, tempfile, shutil, unittest
import os, sys, tempfile, shutil, unittest
import xml.etree.ElementTree as et
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -82,7 +82,7 @@ def CheckXMLFile(f, header):
return False

# Check the file header
return string.upper(tree.getroot().tag) == string.upper(header)
return tree.getroot().tag.upper() == header.upper()


def CopyAircraftDef(script_path, sandbox):
Expand Down Expand Up @@ -122,7 +122,7 @@ def CopyAircraftDef(script_path, sandbox):
else:
name_with_system_path = os.path.join(path_to_jsbsim_aircrafts,
'Systems', name)
print name_with_system_path
print(name_with_system_path)
if os.path.exists(name_with_system_path):
system_path = sandbox(aircraft_path, 'Systems')
if not os.path.exists(system_path):
Expand Down
2 changes: 1 addition & 1 deletion tests/TestAccelerometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def testOnGround(self):
fdm['ic/psi-true-rad'] = 0.0
fdm.run_ic()

for i in xrange(1000):
for i in range(1000):
fdm.run()

r = fdm['position/radius-to-vehicle-ft']
Expand Down
4 changes: 2 additions & 2 deletions tests/TestEngineIndexedProps.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def testFunctionWithIndexedProps(self):
fdm['propulsion/engine[1]/engine-rpm'],
fdm['propulsion/engine[2]/engine-rpm'],
fdm['propulsion/engine[3]/engine-rpm']]
for i in xrange(4):
for i in range(4):
maxPower = max(0.0, 1.0-rpm[i]/400)*498.941*0.10471976*rpm[i]
self.assertAlmostEqual(fdm['propulsion/engine[%d]/starter-max-power-W' % (i,)],
maxPower)
Expand Down Expand Up @@ -108,7 +108,7 @@ def testTableWithIndexedVars(self):
fdm['propulsion/engine[1]/engine-rpm'],
fdm['propulsion/engine[2]/engine-rpm'],
fdm['propulsion/engine[3]/engine-rpm']]
for i in xrange(4):
for i in range(4):
tabularData = max(0.0, 1.0-rpm[i]/400)
maxPower = tabularData*498.941*0.10471976*rpm[i]
self.assertAlmostEqual(fdm['propulsion/engine[%d]/starter-max-power-W' % (i,)],
Expand Down
4 changes: 2 additions & 2 deletions tests/TestInitialConditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ def test_initial_latitude(self):
IC_file = self.sandbox.path_to_jsbsim_file('aircraft', 'ball',
'reset00'+v+'.xml')

for i in xrange(8):
for latitude_pos in xrange(4):
for i in range(8):
for latitude_pos in range(4):
IC_tree = et.parse(IC_file)
IC_root = IC_tree.getroot()
if v:
Expand Down
30 changes: 15 additions & 15 deletions tests/TestInputSocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ def __del__(self):

def sendCommand(self, command):
self.cond.acquire()
self.tn.write(command+"\n")
self.tn.write("{}\n".format(command).encode())
# Wait for a time step to be executed before reading the output from
# telnet
self.cond.wait()
msg = self.tn.read_very_eager()
msg = self.tn.read_very_eager().decode()
self.cond.release()
self.thread.join(0.1)
return msg
Expand All @@ -111,8 +111,8 @@ def getDeltaT(self):
return dt

def getPropertyValue(self, property):
msg = string.split(self.sendCommand("get "+property), '\n')
return float(string.split(msg[0], '=')[1])
msg = self.sendCommand("get "+property).split('\n')
return float(msg[0].split('=')[1])

def getOutput(self):
self.cond.acquire()
Expand All @@ -136,14 +136,14 @@ def setUp(self):

def sanityCheck(self, _tn):
# Check that the connection has been established
out = _tn.getOutput()
self.assertTrue(string.split(out, '\n')[0] == 'Connected to JSBSim server',
out = _tn.getOutput().decode()
self.assertTrue(out.split('\n')[0] == 'Connected to JSBSim server',
msg="Not connected to the JSBSim server.\nGot message '%s' instead" % (out,))

# Check that "help" returns the minimum set of commands that will be
# tested
self.assertEqual(sorted(map(lambda x: string.strip(string.split(x, '{')[0]),
string.split(_tn.sendCommand("help"), '\n')[2:-2])),
self.assertEqual(sorted(map(lambda x: x.split('{')[0].strip(),
_tn.sendCommand("help").split('\n')[2:-2])),
['get', 'help', 'hold', 'info', 'iterate', 'quit', 'resume', 'set'])

def test_no_input(self):
Expand All @@ -153,7 +153,7 @@ def test_no_input(self):
fdm.hold()

with self.assertRaises(socket.error):
TelnetInterface(fdm, 5., 1137)
TelnetInterface(fdm, 5., 2222)

def test_input_socket(self):
# First, extract the time step from the script file
Expand All @@ -179,14 +179,14 @@ def test_input_socket(self):
self.sanityCheck(tn)

# Check the aircraft name and its version
msg = string.split(tn.sendCommand("info"), '\n')
self.assertEqual(string.strip(string.split(msg[2], ':')[1]),
string.strip(root.attrib['name']))
self.assertEqual(string.strip(string.split(msg[1], ':')[1]),
string.strip(root.attrib['version']))
msg = tn.sendCommand("info").split('\n')
self.assertEqual(msg[2].split(':')[1].strip(),
root.attrib['name'].strip())
self.assertEqual(msg[1].split(':')[1].strip(),
root.attrib['version'].strip())

# Check that the simulation time is 0.0
self.assertEqual(float(string.strip(string.split(msg[3], ':')[1])), 0.0)
self.assertEqual(float(msg[3].split(':')[1].strip()), 0.0)
self.assertEqual(tn.getSimTime(), 0.0)
self.assertEqual(tn.getPropertyValue("simulation/sim-time-sec"), 0.0)

Expand Down
4 changes: 2 additions & 2 deletions tests/TestLGearSteer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#

import os
import fpectl
import xml.etree.ElementTree as et
from JSBSim_utils import JSBSimTestCase, CreateFDM, RunTest, CopyAircraftDef
import fpectl


class TestLGearSteer(JSBSimTestCase):
Expand All @@ -38,7 +38,7 @@ def test_direct_steer(self):
fpectl.turnon_sigfpe()

grndreact = fdm.get_ground_reactions()
for i in xrange(grndreact.get_num_gear_units()):
for i in range(grndreact.get_num_gear_units()):
gear = grndreact.get_gear_unit(i)
self.assertEqual(gear.get_steer_norm(), 0.0)

Expand Down
6 changes: 3 additions & 3 deletions tests/TestPitotAngle.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ def test_pitot_angle(self):
weight = fdm['inertia/weight-lbs']
spring_tag = contact_tag.find('./spring_coeff')
spring_coeff = float(spring_tag.text)
print "Weight=%d Spring=%d" % (weight, spring_coeff)
print("Weight=%d Spring=%d" % (weight, spring_coeff))
fdm['ic/h-sl-ft'] = weight / spring_coeff
fdm['forces/hold-down'] = 1.0
fdm.run_ic()

ExecuteUntil(fdm, 10.)

for i in xrange(36):
for j in xrange(-9, 10):
for i in range(36):
for j in range(-9, 10):
angle = math.pi * i / 18.0
angle2 = math.pi * j / 18.0
ca2 = math.cos(angle2)
Expand Down
2 changes: 1 addition & 1 deletion tests/TestPointMassInertia.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_point_mass_inertia(self):
pointmass_element.remove(form_element)

inertia = np.zeros((3, 3))
if string.strip(shape) == 'tube':
if shape.strip() == 'tube':
inertia[0, 0] = radius * radius
inertia[1, 1] = (6.0 * inertia[0, 0] + length * length) / 12.0
inertia[2, 2] = inertia[1, 1]
Expand Down
4 changes: 2 additions & 2 deletions tests/TestSuspend.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def testSuspend(self):

fdm.suspend_integration()
fdm.disable_output()
for i in xrange(5):
for i in range(5):
fdm.run()
fdm.resume_integration()
fdm.enable_output()
Expand All @@ -74,7 +74,7 @@ def testHold(self):
ExecuteUntil(fdm, 1.0)

fdm.hold()
for i in xrange(5):
for i in range(5):
fdm.run()
fdm.resume()

Expand Down
50 changes: 47 additions & 3 deletions tests/fpectlmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
#if defined(_MSC_VER)
# include <float.h>
static unsigned int fp_flags = 0;
#elif defined(__GNUC__) && !defined(sgi)
#elif (defined(__GNUC__) || defined(__clang__)) && !defined(sgi)
# include <fenv.h>
static int fp_flags = 0;
#endif
Expand Down Expand Up @@ -106,8 +106,12 @@ static PyObject *turnon_sigfpe(PyObject *self, PyObject *args)
_clearfp();
fp_flags = _controlfp(_controlfp(0, 0) & ~(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW),
_MCW_EM);
#elif defined(__clang__)
fp_flags = feraiseexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);

#elif defined(__GNUC__) && !defined(sgi)
fp_flags = feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);

#endif

handler = PyOS_setsig(SIGFPE, sigfpe_handler);
Expand All @@ -119,6 +123,10 @@ static PyObject *turnoff_sigfpe(PyObject *self, PyObject *args)
{
#if defined(_MSC_VER)
_controlfp(fp_flags, _MCW_EM);

#elif defined(__clang__)
feraiseexcept(fp_flags);

#elif defined(__GNUC__) && !defined(sgi)
fedisableexcept(fp_flags);
#endif
Expand All @@ -128,16 +136,52 @@ static PyObject *turnoff_sigfpe(PyObject *self, PyObject *args)
return Py_None;
}

struct module_state {
PyObject *error;
};

#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))

static struct PyModuleDef fpectl = {
PyModuleDef_HEAD_INIT,
"fpectl",
NULL,
sizeof(struct module_state),
fpectl_methods,
NULL,
NULL,
NULL,
NULL
};

PyMODINIT_FUNC PyInit_fpectl(void)
#else
PyMODINIT_FUNC initfpectl(void)
#endif
{
PyObject *m = Py_InitModule("fpectl", fpectl_methods);
#if PY_MAJOR_VERSION >= 3
PyObject *m = PyModule_Create(&fpectl);
#else
PyObject *m = Py_InitModule("fpectl", fpectl_methods);
#endif

if (m == NULL)
return;
#if PY_MAJOR_VERSION >= 3
return NULL;
#else
return;
#endif

PyObject *d = PyModule_GetDict(m);
fpe_error = PyErr_NewException((char*)"fpectl.FloatingPointError",
PyExc_FloatingPointError, NULL);
if (fpe_error != NULL)
PyDict_SetItemString(d, "FloatingPointError", fpe_error);

#if PY_MAJOR_VERSION >= 3
return m;
#endif
}

static PyObject *test_sigfpe(PyObject *self, PyObject *args)
Expand Down
Loading

0 comments on commit 67f2541

Please sign in to comment.