summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/_iotools.py8
-rw-r--r--numpy/lib/tests/test__iotools.py18
2 files changed, 17 insertions, 9 deletions
diff --git a/numpy/lib/_iotools.py b/numpy/lib/_iotools.py
index 9108b2e4c..f2adcda10 100644
--- a/numpy/lib/_iotools.py
+++ b/numpy/lib/_iotools.py
@@ -27,6 +27,7 @@ else:
_bytes_to_complex = complex
_bytes_to_name = str
+
def _is_string_like(obj):
"""
Check whether obj behaves like a string.
@@ -37,6 +38,7 @@ def _is_string_like(obj):
return False
return True
+
def _is_bytes_like(obj):
"""
Check whether obj behaves like a bytes object.
@@ -445,6 +447,7 @@ class ConverterError(Exception):
"""
pass
+
class ConverterLockError(ConverterError):
"""
Exception raised when an attempt is made to upgrade a locked converter.
@@ -452,6 +455,7 @@ class ConverterLockError(ConverterError):
"""
pass
+
class ConversionWarning(UserWarning):
"""
Warning issued when a string converter has a problem.
@@ -708,7 +712,7 @@ class StringConverter(object):
"""
self._checked = True
try:
- self._strict_call(value)
+ return self._strict_call(value)
except ValueError:
# Raise an exception if we locked the converter...
if self._locked:
@@ -728,7 +732,7 @@ class StringConverter(object):
self.default = self._initial_default
else:
self.default = default
- self.upgrade(value)
+ return self.upgrade(value)
def iterupgrade(self, value):
self._checked = True
diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py
index 4db19382a..92ca1c973 100644
--- a/numpy/lib/tests/test__iotools.py
+++ b/numpy/lib/tests/test__iotools.py
@@ -7,7 +7,7 @@ from datetime import date
import numpy as np
from numpy.compat import asbytes, asbytes_nested
from numpy.testing import (
- run_module_suite, TestCase, assert_, assert_equal
+ run_module_suite, TestCase, assert_, assert_equal, assert_allclose
)
from numpy.lib._iotools import (
LineSplitter, NameValidator, StringConverter,
@@ -76,7 +76,7 @@ class TestLineSplitter(TestCase):
test = LineSplitter((6, 6, 9))(strg)
assert_equal(test, asbytes_nested(['1', '3 4', '5 6']))
-#-------------------------------------------------------------------------------
+# -----------------------------------------------------------------------------
class TestNameValidator(TestCase):
@@ -127,7 +127,7 @@ class TestNameValidator(TestCase):
assert_(validator(namelist) is None)
assert_equal(validator(namelist, nbfields=3), ['f0', 'f1', 'f2'])
-#-------------------------------------------------------------------------------
+# -----------------------------------------------------------------------------
def _bytes_to_date(s):
@@ -150,13 +150,17 @@ class TestStringConverter(TestCase):
"Tests the upgrade method."
converter = StringConverter()
assert_equal(converter._status, 0)
- converter.upgrade(asbytes('0'))
+ # test int
+ assert_equal(converter.upgrade(asbytes('0')), 0)
assert_equal(converter._status, 1)
- converter.upgrade(asbytes('0.'))
+ # test float
+ assert_allclose(converter.upgrade(asbytes('0.')), 0.0)
assert_equal(converter._status, 2)
- converter.upgrade(asbytes('0j'))
+ # test complex
+ assert_equal(converter.upgrade(asbytes('0j')), complex('0j'))
assert_equal(converter._status, 3)
- converter.upgrade(asbytes('a'))
+ # test str
+ assert_equal(converter.upgrade(asbytes('a')), asbytes('a'))
assert_equal(converter._status, len(converter._mapper) - 1)
def test_missing(self):