[Python] Improve handling of 'bytes' objects being passed in
This commit is contained in:
parent
f3eaef1675
commit
64c66437eb
7 changed files with 23 additions and 22 deletions
|
|
@ -65,7 +65,7 @@ cdef class Kinetics(_SolutionBase):
|
|||
argument is unused.
|
||||
"""
|
||||
cdef int k
|
||||
if isinstance(species, (str, unicode)):
|
||||
if isinstance(species, (str, unicode, bytes)):
|
||||
return self.kinetics.kineticsSpeciesIndex(stringify(species))
|
||||
else:
|
||||
k = species
|
||||
|
|
@ -144,7 +144,7 @@ cdef class Kinetics(_SolutionBase):
|
|||
reaction *i_reaction*.
|
||||
"""
|
||||
cdef int k
|
||||
if isinstance(k_spec, (str, unicode)):
|
||||
if isinstance(k_spec, (str, unicode, bytes)):
|
||||
k = self.kinetics_species_index(k_spec)
|
||||
else:
|
||||
k = k_spec
|
||||
|
|
@ -159,7 +159,7 @@ cdef class Kinetics(_SolutionBase):
|
|||
reaction *i_reaction*.
|
||||
"""
|
||||
cdef int k
|
||||
if isinstance(k_spec, (str, unicode)):
|
||||
if isinstance(k_spec, (str, unicode, bytes)):
|
||||
k = self.kinetics_species_index(k_spec)
|
||||
else:
|
||||
k = k_spec
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ cdef class Mixture:
|
|||
>>> mix.element_index('H')
|
||||
2
|
||||
"""
|
||||
if isinstance(element, (str, unicode)):
|
||||
if isinstance(element, (str, unicode, bytes)):
|
||||
index = self.mix.elementIndex(stringify(element))
|
||||
elif isinstance(element, (int, float)):
|
||||
index = <int>element
|
||||
|
|
@ -118,7 +118,7 @@ cdef class Mixture:
|
|||
"""
|
||||
p = self.phase_index(phase)
|
||||
|
||||
if isinstance(species, (str, unicode)):
|
||||
if isinstance(species, (str, unicode, bytes)):
|
||||
k = self.phase(p).species_index(species)
|
||||
elif isinstance(species, (int, float)):
|
||||
k = <int?>species
|
||||
|
|
@ -159,7 +159,7 @@ cdef class Mixture:
|
|||
return int(p)
|
||||
else:
|
||||
raise IndexError("Phase index '{0}' out of range.".format(p))
|
||||
elif isinstance(p, (str, unicode)):
|
||||
elif isinstance(p, (str, unicode, bytes)):
|
||||
for i, phase in enumerate(self._phases):
|
||||
if phase.name == p:
|
||||
return i
|
||||
|
|
@ -248,7 +248,7 @@ cdef class Mixture:
|
|||
return data
|
||||
|
||||
def __set__(self, moles):
|
||||
if isinstance(moles, (str, unicode)):
|
||||
if isinstance(moles, (str, unicode, bytes)):
|
||||
self.mix.setMolesByName(stringify(moles))
|
||||
return
|
||||
|
||||
|
|
|
|||
|
|
@ -506,7 +506,7 @@ cdef class Sim1D:
|
|||
def _get_indices(self, dom, comp):
|
||||
idom = self.domain_index(dom)
|
||||
dom = self.domains[idom]
|
||||
if isinstance(comp, (str, unicode)):
|
||||
if isinstance(comp, (str, unicode, bytes)):
|
||||
kcomp = dom.component_index(comp)
|
||||
else:
|
||||
kcomp = comp
|
||||
|
|
|
|||
|
|
@ -861,7 +861,7 @@ cdef class ReactorNet:
|
|||
"""
|
||||
if isinstance(component, int):
|
||||
return self.net.sensitivity(component, p)
|
||||
elif isinstance(component, (str, unicode)):
|
||||
elif isinstance(component, (str, unicode, bytes)):
|
||||
return self.net.sensitivity(stringify(component), p, r)
|
||||
|
||||
def sensitivities(self):
|
||||
|
|
|
|||
|
|
@ -26,8 +26,8 @@ class TestThermoPhase(utilities.CanteraTest):
|
|||
self.assertEqual(i, self.phase.element_index(i))
|
||||
|
||||
def test_n_atoms(self):
|
||||
data = [(1, 'O', 'O'), (2, 'O', 'O2'), (1, 'H', 'OH'),
|
||||
(2, 'H', 'H2O'), (2, 'O', 'H2O2'), (1, 'Ar', 'AR'),
|
||||
data = [(1, 'O', 'O'), (2, 'O', 'O2'), (1, b'H', b'OH'),
|
||||
(2, 'H', 'H2O'), (2, u'O', u'H2O2'), (1, 'Ar', 'AR'),
|
||||
(0, 'O', 'H'), (0, 'H', 'AR'), (0, 'Ar', 'HO2')]
|
||||
for (n, elem, species) in data:
|
||||
self.assertEqual(self.phase.n_atoms(species, elem), n)
|
||||
|
|
@ -118,12 +118,12 @@ class TestThermoPhase(utilities.CanteraTest):
|
|||
self.assertArrayNear(X0, self.phase.X)
|
||||
|
||||
def test_setCompositionDict(self):
|
||||
self.phase.X = {'H2':1.0, 'O2':3.0}
|
||||
self.phase.X = {b'H2':1.0, b'O2':3.0}
|
||||
X = self.phase.X
|
||||
self.assertNear(X[0], 0.25)
|
||||
self.assertNear(X[3], 0.75)
|
||||
|
||||
self.phase.Y = {'H2':1.0, 'O2':3.0}
|
||||
self.phase.Y = {u'H2':1.0, u'O2':3.0}
|
||||
Y = self.phase.Y
|
||||
self.assertNear(Y[0], 0.25)
|
||||
self.assertNear(Y[3], 0.75)
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ cdef enum Thermasis:
|
|||
|
||||
cdef stdmap[string,double] comp_map(dict X) except *:
|
||||
cdef stdmap[string,double] m
|
||||
cdef str species
|
||||
cdef float val
|
||||
for species,value in X.items():
|
||||
m[stringify(species)] = value
|
||||
return m
|
||||
|
|
@ -219,7 +217,7 @@ cdef class ThermoPhase(_SolutionBase):
|
|||
an integer. In the latter case, the index is checked for validity and
|
||||
returned. If no such element is present, an exception is thrown.
|
||||
"""
|
||||
if isinstance(element, (str, unicode)):
|
||||
if isinstance(element, (str, unicode, bytes)):
|
||||
index = self.thermo.elementIndex(stringify(element))
|
||||
elif isinstance(element, (int, float)):
|
||||
index = <int>element
|
||||
|
|
@ -273,7 +271,7 @@ cdef class ThermoPhase(_SolutionBase):
|
|||
an integer. In the latter case, the index is checked for validity and
|
||||
returned. If no such species is present, an exception is thrown.
|
||||
"""
|
||||
if isinstance(species, (str, unicode)):
|
||||
if isinstance(species, (str, unicode, bytes)):
|
||||
index = self.thermo.speciesIndex(stringify(species))
|
||||
elif isinstance(species, (int, float)):
|
||||
index = <int>species
|
||||
|
|
@ -287,7 +285,7 @@ cdef class ThermoPhase(_SolutionBase):
|
|||
|
||||
def species(self, k):
|
||||
s = Species(init=False)
|
||||
if isinstance(k, (str, unicode)):
|
||||
if isinstance(k, (str, unicode, bytes)):
|
||||
s._assign(self.thermo.species(stringify(k)))
|
||||
elif isinstance(k, (int, float)):
|
||||
s._assign(self.thermo.species(<int>k))
|
||||
|
|
@ -351,7 +349,7 @@ cdef class ThermoPhase(_SolutionBase):
|
|||
def __get__(self):
|
||||
return self._getArray1(thermo_getMassFractions)
|
||||
def __set__(self, Y):
|
||||
if isinstance(Y, (str, unicode)):
|
||||
if isinstance(Y, (str, unicode, bytes)):
|
||||
self.thermo.setMassFractionsByName(stringify(Y))
|
||||
elif isinstance(Y, dict):
|
||||
self.thermo.setMassFractionsByName(comp_map(Y))
|
||||
|
|
@ -372,7 +370,7 @@ cdef class ThermoPhase(_SolutionBase):
|
|||
def __get__(self):
|
||||
return self._getArray1(thermo_getMoleFractions)
|
||||
def __set__(self, X):
|
||||
if isinstance(X, (str, unicode)):
|
||||
if isinstance(X, (str, unicode, bytes)):
|
||||
self.thermo.setMoleFractionsByName(stringify(X))
|
||||
elif isinstance(X, dict):
|
||||
self.thermo.setMoleFractionsByName(comp_map(X))
|
||||
|
|
|
|||
|
|
@ -7,8 +7,11 @@ CxxSetLogger(_logger)
|
|||
cdef string stringify(x):
|
||||
""" Converts Python strings to std::string. """
|
||||
# This method works with both Python 2.x and 3.x.
|
||||
tmp = bytes(x.encode())
|
||||
return string(tmp)
|
||||
if isinstance(x, bytes):
|
||||
return string(<bytes>x)
|
||||
else:
|
||||
tmp = bytes(x.encode())
|
||||
return string(tmp)
|
||||
|
||||
cdef pystr(string x):
|
||||
cdef bytes s = x.c_str()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue