[Numpy-svn] r5444 - in branches/1.1.x/numpy/core: src tests
numpy-svn at scipy.org
numpy-svn at scipy.org
Thu Jul 17 04:12:39 EDT 2008
Author: ptvirtan
Date: 2008-07-17 03:12:28 -0500 (Thu, 17 Jul 2008)
New Revision: 5444
Modified:
branches/1.1.x/numpy/core/src/multiarraymodule.c
branches/1.1.x/numpy/core/tests/test_multiarray.py
Log:
Backport r5438 to 1.1.x. Fixes #837.
Modified: branches/1.1.x/numpy/core/src/multiarraymodule.c
===================================================================
--- branches/1.1.x/numpy/core/src/multiarraymodule.c 2008-07-17 04:20:42 UTC (rev 5443)
+++ branches/1.1.x/numpy/core/src/multiarraymodule.c 2008-07-17 08:12:28 UTC (rev 5444)
@@ -6040,7 +6040,8 @@
/* Assuming that the separator is the next bit in the string (file), skip it.
Single spaces in the separator are matched to arbitrary-long sequences
- of whitespace in the input.
+ of whitespace in the input. If the separator consists only of spaces,
+ it matches one or more whitespace characters.
If we can't match the separator, return -2.
If we hit the end of the string (file), return -1.
@@ -6058,10 +6059,17 @@
result = -1;
break;
} else if (*sep == '\0') {
- /* matched separator */
- result = 0;
- break;
+ if (string != *s) {
+ /* matched separator */
+ result = 0;
+ break;
+ } else {
+ /* separator was whitespace wildcard that didn't match */
+ result = -2;
+ break;
+ }
} else if (*sep == ' ') {
+ /* whitespace wildcard */
if (!isspace(c)) {
sep++;
continue;
@@ -6082,20 +6090,31 @@
fromfile_skip_separator(FILE **fp, const char *sep, void *stream_data)
{
int result = 0;
+ const char *sep_start = sep;
while (1) {
int c = fgetc(*fp);
if (c == EOF) {
result = -1;
break;
} else if (*sep == '\0') {
- /* matched separator */
ungetc(c, *fp);
- result = 0;
- break;
+ if (sep != sep_start) {
+ /* matched separator */
+ result = 0;
+ break;
+ } else {
+ /* separator was whitespace wildcard that didn't match */
+ result = -2;
+ break;
+ }
} else if (*sep == ' ') {
+ /* whitespace wildcard */
if (!isspace(c)) {
sep++;
+ sep_start++;
ungetc(c, *fp);
+ } else if (sep == sep_start) {
+ sep_start--;
}
} else if (*sep != c) {
ungetc(c, *fp);
Modified: branches/1.1.x/numpy/core/tests/test_multiarray.py
===================================================================
--- branches/1.1.x/numpy/core/tests/test_multiarray.py 2008-07-17 04:20:42 UTC (rev 5443)
+++ branches/1.1.x/numpy/core/tests/test_multiarray.py 2008-07-17 08:12:28 UTC (rev 5444)
@@ -142,6 +142,10 @@
assert_array_equal(a, [1.,2.,3.,4.])
assert_array_equal(a,b)
+ def test_malformed(self):
+ a = fromstring('1.234 1,234', sep=' ')
+ assert_array_equal(a, [1.234, 1.])
+
class TestZeroRank(NumpyTestCase):
def setUp(self):
self.d = array(0), array('x', object)
@@ -801,6 +805,14 @@
y = np.fromfile(filename,dtype=self.dtype)
assert_array_equal(y,self.x.flat)
+ def test_malformed(self):
+ filename = tempfile.mktemp()
+ f = open(filename,'w')
+ f.write("1.234 1,234")
+ f.close()
+ y = np.fromfile(filename, sep=' ')
+ assert_array_equal(y, [1.234, 1.])
+
class TestFromBuffer(ParametricTestCase):
def tst_basic(self,buffer,expected,kwargs):
assert_array_equal(np.frombuffer(buffer,**kwargs),expected)
More information about the Numpy-svn
mailing list