From 64c66437ebccd6f36b5c1bc3a823789dd8247d18 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 22 Apr 2015 14:01:17 -0400 Subject: [PATCH] [Python] Improve handling of 'bytes' objects being passed in --- interfaces/cython/cantera/kinetics.pyx | 6 +++--- interfaces/cython/cantera/mixture.pyx | 8 ++++---- interfaces/cython/cantera/onedim.pyx | 2 +- interfaces/cython/cantera/reactor.pyx | 2 +- interfaces/cython/cantera/test/test_thermo.py | 8 ++++---- interfaces/cython/cantera/thermo.pyx | 12 +++++------- interfaces/cython/cantera/utils.pyx | 7 +++++-- 7 files changed, 23 insertions(+), 22 deletions(-) diff --git a/interfaces/cython/cantera/kinetics.pyx b/interfaces/cython/cantera/kinetics.pyx index 89918b3cc..69939039c 100644 --- a/interfaces/cython/cantera/kinetics.pyx +++ b/interfaces/cython/cantera/kinetics.pyx @@ -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 diff --git a/interfaces/cython/cantera/mixture.pyx b/interfaces/cython/cantera/mixture.pyx index 42223ecc3..92186ceee 100644 --- a/interfaces/cython/cantera/mixture.pyx +++ b/interfaces/cython/cantera/mixture.pyx @@ -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 = 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 = 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 diff --git a/interfaces/cython/cantera/onedim.pyx b/interfaces/cython/cantera/onedim.pyx index 5ea6eef20..14a18bb2f 100644 --- a/interfaces/cython/cantera/onedim.pyx +++ b/interfaces/cython/cantera/onedim.pyx @@ -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 diff --git a/interfaces/cython/cantera/reactor.pyx b/interfaces/cython/cantera/reactor.pyx index 519d8bfcb..2cfce2a34 100644 --- a/interfaces/cython/cantera/reactor.pyx +++ b/interfaces/cython/cantera/reactor.pyx @@ -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): diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index 095b37f0e..6563536c4 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -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) diff --git a/interfaces/cython/cantera/thermo.pyx b/interfaces/cython/cantera/thermo.pyx index c89e390d6..255af5f8b 100644 --- a/interfaces/cython/cantera/thermo.pyx +++ b/interfaces/cython/cantera/thermo.pyx @@ -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 = 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 = 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(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)) diff --git a/interfaces/cython/cantera/utils.pyx b/interfaces/cython/cantera/utils.pyx index ca92ed084..d808bdc8b 100644 --- a/interfaces/cython/cantera/utils.pyx +++ b/interfaces/cython/cantera/utils.pyx @@ -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(x) + else: + tmp = bytes(x.encode()) + return string(tmp) cdef pystr(string x): cdef bytes s = x.c_str()