summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorStefan Krah <skrah@bytereef.org>2015-01-29 13:29:51 (GMT)
committerStefan Krah <skrah@bytereef.org>2015-01-29 13:29:51 (GMT)
commitf5324d7074d17b026ecd5ef7d7fc16f5e71b0db2 (patch)
tree0b5d602bf7d5372ed54df211b3ebc31b21f59653 /Objects
parent6d14405fdb9357b689d76dcfd230018c0ce8c3a8 (diff)
parentfa5d6a5ff3ca247d9c2eaf51853ff39c98c09f4a (diff)
downloadcpython-f5324d7074d17b026ecd5ef7d7fc16f5e71b0db2.zip
cpython-f5324d7074d17b026ecd5ef7d7fc16f5e71b0db2.tar.gz
cpython-f5324d7074d17b026ecd5ef7d7fc16f5e71b0db2.tar.bz2
Closes #22668: Merge from 3.4.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/memoryobject.c56
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;