[Numpy-svn] r2980 - in trunk/numpy/core: . src
numpy-svn at scipy.org
numpy-svn at scipy.org
Wed Aug 9 20:14:44 EDT 2006
Author: oliphant
Date: 2006-08-09 19:14:42 -0500 (Wed, 09 Aug 2006)
New Revision: 2980
Modified:
trunk/numpy/core/_internal.py
trunk/numpy/core/src/multiarraymodule.c
Log:
Fix byte-order problems in comma-string formats and size-specified fields.
Modified: trunk/numpy/core/_internal.py
===================================================================
--- trunk/numpy/core/_internal.py 2006-08-08 21:16:05 UTC (rev 2979)
+++ trunk/numpy/core/_internal.py 2006-08-10 00:14:42 UTC (rev 2980)
@@ -4,7 +4,13 @@
import re
from multiarray import dtype, ndarray
+import sys
+if (sys.byteorder == 'little'):
+ _nbo = '<'
+else:
+ _nbo = '>'
+
def _makenames_list(adict):
allfields = []
fnames = adict.keys()
@@ -141,10 +147,15 @@
return newlist
-format_re = re.compile(r'(?P<repeat> *[(]?[ ,0-9]*[)]? *)(?P<dtype>[><|A-Za-z0-9.]*)')
+format_re = re.compile(r'(?P<order1>[<>|=]?)(?P<repeats> *[(]?[ ,0-9]*[)]? *)(?P<order2>[<>|=]?)(?P<dtype>[A-Za-z0-9.]*)')
# astr is a string (perhaps comma separated)
+_convorder = {'=': _nbo,
+ '|': '|',
+ '>': '>',
+ '<': '<'}
+
def _commastring(astr):
res = _split(astr)
if (len(res)) < 1:
@@ -153,10 +164,24 @@
for k,item in enumerate(res):
# convert item
try:
- (repeats, dtype) = format_re.match(item).groups()
+ (order1, repeats, order2, dtype) = format_re.match(item).groups()
except (TypeError, AttributeError):
raise ValueError('format %s is not recognized' % item)
+ if order2 == '':
+ order = order1
+ elif order1 == '':
+ order = order2
+ else:
+ order1 = _convorder[order1]
+ order2 = _convorder[order2]
+ if (order1 != order2):
+ raise ValueError('in-consistent byte-order specification %s and %s' % (order1, order2))
+ order = order1
+
+ if order in ['|', '=', _nbo]:
+ order = ''
+ dtype = '%s%s' % (order, dtype)
if (repeats == ''):
newitem = dtype
else:
Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c 2006-08-08 21:16:05 UTC (rev 2979)
+++ trunk/numpy/core/src/multiarraymodule.c 2006-08-10 00:14:42 UTC (rev 2980)
@@ -4193,13 +4193,19 @@
}
static int
-_could_be_commastring(char *type, int len)
+_check_for_commastring(char *type, int len)
{
int i;
- if (type[0] >= '1' && type[0] <= '9') return 1;
+
+ if ((type[0] >= '0' && type[0] <= '9') ||
+ ((len > 1) && (type[0] == '>' || type[0] == '<' || \
+ type[0] == '|' || type[0] == '=') && \
+ (type[1] >= '0' && type[1] <= '9')))
+ return 1;
for (i=1;i<len;i++)
if (type[i] == ',') return 1;
- return 0;
+
+ return 0;
}
/*
@@ -4303,26 +4309,25 @@
type = PyString_AS_STRING(obj);
len = PyString_GET_SIZE(obj);
if (len <= 0) goto fail;
- check_num = (int) type[0];
- if ((char) check_num == '>' || (char) check_num == '<' || \
- (char) check_num == '|' || (char) check_num == '=') {
- if (len <= 1) goto fail;
- endian = (char) check_num;
- type++; len--;
- check_num = (int) type[0];
- if (endian == '|') endian = '=';
- }
- if (len > 1) {
+
+ /* check for commas present
+ or first (or second) element a digit */
+ if (_check_for_commastring(type, len)) {
+ *at = _convert_from_commastring(obj, 0);
+ if (*at) return PY_SUCCEED;
+ return PY_FAIL;
+ }
+ check_num = (int) type[0];
+ if ((char) check_num == '>' || (char) check_num == '<' || \
+ (char) check_num == '|' || (char) check_num == '=') {
+ if (len <= 1) goto fail;
+ endian = (char) check_num;
+ type++; len--;
+ check_num = (int) type[0];
+ if (endian == '|') endian = '=';
+ }
+ if (len > 1) {
elsize = atoi(type+1);
- /* check for commas present
- or first element a digit */
- if (_could_be_commastring(type, len)) {
- /* see if it can be converted from
- a comma-separated string */
- *at = _convert_from_commastring(obj, 0);
- if (*at) return PY_SUCCEED;
- else return PY_FAIL;
- }
if (elsize == 0) {
check_num = PyArray_NOTYPE+10;
}
More information about the Numpy-svn
mailing list