summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-05-15 05:55:52 (GMT)
committerGitHub <noreply@github.com>2018-05-15 05:55:52 (GMT)
commit726894addc02effaa369fded3caaba94875c1f3d (patch)
tree0dff9eb1fbc4a2786284f4287fe54b60a9e23d8c
parent295465dc2963d2d9c4bc6241a80e0f1fa73e1f9c (diff)
downloadcpython-726894addc02effaa369fded3caaba94875c1f3d.zip
cpython-726894addc02effaa369fded3caaba94875c1f3d.tar.gz
cpython-726894addc02effaa369fded3caaba94875c1f3d.tar.bz2
bpo-16865: Support arrays >=2GB in ctypes. (GH-3006)
(cherry picked from commit 735abadd5bd91db4a9e6f4311969b0afacca0a1a) Co-authored-by: Segev Finer <segev208@gmail.com>
-rw-r--r--Lib/ctypes/test/test_arrays.py7
-rw-r--r--Misc/NEWS.d/next/Library/2017-09-29-16-40-38.bpo-16865.l-f6I_.rst1
-rw-r--r--Modules/_ctypes/_ctypes.c16
3 files changed, 16 insertions, 8 deletions
diff --git a/Lib/ctypes/test/test_arrays.py b/Lib/ctypes/test/test_arrays.py
index 4ed566b..6e562cf 100644
--- a/Lib/ctypes/test/test_arrays.py
+++ b/Lib/ctypes/test/test_arrays.py
@@ -1,4 +1,6 @@
import unittest
+from test.support import bigmemtest, _2G
+import sys
from ctypes import *
from ctypes.test import need_symbol
@@ -181,5 +183,10 @@ class ArrayTestCase(unittest.TestCase):
_type_ = c_int
_length_ = 1.87
+ @unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
+ @bigmemtest(size=_2G, memuse=1, dry_run=False)
+ def test_large_array(self, size):
+ c_char * size
+
if __name__ == '__main__':
unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2017-09-29-16-40-38.bpo-16865.l-f6I_.rst b/Misc/NEWS.d/next/Library/2017-09-29-16-40-38.bpo-16865.l-f6I_.rst
new file mode 100644
index 0000000..afaff73
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-09-29-16-40-38.bpo-16865.l-f6I_.rst
@@ -0,0 +1 @@
+Support arrays >=2GiB in :mod:`ctypes`. Patch by Segev Finer.
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 2c0a769..69d73f5 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -1392,8 +1392,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
StgDictObject *stgdict;
StgDictObject *itemdict;
PyObject *length_attr, *type_attr;
- long length;
- int overflow;
+ Py_ssize_t length;
Py_ssize_t itemsize, itemalign;
/* create the new instance (which is a class,
@@ -1415,14 +1414,15 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_XDECREF(length_attr);
goto error;
}
- length = PyLong_AsLongAndOverflow(length_attr, &overflow);
- if (overflow) {
- PyErr_SetString(PyExc_OverflowError,
- "The '_length_' attribute is too large");
- Py_DECREF(length_attr);
+ length = PyLong_AsSsize_t(length_attr);
+ Py_DECREF(length_attr);
+ if (length == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ PyErr_SetString(PyExc_OverflowError,
+ "The '_length_' attribute is too large");
+ }
goto error;
}
- Py_DECREF(length_attr);
type_attr = PyObject_GetAttrString((PyObject *)result, "_type_");
if (!type_attr) {