diff options
author | Raymond Hettinger <python@rcn.com> | 2015-01-30 06:02:17 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2015-01-30 06:02:17 (GMT) |
commit | 91496a08d4cb0b185fed53692cd9f36c76de9725 (patch) | |
tree | fd54565fa983af81617e85c276a92ca768a79240 /Objects | |
parent | c5644126a2906c61bb40cc6d3186060c358de734 (diff) | |
parent | 6ef0285aec5bd4c6252d975dceeac1201bcc181d (diff) | |
download | cpython-91496a08d4cb0b185fed53692cd9f36c76de9725.zip cpython-91496a08d4cb0b185fed53692cd9f36c76de9725.tar.gz cpython-91496a08d4cb0b185fed53692cd9f36c76de9725.tar.bz2 |
merge
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/memoryobject.c | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 935da04..b611dc8 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -1132,6 +1132,51 @@ get_native_fmtchar(char *result, const char *fmt) return -1; } +Py_LOCAL_INLINE(char *) +get_native_fmtstr(const char *fmt) +{ + int at = 0; + + if (fmt[0] == '@') { + at = 1; + fmt++; + } + if (fmt[0] == '\0' || fmt[1] != '\0') { + return NULL; + } + +#define RETURN(s) do { return at ? "@" s : s; } while (0) + + switch (fmt[0]) { + case 'c': RETURN("c"); + case 'b': RETURN("b"); + case 'B': RETURN("B"); + case 'h': RETURN("h"); + case 'H': RETURN("H"); + case 'i': RETURN("i"); + case 'I': RETURN("I"); + case 'l': RETURN("l"); + case 'L': RETURN("L"); + #ifdef HAVE_LONG_LONG + case 'q': RETURN("q"); + case 'Q': RETURN("Q"); + #endif + case 'n': RETURN("n"); + case 'N': RETURN("N"); + case 'f': RETURN("f"); + case 'd': RETURN("d"); + #ifdef HAVE_C99_BOOL + case '?': RETURN("?"); + #else + case '?': RETURN("?"); + #endif + case 'P': RETURN("P"); + } + + return NULL; +} + + /* Cast a memoryview's data type to 'format'. The input array must be C-contiguous. At least one of input-format, output-format must have byte size. The output array is 1-D, with the same byte length as the @@ -1181,10 +1226,13 @@ cast_to_1D(PyMemoryViewObject *mv, PyObject *format) goto out; } - strncpy(mv->format, PyBytes_AS_STRING(asciifmt), - _Py_MEMORYVIEW_MAX_FORMAT); - mv->format[_Py_MEMORYVIEW_MAX_FORMAT-1] = '\0'; - view->format = mv->format; + view->format = get_native_fmtstr(PyBytes_AS_STRING(asciifmt)); + if (view->format == NULL) { + /* NOT_REACHED: get_native_fmtchar() already validates the format. */ + PyErr_SetString(PyExc_RuntimeError, + "memoryview: internal error"); + goto out; + } view->itemsize = itemsize; view->ndim = 1; |