[Python] Fix handling of bad input to 'stringify'

Cases where a string is required but a non-string is provided will now generate
an exception instead of substituting the empty string.
This commit is contained in:
Ray Speth 2015-10-14 12:58:08 -04:00
parent 512dffe859
commit 8591a7ee5c
3 changed files with 8 additions and 2 deletions

View file

@ -982,7 +982,7 @@ cdef class ReactionPathDiagram:
cdef CxxStringStream* _log
# free functions
cdef string stringify(x)
cdef string stringify(x) except *
cdef pystr(string x)
cdef np.ndarray get_species_array(Kinetics kin, kineticsMethod1d method)
cdef np.ndarray get_reaction_array(Kinetics kin, kineticsMethod1d method)

View file

@ -1003,3 +1003,9 @@ class TestQuantity(utilities.CanteraTest):
with self.assertRaises(Exception):
q1+q2
class TestMisc(utilities.CanteraTest):
def test_stringify_bad(self):
with self.assertRaises(AttributeError):
ct.Solution(3)

View file

@ -4,7 +4,7 @@ cdef int _pythonMajorVersion = sys.version_info[0]
cdef CxxPythonLogger* _logger = new CxxPythonLogger()
CxxSetLogger(_logger)
cdef string stringify(x):
cdef string stringify(x) except *:
""" Converts Python strings to std::string. """
# This method works with both Python 2.x and 3.x.
if isinstance(x, bytes):