Skip to content

Commit

Permalink
Backport r5438 to 1.1.x. Fixes #837.
Browse files Browse the repository at this point in the history
  • Loading branch information
pv committed Jul 17, 2008
1 parent 1359da2 commit e83b078
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
33 changes: 26 additions & 7 deletions numpy/core/src/multiarraymodule.c
Expand Up @@ -6040,7 +6040,8 @@ swab_separator(char *sep)
/* 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.
Expand All @@ -6058,10 +6059,17 @@ fromstr_skip_separator(char **s, const char *sep, const char *end)
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;
Expand All @@ -6082,20 +6090,31 @@ static int
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);
Expand Down
12 changes: 12 additions & 0 deletions numpy/core/tests/test_multiarray.py
Expand Up @@ -142,6 +142,10 @@ def check_ascii(self):
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)
Expand Down Expand Up @@ -801,6 +805,14 @@ def test_filename(self):
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)
Expand Down

0 comments on commit e83b078

Please sign in to comment.