[Cython] Use __slots__ to prevent accidental invalid attribute assignment

This commit is contained in:
Ray Speth 2014-07-30 16:59:17 +00:00
parent c8bb3a0c52
commit 03a4f63729
6 changed files with 45 additions and 1 deletions

View file

@ -1,4 +1,5 @@
class Solution(ThermoPhase, Kinetics, Transport):
__slots__ = ()
"""
A class for chemically-reacting solutions.
@ -13,6 +14,7 @@ class Solution(ThermoPhase, Kinetics, Transport):
"""
class Interface(InterfacePhase, InterfaceKinetics):
__slots__ = ('_phase_indices',)
"""
Two-dimensional interfaces.
@ -23,6 +25,7 @@ class Interface(InterfacePhase, InterfaceKinetics):
"""
class DustyGas(ThermoPhase, Kinetics, DustyGasTransport):
__slots__ = ()
"""
A composite class which models a gas in a stationary, solid, porous medium.

View file

@ -10,6 +10,7 @@ except ImportError:
class FlameBase(Sim1D):
""" Base class for flames with a single flow domain """
__slots__ = ('gas',)
def __init__(self, domains, gas, grid=None):
"""
@ -223,6 +224,7 @@ for attr in ['forward_rates_of_progress', 'reverse_rates_of_progress', 'net_rate
class FreeFlame(FlameBase):
"""A freely-propagating flat flame."""
__slots__ = ('inlet', 'outlet', 'flame')
def __init__(self, gas, grid=None):
"""
@ -268,6 +270,7 @@ class FreeFlame(FlameBase):
class BurnerFlame(FlameBase):
"""A burner-stabilized flat flame."""
__slots__ = ('burner', 'flame', 'outlet')
def __init__(self, gas, grid=None):
"""
@ -321,6 +324,7 @@ class BurnerFlame(FlameBase):
class CounterflowDiffusionFlame(FlameBase):
""" A counterflow diffusion flame """
__slots__ = ('fuel_inlet', 'flame', 'oxidizer_inlet')
def __init__(self, gas, grid=None):
"""
@ -434,6 +438,8 @@ class CounterflowDiffusionFlame(FlameBase):
class ImpingingJet(FlameBase):
"""An axisymmetric flow impinging on a surface at normal incidence."""
__slots__ = ('inlet', 'flame', 'surface')
def __init__(self, gas, grid=None, surface=None):
"""
:param gas:
@ -500,6 +506,7 @@ class ImpingingJet(FlameBase):
class CounterflowPremixedFlame(FlameBase):
""" A premixed counterflow flame """
__slots__ = ('reactants', 'flame', 'products')
def __init__(self, gas, grid=None):
"""

View file

@ -180,3 +180,10 @@ class TestMixture(utilities.CanteraTest):
self.assertArrayNear(E1, E2)
self.assertNear(self.mix.T, 400)
self.assertNear(self.mix.P, 2 * ct.one_atm)
def test_invalid_property(self):
x = self.mix
with self.assertRaises(AttributeError):
x.foobar = 300
with self.assertRaises(AttributeError):
x.foobar

View file

@ -74,6 +74,18 @@ class TestOnedim(utilities.CanteraTest):
with self.assertRaises(NotImplementedError):
copy.copy(flame)
def test_invalid_property(self):
gas1 = ct.Solution('h2o2.xml')
inlet = ct.Inlet1D(name='something', phase=gas1)
flame = ct.FreeFlow(gas1)
sim = ct.Sim1D((inlet, flame))
for x in (inlet, flame, sim):
with self.assertRaises(AttributeError):
x.foobar = 300
with self.assertRaises(AttributeError):
x.foobar
class TestFreeFlame(utilities.CanteraTest):
tol_ss = [1.0e-5, 1.0e-14] # [rtol atol] for steady-state problem

View file

@ -512,6 +512,14 @@ class TestReactor(utilities.CanteraTest):
with self.assertRaises(NotImplementedError):
copy.copy(self.net)
def test_invalid_property(self):
self.make_reactors()
for x in (self.r1, self.net):
with self.assertRaises(AttributeError):
x.foobar = 300
with self.assertRaises(AttributeError):
x.foobar
class TestIdealGasReactor(TestReactor):
reactorClass = ct.IdealGasReactor

View file

@ -8,7 +8,7 @@ class TestThermoPhase(utilities.CanteraTest):
def setUp(self):
self.phase = ct.Solution('h2o2.xml')
def teste_phases(self):
def test_phases(self):
self.assertEqual(self.phase.n_phases, 1)
def test_species(self):
@ -308,6 +308,13 @@ class TestThermoPhase(utilities.CanteraTest):
with self.assertRaises(AssertionError):
self.phase.UVX = -4e5, 4.4, 'H2:1.0', -1
def test_invalid_property(self):
x = self.phase
with self.assertRaises(AttributeError):
x.foobar = 300
with self.assertRaises(AttributeError):
x.foobar
def check_getters(self):
T,P,X = self.phase.TPX
self.assertNear(T, self.phase.T)