aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/cffi
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.ru>2022-02-10 16:44:39 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:44:39 +0300
commite9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch)
tree64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/python/cffi
parent2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff)
downloadydb-e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0.tar.gz
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/python/cffi')
-rw-r--r--contrib/python/cffi/.dist-info/METADATA74
-rw-r--r--contrib/python/cffi/.dist-info/entry_points.txt6
-rw-r--r--contrib/python/cffi/.dist-info/top_level.txt4
-rw-r--r--contrib/python/cffi/AUTHORS16
-rw-r--r--contrib/python/cffi/LICENSE52
-rw-r--r--contrib/python/cffi/README.md60
-rw-r--r--contrib/python/cffi/c/_cffi_backend.c3456
-rw-r--r--contrib/python/cffi/c/call_python.c94
-rw-r--r--contrib/python/cffi/c/cdlopen.c44
-rw-r--r--contrib/python/cffi/c/cffi1_module.c18
-rw-r--r--contrib/python/cffi/c/cglob.c2
-rw-r--r--contrib/python/cffi/c/ffi_obj.c102
-rw-r--r--contrib/python/cffi/c/file_emulator.h2
-rw-r--r--contrib/python/cffi/c/lib_obj.c92
-rw-r--r--contrib/python/cffi/c/libffi_msvc/ffi.c46
-rw-r--r--contrib/python/cffi/c/libffi_msvc/prep_cif.c8
-rw-r--r--contrib/python/cffi/c/minibuffer.h250
-rw-r--r--contrib/python/cffi/c/misc_thread_common.h530
-rw-r--r--contrib/python/cffi/c/misc_win32.h18
-rw-r--r--contrib/python/cffi/c/parse_c_type.c34
-rw-r--r--contrib/python/cffi/c/realize_c_type.c88
-rw-r--r--contrib/python/cffi/c/test_c.py2028
-rw-r--r--contrib/python/cffi/c/wchar_helper.h290
-rw-r--r--contrib/python/cffi/c/wchar_helper_3.h298
-rw-r--r--contrib/python/cffi/cffi/__init__.py10
-rw-r--r--contrib/python/cffi/cffi/_cffi_errors.h298
-rw-r--r--contrib/python/cffi/cffi/_cffi_include.h278
-rw-r--r--contrib/python/cffi/cffi/_embedding.h174
-rw-r--r--contrib/python/cffi/cffi/api.py308
-rw-r--r--contrib/python/cffi/cffi/backend_ctypes.py46
-rw-r--r--contrib/python/cffi/cffi/cffi_opcode.py20
-rw-r--r--contrib/python/cffi/cffi/commontypes.py10
-rw-r--r--contrib/python/cffi/cffi/cparser.py462
-rw-r--r--contrib/python/cffi/cffi/error.py62
-rw-r--r--contrib/python/cffi/cffi/ffiplatform.py26
-rw-r--r--contrib/python/cffi/cffi/model.py106
-rw-r--r--contrib/python/cffi/cffi/parse_c_type.h10
-rw-r--r--contrib/python/cffi/cffi/pkgconfig.py242
-rw-r--r--contrib/python/cffi/cffi/recompiler.py306
-rw-r--r--contrib/python/cffi/cffi/setuptools_ext.py72
-rw-r--r--contrib/python/cffi/cffi/vengine_cpy.py180
-rw-r--r--contrib/python/cffi/cffi/vengine_gen.py30
-rw-r--r--contrib/python/cffi/cffi/verifier.py20
-rw-r--r--contrib/python/cffi/gen/lib/ya.make4
-rw-r--r--contrib/python/cffi/gen/ya.make2
-rw-r--r--contrib/python/cffi/ya.make14
46 files changed, 5146 insertions, 5146 deletions
diff --git a/contrib/python/cffi/.dist-info/METADATA b/contrib/python/cffi/.dist-info/METADATA
index 720a88f2aa..b708a09f61 100644
--- a/contrib/python/cffi/.dist-info/METADATA
+++ b/contrib/python/cffi/.dist-info/METADATA
@@ -1,37 +1,37 @@
-Metadata-Version: 2.1
-Name: cffi
-Version: 1.15.0
-Summary: Foreign Function Interface for Python calling C code.
-Home-page: http://cffi.readthedocs.org
-Author: Armin Rigo, Maciej Fijalkowski
-Author-email: python-cffi@googlegroups.com
-License: MIT
-Platform: UNKNOWN
-Classifier: Programming Language :: Python
-Classifier: Programming Language :: Python :: 2
-Classifier: Programming Language :: Python :: 2.7
-Classifier: Programming Language :: Python :: 3
-Classifier: Programming Language :: Python :: 3.6
-Classifier: Programming Language :: Python :: 3.7
-Classifier: Programming Language :: Python :: 3.8
-Classifier: Programming Language :: Python :: 3.9
-Classifier: Programming Language :: Python :: 3.10
-Classifier: Programming Language :: Python :: Implementation :: CPython
-Classifier: Programming Language :: Python :: Implementation :: PyPy
-Classifier: License :: OSI Approved :: MIT License
-License-File: LICENSE
-Requires-Dist: pycparser
-
-
-CFFI
-====
-
-Foreign Function Interface for Python calling C code.
-Please see the `Documentation <http://cffi.readthedocs.org/>`_.
-
-Contact
--------
-
-`Mailing list <https://groups.google.com/forum/#!forum/python-cffi>`_
-
-
+Metadata-Version: 2.1
+Name: cffi
+Version: 1.15.0
+Summary: Foreign Function Interface for Python calling C code.
+Home-page: http://cffi.readthedocs.org
+Author: Armin Rigo, Maciej Fijalkowski
+Author-email: python-cffi@googlegroups.com
+License: MIT
+Platform: UNKNOWN
+Classifier: Programming Language :: Python
+Classifier: Programming Language :: Python :: 2
+Classifier: Programming Language :: Python :: 2.7
+Classifier: Programming Language :: Python :: 3
+Classifier: Programming Language :: Python :: 3.6
+Classifier: Programming Language :: Python :: 3.7
+Classifier: Programming Language :: Python :: 3.8
+Classifier: Programming Language :: Python :: 3.9
+Classifier: Programming Language :: Python :: 3.10
+Classifier: Programming Language :: Python :: Implementation :: CPython
+Classifier: Programming Language :: Python :: Implementation :: PyPy
+Classifier: License :: OSI Approved :: MIT License
+License-File: LICENSE
+Requires-Dist: pycparser
+
+
+CFFI
+====
+
+Foreign Function Interface for Python calling C code.
+Please see the `Documentation <http://cffi.readthedocs.org/>`_.
+
+Contact
+-------
+
+`Mailing list <https://groups.google.com/forum/#!forum/python-cffi>`_
+
+
diff --git a/contrib/python/cffi/.dist-info/entry_points.txt b/contrib/python/cffi/.dist-info/entry_points.txt
index 9577a180cc..eee7e0fb1f 100644
--- a/contrib/python/cffi/.dist-info/entry_points.txt
+++ b/contrib/python/cffi/.dist-info/entry_points.txt
@@ -1,3 +1,3 @@
-[distutils.setup_keywords]
-cffi_modules = cffi.setuptools_ext:cffi_modules
-
+[distutils.setup_keywords]
+cffi_modules = cffi.setuptools_ext:cffi_modules
+
diff --git a/contrib/python/cffi/.dist-info/top_level.txt b/contrib/python/cffi/.dist-info/top_level.txt
index 42b1f02695..f64577957e 100644
--- a/contrib/python/cffi/.dist-info/top_level.txt
+++ b/contrib/python/cffi/.dist-info/top_level.txt
@@ -1,2 +1,2 @@
-_cffi_backend
-cffi
+_cffi_backend
+cffi
diff --git a/contrib/python/cffi/AUTHORS b/contrib/python/cffi/AUTHORS
index 21cb42000d..370a25d317 100644
--- a/contrib/python/cffi/AUTHORS
+++ b/contrib/python/cffi/AUTHORS
@@ -1,8 +1,8 @@
-This package has been mostly done by Armin Rigo with help from
-Maciej Fijałkowski. The idea is heavily based (although not directly
-copied) from LuaJIT ffi by Mike Pall.
-
-
-Other contributors:
-
- Google Inc.
+This package has been mostly done by Armin Rigo with help from
+Maciej Fijałkowski. The idea is heavily based (although not directly
+copied) from LuaJIT ffi by Mike Pall.
+
+
+Other contributors:
+
+ Google Inc.
diff --git a/contrib/python/cffi/LICENSE b/contrib/python/cffi/LICENSE
index 6dff3d06ee..29225eee9e 100644
--- a/contrib/python/cffi/LICENSE
+++ b/contrib/python/cffi/LICENSE
@@ -1,26 +1,26 @@
-
-Except when otherwise stated (look for LICENSE files in directories or
-information at the beginning of each file) all software and
-documentation is licensed as follows:
-
- The MIT License
-
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or
- sell copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included
- in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- DEALINGS IN THE SOFTWARE.
-
+
+Except when otherwise stated (look for LICENSE files in directories or
+information at the beginning of each file) all software and
+documentation is licensed as follows:
+
+ The MIT License
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or
+ sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+
diff --git a/contrib/python/cffi/README.md b/contrib/python/cffi/README.md
index ddcb76fe2e..a68639e854 100644
--- a/contrib/python/cffi/README.md
+++ b/contrib/python/cffi/README.md
@@ -1,30 +1,30 @@
-CFFI
-====
-
-Foreign Function Interface for Python calling C code.
-Please see the [Documentation](http://cffi.readthedocs.org/) or uncompiled
-in the doc/ subdirectory.
-
-Download
---------
-
-[Download page](https://foss.heptapod.net/pypy/cffi/-/tags)
-
-Contact
--------
-
-[Mailing list](https://groups.google.com/forum/#!forum/python-cffi)
-
-Testing/development tips
-------------------------
-
-To run tests under CPython, run::
-
- pip install pytest # if you don't have py.test already
- pip install pycparser
- python setup.py build_ext -f -i
- py.test c/ testing/
-
-If you run in another directory (either the tests or another program),
-you should use the environment variable ``PYTHONPATH=/path`` to point
-to the location that contains the ``_cffi_backend.so`` just compiled.
+CFFI
+====
+
+Foreign Function Interface for Python calling C code.
+Please see the [Documentation](http://cffi.readthedocs.org/) or uncompiled
+in the doc/ subdirectory.
+
+Download
+--------
+
+[Download page](https://foss.heptapod.net/pypy/cffi/-/tags)
+
+Contact
+-------
+
+[Mailing list](https://groups.google.com/forum/#!forum/python-cffi)
+
+Testing/development tips
+------------------------
+
+To run tests under CPython, run::
+
+ pip install pytest # if you don't have py.test already
+ pip install pycparser
+ python setup.py build_ext -f -i
+ py.test c/ testing/
+
+If you run in another directory (either the tests or another program),
+you should use the environment variable ``PYTHONPATH=/path`` to point
+to the location that contains the ``_cffi_backend.so`` just compiled.
diff --git a/contrib/python/cffi/c/_cffi_backend.c b/contrib/python/cffi/c/_cffi_backend.c
index db7fad8e08..a92402b6a2 100644
--- a/contrib/python/cffi/c/_cffi_backend.c
+++ b/contrib/python/cffi/c/_cffi_backend.c
@@ -2,7 +2,7 @@
#include <Python.h>
#include "structmember.h"
-#define CFFI_VERSION "1.15.0"
+#define CFFI_VERSION "1.15.0"
#ifdef MS_WIN32
#include <windows.h>
@@ -61,66 +61,66 @@
#endif
-/* Define the following macro ONLY if you trust libffi's version of
- * ffi_closure_alloc() more than the code in malloc_closure.h.
- * IMPORTANT: DO NOT ENABLE THIS ON LINUX, unless you understand exactly
- * why I recommend against it and decide that you trust it more than my
- * analysis below.
- *
- * There are two versions of this code: one inside libffi itself, and
- * one inside malloc_closure.h here. Both should be fine as long as the
- * Linux distribution does _not_ enable extra security features. If it
- * does, then the code in malloc_closure.h will cleanly crash because
- * there is no reasonable way to obtain a read-write-execute memory
- * page. On the other hand, the code in libffi will appear to
- * work---but will actually randomly crash after a fork() if the child
- * does not immediately call exec(). This second crash is of the kind
- * that can be turned into an attack vector by a motivated attacker.
- * So, _enabling_ extra security features _opens_ an attack vector.
- * That sounds like a horribly bad idea to me, and is the reason for why
- * I prefer CFFI crashing cleanly.
- *
- * Currently, we use libffi's ffi_closure_alloc() on NetBSD. It is
- * known that on the NetBSD kernel, a different strategy is used which
- * should not be open to the fork() bug.
- *
- * This is also used on macOS, provided we are executing on macOS 10.15 or
- * above. It's a mess because it needs runtime checks in that case.
- */
-#ifdef __NetBSD__
-
-# define CFFI_CHECK_FFI_CLOSURE_ALLOC 1
-# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 1
-# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC 1
-# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 1
-# define CFFI_CHECK_FFI_PREP_CIF_VAR 0
-# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 0
-
-#elif defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE)
-
-# define CFFI_CHECK_FFI_CLOSURE_ALLOC __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)
-# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 1
-# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)
-# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 1
-# define CFFI_CHECK_FFI_PREP_CIF_VAR __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)
-# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 1
-
-#else
-
-# define CFFI_CHECK_FFI_CLOSURE_ALLOC 0
-# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 0
-# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC 0
-# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 0
-# define CFFI_CHECK_FFI_PREP_CIF_VAR 0
-# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 0
-
-#endif
-
-/* always includes this, even if it turns out not to be used on NetBSD
- because calls are behind "if (0)" */
-#include "malloc_closure.h"
-
-
+/* Define the following macro ONLY if you trust libffi's version of
+ * ffi_closure_alloc() more than the code in malloc_closure.h.
+ * IMPORTANT: DO NOT ENABLE THIS ON LINUX, unless you understand exactly
+ * why I recommend against it and decide that you trust it more than my
+ * analysis below.
+ *
+ * There are two versions of this code: one inside libffi itself, and
+ * one inside malloc_closure.h here. Both should be fine as long as the
+ * Linux distribution does _not_ enable extra security features. If it
+ * does, then the code in malloc_closure.h will cleanly crash because
+ * there is no reasonable way to obtain a read-write-execute memory
+ * page. On the other hand, the code in libffi will appear to
+ * work---but will actually randomly crash after a fork() if the child
+ * does not immediately call exec(). This second crash is of the kind
+ * that can be turned into an attack vector by a motivated attacker.
+ * So, _enabling_ extra security features _opens_ an attack vector.
+ * That sounds like a horribly bad idea to me, and is the reason for why
+ * I prefer CFFI crashing cleanly.
+ *
+ * Currently, we use libffi's ffi_closure_alloc() on NetBSD. It is
+ * known that on the NetBSD kernel, a different strategy is used which
+ * should not be open to the fork() bug.
+ *
+ * This is also used on macOS, provided we are executing on macOS 10.15 or
+ * above. It's a mess because it needs runtime checks in that case.
+ */
+#ifdef __NetBSD__
+
+# define CFFI_CHECK_FFI_CLOSURE_ALLOC 1
+# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 1
+# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC 1
+# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 1
+# define CFFI_CHECK_FFI_PREP_CIF_VAR 0
+# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 0
+
+#elif defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE)
+
+# define CFFI_CHECK_FFI_CLOSURE_ALLOC __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)
+# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 1
+# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)
+# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 1
+# define CFFI_CHECK_FFI_PREP_CIF_VAR __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)
+# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 1
+
+#else
+
+# define CFFI_CHECK_FFI_CLOSURE_ALLOC 0
+# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 0
+# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC 0
+# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 0
+# define CFFI_CHECK_FFI_PREP_CIF_VAR 0
+# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 0
+
+#endif
+
+/* always includes this, even if it turns out not to be used on NetBSD
+ because calls are behind "if (0)" */
+#include "malloc_closure.h"
+
+
#if PY_MAJOR_VERSION >= 3
# define STR_OR_BYTES "bytes"
# define PyText_Type PyUnicode_Type
@@ -129,11 +129,11 @@
# define PyText_FromFormat PyUnicode_FromFormat
# define PyText_AsUTF8 _PyUnicode_AsString /* PyUnicode_AsUTF8 in Py3.3 */
# define PyText_AS_UTF8 _PyUnicode_AsString
-# if PY_VERSION_HEX >= 0x03030000
-# define PyText_GetSize PyUnicode_GetLength
-# else
-# define PyText_GetSize PyUnicode_GetSize
-# endif
+# if PY_VERSION_HEX >= 0x03030000
+# define PyText_GetSize PyUnicode_GetLength
+# else
+# define PyText_GetSize PyUnicode_GetSize
+# endif
# define PyText_FromString PyUnicode_FromString
# define PyText_FromStringAndSize PyUnicode_FromStringAndSize
# define PyText_InternInPlace PyUnicode_InternInPlace
@@ -176,50 +176,50 @@
(PyCObject_FromVoidPtr(pointer, destructor))
#endif
-#if PY_VERSION_HEX < 0x030900a4
-# define Py_SET_REFCNT(obj, val) (Py_REFCNT(obj) = (val))
-#endif
-
-#if PY_VERSION_HEX >= 0x03080000
-# define USE_WRITEUNRAISABLEMSG
-#endif
-
+#if PY_VERSION_HEX < 0x030900a4
+# define Py_SET_REFCNT(obj, val) (Py_REFCNT(obj) = (val))
+#endif
+
+#if PY_VERSION_HEX >= 0x03080000
+# define USE_WRITEUNRAISABLEMSG
+#endif
+
/************************************************************/
/* base type flag: exactly one of the following: */
-#define CT_PRIMITIVE_SIGNED 0x001 /* signed integer */
-#define CT_PRIMITIVE_UNSIGNED 0x002 /* unsigned integer */
-#define CT_PRIMITIVE_CHAR 0x004 /* char, wchar_t, charN_t */
-#define CT_PRIMITIVE_FLOAT 0x008 /* float, double, long double */
-#define CT_POINTER 0x010 /* pointer, excluding ptr-to-func */
-#define CT_ARRAY 0x020 /* array */
-#define CT_STRUCT 0x040 /* struct */
-#define CT_UNION 0x080 /* union */
-#define CT_FUNCTIONPTR 0x100 /* pointer to function */
-#define CT_VOID 0x200 /* void */
-#define CT_PRIMITIVE_COMPLEX 0x400 /* float _Complex, double _Complex */
+#define CT_PRIMITIVE_SIGNED 0x001 /* signed integer */
+#define CT_PRIMITIVE_UNSIGNED 0x002 /* unsigned integer */
+#define CT_PRIMITIVE_CHAR 0x004 /* char, wchar_t, charN_t */
+#define CT_PRIMITIVE_FLOAT 0x008 /* float, double, long double */
+#define CT_POINTER 0x010 /* pointer, excluding ptr-to-func */
+#define CT_ARRAY 0x020 /* array */
+#define CT_STRUCT 0x040 /* struct */
+#define CT_UNION 0x080 /* union */
+#define CT_FUNCTIONPTR 0x100 /* pointer to function */
+#define CT_VOID 0x200 /* void */
+#define CT_PRIMITIVE_COMPLEX 0x400 /* float _Complex, double _Complex */
/* other flags that may also be set in addition to the base flag: */
-#define CT_IS_VOIDCHAR_PTR 0x00001000
-#define CT_PRIMITIVE_FITS_LONG 0x00002000
-#define CT_IS_OPAQUE 0x00004000
-#define CT_IS_ENUM 0x00008000
-#define CT_IS_PTR_TO_OWNED 0x00010000 /* only owned if CDataOwning_Type */
-#define CT_CUSTOM_FIELD_POS 0x00020000
-#define CT_IS_LONGDOUBLE 0x00040000
-#define CT_IS_BOOL 0x00080000
-#define CT_IS_FILE 0x00100000
-#define CT_IS_VOID_PTR 0x00200000
-#define CT_WITH_VAR_ARRAY 0x00400000 /* with open-ended array, anywhere */
-/* unused 0x00800000 */
-#define CT_LAZY_FIELD_LIST 0x01000000
-#define CT_WITH_PACKED_CHANGE 0x02000000
-#define CT_IS_SIGNED_WCHAR 0x04000000
+#define CT_IS_VOIDCHAR_PTR 0x00001000
+#define CT_PRIMITIVE_FITS_LONG 0x00002000
+#define CT_IS_OPAQUE 0x00004000
+#define CT_IS_ENUM 0x00008000
+#define CT_IS_PTR_TO_OWNED 0x00010000 /* only owned if CDataOwning_Type */
+#define CT_CUSTOM_FIELD_POS 0x00020000
+#define CT_IS_LONGDOUBLE 0x00040000
+#define CT_IS_BOOL 0x00080000
+#define CT_IS_FILE 0x00100000
+#define CT_IS_VOID_PTR 0x00200000
+#define CT_WITH_VAR_ARRAY 0x00400000 /* with open-ended array, anywhere */
+/* unused 0x00800000 */
+#define CT_LAZY_FIELD_LIST 0x01000000
+#define CT_WITH_PACKED_CHANGE 0x02000000
+#define CT_IS_SIGNED_WCHAR 0x04000000
#define CT_PRIMITIVE_ANY (CT_PRIMITIVE_SIGNED | \
CT_PRIMITIVE_UNSIGNED | \
CT_PRIMITIVE_CHAR | \
- CT_PRIMITIVE_FLOAT | \
- CT_PRIMITIVE_COMPLEX)
+ CT_PRIMITIVE_FLOAT | \
+ CT_PRIMITIVE_COMPLEX)
typedef struct _ctypedescr {
PyObject_VAR_HEAD
@@ -274,14 +274,14 @@ static PyTypeObject CField_Type;
static PyTypeObject CData_Type;
static PyTypeObject CDataOwning_Type;
static PyTypeObject CDataOwningGC_Type;
-static PyTypeObject CDataFromBuf_Type;
+static PyTypeObject CDataFromBuf_Type;
static PyTypeObject CDataGCP_Type;
#define CTypeDescr_Check(ob) (Py_TYPE(ob) == &CTypeDescr_Type)
#define CData_Check(ob) (Py_TYPE(ob) == &CData_Type || \
Py_TYPE(ob) == &CDataOwning_Type || \
Py_TYPE(ob) == &CDataOwningGC_Type || \
- Py_TYPE(ob) == &CDataFromBuf_Type || \
+ Py_TYPE(ob) == &CDataFromBuf_Type || \
Py_TYPE(ob) == &CDataGCP_Type)
#define CDataOwn_Check(ob) (Py_TYPE(ob) == &CDataOwning_Type || \
Py_TYPE(ob) == &CDataOwningGC_Type)
@@ -314,14 +314,14 @@ typedef struct {
typedef struct {
CDataObject head;
- PyObject *structobj; /* for ffi.new_handle() or ffi.new("struct *") */
+ PyObject *structobj; /* for ffi.new_handle() or ffi.new("struct *") */
} CDataObject_own_structptr;
typedef struct {
CDataObject head;
Py_ssize_t length; /* same as CDataObject_own_length up to here */
Py_buffer *bufferview;
-} CDataObject_frombuf;
+} CDataObject_frombuf;
typedef struct {
CDataObject head;
@@ -331,11 +331,11 @@ typedef struct {
} CDataObject_gcp;
typedef struct {
- CDataObject head;
- ffi_closure *closure;
-} CDataObject_closure;
-
-typedef struct {
+ CDataObject head;
+ ffi_closure *closure;
+} CDataObject_closure;
+
+typedef struct {
ffi_cif cif;
/* the following information is used when doing the call:
- a buffer of size 'exchange_size' is malloced
@@ -363,14 +363,14 @@ typedef struct {
# include "file_emulator.h"
#endif
-#ifdef PyUnicode_KIND /* Python >= 3.3 */
-# include "wchar_helper_3.h"
-#else
+#ifdef PyUnicode_KIND /* Python >= 3.3 */
+# include "wchar_helper_3.h"
+#else
# include "wchar_helper.h"
#endif
-#include "../cffi/_cffi_errors.h"
-
+#include "../cffi/_cffi_errors.h"
+
typedef struct _cffi_allocator_s {
PyObject *ca_alloc, *ca_free;
int ca_dont_clear;
@@ -439,10 +439,10 @@ ctypedescr_dealloc(CTypeDescrObject *ct)
if (ct->ct_unique_key != NULL) {
/* revive dead object temporarily for DelItem */
- Py_SET_REFCNT(ct, 43);
+ Py_SET_REFCNT(ct, 43);
PyDict_DelItem(unique_cache, ct->ct_unique_key);
assert(Py_REFCNT(ct) == 42);
- Py_SET_REFCNT(ct, 0);
+ Py_SET_REFCNT(ct, 0);
Py_DECREF(ct->ct_unique_key);
}
Py_XDECREF(ct->ct_itemdescr);
@@ -539,8 +539,8 @@ static PyObject *ctypeget_length(CTypeDescrObject *ct, void *context)
static PyObject *
get_field_name(CTypeDescrObject *ct, CFieldObject *cf); /* forward */
-/* returns 0 if the struct ctype is opaque, 1 if it is not, or -1 if
- an exception occurs */
+/* returns 0 if the struct ctype is opaque, 1 if it is not, or -1 if
+ an exception occurs */
#define force_lazy_struct(ct) \
((ct)->ct_stuff != NULL ? 1 : do_realize_lazy_struct(ct))
@@ -688,7 +688,7 @@ static PyMethodDef ctypedescr_methods[] = {
static PyTypeObject CTypeDescr_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.CType",
+ "_cffi_backend.CType",
offsetof(CTypeDescrObject, ct_name),
sizeof(char),
(destructor)ctypedescr_dealloc, /* tp_dealloc */
@@ -929,21 +929,21 @@ read_raw_unsigned_data(char *target, int size)
return 0;
}
-#ifdef __GNUC__
-/* This is a workaround for what I think is a GCC bug on several
- platforms. See issue #378. */
-__attribute__((noinline))
-#endif
-void _cffi_memcpy(char *target, const void *src, size_t size)
-{
- memcpy(target, src, size);
-}
-
+#ifdef __GNUC__
+/* This is a workaround for what I think is a GCC bug on several
+ platforms. See issue #378. */
+__attribute__((noinline))
+#endif
+void _cffi_memcpy(char *target, const void *src, size_t size)
+{
+ memcpy(target, src, size);
+}
+
#define _write_raw_data(type) \
do { \
if (size == sizeof(type)) { \
type r = (type)source; \
- _cffi_memcpy(target, &r, sizeof(type)); \
+ _cffi_memcpy(target, &r, sizeof(type)); \
return; \
} \
} while(0)
@@ -977,26 +977,26 @@ read_raw_longdouble_data(char *target)
return 0;
}
-static Py_complex
-read_raw_complex_data(char *target, int size)
-{
- Py_complex r = {0.0, 0.0};
- if (size == 2*sizeof(float)) {
- float real_part, imag_part;
- memcpy(&real_part, target + 0, sizeof(float));
- memcpy(&imag_part, target + sizeof(float), sizeof(float));
- r.real = real_part;
- r.imag = imag_part;
- return r;
- }
- if (size == 2*sizeof(double)) {
- memcpy(&r, target, 2*sizeof(double));
- return r;
- }
- Py_FatalError("read_raw_complex_data: bad complex size");
- return r;
-}
-
+static Py_complex
+read_raw_complex_data(char *target, int size)
+{
+ Py_complex r = {0.0, 0.0};
+ if (size == 2*sizeof(float)) {
+ float real_part, imag_part;
+ memcpy(&real_part, target + 0, sizeof(float));
+ memcpy(&imag_part, target + sizeof(float), sizeof(float));
+ r.real = real_part;
+ r.imag = imag_part;
+ return r;
+ }
+ if (size == 2*sizeof(double)) {
+ memcpy(&r, target, 2*sizeof(double));
+ return r;
+ }
+ Py_FatalError("read_raw_complex_data: bad complex size");
+ return r;
+}
+
static void
write_raw_float_data(char *target, double source, int size)
{
@@ -1012,25 +1012,25 @@ write_raw_longdouble_data(char *target, long double source)
_write_raw_data(long double);
}
-#define _write_raw_complex_data(type) \
- do { \
- if (size == 2*sizeof(type)) { \
- type r = (type)source.real; \
- type i = (type)source.imag; \
- _cffi_memcpy(target, &r, sizeof(type)); \
- _cffi_memcpy(target+sizeof(type), &i, sizeof(type)); \
- return; \
- } \
- } while(0)
-
-static void
-write_raw_complex_data(char *target, Py_complex source, int size)
-{
- _write_raw_complex_data(float);
- _write_raw_complex_data(double);
- Py_FatalError("write_raw_complex_data: bad complex size");
-}
-
+#define _write_raw_complex_data(type) \
+ do { \
+ if (size == 2*sizeof(type)) { \
+ type r = (type)source.real; \
+ type i = (type)source.imag; \
+ _cffi_memcpy(target, &r, sizeof(type)); \
+ _cffi_memcpy(target+sizeof(type), &i, sizeof(type)); \
+ return; \
+ } \
+ } while(0)
+
+static void
+write_raw_complex_data(char *target, Py_complex source, int size)
+{
+ _write_raw_complex_data(float);
+ _write_raw_complex_data(double);
+ Py_FatalError("write_raw_complex_data: bad complex size");
+}
+
static PyObject *
new_simple_cdata(char *data, CTypeDescrObject *ct)
{
@@ -1105,23 +1105,23 @@ convert_to_object(char *data, CTypeDescrObject *ct)
/*READ(data, ct->ct_size)*/
value = read_raw_unsigned_data(data, ct->ct_size);
- if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG) {
- if (ct->ct_flags & CT_IS_BOOL) {
- PyObject *x;
- switch ((int)value) {
- case 0: x = Py_False; break;
- case 1: x = Py_True; break;
- default:
- PyErr_Format(PyExc_ValueError,
- "got a _Bool of value %d, expected 0 or 1",
- (int)value);
- return NULL;
- }
- Py_INCREF(x);
- return x;
- }
+ if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG) {
+ if (ct->ct_flags & CT_IS_BOOL) {
+ PyObject *x;
+ switch ((int)value) {
+ case 0: x = Py_False; break;
+ case 1: x = Py_True; break;
+ default:
+ PyErr_Format(PyExc_ValueError,
+ "got a _Bool of value %d, expected 0 or 1",
+ (int)value);
+ return NULL;
+ }
+ Py_INCREF(x);
+ return x;
+ }
return PyInt_FromLong((long)value);
- }
+ }
else
return PyLong_FromUnsignedLongLong(value);
}
@@ -1141,19 +1141,19 @@ convert_to_object(char *data, CTypeDescrObject *ct)
}
else if (ct->ct_flags & CT_PRIMITIVE_CHAR) {
/*READ(data, ct->ct_size)*/
- switch (ct->ct_size) {
- case sizeof(char):
+ switch (ct->ct_size) {
+ case sizeof(char):
return PyBytes_FromStringAndSize(data, 1);
- case 2:
- return _my_PyUnicode_FromChar16((cffi_char16_t *)data, 1);
- case 4:
- return _my_PyUnicode_FromChar32((cffi_char32_t *)data, 1);
- }
+ case 2:
+ return _my_PyUnicode_FromChar16((cffi_char16_t *)data, 1);
+ case 4:
+ return _my_PyUnicode_FromChar32((cffi_char32_t *)data, 1);
+ }
+ }
+ else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
+ Py_complex value = read_raw_complex_data(data, ct->ct_size);
+ return PyComplex_FromCComplex(value);
}
- else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
- Py_complex value = read_raw_complex_data(data, ct->ct_size);
- return PyComplex_FromCComplex(value);
- }
PyErr_Format(PyExc_SystemError,
"convert_to_object: '%s'", ct->ct_name);
@@ -1227,65 +1227,65 @@ static int _convert_to_char(PyObject *init)
return -1;
}
-static cffi_char16_t _convert_to_char16_t(PyObject *init)
+static cffi_char16_t _convert_to_char16_t(PyObject *init)
{
- char err_got[80];
- err_got[0] = 0;
-
+ char err_got[80];
+ err_got[0] = 0;
+
if (PyUnicode_Check(init)) {
- cffi_char16_t ordinal;
- if (_my_PyUnicode_AsSingleChar16(init, &ordinal, err_got) == 0)
+ cffi_char16_t ordinal;
+ if (_my_PyUnicode_AsSingleChar16(init, &ordinal, err_got) == 0)
return ordinal;
}
if (CData_Check(init) &&
(((CDataObject *)init)->c_type->ct_flags & CT_PRIMITIVE_CHAR) &&
- (((CDataObject *)init)->c_type->ct_size == 2)) {
+ (((CDataObject *)init)->c_type->ct_size == 2)) {
char *data = ((CDataObject *)init)->c_data;
- /*READ(data, 2)*/
- return *(cffi_char16_t *)data;
+ /*READ(data, 2)*/
+ return *(cffi_char16_t *)data;
}
PyErr_Format(PyExc_TypeError,
- "initializer for ctype 'char16_t' must be a unicode string "
- "of length 1, not %.200s",
- err_got[0] == 0 ? Py_TYPE(init)->tp_name : err_got);
- return (cffi_char16_t)-1;
+ "initializer for ctype 'char16_t' must be a unicode string "
+ "of length 1, not %.200s",
+ err_got[0] == 0 ? Py_TYPE(init)->tp_name : err_got);
+ return (cffi_char16_t)-1;
}
-static cffi_char32_t _convert_to_char32_t(PyObject *init)
-{
- char err_got[80];
- err_got[0] = 0;
-
- if (PyUnicode_Check(init)) {
- cffi_char32_t ordinal;
- if (_my_PyUnicode_AsSingleChar32(init, &ordinal, err_got) == 0)
- return ordinal;
- }
- if (CData_Check(init) &&
- (((CDataObject *)init)->c_type->ct_flags & CT_PRIMITIVE_CHAR) &&
- (((CDataObject *)init)->c_type->ct_size == 4)) {
- char *data = ((CDataObject *)init)->c_data;
- /*READ(data, 4)*/
- return *(cffi_char32_t *)data;
- }
- PyErr_Format(PyExc_TypeError,
- "initializer for ctype 'char32_t' must be a unicode string "
- "of length 1, not %.200s",
- err_got[0] == 0 ? Py_TYPE(init)->tp_name : err_got);
- return (cffi_char32_t)-1;
-}
-
-static int _convert_error(PyObject *init, CTypeDescrObject *ct,
+static cffi_char32_t _convert_to_char32_t(PyObject *init)
+{
+ char err_got[80];
+ err_got[0] = 0;
+
+ if (PyUnicode_Check(init)) {
+ cffi_char32_t ordinal;
+ if (_my_PyUnicode_AsSingleChar32(init, &ordinal, err_got) == 0)
+ return ordinal;
+ }
+ if (CData_Check(init) &&
+ (((CDataObject *)init)->c_type->ct_flags & CT_PRIMITIVE_CHAR) &&
+ (((CDataObject *)init)->c_type->ct_size == 4)) {
+ char *data = ((CDataObject *)init)->c_data;
+ /*READ(data, 4)*/
+ return *(cffi_char32_t *)data;
+ }
+ PyErr_Format(PyExc_TypeError,
+ "initializer for ctype 'char32_t' must be a unicode string "
+ "of length 1, not %.200s",
+ err_got[0] == 0 ? Py_TYPE(init)->tp_name : err_got);
+ return (cffi_char32_t)-1;
+}
+
+static int _convert_error(PyObject *init, CTypeDescrObject *ct,
const char *expected)
{
if (CData_Check(init)) {
- CTypeDescrObject *ct2 = ((CDataObject *)init)->c_type;
- if (strcmp(ct->ct_name, ct2->ct_name) != 0)
+ CTypeDescrObject *ct2 = ((CDataObject *)init)->c_type;
+ if (strcmp(ct->ct_name, ct2->ct_name) != 0)
PyErr_Format(PyExc_TypeError,
"initializer for ctype '%s' must be a %s, "
"not cdata '%s'",
- ct->ct_name, expected, ct2->ct_name);
- else if (ct != ct2) {
+ ct->ct_name, expected, ct2->ct_name);
+ else if (ct != ct2) {
/* in case we'd give the error message "initializer for
ctype 'A' must be a pointer to same type, not cdata
'B'", but with A=B, then give instead a different error
@@ -1294,21 +1294,21 @@ static int _convert_error(PyObject *init, CTypeDescrObject *ct,
"initializer for ctype '%s' appears indeed to be '%s',"
" but the types are different (check that you are not"
" e.g. mixing up different ffi instances)",
- ct->ct_name, ct2->ct_name);
+ ct->ct_name, ct2->ct_name);
+ }
+ else
+ {
+ PyErr_Format(PyExc_SystemError,
+ "initializer for ctype '%s' is correct, but we get "
+ "an internal mismatch--please report a bug",
+ ct->ct_name);
}
- else
- {
- PyErr_Format(PyExc_SystemError,
- "initializer for ctype '%s' is correct, but we get "
- "an internal mismatch--please report a bug",
- ct->ct_name);
- }
}
else
PyErr_Format(PyExc_TypeError,
"initializer for ctype '%s' must be a %s, "
"not %.200s",
- ct->ct_name, expected, Py_TYPE(init)->tp_name);
+ ct->ct_name, expected, Py_TYPE(init)->tp_name);
return -1;
}
@@ -1318,7 +1318,7 @@ static int /* forward */
convert_from_object_bitfield(char *data, CFieldObject *cf, PyObject *init);
static Py_ssize_t
-get_new_array_length(CTypeDescrObject *ctitem, PyObject **pvalue)
+get_new_array_length(CTypeDescrObject *ctitem, PyObject **pvalue)
{
PyObject *value = *pvalue;
@@ -1331,24 +1331,24 @@ get_new_array_length(CTypeDescrObject *ctitem, PyObject **pvalue)
}
else if (PyUnicode_Check(value)) {
/* from a unicode, we add the null terminator */
- int length;
- if (ctitem->ct_size == 2)
- length = _my_PyUnicode_SizeAsChar16(value);
- else
- length = _my_PyUnicode_SizeAsChar32(value);
- return length + 1;
+ int length;
+ if (ctitem->ct_size == 2)
+ length = _my_PyUnicode_SizeAsChar16(value);
+ else
+ length = _my_PyUnicode_SizeAsChar32(value);
+ return length + 1;
}
else {
Py_ssize_t explicitlength;
explicitlength = PyNumber_AsSsize_t(value, PyExc_OverflowError);
if (explicitlength < 0) {
- if (PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_TypeError))
- PyErr_Format(PyExc_TypeError,
- "expected new array length or list/tuple/str, "
- "not %.200s", Py_TYPE(value)->tp_name);
- }
- else
+ if (PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError))
+ PyErr_Format(PyExc_TypeError,
+ "expected new array length or list/tuple/str, "
+ "not %.200s", Py_TYPE(value)->tp_name);
+ }
+ else
PyErr_SetString(PyExc_ValueError, "negative array length");
return -1;
}
@@ -1368,46 +1368,46 @@ convert_field_from_object(char *data, CFieldObject *cf, PyObject *value)
}
static int
-add_varsize_length(Py_ssize_t offset, Py_ssize_t itemsize,
- Py_ssize_t varsizelength, Py_ssize_t *optvarsize)
-{
- /* update '*optvarsize' to account for an array of 'varsizelength'
- elements, each of size 'itemsize', that starts at 'offset'. */
- Py_ssize_t size = ADD_WRAPAROUND(offset,
- MUL_WRAPAROUND(itemsize, varsizelength));
- if (size < 0 ||
- ((size - offset) / itemsize) != varsizelength) {
- PyErr_SetString(PyExc_OverflowError,
- "array size would overflow a Py_ssize_t");
- return -1;
- }
- if (size > *optvarsize)
- *optvarsize = size;
- return 0;
-}
-
-static int
-convert_struct_from_object(char *data, CTypeDescrObject *ct, PyObject *init,
- Py_ssize_t *optvarsize); /* forward */
-
-static int
+add_varsize_length(Py_ssize_t offset, Py_ssize_t itemsize,
+ Py_ssize_t varsizelength, Py_ssize_t *optvarsize)
+{
+ /* update '*optvarsize' to account for an array of 'varsizelength'
+ elements, each of size 'itemsize', that starts at 'offset'. */
+ Py_ssize_t size = ADD_WRAPAROUND(offset,
+ MUL_WRAPAROUND(itemsize, varsizelength));
+ if (size < 0 ||
+ ((size - offset) / itemsize) != varsizelength) {
+ PyErr_SetString(PyExc_OverflowError,
+ "array size would overflow a Py_ssize_t");
+ return -1;
+ }
+ if (size > *optvarsize)
+ *optvarsize = size;
+ return 0;
+}
+
+static int
+convert_struct_from_object(char *data, CTypeDescrObject *ct, PyObject *init,
+ Py_ssize_t *optvarsize); /* forward */
+
+static int
convert_vfield_from_object(char *data, CFieldObject *cf, PyObject *value,
Py_ssize_t *optvarsize)
{
/* a special case for var-sized C99 arrays */
if ((cf->cf_type->ct_flags & CT_ARRAY) && cf->cf_type->ct_size < 0) {
- Py_ssize_t varsizelength = get_new_array_length(
- cf->cf_type->ct_itemdescr, &value);
+ Py_ssize_t varsizelength = get_new_array_length(
+ cf->cf_type->ct_itemdescr, &value);
if (varsizelength < 0)
return -1;
if (optvarsize != NULL) {
/* in this mode, the only purpose of this function is to compute
the real size of the structure from a var-sized C99 array */
assert(data == NULL);
- return add_varsize_length(cf->cf_offset,
- cf->cf_type->ct_itemdescr->ct_size,
- varsizelength,
- optvarsize);
+ return add_varsize_length(cf->cf_offset,
+ cf->cf_type->ct_itemdescr->ct_size,
+ varsizelength,
+ optvarsize);
}
/* if 'value' was only an integer, get_new_array_length() returns
it and convert 'value' to be None. Detect if this was the case,
@@ -1416,44 +1416,44 @@ convert_vfield_from_object(char *data, CFieldObject *cf, PyObject *value,
if (value == Py_None)
return 0;
}
- if (optvarsize == NULL) {
+ if (optvarsize == NULL) {
return convert_field_from_object(data, cf, value);
- }
- else if ((cf->cf_type->ct_flags & CT_WITH_VAR_ARRAY) != 0 &&
- !CData_Check(value)) {
- Py_ssize_t subsize = cf->cf_type->ct_size;
- if (convert_struct_from_object(NULL, cf->cf_type, value, &subsize) < 0)
- return -1;
- return add_varsize_length(cf->cf_offset, 1, subsize, optvarsize);
- }
+ }
+ else if ((cf->cf_type->ct_flags & CT_WITH_VAR_ARRAY) != 0 &&
+ !CData_Check(value)) {
+ Py_ssize_t subsize = cf->cf_type->ct_size;
+ if (convert_struct_from_object(NULL, cf->cf_type, value, &subsize) < 0)
+ return -1;
+ return add_varsize_length(cf->cf_offset, 1, subsize, optvarsize);
+ }
else
return 0;
}
static int
-must_be_array_of_zero_or_one(const char *data, Py_ssize_t n)
-{
- Py_ssize_t i;
- for (i = 0; i < n; i++) {
- if (((unsigned char)data[i]) > 1) {
- PyErr_SetString(PyExc_ValueError,
- "an array of _Bool can only contain \\x00 or \\x01");
- return -1;
- }
- }
- return 0;
-}
-
-static Py_ssize_t
-get_array_length(CDataObject *cd)
-{
- if (cd->c_type->ct_length < 0)
- return ((CDataObject_own_length *)cd)->length;
- else
- return cd->c_type->ct_length;
-}
-
-static int
+must_be_array_of_zero_or_one(const char *data, Py_ssize_t n)
+{
+ Py_ssize_t i;
+ for (i = 0; i < n; i++) {
+ if (((unsigned char)data[i]) > 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "an array of _Bool can only contain \\x00 or \\x01");
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static Py_ssize_t
+get_array_length(CDataObject *cd)
+{
+ if (cd->c_type->ct_length < 0)
+ return ((CDataObject_own_length *)cd)->length;
+ else
+ return cd->c_type->ct_length;
+}
+
+static int
convert_array_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
{
/* used by convert_from_object(), and also to decode lists/tuples/unicodes
@@ -1500,9 +1500,9 @@ convert_array_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
if (n != ct->ct_length)
n++;
srcdata = PyBytes_AS_STRING(init);
- if (ctitem->ct_flags & CT_IS_BOOL)
- if (must_be_array_of_zero_or_one(srcdata, n) < 0)
- return -1;
+ if (ctitem->ct_flags & CT_IS_BOOL)
+ if (must_be_array_of_zero_or_one(srcdata, n) < 0)
+ return -1;
memcpy(data, srcdata, n);
return 0;
}
@@ -1512,12 +1512,12 @@ convert_array_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
expected = "unicode or list or tuple";
goto cannot_convert;
}
-
- if (ctitem->ct_size == 4)
- n = _my_PyUnicode_SizeAsChar32(init);
- else
- n = _my_PyUnicode_SizeAsChar16(init);
-
+
+ if (ctitem->ct_size == 4)
+ n = _my_PyUnicode_SizeAsChar32(init);
+ else
+ n = _my_PyUnicode_SizeAsChar16(init);
+
if (ct->ct_length >= 0 && n > ct->ct_length) {
PyErr_Format(PyExc_IndexError,
"initializer unicode is too long for '%s' "
@@ -1526,10 +1526,10 @@ convert_array_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
}
if (n != ct->ct_length)
n++;
- if (ctitem->ct_size == 4)
- return _my_PyUnicode_AsChar32(init, (cffi_char32_t *)data, n);
- else
- return _my_PyUnicode_AsChar16(init, (cffi_char16_t *)data, n);
+ if (ctitem->ct_size == 4)
+ return _my_PyUnicode_AsChar32(init, (cffi_char32_t *)data, n);
+ else
+ return _my_PyUnicode_AsChar16(init, (cffi_char16_t *)data, n);
}
}
else {
@@ -1538,24 +1538,24 @@ convert_array_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
}
cannot_convert:
- if ((ct->ct_flags & CT_ARRAY) && CData_Check(init))
- {
- CDataObject *cd = (CDataObject *)init;
- if (cd->c_type == ct)
- {
- Py_ssize_t n = get_array_length(cd);
- memcpy(data, cd->c_data, n * ctitem->ct_size);
- return 0;
- }
- }
- return _convert_error(init, ct, expected);
+ if ((ct->ct_flags & CT_ARRAY) && CData_Check(init))
+ {
+ CDataObject *cd = (CDataObject *)init;
+ if (cd->c_type == ct)
+ {
+ Py_ssize_t n = get_array_length(cd);
+ memcpy(data, cd->c_data, n * ctitem->ct_size);
+ return 0;
+ }
+ }
+ return _convert_error(init, ct, expected);
}
static int
convert_struct_from_object(char *data, CTypeDescrObject *ct, PyObject *init,
Py_ssize_t *optvarsize)
{
- /* does not accept 'init' being already a CData */
+ /* does not accept 'init' being already a CData */
const char *expected;
if (force_lazy_struct(ct) <= 0) {
@@ -1602,7 +1602,7 @@ convert_struct_from_object(char *data, CTypeDescrObject *ct, PyObject *init,
}
expected = optvarsize == NULL ? "list or tuple or dict or struct-cdata"
: "list or tuple or dict";
- return _convert_error(init, ct, expected);
+ return _convert_error(init, ct, expected);
}
#ifdef __GNUC__
@@ -1652,8 +1652,8 @@ convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
/* for backward compatibility, accept "char *" as either
source of target. This is not what C does, though,
so emit a warning that will eventually turn into an
- error. The warning is turned off if both types are
- pointers to single bytes. */
+ error. The warning is turned off if both types are
+ pointers to single bytes. */
char *msg = (ct->ct_flags & CT_IS_VOIDCHAR_PTR ?
"implicit cast to 'char *' from a different pointer type: "
"will be forbidden in the future (check that the types "
@@ -1663,12 +1663,12 @@ convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
"will be forbidden in the future (check that the types "
"are as you expect; use an explicit ffi.cast() if they "
"are correct)");
- if ((ct->ct_flags & ctinit->ct_flags & CT_POINTER) &&
- ct->ct_itemdescr->ct_size == 1 &&
- ctinit->ct_itemdescr->ct_size == 1) {
- /* no warning */
- }
- else if (PyErr_WarnEx(PyExc_UserWarning, msg, 1))
+ if ((ct->ct_flags & ctinit->ct_flags & CT_POINTER) &&
+ ct->ct_itemdescr->ct_size == 1 &&
+ ctinit->ct_itemdescr->ct_size == 1) {
+ /* no warning */
+ }
+ else if (PyErr_WarnEx(PyExc_UserWarning, msg, 1))
return -1;
}
else {
@@ -1695,15 +1695,15 @@ convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
unsigned PY_LONG_LONG value = _my_PyLong_AsUnsignedLongLong(init, 1);
if (value == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
return -1;
- if (ct->ct_flags & CT_IS_BOOL) {
- if (value > 1ULL) /* value != 0 && value != 1 */
+ if (ct->ct_flags & CT_IS_BOOL) {
+ if (value > 1ULL) /* value != 0 && value != 1 */
+ goto overflow;
+ }
+ else {
+ write_raw_integer_data(buf, value, ct->ct_size);
+ if (value != read_raw_unsigned_data(buf, ct->ct_size))
goto overflow;
- }
- else {
- write_raw_integer_data(buf, value, ct->ct_size);
- if (value != read_raw_unsigned_data(buf, ct->ct_size))
- goto overflow;
- }
+ }
write_raw_integer_data(data, value, ct->ct_size);
return 0;
}
@@ -1729,29 +1729,29 @@ convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
return 0;
}
if (ct->ct_flags & CT_PRIMITIVE_CHAR) {
- switch (ct->ct_size) {
- case sizeof(char): {
+ switch (ct->ct_size) {
+ case sizeof(char): {
int res = _convert_to_char(init);
if (res < 0)
return -1;
data[0] = res;
return 0;
}
- case 2: {
- cffi_char16_t res = _convert_to_char16_t(init);
- if (res == (cffi_char16_t)-1 && PyErr_Occurred())
+ case 2: {
+ cffi_char16_t res = _convert_to_char16_t(init);
+ if (res == (cffi_char16_t)-1 && PyErr_Occurred())
+ return -1;
+ *(cffi_char16_t *)data = res;
+ return 0;
+ }
+ case 4: {
+ cffi_char32_t res = _convert_to_char32_t(init);
+ if (res == (cffi_char32_t)-1 && PyErr_Occurred())
return -1;
- *(cffi_char16_t *)data = res;
+ *(cffi_char32_t *)data = res;
return 0;
}
- case 4: {
- cffi_char32_t res = _convert_to_char32_t(init);
- if (res == (cffi_char32_t)-1 && PyErr_Occurred())
- return -1;
- *(cffi_char32_t *)data = res;
- return 0;
- }
- }
+ }
}
if (ct->ct_flags & (CT_STRUCT|CT_UNION)) {
@@ -1763,13 +1763,13 @@ convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
}
return convert_struct_from_object(data, ct, init, NULL);
}
- if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
- Py_complex value = PyComplex_AsCComplex(init);
- if (PyErr_Occurred())
- return -1;
- write_raw_complex_data(data, value, ct->ct_size);
- return 0;
- }
+ if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
+ Py_complex value = PyComplex_AsCComplex(init);
+ if (PyErr_Occurred())
+ return -1;
+ write_raw_complex_data(data, value, ct->ct_size);
+ return 0;
+ }
PyErr_Format(PyExc_SystemError,
"convert_from_object: '%s'", ct->ct_name);
return -1;
@@ -1778,7 +1778,7 @@ convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
return _convert_overflow(init, ct->ct_name);
cannot_convert:
- return _convert_error(init, ct, expected);
+ return _convert_error(init, ct, expected);
}
static int
@@ -1890,7 +1890,7 @@ static void cdataowning_dealloc(CDataObject *cd)
assert(!(cd->c_type->ct_flags & (CT_IS_VOID_PTR | CT_FUNCTIONPTR)));
if (cd->c_type->ct_flags & CT_IS_PTR_TO_OWNED) {
- /* for ffi.new("struct *") */
+ /* for ffi.new("struct *") */
Py_DECREF(((CDataObject_own_structptr *)cd)->structobj);
}
#if defined(CFFI_MEM_DEBUG) || defined(CFFI_MEM_LEAK)
@@ -1918,31 +1918,31 @@ static void cdataowninggc_dealloc(CDataObject *cd)
Py_DECREF(x);
}
else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) { /* a callback */
- ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
+ ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
PyObject *args = (PyObject *)(closure->user_data);
Py_XDECREF(args);
-#if CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE
- if (CFFI_CHECK_FFI_CLOSURE_ALLOC) {
- ffi_closure_free(closure);
- } else
-#endif
- cffi_closure_free(closure);
+#if CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE
+ if (CFFI_CHECK_FFI_CLOSURE_ALLOC) {
+ ffi_closure_free(closure);
+ } else
+#endif
+ cffi_closure_free(closure);
}
- else {
- Py_FatalError("cdata CDataOwningGC_Type with unexpected type flags");
+ else {
+ Py_FatalError("cdata CDataOwningGC_Type with unexpected type flags");
}
cdata_dealloc(cd);
}
-static void cdatafrombuf_dealloc(CDataObject *cd)
-{
- Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview;
- cdata_dealloc(cd);
-
- PyBuffer_Release(view);
- PyObject_Free(view);
-}
-
+static void cdatafrombuf_dealloc(CDataObject *cd)
+{
+ Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview;
+ cdata_dealloc(cd);
+
+ PyBuffer_Release(view);
+ PyObject_Free(view);
+}
+
static int cdataowninggc_traverse(CDataObject *cd, visitproc visit, void *arg)
{
if (cd->c_type->ct_flags & CT_IS_VOID_PTR) { /* a handle */
@@ -1950,20 +1950,20 @@ static int cdataowninggc_traverse(CDataObject *cd, visitproc visit, void *arg)
Py_VISIT(x);
}
else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) { /* a callback */
- ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
+ ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
PyObject *args = (PyObject *)(closure->user_data);
Py_VISIT(args);
}
return 0;
}
-static int cdatafrombuf_traverse(CDataObject *cd, visitproc visit, void *arg)
-{
- Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview;
- Py_VISIT(view->obj);
- return 0;
-}
-
+static int cdatafrombuf_traverse(CDataObject *cd, visitproc visit, void *arg)
+{
+ Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview;
+ Py_VISIT(view->obj);
+ return 0;
+}
+
static int cdataowninggc_clear(CDataObject *cd)
{
if (cd->c_type->ct_flags & CT_IS_VOID_PTR) { /* a handle */
@@ -1974,7 +1974,7 @@ static int cdataowninggc_clear(CDataObject *cd)
Py_DECREF(x);
}
else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) { /* a callback */
- ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
+ ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
PyObject *args = (PyObject *)(closure->user_data);
closure->user_data = NULL;
Py_XDECREF(args);
@@ -1982,16 +1982,16 @@ static int cdataowninggc_clear(CDataObject *cd)
return 0;
}
-static int cdatafrombuf_clear(CDataObject *cd)
-{
- Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview;
- PyBuffer_Release(view);
- return 0;
-}
-
+static int cdatafrombuf_clear(CDataObject *cd)
+{
+ Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview;
+ PyBuffer_Release(view);
+ return 0;
+}
+
/* forward */
-static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
- char *objdescr, PyObject *obj,
+static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
+ char *objdescr, PyObject *obj,
char *extra_error_line);
@@ -2011,15 +2011,15 @@ static void gcp_finalize(PyObject *destructor, PyObject *origobj)
Py_DECREF(result);
}
else {
- PyObject *t, *v, *tb;
- PyErr_Fetch(&t, &v, &tb);
- /* Don't use error capture here, because it is very much
- * like errors at __del__(), and these ones are not captured
- * either */
- /* ecap = _cffi_start_error_capture(); */
- _my_PyErr_WriteUnraisable(t, v, tb, "From callback for ffi.gc ",
+ PyObject *t, *v, *tb;
+ PyErr_Fetch(&t, &v, &tb);
+ /* Don't use error capture here, because it is very much
+ * like errors at __del__(), and these ones are not captured
+ * either */
+ /* ecap = _cffi_start_error_capture(); */
+ _my_PyErr_WriteUnraisable(t, v, tb, "From callback for ffi.gc ",
origobj, NULL);
- /* _cffi_stop_error_capture(ecap); */
+ /* _cffi_stop_error_capture(ecap); */
}
Py_DECREF(destructor);
@@ -2167,35 +2167,35 @@ static Py_ssize_t _cdata_var_byte_size(CDataObject *cd)
return -1;
}
-static PyObject *_frombuf_repr(CDataObject *cd, const char *cd_type_name)
-{
- Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview;
- const char *obj_tp_name;
- if (view->obj == NULL) {
- return PyText_FromFormat(
- "<cdata '%s' buffer RELEASED>",
- cd_type_name);
- }
-
- obj_tp_name = Py_TYPE(view->obj)->tp_name;
- if (cd->c_type->ct_flags & CT_ARRAY)
- {
- Py_ssize_t buflen = get_array_length(cd);
- return PyText_FromFormat(
- "<cdata '%s' buffer len %zd from '%.200s' object>",
- cd_type_name,
- buflen,
- obj_tp_name);
- }
- else
- {
- return PyText_FromFormat(
- "<cdata '%s' buffer from '%.200s' object>",
- cd_type_name,
- obj_tp_name);
- }
-}
-
+static PyObject *_frombuf_repr(CDataObject *cd, const char *cd_type_name)
+{
+ Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview;
+ const char *obj_tp_name;
+ if (view->obj == NULL) {
+ return PyText_FromFormat(
+ "<cdata '%s' buffer RELEASED>",
+ cd_type_name);
+ }
+
+ obj_tp_name = Py_TYPE(view->obj)->tp_name;
+ if (cd->c_type->ct_flags & CT_ARRAY)
+ {
+ Py_ssize_t buflen = get_array_length(cd);
+ return PyText_FromFormat(
+ "<cdata '%s' buffer len %zd from '%.200s' object>",
+ cd_type_name,
+ buflen,
+ obj_tp_name);
+ }
+ else
+ {
+ return PyText_FromFormat(
+ "<cdata '%s' buffer from '%.200s' object>",
+ cd_type_name,
+ obj_tp_name);
+ }
+}
+
static PyObject *cdataowning_repr(CDataObject *cd)
{
Py_ssize_t size = _cdata_var_byte_size(cd);
@@ -2218,21 +2218,21 @@ static PyObject *cdataowninggc_repr(CDataObject *cd)
return _cdata_repr2(cd, "handle to", x);
}
else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) { /* a callback */
- ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
- PyObject *args = (PyObject *)closure->user_data;
+ ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
+ PyObject *args = (PyObject *)closure->user_data;
if (args == NULL)
return cdata_repr(cd);
else
return _cdata_repr2(cd, "calling", PyTuple_GET_ITEM(args, 1));
}
- return cdataowning_repr(cd); /* but should be unreachable */
+ return cdataowning_repr(cd); /* but should be unreachable */
+}
+
+static PyObject *cdatafrombuf_repr(CDataObject *cd)
+{
+ return _frombuf_repr(cd, cd->c_type->ct_name);
}
-static PyObject *cdatafrombuf_repr(CDataObject *cd)
-{
- return _frombuf_repr(cd, cd->c_type->ct_name);
-}
-
static int cdata_nonzero(CDataObject *cd)
{
if (cd->c_type->ct_flags & CT_PRIMITIVE_ANY) {
@@ -2246,11 +2246,11 @@ static int cdata_nonzero(CDataObject *cd)
return read_raw_longdouble_data(cd->c_data) != 0.0;
return read_raw_float_data(cd->c_data, cd->c_type->ct_size) != 0.0;
}
- if (cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) {
- Py_complex value = read_raw_complex_data(cd->c_data,
- cd->c_type->ct_size);
- return value.real != 0.0 || value.imag != 0.0;
- }
+ if (cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) {
+ Py_complex value = read_raw_complex_data(cd->c_data,
+ cd->c_type->ct_size);
+ return value.real != 0.0 || value.imag != 0.0;
+ }
}
return cd->c_data != NULL;
}
@@ -2267,26 +2267,26 @@ static PyObject *cdata_int(CDataObject *cd)
return PyInt_FromLong(value);
}
if (cd->c_type->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED)) {
- PyObject *result = convert_to_object(cd->c_data, cd->c_type);
- if (result != NULL && PyBool_Check(result))
- result = PyInt_FromLong(PyInt_AsLong(result));
- return result;
+ PyObject *result = convert_to_object(cd->c_data, cd->c_type);
+ if (result != NULL && PyBool_Check(result))
+ result = PyInt_FromLong(PyInt_AsLong(result));
+ return result;
}
else if (cd->c_type->ct_flags & CT_PRIMITIVE_CHAR) {
/*READ(cd->c_data, cd->c_type->ct_size)*/
- switch (cd->c_type->ct_size) {
- case sizeof(char):
+ switch (cd->c_type->ct_size) {
+ case sizeof(char):
return PyInt_FromLong((unsigned char)cd->c_data[0]);
- case 2:
- return PyInt_FromLong((long)*(cffi_char16_t *)cd->c_data);
- case 4:
- if (cd->c_type->ct_flags & CT_IS_SIGNED_WCHAR)
- return PyInt_FromLong((long)*(int32_t *)cd->c_data);
- else if (sizeof(long) > 4)
- return PyInt_FromLong(*(uint32_t *)cd->c_data);
- else
- return PyLong_FromUnsignedLong(*(uint32_t *)cd->c_data);
- }
+ case 2:
+ return PyInt_FromLong((long)*(cffi_char16_t *)cd->c_data);
+ case 4:
+ if (cd->c_type->ct_flags & CT_IS_SIGNED_WCHAR)
+ return PyInt_FromLong((long)*(int32_t *)cd->c_data);
+ else if (sizeof(long) > 4)
+ return PyInt_FromLong(*(uint32_t *)cd->c_data);
+ else
+ return PyLong_FromUnsignedLong(*(uint32_t *)cd->c_data);
+ }
}
else if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) {
PyObject *o = cdata_float(cd);
@@ -2336,101 +2336,101 @@ static PyObject *cdata_float(CDataObject *cd)
static PyObject *cdata_richcompare(PyObject *v, PyObject *w, int op)
{
- int v_is_ptr, w_is_ptr;
+ int v_is_ptr, w_is_ptr;
PyObject *pyres;
assert(CData_Check(v));
-
- /* Comparisons involving a primitive cdata work differently than
- * comparisons involving a struct/array/pointer.
- *
- * If v or w is a struct/array/pointer, then the other must be too
- * (otherwise we return NotImplemented and leave the case to
- * Python). If both are, then we compare the addresses.
- *
- * If v and/or w is a primitive cdata, then we convert the cdata(s)
- * to regular Python objects and redo the comparison there.
- */
-
- v_is_ptr = !(((CDataObject *)v)->c_type->ct_flags & CT_PRIMITIVE_ANY);
- w_is_ptr = CData_Check(w) &&
- !(((CDataObject *)w)->c_type->ct_flags & CT_PRIMITIVE_ANY);
-
- if (v_is_ptr && w_is_ptr) {
- int res;
- char *v_cdata = ((CDataObject *)v)->c_data;
- char *w_cdata = ((CDataObject *)w)->c_data;
-
- switch (op) {
- case Py_EQ: res = (v_cdata == w_cdata); break;
- case Py_NE: res = (v_cdata != w_cdata); break;
- case Py_LT: res = (v_cdata < w_cdata); break;
- case Py_LE: res = (v_cdata <= w_cdata); break;
- case Py_GT: res = (v_cdata > w_cdata); break;
- case Py_GE: res = (v_cdata >= w_cdata); break;
- default: res = -1;
- }
- pyres = res ? Py_True : Py_False;
- }
- else if (v_is_ptr || w_is_ptr) {
+
+ /* Comparisons involving a primitive cdata work differently than
+ * comparisons involving a struct/array/pointer.
+ *
+ * If v or w is a struct/array/pointer, then the other must be too
+ * (otherwise we return NotImplemented and leave the case to
+ * Python). If both are, then we compare the addresses.
+ *
+ * If v and/or w is a primitive cdata, then we convert the cdata(s)
+ * to regular Python objects and redo the comparison there.
+ */
+
+ v_is_ptr = !(((CDataObject *)v)->c_type->ct_flags & CT_PRIMITIVE_ANY);
+ w_is_ptr = CData_Check(w) &&
+ !(((CDataObject *)w)->c_type->ct_flags & CT_PRIMITIVE_ANY);
+
+ if (v_is_ptr && w_is_ptr) {
+ int res;
+ char *v_cdata = ((CDataObject *)v)->c_data;
+ char *w_cdata = ((CDataObject *)w)->c_data;
+
+ switch (op) {
+ case Py_EQ: res = (v_cdata == w_cdata); break;
+ case Py_NE: res = (v_cdata != w_cdata); break;
+ case Py_LT: res = (v_cdata < w_cdata); break;
+ case Py_LE: res = (v_cdata <= w_cdata); break;
+ case Py_GT: res = (v_cdata > w_cdata); break;
+ case Py_GE: res = (v_cdata >= w_cdata); break;
+ default: res = -1;
+ }
+ pyres = res ? Py_True : Py_False;
+ }
+ else if (v_is_ptr || w_is_ptr) {
pyres = Py_NotImplemented;
}
- else {
- PyObject *aa[2];
- int i;
-
- aa[0] = v; Py_INCREF(v);
- aa[1] = w; Py_INCREF(w);
- pyres = NULL;
-
- for (i = 0; i < 2; i++) {
- v = aa[i];
- if (!CData_Check(v))
- continue;
- w = convert_to_object(((CDataObject *)v)->c_data,
- ((CDataObject *)v)->c_type);
- if (w == NULL)
- goto error;
- if (CData_Check(w)) {
- Py_DECREF(w);
- PyErr_Format(PyExc_NotImplementedError,
- "cannot use <cdata '%s'> in a comparison",
- ((CDataObject *)v)->c_type->ct_name);
- goto error;
- }
- aa[i] = w;
- Py_DECREF(v);
- }
- pyres = PyObject_RichCompare(aa[0], aa[1], op);
- error:
- Py_DECREF(aa[1]);
- Py_DECREF(aa[0]);
- return pyres;
- }
+ else {
+ PyObject *aa[2];
+ int i;
+
+ aa[0] = v; Py_INCREF(v);
+ aa[1] = w; Py_INCREF(w);
+ pyres = NULL;
+
+ for (i = 0; i < 2; i++) {
+ v = aa[i];
+ if (!CData_Check(v))
+ continue;
+ w = convert_to_object(((CDataObject *)v)->c_data,
+ ((CDataObject *)v)->c_type);
+ if (w == NULL)
+ goto error;
+ if (CData_Check(w)) {
+ Py_DECREF(w);
+ PyErr_Format(PyExc_NotImplementedError,
+ "cannot use <cdata '%s'> in a comparison",
+ ((CDataObject *)v)->c_type->ct_name);
+ goto error;
+ }
+ aa[i] = w;
+ Py_DECREF(v);
+ }
+ pyres = PyObject_RichCompare(aa[0], aa[1], op);
+ error:
+ Py_DECREF(aa[1]);
+ Py_DECREF(aa[0]);
+ return pyres;
+ }
Py_INCREF(pyres);
return pyres;
}
-#if PY_MAJOR_VERSION < 3
-typedef long Py_hash_t;
-#endif
-
-static Py_hash_t cdata_hash(PyObject *v)
-{
- if (((CDataObject *)v)->c_type->ct_flags & CT_PRIMITIVE_ANY) {
- PyObject *vv = convert_to_object(((CDataObject *)v)->c_data,
- ((CDataObject *)v)->c_type);
- if (vv == NULL)
- return -1;
- if (!CData_Check(vv)) {
- Py_hash_t hash = PyObject_Hash(vv);
- Py_DECREF(vv);
- return hash;
- }
- Py_DECREF(vv);
- }
- return _Py_HashPointer(((CDataObject *)v)->c_data);
+#if PY_MAJOR_VERSION < 3
+typedef long Py_hash_t;
+#endif
+
+static Py_hash_t cdata_hash(PyObject *v)
+{
+ if (((CDataObject *)v)->c_type->ct_flags & CT_PRIMITIVE_ANY) {
+ PyObject *vv = convert_to_object(((CDataObject *)v)->c_data,
+ ((CDataObject *)v)->c_type);
+ if (vv == NULL)
+ return -1;
+ if (!CData_Check(vv)) {
+ Py_hash_t hash = PyObject_Hash(vv);
+ Py_DECREF(vv);
+ return hash;
+ }
+ Py_DECREF(vv);
+ }
+ return _Py_HashPointer(((CDataObject *)v)->c_data);
}
static Py_ssize_t
@@ -2472,7 +2472,7 @@ _cdata_get_indexed_ptr(CDataObject *cd, PyObject *key)
else if (cd->c_type->ct_flags & CT_ARRAY) {
if (i < 0) {
PyErr_SetString(PyExc_IndexError,
- "negative index");
+ "negative index");
return NULL;
}
if (i >= get_array_length(cd)) {
@@ -2525,7 +2525,7 @@ _cdata_getslicearg(CDataObject *cd, PySliceObject *slice, Py_ssize_t bounds[])
if (ct->ct_flags & CT_ARRAY) {
if (start < 0) {
PyErr_SetString(PyExc_IndexError,
- "negative index");
+ "negative index");
return NULL;
}
if (stop > get_array_length(cd)) {
@@ -2813,26 +2813,26 @@ cdata_sub(PyObject *v, PyObject *w)
return _cdata_add_or_sub(v, w, -1);
}
-static void
-_cdata_attr_errmsg(char *errmsg, CDataObject *cd, PyObject *attr)
-{
- const char *text;
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return;
- PyErr_Clear();
- text = PyText_AsUTF8(attr);
- if (text == NULL)
- return;
- PyErr_Format(PyExc_AttributeError, errmsg, cd->c_type->ct_name, text);
-}
-
+static void
+_cdata_attr_errmsg(char *errmsg, CDataObject *cd, PyObject *attr)
+{
+ const char *text;
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
+ return;
+ PyErr_Clear();
+ text = PyText_AsUTF8(attr);
+ if (text == NULL)
+ return;
+ PyErr_Format(PyExc_AttributeError, errmsg, cd->c_type->ct_name, text);
+}
+
static PyObject *
cdata_getattro(CDataObject *cd, PyObject *attr)
{
CFieldObject *cf;
CTypeDescrObject *ct = cd->c_type;
- char *errmsg = "cdata '%s' has no attribute '%s'";
- PyObject *x;
+ char *errmsg = "cdata '%s' has no attribute '%s'";
+ PyObject *x;
if (ct->ct_flags & CT_POINTER)
ct = ct->ct_itemdescr;
@@ -2864,19 +2864,19 @@ cdata_getattro(CDataObject *cd, PyObject *attr)
return new_simple_cdata(data,
(CTypeDescrObject *)cf->cf_type->ct_stuff);
}
- errmsg = "cdata '%s' has no field '%s'";
+ errmsg = "cdata '%s' has no field '%s'";
break;
case -1:
return NULL;
default:
- errmsg = "cdata '%s' points to an opaque type: cannot read fields";
+ errmsg = "cdata '%s' points to an opaque type: cannot read fields";
break;
}
}
- x = PyObject_GenericGetAttr((PyObject *)cd, attr);
- if (x == NULL)
- _cdata_attr_errmsg(errmsg, cd, attr);
- return x;
+ x = PyObject_GenericGetAttr((PyObject *)cd, attr);
+ if (x == NULL)
+ _cdata_attr_errmsg(errmsg, cd, attr);
+ return x;
}
static int
@@ -2884,8 +2884,8 @@ cdata_setattro(CDataObject *cd, PyObject *attr, PyObject *value)
{
CFieldObject *cf;
CTypeDescrObject *ct = cd->c_type;
- char *errmsg = "cdata '%s' has no attribute '%s'";
- int x;
+ char *errmsg = "cdata '%s' has no attribute '%s'";
+ int x;
if (ct->ct_flags & CT_POINTER)
ct = ct->ct_itemdescr;
@@ -2905,27 +2905,27 @@ cdata_setattro(CDataObject *cd, PyObject *attr, PyObject *value)
return -1;
}
}
- errmsg = "cdata '%s' has no field '%s'";
+ errmsg = "cdata '%s' has no field '%s'";
break;
case -1:
return -1;
default:
- errmsg = "cdata '%s' points to an opaque type: cannot write fields";
+ errmsg = "cdata '%s' points to an opaque type: cannot write fields";
break;
}
}
- x = PyObject_GenericSetAttr((PyObject *)cd, attr, value);
- if (x < 0)
- _cdata_attr_errmsg(errmsg, cd, attr);
- return x;
+ x = PyObject_GenericSetAttr((PyObject *)cd, attr, value);
+ if (x < 0)
+ _cdata_attr_errmsg(errmsg, cd, attr);
+ return x;
}
static PyObject *
convert_struct_to_owning_object(char *data, CTypeDescrObject *ct); /*forward*/
static cif_description_t *
-fb_prepare_cif(PyObject *fargs, CTypeDescrObject *, Py_ssize_t, ffi_abi);
- /*forward*/
+fb_prepare_cif(PyObject *fargs, CTypeDescrObject *, Py_ssize_t, ffi_abi);
+ /*forward*/
static PyObject *new_primitive_type(const char *name); /*forward*/
@@ -2968,10 +2968,10 @@ _prepare_pointer_call_argument(CTypeDescrObject *ctptr, PyObject *init,
length = PyBytes_GET_SIZE(init) + 1;
#else
*output_data = PyBytes_AS_STRING(init);
- if (ctitem->ct_flags & CT_IS_BOOL)
- if (must_be_array_of_zero_or_one(*output_data,
- PyBytes_GET_SIZE(init)) < 0)
- return -1;
+ if (ctitem->ct_flags & CT_IS_BOOL)
+ if (must_be_array_of_zero_or_one(*output_data,
+ PyBytes_GET_SIZE(init)) < 0)
+ return -1;
return 0;
#endif
}
@@ -2983,11 +2983,11 @@ _prepare_pointer_call_argument(CTypeDescrObject *ctptr, PyObject *init,
}
else if (PyUnicode_Check(init)) {
/* from a unicode, we add the null terminator */
- if (ctitem->ct_size == 2)
- length = _my_PyUnicode_SizeAsChar16(init);
- else
- length = _my_PyUnicode_SizeAsChar32(init);
- length += 1;
+ if (ctitem->ct_size == 2)
+ length = _my_PyUnicode_SizeAsChar16(init);
+ else
+ length = _my_PyUnicode_SizeAsChar32(init);
+ length += 1;
}
else if ((ctitem->ct_flags & CT_IS_FILE) && PyFile_Check(init)) {
*output_data = (char *)PyFile_AsFile(init);
@@ -3028,22 +3028,22 @@ cdata_call(CDataObject *cd, PyObject *args, PyObject *kwds)
CTypeDescrObject *fresult;
char *resultdata;
char *errormsg;
- struct freeme_s {
- struct freeme_s *next;
- union_alignment alignment;
- } *freeme = NULL;
+ struct freeme_s {
+ struct freeme_s *next;
+ union_alignment alignment;
+ } *freeme = NULL;
if (!(cd->c_type->ct_flags & CT_FUNCTIONPTR)) {
PyErr_Format(PyExc_TypeError, "cdata '%s' is not callable",
cd->c_type->ct_name);
return NULL;
}
- if (cd->c_data == NULL) {
- PyErr_Format(PyExc_RuntimeError,
- "cannot call null pointer pointer from cdata '%s'",
- cd->c_type->ct_name);
- return NULL;
- }
+ if (cd->c_data == NULL) {
+ PyErr_Format(PyExc_RuntimeError,
+ "cannot call null pointer pointer from cdata '%s'",
+ cd->c_type->ct_name);
+ return NULL;
+ }
if (kwds != NULL && PyDict_Size(kwds) != 0) {
PyErr_SetString(PyExc_TypeError,
"a cdata function cannot be called with keyword arguments");
@@ -3118,7 +3118,7 @@ cdata_call(CDataObject *cd, PyObject *args, PyObject *kwds)
#else
fabi = PyLong_AS_LONG(PyTuple_GET_ITEM(signature, 0));
#endif
- cif_descr = fb_prepare_cif(fvarargs, fresult, nargs_declared, fabi);
+ cif_descr = fb_prepare_cif(fvarargs, fresult, nargs_declared, fabi);
if (cif_descr == NULL)
goto error;
}
@@ -3152,21 +3152,21 @@ cdata_call(CDataObject *cd, PyObject *args, PyObject *kwds)
else if (datasize < 0)
goto error;
else {
- if (datasize <= 512) {
- tmpbuf = alloca(datasize);
- }
- else {
- struct freeme_s *fp = (struct freeme_s *)PyObject_Malloc(
- offsetof(struct freeme_s, alignment) +
- (size_t)datasize);
- if (fp == NULL) {
- PyErr_NoMemory();
- goto error;
- }
- fp->next = freeme;
- freeme = fp;
- tmpbuf = (char *)&fp->alignment;
- }
+ if (datasize <= 512) {
+ tmpbuf = alloca(datasize);
+ }
+ else {
+ struct freeme_s *fp = (struct freeme_s *)PyObject_Malloc(
+ offsetof(struct freeme_s, alignment) +
+ (size_t)datasize);
+ if (fp == NULL) {
+ PyErr_NoMemory();
+ goto error;
+ }
+ fp->next = freeme;
+ freeme = fp;
+ tmpbuf = (char *)&fp->alignment;
+ }
memset(tmpbuf, 0, datasize);
*(char **)data = tmpbuf;
if (convert_array_from_object(tmpbuf, argtype, obj) < 0)
@@ -3211,11 +3211,11 @@ cdata_call(CDataObject *cd, PyObject *args, PyObject *kwds)
/* fall-through */
error:
- while (freeme != NULL) {
- void *p = (void *)freeme;
- freeme = freeme->next;
- PyObject_Free(p);
- }
+ while (freeme != NULL) {
+ void *p = (void *)freeme;
+ freeme = freeme->next;
+ PyObject_Free(p);
+ }
if (buffer)
PyObject_Free(buffer);
if (fvarargs != NULL) {
@@ -3247,92 +3247,92 @@ static PyObject *cdata_dir(PyObject *cd, PyObject *noarg)
}
}
-static PyObject *cdata_complex(PyObject *cd_, PyObject *noarg)
-{
- CDataObject *cd = (CDataObject *)cd_;
-
- if (cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) {
- Py_complex value = read_raw_complex_data(cd->c_data, cd->c_type->ct_size);
- PyObject *op = PyComplex_FromCComplex(value);
- return op;
- }
- /* <cdata 'float'> or <cdata 'int'> cannot be directly converted by
- calling complex(), just like <cdata 'int'> cannot be directly
- converted by calling float() */
-
- PyErr_Format(PyExc_TypeError, "complex() not supported on cdata '%s'",
- cd->c_type->ct_name);
- return NULL;
-}
-
-static int explicit_release_case(PyObject *cd)
-{
- CTypeDescrObject *ct = ((CDataObject *)cd)->c_type;
- if (Py_TYPE(cd) == &CDataOwning_Type) {
- if ((ct->ct_flags & (CT_POINTER | CT_ARRAY)) != 0) /* ffi.new() */
- return 0;
- }
- else if (Py_TYPE(cd) == &CDataFromBuf_Type) {
- return 1; /* ffi.from_buffer() */
- }
- else if (Py_TYPE(cd) == &CDataGCP_Type) {
- return 2; /* ffi.gc() */
- }
- PyErr_SetString(PyExc_ValueError,
- "only 'cdata' object from ffi.new(), ffi.gc(), ffi.from_buffer() "
- "or ffi.new_allocator()() can be used with the 'with' keyword or "
- "ffi.release()");
- return -1;
-}
-
-static PyObject *cdata_enter(PyObject *cd, PyObject *noarg)
-{
- if (explicit_release_case(cd) < 0) /* only to check the ctype */
- return NULL;
- Py_INCREF(cd);
- return cd;
-}
-
-static PyObject *cdata_exit(PyObject *cd, PyObject *args)
-{
- /* 'args' ignored */
- CTypeDescrObject *ct;
- Py_buffer *view;
- switch (explicit_release_case(cd))
- {
- case 0: /* ffi.new() */
- /* no effect on CPython: raw memory is allocated with the
- same malloc() as the object itself, so it can't be
- released independently. If we use a custom allocator,
- then it's implemented with ffi.gc(). */
- ct = ((CDataObject *)cd)->c_type;
- if (ct->ct_flags & CT_IS_PTR_TO_OWNED) {
- PyObject *x = ((CDataObject_own_structptr *)cd)->structobj;
- if (Py_TYPE(x) == &CDataGCP_Type) {
- /* this is a special case for
- ffi.new_allocator()("struct-or-union *") */
- cdatagcp_finalize((CDataObject_gcp *)x);
- }
- }
- break;
-
- case 1: /* ffi.from_buffer() */
- view = ((CDataObject_frombuf *)cd)->bufferview;
- PyBuffer_Release(view);
- break;
-
- case 2: /* ffi.gc() or ffi.new_allocator()("not-struct-nor-union") */
- /* call the destructor immediately */
- cdatagcp_finalize((CDataObject_gcp *)cd);
- break;
-
- default:
- return NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
-}
-
+static PyObject *cdata_complex(PyObject *cd_, PyObject *noarg)
+{
+ CDataObject *cd = (CDataObject *)cd_;
+
+ if (cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) {
+ Py_complex value = read_raw_complex_data(cd->c_data, cd->c_type->ct_size);
+ PyObject *op = PyComplex_FromCComplex(value);
+ return op;
+ }
+ /* <cdata 'float'> or <cdata 'int'> cannot be directly converted by
+ calling complex(), just like <cdata 'int'> cannot be directly
+ converted by calling float() */
+
+ PyErr_Format(PyExc_TypeError, "complex() not supported on cdata '%s'",
+ cd->c_type->ct_name);
+ return NULL;
+}
+
+static int explicit_release_case(PyObject *cd)
+{
+ CTypeDescrObject *ct = ((CDataObject *)cd)->c_type;
+ if (Py_TYPE(cd) == &CDataOwning_Type) {
+ if ((ct->ct_flags & (CT_POINTER | CT_ARRAY)) != 0) /* ffi.new() */
+ return 0;
+ }
+ else if (Py_TYPE(cd) == &CDataFromBuf_Type) {
+ return 1; /* ffi.from_buffer() */
+ }
+ else if (Py_TYPE(cd) == &CDataGCP_Type) {
+ return 2; /* ffi.gc() */
+ }
+ PyErr_SetString(PyExc_ValueError,
+ "only 'cdata' object from ffi.new(), ffi.gc(), ffi.from_buffer() "
+ "or ffi.new_allocator()() can be used with the 'with' keyword or "
+ "ffi.release()");
+ return -1;
+}
+
+static PyObject *cdata_enter(PyObject *cd, PyObject *noarg)
+{
+ if (explicit_release_case(cd) < 0) /* only to check the ctype */
+ return NULL;
+ Py_INCREF(cd);
+ return cd;
+}
+
+static PyObject *cdata_exit(PyObject *cd, PyObject *args)
+{
+ /* 'args' ignored */
+ CTypeDescrObject *ct;
+ Py_buffer *view;
+ switch (explicit_release_case(cd))
+ {
+ case 0: /* ffi.new() */
+ /* no effect on CPython: raw memory is allocated with the
+ same malloc() as the object itself, so it can't be
+ released independently. If we use a custom allocator,
+ then it's implemented with ffi.gc(). */
+ ct = ((CDataObject *)cd)->c_type;
+ if (ct->ct_flags & CT_IS_PTR_TO_OWNED) {
+ PyObject *x = ((CDataObject_own_structptr *)cd)->structobj;
+ if (Py_TYPE(x) == &CDataGCP_Type) {
+ /* this is a special case for
+ ffi.new_allocator()("struct-or-union *") */
+ cdatagcp_finalize((CDataObject_gcp *)x);
+ }
+ }
+ break;
+
+ case 1: /* ffi.from_buffer() */
+ view = ((CDataObject_frombuf *)cd)->bufferview;
+ PyBuffer_Release(view);
+ break;
+
+ case 2: /* ffi.gc() or ffi.new_allocator()("not-struct-nor-union") */
+ /* call the destructor immediately */
+ cdatagcp_finalize((CDataObject_gcp *)cd);
+ break;
+
+ default:
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
static PyObject *cdata_iter(CDataObject *);
static PyNumberMethods CData_as_number = {
@@ -3382,16 +3382,16 @@ static PyMappingMethods CDataOwn_as_mapping = {
};
static PyMethodDef cdata_methods[] = {
- {"__dir__", cdata_dir, METH_NOARGS},
- {"__complex__", cdata_complex, METH_NOARGS},
- {"__enter__", cdata_enter, METH_NOARGS},
- {"__exit__", cdata_exit, METH_VARARGS},
- {NULL, NULL} /* sentinel */
+ {"__dir__", cdata_dir, METH_NOARGS},
+ {"__complex__", cdata_complex, METH_NOARGS},
+ {"__enter__", cdata_enter, METH_NOARGS},
+ {"__exit__", cdata_exit, METH_VARARGS},
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject CData_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend._CDataBase",
+ "_cffi_backend._CDataBase",
sizeof(CDataObject),
0,
(destructor)cdata_dealloc, /* tp_dealloc */
@@ -3403,16 +3403,16 @@ static PyTypeObject CData_Type = {
&CData_as_number, /* tp_as_number */
0, /* tp_as_sequence */
&CData_as_mapping, /* tp_as_mapping */
- cdata_hash, /* tp_hash */
+ cdata_hash, /* tp_hash */
(ternaryfunc)cdata_call, /* tp_call */
0, /* tp_str */
(getattrofunc)cdata_getattro, /* tp_getattro */
(setattrofunc)cdata_setattro, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
- "The internal base type for CData objects. Use FFI.CData to access "
- "it. Always check with isinstance(): subtypes are sometimes returned "
- "on CPython, for performance reasons.", /* tp_doc */
+ "The internal base type for CData objects. Use FFI.CData to access "
+ "it. Always check with isinstance(): subtypes are sometimes returned "
+ "on CPython, for performance reasons.", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
cdata_richcompare, /* tp_richcompare */
@@ -3420,22 +3420,22 @@ static PyTypeObject CData_Type = {
(getiterfunc)cdata_iter, /* tp_iter */
0, /* tp_iternext */
cdata_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- PyObject_Del, /* tp_free */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ PyObject_Del, /* tp_free */
};
static PyTypeObject CDataOwning_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.__CDataOwn",
+ "_cffi_backend.__CDataOwn",
sizeof(CDataObject),
0,
(destructor)cdataowning_dealloc, /* tp_dealloc */
@@ -3444,42 +3444,42 @@ static PyTypeObject CDataOwning_Type = {
0, /* tp_setattr */
0, /* tp_compare */
(reprfunc)cdataowning_repr, /* tp_repr */
- 0, /* inherited */ /* tp_as_number */
+ 0, /* inherited */ /* tp_as_number */
0, /* tp_as_sequence */
&CDataOwn_as_mapping, /* tp_as_mapping */
- 0, /* inherited */ /* tp_hash */
- 0, /* inherited */ /* tp_call */
+ 0, /* inherited */ /* tp_hash */
+ 0, /* inherited */ /* tp_call */
0, /* tp_str */
- 0, /* inherited */ /* tp_getattro */
- 0, /* inherited */ /* tp_setattro */
+ 0, /* inherited */ /* tp_getattro */
+ 0, /* inherited */ /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
- "This is an internal subtype of _CDataBase for performance only on "
- "CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */
+ "This is an internal subtype of _CDataBase for performance only on "
+ "CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
- 0, /* inherited */ /* tp_richcompare */
- 0, /* inherited */ /* tp_weaklistoffset */
- 0, /* inherited */ /* tp_iter */
+ 0, /* inherited */ /* tp_richcompare */
+ 0, /* inherited */ /* tp_weaklistoffset */
+ 0, /* inherited */ /* tp_iter */
0, /* tp_iternext */
- 0, /* inherited */ /* tp_methods */
+ 0, /* inherited */ /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
&CData_Type, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- free, /* tp_free */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ free, /* tp_free */
};
static PyTypeObject CDataOwningGC_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.__CDataOwnGC",
- sizeof(CDataObject_own_structptr),
+ "_cffi_backend.__CDataOwnGC",
+ sizeof(CDataObject_own_structptr),
0,
(destructor)cdataowninggc_dealloc, /* tp_dealloc */
0, /* tp_print */
@@ -3487,86 +3487,86 @@ static PyTypeObject CDataOwningGC_Type = {
0, /* tp_setattr */
0, /* tp_compare */
(reprfunc)cdataowninggc_repr, /* tp_repr */
- 0, /* inherited */ /* tp_as_number */
+ 0, /* inherited */ /* tp_as_number */
0, /* tp_as_sequence */
- 0, /* inherited */ /* tp_as_mapping */
- 0, /* inherited */ /* tp_hash */
- 0, /* inherited */ /* tp_call */
+ 0, /* inherited */ /* tp_as_mapping */
+ 0, /* inherited */ /* tp_hash */
+ 0, /* inherited */ /* tp_call */
0, /* tp_str */
- 0, /* inherited */ /* tp_getattro */
- 0, /* inherited */ /* tp_setattro */
+ 0, /* inherited */ /* tp_getattro */
+ 0, /* inherited */ /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES /* tp_flags */
| Py_TPFLAGS_HAVE_GC,
- "This is an internal subtype of _CDataBase for performance only on "
- "CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */
+ "This is an internal subtype of _CDataBase for performance only on "
+ "CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */
(traverseproc)cdataowninggc_traverse, /* tp_traverse */
(inquiry)cdataowninggc_clear, /* tp_clear */
- 0, /* inherited */ /* tp_richcompare */
- 0, /* inherited */ /* tp_weaklistoffset */
- 0, /* inherited */ /* tp_iter */
+ 0, /* inherited */ /* tp_richcompare */
+ 0, /* inherited */ /* tp_weaklistoffset */
+ 0, /* inherited */ /* tp_iter */
0, /* tp_iternext */
- 0, /* inherited */ /* tp_methods */
+ 0, /* inherited */ /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
&CDataOwning_Type, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
+};
+
+static PyTypeObject CDataFromBuf_Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_cffi_backend.__CDataFromBuf",
+ sizeof(CDataObject_frombuf),
+ 0,
+ (destructor)cdatafrombuf_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_compare */
+ (reprfunc)cdatafrombuf_repr, /* tp_repr */
+ 0, /* inherited */ /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* inherited */ /* tp_as_mapping */
+ 0, /* inherited */ /* tp_hash */
+ 0, /* inherited */ /* tp_call */
+ 0, /* tp_str */
+ 0, /* inherited */ /* tp_getattro */
+ 0, /* inherited */ /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES /* tp_flags */
+ | Py_TPFLAGS_HAVE_GC,
+ "This is an internal subtype of _CDataBase for performance only on "
+ "CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */
+ (traverseproc)cdatafrombuf_traverse, /* tp_traverse */
+ (inquiry)cdatafrombuf_clear, /* tp_clear */
+ 0, /* inherited */ /* tp_richcompare */
+ 0, /* inherited */ /* tp_weaklistoffset */
+ 0, /* inherited */ /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* inherited */ /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ &CData_Type, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
-static PyTypeObject CDataFromBuf_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.__CDataFromBuf",
- sizeof(CDataObject_frombuf),
- 0,
- (destructor)cdatafrombuf_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_compare */
- (reprfunc)cdatafrombuf_repr, /* tp_repr */
- 0, /* inherited */ /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* inherited */ /* tp_as_mapping */
- 0, /* inherited */ /* tp_hash */
- 0, /* inherited */ /* tp_call */
- 0, /* tp_str */
- 0, /* inherited */ /* tp_getattro */
- 0, /* inherited */ /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES /* tp_flags */
- | Py_TPFLAGS_HAVE_GC,
- "This is an internal subtype of _CDataBase for performance only on "
- "CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */
- (traverseproc)cdatafrombuf_traverse, /* tp_traverse */
- (inquiry)cdatafrombuf_clear, /* tp_clear */
- 0, /* inherited */ /* tp_richcompare */
- 0, /* inherited */ /* tp_weaklistoffset */
- 0, /* inherited */ /* tp_iter */
- 0, /* tp_iternext */
- 0, /* inherited */ /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- &CData_Type, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- PyObject_GC_Del, /* tp_free */
-};
-
static PyTypeObject CDataGCP_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.__CDataGCP",
+ "_cffi_backend.__CDataGCP",
sizeof(CDataObject_gcp),
0,
(destructor)cdatagcp_dealloc, /* tp_dealloc */
@@ -3574,30 +3574,30 @@ static PyTypeObject CDataGCP_Type = {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
- 0, /* inherited */ /* tp_repr */
- 0, /* inherited */ /* tp_as_number */
+ 0, /* inherited */ /* tp_repr */
+ 0, /* inherited */ /* tp_as_number */
0, /* tp_as_sequence */
- 0, /* inherited */ /* tp_as_mapping */
- 0, /* inherited */ /* tp_hash */
- 0, /* inherited */ /* tp_call */
+ 0, /* inherited */ /* tp_as_mapping */
+ 0, /* inherited */ /* tp_hash */
+ 0, /* inherited */ /* tp_call */
0, /* tp_str */
- 0, /* inherited */ /* tp_getattro */
- 0, /* inherited */ /* tp_setattro */
+ 0, /* inherited */ /* tp_getattro */
+ 0, /* inherited */ /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES /* tp_flags */
#ifdef Py_TPFLAGS_HAVE_FINALIZE
| Py_TPFLAGS_HAVE_FINALIZE
#endif
| Py_TPFLAGS_HAVE_GC,
- "This is an internal subtype of _CDataBase for performance only on "
- "CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */
+ "This is an internal subtype of _CDataBase for performance only on "
+ "CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */
(traverseproc)cdatagcp_traverse, /* tp_traverse */
0, /* tp_clear */
- 0, /* inherited */ /* tp_richcompare */
- 0, /* inherited */ /* tp_weaklistoffset */
- 0, /* inherited */ /* tp_iter */
+ 0, /* inherited */ /* tp_richcompare */
+ 0, /* inherited */ /* tp_weaklistoffset */
+ 0, /* inherited */ /* tp_iter */
0, /* tp_iternext */
- 0, /* inherited */ /* tp_methods */
+ 0, /* inherited */ /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
&CData_Type, /* tp_base */
@@ -3609,7 +3609,7 @@ static PyTypeObject CDataGCP_Type = {
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
- 0, /* inherited */ /* tp_free */
+ 0, /* inherited */ /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
@@ -3651,7 +3651,7 @@ cdataiter_dealloc(CDataIterObject *it)
static PyTypeObject CDataIter_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.__CData_iterator", /* tp_name */
+ "_cffi_backend.__CData_iterator", /* tp_name */
sizeof(CDataIterObject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
@@ -3706,16 +3706,16 @@ cdata_iter(CDataObject *cd)
/************************************************************/
static CDataObject *allocate_owning_object(Py_ssize_t size,
- CTypeDescrObject *ct,
- int dont_clear)
+ CTypeDescrObject *ct,
+ int dont_clear)
{
- /* note: objects with &CDataOwning_Type are always allocated with
- either a plain malloc() or calloc(), and freed with free(). */
+ /* note: objects with &CDataOwning_Type are always allocated with
+ either a plain malloc() or calloc(), and freed with free(). */
CDataObject *cd;
- if (dont_clear)
- cd = malloc(size);
- else
- cd = calloc(size, 1);
+ if (dont_clear)
+ cd = malloc(size);
+ else
+ cd = calloc(size, 1);
if (PyObject_Init((PyObject *)cd, &CDataOwning_Type) == NULL)
return NULL;
@@ -3741,9 +3741,9 @@ convert_struct_to_owning_object(char *data, CTypeDescrObject *ct)
if (ct->ct_flags & CT_WITH_VAR_ARRAY) {
PyErr_SetString(PyExc_TypeError,
"return type is a struct/union with a varsize array member");
- return NULL;
+ return NULL;
}
- cd = allocate_owning_object(dataoffset + datasize, ct, /*dont_clear=*/1);
+ cd = allocate_owning_object(dataoffset + datasize, ct, /*dont_clear=*/1);
if (cd == NULL)
return NULL;
cd->c_data = ((char *)cd) + dataoffset;
@@ -3781,8 +3781,8 @@ static CDataObject *allocate_with_allocator(Py_ssize_t basesize,
CDataObject *cd;
if (allocator->ca_alloc == NULL) {
- cd = allocate_owning_object(basesize + datasize, ct,
- allocator->ca_dont_clear);
+ cd = allocate_owning_object(basesize + datasize, ct,
+ allocator->ca_dont_clear);
if (cd == NULL)
return NULL;
cd->c_data = ((char *)cd) + basesize;
@@ -3815,8 +3815,8 @@ static CDataObject *allocate_with_allocator(Py_ssize_t basesize,
cd = allocate_gcp_object(cd, ct, allocator->ca_free);
Py_DECREF(res);
- if (!allocator->ca_dont_clear)
- memset(cd->c_data, 0, datasize);
+ if (!allocator->ca_dont_clear)
+ memset(cd->c_data, 0, datasize);
}
return cd;
}
@@ -3864,7 +3864,7 @@ static PyObject *direct_newp(CTypeDescrObject *ct, PyObject *init,
dataoffset = offsetof(CDataObject_own_nolength, alignment);
datasize = ct->ct_size;
if (datasize < 0) {
- explicitlength = get_new_array_length(ct->ct_itemdescr, &init);
+ explicitlength = get_new_array_length(ct->ct_itemdescr, &init);
if (explicitlength < 0)
return NULL;
ctitem = ct->ct_itemdescr;
@@ -3897,8 +3897,8 @@ static PyObject *direct_newp(CTypeDescrObject *ct, PyObject *init,
if (cds == NULL)
return NULL;
- cd = allocate_owning_object(sizeof(CDataObject_own_structptr), ct,
- /*dont_clear=*/1);
+ cd = allocate_owning_object(sizeof(CDataObject_own_structptr), ct,
+ /*dont_clear=*/1);
if (cd == NULL) {
Py_DECREF(cds);
return NULL;
@@ -4035,22 +4035,22 @@ static CDataObject *cast_to_integer_or_char(CTypeDescrObject *ct, PyObject *ob)
}
#endif
else if (PyUnicode_Check(ob)) {
- char err_buf[80];
- cffi_char32_t ordinal;
- if (_my_PyUnicode_AsSingleChar32(ob, &ordinal, err_buf) < 0) {
+ char err_buf[80];
+ cffi_char32_t ordinal;
+ if (_my_PyUnicode_AsSingleChar32(ob, &ordinal, err_buf) < 0) {
PyErr_Format(PyExc_TypeError,
- "cannot cast %s to ctype '%s'", err_buf, ct->ct_name);
+ "cannot cast %s to ctype '%s'", err_buf, ct->ct_name);
return NULL;
}
- /* the types char16_t and char32_t are both unsigned. However,
- wchar_t might be signed. In theory it does not matter,
- because 'ordinal' comes from a regular Python unicode. */
-#ifdef HAVE_WCHAR_H
- if (ct->ct_flags & CT_IS_SIGNED_WCHAR)
- value = (wchar_t)ordinal;
- else
-#endif
- value = ordinal;
+ /* the types char16_t and char32_t are both unsigned. However,
+ wchar_t might be signed. In theory it does not matter,
+ because 'ordinal' comes from a regular Python unicode. */
+#ifdef HAVE_WCHAR_H
+ if (ct->ct_flags & CT_IS_SIGNED_WCHAR)
+ value = (wchar_t)ordinal;
+ else
+#endif
+ value = ordinal;
}
else if (PyBytes_Check(ob)) {
int res = _convert_to_char(ob);
@@ -4077,35 +4077,35 @@ static CDataObject *cast_to_integer_or_char(CTypeDescrObject *ct, PyObject *ob)
return cd;
}
-/* returns -1 if cannot cast, 0 if we don't get a value, 1 if we do */
-static int check_bytes_for_float_compatible(PyObject *io, double *out_value)
-{
- if (PyBytes_Check(io)) {
- if (PyBytes_GET_SIZE(io) != 1)
- goto error;
- *out_value = (unsigned char)PyBytes_AS_STRING(io)[0];
- return 1;
- }
- else if (PyUnicode_Check(io)) {
- char ignored[80];
- cffi_char32_t ordinal;
- if (_my_PyUnicode_AsSingleChar32(io, &ordinal, ignored) < 0)
- goto error;
- /* the signness of the 32-bit version of wide chars should not
- * matter here, because 'ordinal' comes from a normal Python
- * unicode string */
- *out_value = ordinal;
- return 1;
- }
- *out_value = 0; /* silence a gcc warning if this function is inlined */
- return 0;
-
- error:
- Py_DECREF(io);
- *out_value = 0; /* silence a gcc warning if this function is inlined */
- return -1;
-}
-
+/* returns -1 if cannot cast, 0 if we don't get a value, 1 if we do */
+static int check_bytes_for_float_compatible(PyObject *io, double *out_value)
+{
+ if (PyBytes_Check(io)) {
+ if (PyBytes_GET_SIZE(io) != 1)
+ goto error;
+ *out_value = (unsigned char)PyBytes_AS_STRING(io)[0];
+ return 1;
+ }
+ else if (PyUnicode_Check(io)) {
+ char ignored[80];
+ cffi_char32_t ordinal;
+ if (_my_PyUnicode_AsSingleChar32(io, &ordinal, ignored) < 0)
+ goto error;
+ /* the signness of the 32-bit version of wide chars should not
+ * matter here, because 'ordinal' comes from a normal Python
+ * unicode string */
+ *out_value = ordinal;
+ return 1;
+ }
+ *out_value = 0; /* silence a gcc warning if this function is inlined */
+ return 0;
+
+ error:
+ Py_DECREF(io);
+ *out_value = 0; /* silence a gcc warning if this function is inlined */
+ return -1;
+}
+
static PyObject *do_cast(CTypeDescrObject *ct, PyObject *ob)
{
CDataObject *cd;
@@ -4147,7 +4147,7 @@ static PyObject *do_cast(CTypeDescrObject *ct, PyObject *ob)
/* cast to a float */
double value;
PyObject *io;
- int res;
+ int res;
if (CData_Check(ob)) {
CDataObject *cdsrc = (CDataObject *)ob;
@@ -4163,22 +4163,22 @@ static PyObject *do_cast(CTypeDescrObject *ct, PyObject *ob)
Py_INCREF(io);
}
- res = check_bytes_for_float_compatible(io, &value);
- if (res == -1)
- goto cannot_cast;
- if (res == 0) {
- if ((ct->ct_flags & CT_IS_LONGDOUBLE) &&
- CData_Check(io) &&
- (((CDataObject *)io)->c_type->ct_flags & CT_IS_LONGDOUBLE)) {
- long double lvalue;
- char *data = ((CDataObject *)io)->c_data;
- /*READ(data, sizeof(long double)*/
- lvalue = read_raw_longdouble_data(data);
+ res = check_bytes_for_float_compatible(io, &value);
+ if (res == -1)
+ goto cannot_cast;
+ if (res == 0) {
+ if ((ct->ct_flags & CT_IS_LONGDOUBLE) &&
+ CData_Check(io) &&
+ (((CDataObject *)io)->c_type->ct_flags & CT_IS_LONGDOUBLE)) {
+ long double lvalue;
+ char *data = ((CDataObject *)io)->c_data;
+ /*READ(data, sizeof(long double)*/
+ lvalue = read_raw_longdouble_data(data);
Py_DECREF(io);
- cd = _new_casted_primitive(ct);
- if (cd != NULL)
- write_raw_longdouble_data(cd->c_data, lvalue);
- return (PyObject *)cd;
+ cd = _new_casted_primitive(ct);
+ if (cd != NULL)
+ write_raw_longdouble_data(cd->c_data, lvalue);
+ return (PyObject *)cd;
}
value = PyFloat_AsDouble(io);
}
@@ -4195,45 +4195,45 @@ static PyObject *do_cast(CTypeDescrObject *ct, PyObject *ob)
}
return (PyObject *)cd;
}
- else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
- /* cast to a complex */
- Py_complex value;
- PyObject *io;
- int res;
-
- if (CData_Check(ob)) {
- CDataObject *cdsrc = (CDataObject *)ob;
-
- if (!(cdsrc->c_type->ct_flags & CT_PRIMITIVE_ANY))
- goto cannot_cast;
- io = convert_to_object(cdsrc->c_data, cdsrc->c_type);
- if (io == NULL)
- return NULL;
- }
- else {
- io = ob;
- Py_INCREF(io);
- }
-
- res = check_bytes_for_float_compatible(io, &value.real);
- if (res == -1)
- goto cannot_cast;
- if (res == 1) {
- // got it from string
- value.imag = 0.0;
- } else {
- value = PyComplex_AsCComplex(io);
- }
- Py_DECREF(io);
- if (PyErr_Occurred()) {
- return NULL;
- }
- cd = _new_casted_primitive(ct);
- if (cd != NULL) {
- write_raw_complex_data(cd->c_data, value, ct->ct_size);
- }
- return (PyObject *)cd;
- }
+ else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
+ /* cast to a complex */
+ Py_complex value;
+ PyObject *io;
+ int res;
+
+ if (CData_Check(ob)) {
+ CDataObject *cdsrc = (CDataObject *)ob;
+
+ if (!(cdsrc->c_type->ct_flags & CT_PRIMITIVE_ANY))
+ goto cannot_cast;
+ io = convert_to_object(cdsrc->c_data, cdsrc->c_type);
+ if (io == NULL)
+ return NULL;
+ }
+ else {
+ io = ob;
+ Py_INCREF(io);
+ }
+
+ res = check_bytes_for_float_compatible(io, &value.real);
+ if (res == -1)
+ goto cannot_cast;
+ if (res == 1) {
+ // got it from string
+ value.imag = 0.0;
+ } else {
+ value = PyComplex_AsCComplex(io);
+ }
+ Py_DECREF(io);
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
+ cd = _new_casted_primitive(ct);
+ if (cd != NULL) {
+ write_raw_complex_data(cd->c_data, value, ct->ct_size);
+ }
+ return (PyObject *)cd;
+ }
else {
PyErr_Format(PyExc_TypeError, "cannot cast to ctype '%s'",
ct->ct_name);
@@ -4267,13 +4267,13 @@ typedef struct {
PyObject_HEAD
void *dl_handle;
char *dl_name;
- int dl_auto_close;
+ int dl_auto_close;
} DynLibObject;
static void dl_dealloc(DynLibObject *dlobj)
{
- if (dlobj->dl_handle != NULL && dlobj->dl_auto_close)
- dlclose(dlobj->dl_handle);
+ if (dlobj->dl_handle != NULL && dlobj->dl_auto_close)
+ dlclose(dlobj->dl_handle);
free(dlobj->dl_name);
PyObject_Del(dlobj);
}
@@ -4283,17 +4283,17 @@ static PyObject *dl_repr(DynLibObject *dlobj)
return PyText_FromFormat("<clibrary '%s'>", dlobj->dl_name);
}
-static int dl_check_closed(DynLibObject *dlobj)
-{
- if (dlobj->dl_handle == NULL)
- {
- PyErr_Format(PyExc_ValueError, "library '%s' has already been closed",
- dlobj->dl_name);
- return -1;
- }
- return 0;
-}
-
+static int dl_check_closed(DynLibObject *dlobj)
+{
+ if (dlobj->dl_handle == NULL)
+ {
+ PyErr_Format(PyExc_ValueError, "library '%s' has already been closed",
+ dlobj->dl_name);
+ return -1;
+ }
+ return 0;
+}
+
static PyObject *dl_load_function(DynLibObject *dlobj, PyObject *args)
{
CTypeDescrObject *ct;
@@ -4304,12 +4304,12 @@ static PyObject *dl_load_function(DynLibObject *dlobj, PyObject *args)
&CTypeDescr_Type, &ct, &funcname))
return NULL;
- if (dl_check_closed(dlobj) < 0)
- return NULL;
-
- if (!(ct->ct_flags & (CT_FUNCTIONPTR | CT_POINTER | CT_ARRAY))) {
- PyErr_Format(PyExc_TypeError,
- "function or pointer or array cdata expected, got '%s'",
+ if (dl_check_closed(dlobj) < 0)
+ return NULL;
+
+ if (!(ct->ct_flags & (CT_FUNCTIONPTR | CT_POINTER | CT_ARRAY))) {
+ PyErr_Format(PyExc_TypeError,
+ "function or pointer or array cdata expected, got '%s'",
ct->ct_name);
return NULL;
}
@@ -4317,15 +4317,15 @@ static PyObject *dl_load_function(DynLibObject *dlobj, PyObject *args)
funcptr = dlsym(dlobj->dl_handle, funcname);
if (funcptr == NULL) {
const char *error = dlerror();
- PyErr_Format(PyExc_AttributeError,
- "function/symbol '%s' not found in library '%s': %s",
+ PyErr_Format(PyExc_AttributeError,
+ "function/symbol '%s' not found in library '%s': %s",
funcname, dlobj->dl_name, error);
return NULL;
}
- if ((ct->ct_flags & CT_ARRAY) && ct->ct_length < 0) {
- ct = (CTypeDescrObject *)ct->ct_stuff;
- }
+ if ((ct->ct_flags & CT_ARRAY) && ct->ct_length < 0) {
+ ct = (CTypeDescrObject *)ct->ct_stuff;
+ }
return new_simple_cdata(funcptr, ct);
}
@@ -4339,9 +4339,9 @@ static PyObject *dl_read_variable(DynLibObject *dlobj, PyObject *args)
&CTypeDescr_Type, &ct, &varname))
return NULL;
- if (dl_check_closed(dlobj) < 0)
- return NULL;
-
+ if (dl_check_closed(dlobj) < 0)
+ return NULL;
+
dlerror(); /* clear error condition */
data = dlsym(dlobj->dl_handle, varname);
if (data == NULL) {
@@ -4367,9 +4367,9 @@ static PyObject *dl_write_variable(DynLibObject *dlobj, PyObject *args)
&CTypeDescr_Type, &ct, &varname, &value))
return NULL;
- if (dl_check_closed(dlobj) < 0)
- return NULL;
-
+ if (dl_check_closed(dlobj) < 0)
+ return NULL;
+
dlerror(); /* clear error condition */
data = dlsym(dlobj->dl_handle, varname);
if (data == NULL) {
@@ -4385,28 +4385,28 @@ static PyObject *dl_write_variable(DynLibObject *dlobj, PyObject *args)
return Py_None;
}
-static PyObject *dl_close_lib(DynLibObject *dlobj, PyObject *no_args)
-{
- if (dlobj->dl_handle != NULL)
- {
- dlclose(dlobj->dl_handle);
- dlobj->dl_handle = NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
-}
-
+static PyObject *dl_close_lib(DynLibObject *dlobj, PyObject *no_args)
+{
+ if (dlobj->dl_handle != NULL)
+ {
+ dlclose(dlobj->dl_handle);
+ dlobj->dl_handle = NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
static PyMethodDef dl_methods[] = {
{"load_function", (PyCFunction)dl_load_function, METH_VARARGS},
{"read_variable", (PyCFunction)dl_read_variable, METH_VARARGS},
{"write_variable", (PyCFunction)dl_write_variable, METH_VARARGS},
- {"close_lib", (PyCFunction)dl_close_lib, METH_NOARGS},
+ {"close_lib", (PyCFunction)dl_close_lib, METH_NOARGS},
{NULL, NULL} /* sentinel */
};
static PyTypeObject dl_type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.CLibrary", /* tp_name */
+ "_cffi_backend.CLibrary", /* tp_name */
sizeof(DynLibObject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
@@ -4436,150 +4436,150 @@ static PyTypeObject dl_type = {
dl_methods, /* tp_methods */
};
-static void *b_do_dlopen(PyObject *args, const char **p_printable_filename,
- PyObject **p_temp, int *auto_close)
+static void *b_do_dlopen(PyObject *args, const char **p_printable_filename,
+ PyObject **p_temp, int *auto_close)
{
- /* Logic to call the correct version of dlopen(). Returns NULL in case of error.
- Otherwise, '*p_printable_filename' will point to a printable char version of
- the filename (maybe utf-8-encoded). '*p_temp' will be set either to NULL or
- to a temporary object that must be freed after looking at printable_filename.
- */
+ /* Logic to call the correct version of dlopen(). Returns NULL in case of error.
+ Otherwise, '*p_printable_filename' will point to a printable char version of
+ the filename (maybe utf-8-encoded). '*p_temp' will be set either to NULL or
+ to a temporary object that must be freed after looking at printable_filename.
+ */
void *handle;
- char *filename_or_null;
+ char *filename_or_null;
int flags = 0;
- *p_temp = NULL;
- *auto_close = 1;
-
+ *p_temp = NULL;
+ *auto_close = 1;
+
if (PyTuple_GET_SIZE(args) == 0 || PyTuple_GET_ITEM(args, 0) == Py_None) {
PyObject *dummy;
if (!PyArg_ParseTuple(args, "|Oi:load_library",
&dummy, &flags))
return NULL;
filename_or_null = NULL;
- *p_printable_filename = "<None>";
- }
- else if (CData_Check(PyTuple_GET_ITEM(args, 0)))
- {
- CDataObject *cd;
- if (!PyArg_ParseTuple(args, "O|i:load_library", &cd, &flags))
- return NULL;
- /* 'flags' is accepted but ignored in this case */
- if ((cd->c_type->ct_flags & CT_IS_VOID_PTR) == 0) {
- PyErr_Format(PyExc_TypeError,
- "dlopen() takes a file name or 'void *' handle, not '%s'",
- cd->c_type->ct_name);
- return NULL;
- }
- handle = cd->c_data;
- if (handle == NULL) {
- PyErr_Format(PyExc_RuntimeError, "cannot call dlopen(NULL)");
- return NULL;
- }
- *p_temp = PyText_FromFormat("%p", handle);
- *p_printable_filename = PyText_AsUTF8(*p_temp);
- *auto_close = 0;
- return handle;
- }
- else
- {
- PyObject *s = PyTuple_GET_ITEM(args, 0);
-#ifdef MS_WIN32
- PyObject *filename_unicode;
- if (PyArg_ParseTuple(args, "U|i:load_library", &filename_unicode, &flags))
- {
- Py_ssize_t sz1;
- wchar_t *w1;
-#if PY_MAJOR_VERSION < 3
- s = PyUnicode_AsUTF8String(s);
- if (s == NULL)
- return NULL;
- *p_temp = s;
-#endif
- *p_printable_filename = PyText_AsUTF8(s);
- if (*p_printable_filename == NULL)
- return NULL;
-
- sz1 = PyUnicode_GetSize(filename_unicode) + 1;
- sz1 *= 2; /* should not be needed, but you never know */
- w1 = alloca(sizeof(wchar_t) * sz1);
- sz1 = PyUnicode_AsWideChar((PyUnicodeObject *)filename_unicode,
- w1, sz1 - 1);
- if (sz1 < 0)
- return NULL;
- w1[sz1] = 0;
- handle = dlopenW(w1);
- goto got_handle;
- }
- PyErr_Clear();
-#endif
- if (!PyArg_ParseTuple(args, "et|i:load_library",
- Py_FileSystemDefaultEncoding, &filename_or_null, &flags))
- return NULL;
-#if PY_MAJOR_VERSION < 3
- if (PyUnicode_Check(s))
- {
- s = PyUnicode_AsUTF8String(s);
- if (s == NULL) {
- PyMem_Free(filename_or_null);
- return NULL;
- }
- *p_temp = s;
- }
-#endif
- *p_printable_filename = PyText_AsUTF8(s);
- if (*p_printable_filename == NULL) {
- PyMem_Free(filename_or_null);
- return NULL;
- }
- }
+ *p_printable_filename = "<None>";
+ }
+ else if (CData_Check(PyTuple_GET_ITEM(args, 0)))
+ {
+ CDataObject *cd;
+ if (!PyArg_ParseTuple(args, "O|i:load_library", &cd, &flags))
+ return NULL;
+ /* 'flags' is accepted but ignored in this case */
+ if ((cd->c_type->ct_flags & CT_IS_VOID_PTR) == 0) {
+ PyErr_Format(PyExc_TypeError,
+ "dlopen() takes a file name or 'void *' handle, not '%s'",
+ cd->c_type->ct_name);
+ return NULL;
+ }
+ handle = cd->c_data;
+ if (handle == NULL) {
+ PyErr_Format(PyExc_RuntimeError, "cannot call dlopen(NULL)");
+ return NULL;
+ }
+ *p_temp = PyText_FromFormat("%p", handle);
+ *p_printable_filename = PyText_AsUTF8(*p_temp);
+ *auto_close = 0;
+ return handle;
+ }
+ else
+ {
+ PyObject *s = PyTuple_GET_ITEM(args, 0);
+#ifdef MS_WIN32
+ PyObject *filename_unicode;
+ if (PyArg_ParseTuple(args, "U|i:load_library", &filename_unicode, &flags))
+ {
+ Py_ssize_t sz1;
+ wchar_t *w1;
+#if PY_MAJOR_VERSION < 3
+ s = PyUnicode_AsUTF8String(s);
+ if (s == NULL)
+ return NULL;
+ *p_temp = s;
+#endif
+ *p_printable_filename = PyText_AsUTF8(s);
+ if (*p_printable_filename == NULL)
+ return NULL;
+
+ sz1 = PyUnicode_GetSize(filename_unicode) + 1;
+ sz1 *= 2; /* should not be needed, but you never know */
+ w1 = alloca(sizeof(wchar_t) * sz1);
+ sz1 = PyUnicode_AsWideChar((PyUnicodeObject *)filename_unicode,
+ w1, sz1 - 1);
+ if (sz1 < 0)
+ return NULL;
+ w1[sz1] = 0;
+ handle = dlopenW(w1);
+ goto got_handle;
+ }
+ PyErr_Clear();
+#endif
+ if (!PyArg_ParseTuple(args, "et|i:load_library",
+ Py_FileSystemDefaultEncoding, &filename_or_null, &flags))
+ return NULL;
+#if PY_MAJOR_VERSION < 3
+ if (PyUnicode_Check(s))
+ {
+ s = PyUnicode_AsUTF8String(s);
+ if (s == NULL) {
+ PyMem_Free(filename_or_null);
+ return NULL;
+ }
+ *p_temp = s;
+ }
+#endif
+ *p_printable_filename = PyText_AsUTF8(s);
+ if (*p_printable_filename == NULL) {
+ PyMem_Free(filename_or_null);
+ return NULL;
+ }
+ }
if ((flags & (RTLD_NOW | RTLD_LAZY)) == 0)
flags |= RTLD_NOW;
-#ifdef MS_WIN32
- if (filename_or_null == NULL) {
- PyErr_SetString(PyExc_OSError, "dlopen(None) not supported on Windows");
- return NULL;
- }
-#endif
-
+#ifdef MS_WIN32
+ if (filename_or_null == NULL) {
+ PyErr_SetString(PyExc_OSError, "dlopen(None) not supported on Windows");
+ return NULL;
+ }
+#endif
+
handle = dlopen(filename_or_null, flags);
- PyMem_Free(filename_or_null);
-
-#ifdef MS_WIN32
- got_handle:
-#endif
+ PyMem_Free(filename_or_null);
+
+#ifdef MS_WIN32
+ got_handle:
+#endif
if (handle == NULL) {
const char *error = dlerror();
- PyErr_Format(PyExc_OSError, "cannot load library '%s': %s",
- *p_printable_filename, error);
+ PyErr_Format(PyExc_OSError, "cannot load library '%s': %s",
+ *p_printable_filename, error);
return NULL;
}
- return handle;
-}
+ return handle;
+}
+
+static PyObject *b_load_library(PyObject *self, PyObject *args)
+{
+ const char *printable_filename;
+ PyObject *temp;
+ void *handle;
+ DynLibObject *dlobj = NULL;
+ int auto_close;
+
+ handle = b_do_dlopen(args, &printable_filename, &temp, &auto_close);
+ if (handle == NULL)
+ goto error;
-static PyObject *b_load_library(PyObject *self, PyObject *args)
-{
- const char *printable_filename;
- PyObject *temp;
- void *handle;
- DynLibObject *dlobj = NULL;
- int auto_close;
-
- handle = b_do_dlopen(args, &printable_filename, &temp, &auto_close);
- if (handle == NULL)
- goto error;
-
dlobj = PyObject_New(DynLibObject, &dl_type);
if (dlobj == NULL) {
dlclose(handle);
- goto error;
+ goto error;
}
dlobj->dl_handle = handle;
dlobj->dl_name = strdup(printable_filename);
- dlobj->dl_auto_close = auto_close;
-
- error:
- Py_XDECREF(temp);
+ dlobj->dl_auto_close = auto_close;
+
+ error:
+ Py_XDECREF(temp);
return (PyObject *)dlobj;
}
@@ -4631,10 +4631,10 @@ static PyObject *get_unique_type(CTypeDescrObject *x,
assert(x->ct_unique_key == NULL);
x->ct_unique_key = key; /* the key will be freed in ctypedescr_dealloc() */
- /* the 'value' in unique_cache doesn't count as 1, but don't use
- Py_DECREF(x) here because it will confuse debug builds into thinking
- there was an extra DECREF in total. */
- ((PyObject *)x)->ob_refcnt--;
+ /* the 'value' in unique_cache doesn't count as 1, but don't use
+ Py_DECREF(x) here because it will confuse debug builds into thinking
+ there was an extra DECREF in total. */
+ ((PyObject *)x)->ob_refcnt--;
return (PyObject *)x;
error:
@@ -4642,11 +4642,11 @@ static PyObject *get_unique_type(CTypeDescrObject *x,
return NULL;
}
-/* according to the C standard, these types should be equivalent to the
- _Complex types for the purposes of storage (not arguments in calls!) */
-typedef float cffi_float_complex_t[2];
-typedef double cffi_double_complex_t[2];
-
+/* according to the C standard, these types should be equivalent to the
+ _Complex types for the purposes of storage (not arguments in calls!) */
+typedef float cffi_float_complex_t[2];
+typedef double cffi_double_complex_t[2];
+
static PyObject *new_primitive_type(const char *name)
{
#define ENUM_PRIMITIVE_TYPES \
@@ -4664,11 +4664,11 @@ static PyObject *new_primitive_type(const char *name)
EPTYPE(f, float, CT_PRIMITIVE_FLOAT ) \
EPTYPE(d, double, CT_PRIMITIVE_FLOAT ) \
EPTYPE(ld, long double, CT_PRIMITIVE_FLOAT | CT_IS_LONGDOUBLE ) \
- EPTYPE2(fc, "float _Complex", cffi_float_complex_t, CT_PRIMITIVE_COMPLEX ) \
- EPTYPE2(dc, "double _Complex", cffi_double_complex_t, CT_PRIMITIVE_COMPLEX ) \
+ EPTYPE2(fc, "float _Complex", cffi_float_complex_t, CT_PRIMITIVE_COMPLEX ) \
+ EPTYPE2(dc, "double _Complex", cffi_double_complex_t, CT_PRIMITIVE_COMPLEX ) \
ENUM_PRIMITIVE_TYPES_WCHAR \
- EPTYPE2(c16, "char16_t", cffi_char16_t, CT_PRIMITIVE_CHAR ) \
- EPTYPE2(c32, "char32_t", cffi_char32_t, CT_PRIMITIVE_CHAR ) \
+ EPTYPE2(c16, "char16_t", cffi_char16_t, CT_PRIMITIVE_CHAR ) \
+ EPTYPE2(c32, "char32_t", cffi_char32_t, CT_PRIMITIVE_CHAR ) \
EPTYPE(b, _Bool, CT_PRIMITIVE_UNSIGNED | CT_IS_BOOL ) \
/* the following types are not primitive in the C sense */ \
EPTYPE(i8, int8_t, CT_PRIMITIVE_SIGNED) \
@@ -4705,8 +4705,8 @@ static PyObject *new_primitive_type(const char *name)
#ifdef HAVE_WCHAR_H
# define ENUM_PRIMITIVE_TYPES_WCHAR \
- EPTYPE(wc, wchar_t, CT_PRIMITIVE_CHAR | \
- (((wchar_t)-1) > 0 ? 0 : CT_IS_SIGNED_WCHAR))
+ EPTYPE(wc, wchar_t, CT_PRIMITIVE_CHAR | \
+ (((wchar_t)-1) > 0 ? 0 : CT_IS_SIGNED_WCHAR))
#else
# define ENUM_PRIMITIVE_TYPES_WCHAR /* nothing */
#endif
@@ -4779,13 +4779,13 @@ static PyObject *new_primitive_type(const char *name)
else
goto bad_ffi_type;
}
- else if (ptypes->flags & CT_PRIMITIVE_COMPLEX) {
- /* As of March 2017, still no libffi support for complex.
- It fails silently if we try to use ffi_type_complex_float
- or ffi_type_complex_double. Better not use it at all.
- */
- ffitype = NULL;
- }
+ else if (ptypes->flags & CT_PRIMITIVE_COMPLEX) {
+ /* As of March 2017, still no libffi support for complex.
+ It fails silently if we try to use ffi_type_complex_float
+ or ffi_type_complex_double. Better not use it at all.
+ */
+ ffitype = NULL;
+ }
else {
switch (ptypes->size) {
case 1: ffitype = &ffi_type_uint8; break;
@@ -5046,12 +5046,12 @@ _add_field(PyObject *interned_fields, PyObject *fname, CTypeDescrObject *ftype,
#define SF_PACKED 0x08
#define SF_STD_FIELD_POS 0x80
-#ifdef MS_WIN32
-# define SF_DEFAULT_PACKING 8
-#else
-# define SF_DEFAULT_PACKING 0x40000000 /* a huge power of two */
-#endif
-
+#ifdef MS_WIN32
+# define SF_DEFAULT_PACKING 8
+#else
+# define SF_DEFAULT_PACKING 0x40000000 /* a huge power of two */
+#endif
+
static int complete_sflags(int sflags)
{
/* add one of the SF_xxx_BITFIELDS flags if none is specified */
@@ -5060,9 +5060,9 @@ static int complete_sflags(int sflags)
#ifdef MS_WIN32
sflags |= SF_MSVC_BITFIELDS;
#else
-# if defined(__APPLE__) && defined(__arm64__)
- sflags |= SF_GCC_X86_BITFIELDS;
-# elif defined(__arm__) || defined(__aarch64__)
+# if defined(__APPLE__) && defined(__arm64__)
+ sflags |= SF_GCC_X86_BITFIELDS;
+# elif defined(__arm__) || defined(__aarch64__)
sflags |= SF_GCC_ARM_BITFIELDS;
# else
sflags |= SF_GCC_X86_BITFIELDS;
@@ -5090,8 +5090,8 @@ static int detect_custom_layout(CTypeDescrObject *ct, int sflags,
if (sflags & SF_STD_FIELD_POS) {
PyErr_Format(FFIError,
"%s: %s%s%s (cdef says %zd, but C compiler says %zd)."
- " fix it or use \"...;\" as the last field in the "
- "cdef for %s to make it flexible",
+ " fix it or use \"...;\" as the last field in the "
+ "cdef for %s to make it flexible",
ct->ct_name, msg1, txt, msg2,
cdef_value, compiler_value,
ct->ct_name);
@@ -5102,37 +5102,37 @@ static int detect_custom_layout(CTypeDescrObject *ct, int sflags,
return 0;
}
-#define ROUNDUP_BYTES(bytes, bits) ((bytes) + ((bits) > 0))
-
+#define ROUNDUP_BYTES(bytes, bits) ((bytes) + ((bits) > 0))
+
static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
{
CTypeDescrObject *ct;
PyObject *fields, *interned_fields, *ignored;
int is_union, alignment;
- Py_ssize_t byteoffset, i, nb_fields, byteoffsetmax, alignedsize;
- int bitoffset;
- Py_ssize_t byteoffsetorg;
+ Py_ssize_t byteoffset, i, nb_fields, byteoffsetmax, alignedsize;
+ int bitoffset;
+ Py_ssize_t byteoffsetorg;
Py_ssize_t totalsize = -1;
int totalalignment = -1;
CFieldObject **previous;
int prev_bitfield_size, prev_bitfield_free;
int sflags = 0, fflags;
- int pack = 0;
+ int pack = 0;
- if (!PyArg_ParseTuple(args, "O!O!|Oniii:complete_struct_or_union",
+ if (!PyArg_ParseTuple(args, "O!O!|Oniii:complete_struct_or_union",
&CTypeDescr_Type, &ct,
&PyList_Type, &fields,
- &ignored, &totalsize, &totalalignment, &sflags,
- &pack))
+ &ignored, &totalsize, &totalalignment, &sflags,
+ &pack))
return NULL;
sflags = complete_sflags(sflags);
- if (sflags & SF_PACKED)
- pack = 1;
- else if (pack <= 0)
- pack = SF_DEFAULT_PACKING;
- else
- sflags |= SF_PACKED;
+ if (sflags & SF_PACKED)
+ pack = 1;
+ else if (pack <= 0)
+ pack = SF_DEFAULT_PACKING;
+ else
+ sflags |= SF_PACKED;
if ((ct->ct_flags & (CT_STRUCT|CT_IS_OPAQUE)) ==
(CT_STRUCT|CT_IS_OPAQUE)) {
@@ -5147,12 +5147,12 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
"first arg must be a non-initialized struct or union ctype");
return NULL;
}
- ct->ct_flags &= ~(CT_CUSTOM_FIELD_POS | CT_WITH_PACKED_CHANGE);
+ ct->ct_flags &= ~(CT_CUSTOM_FIELD_POS | CT_WITH_PACKED_CHANGE);
alignment = 1;
- byteoffset = 0; /* the real value is 'byteoffset+bitoffset*8', which */
- bitoffset = 0; /* counts the offset in bits */
- byteoffsetmax = 0; /* the maximum value of byteoffset-rounded-up-to-byte */
+ byteoffset = 0; /* the real value is 'byteoffset+bitoffset*8', which */
+ bitoffset = 0; /* counts the offset in bits */
+ byteoffsetmax = 0; /* the maximum value of byteoffset-rounded-up-to-byte */
prev_bitfield_size = 0;
prev_bitfield_free = 0;
nb_fields = PyList_GET_SIZE(fields);
@@ -5165,7 +5165,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
for (i=0; i<nb_fields; i++) {
PyObject *fname;
CTypeDescrObject *ftype;
- int fbitsize = -1, falign, falignorg, do_align;
+ int fbitsize = -1, falign, falignorg, do_align;
Py_ssize_t foffset = -1;
if (!PyArg_ParseTuple(PyList_GET_ITEM(fields, i), "O!O!|in:list item",
@@ -5187,29 +5187,29 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
goto error;
}
}
- else if (ftype->ct_flags & (CT_STRUCT|CT_UNION)) {
- if (force_lazy_struct(ftype) < 0) /* for CT_WITH_VAR_ARRAY */
- return NULL;
-
- /* GCC (or maybe C99) accepts var-sized struct fields that are not
- the last field of a larger struct. That's why there is no
- check here for "last field": we propagate the flag
- CT_WITH_VAR_ARRAY to any struct that contains either an open-
- ended array or another struct that recursively contains an
- open-ended array. */
- if (ftype->ct_flags & CT_WITH_VAR_ARRAY)
- ct->ct_flags |= CT_WITH_VAR_ARRAY;
- }
-
+ else if (ftype->ct_flags & (CT_STRUCT|CT_UNION)) {
+ if (force_lazy_struct(ftype) < 0) /* for CT_WITH_VAR_ARRAY */
+ return NULL;
+
+ /* GCC (or maybe C99) accepts var-sized struct fields that are not
+ the last field of a larger struct. That's why there is no
+ check here for "last field": we propagate the flag
+ CT_WITH_VAR_ARRAY to any struct that contains either an open-
+ ended array or another struct that recursively contains an
+ open-ended array. */
+ if (ftype->ct_flags & CT_WITH_VAR_ARRAY)
+ ct->ct_flags |= CT_WITH_VAR_ARRAY;
+ }
+
if (is_union)
- byteoffset = bitoffset = 0; /* reset each field at offset 0 */
+ byteoffset = bitoffset = 0; /* reset each field at offset 0 */
/* update the total alignment requirement, but skip it if the
field is an anonymous bitfield or if SF_PACKED */
- falignorg = get_alignment(ftype);
- if (falignorg < 0)
+ falignorg = get_alignment(ftype);
+ if (falignorg < 0)
goto error;
- falign = (pack < falignorg) ? pack : falignorg;
+ falign = (pack < falignorg) ? pack : falignorg;
do_align = 1;
if (!(sflags & SF_GCC_ARM_BITFIELDS) && fbitsize >= 0) {
@@ -5238,25 +5238,25 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
/* align this field to its own 'falign' by inserting padding */
- /* first, pad to the next byte,
- * then pad to 'falign' or 'falignorg' bytes */
- byteoffset = ROUNDUP_BYTES(byteoffset, bitoffset);
- bitoffset = 0;
- byteoffsetorg = (byteoffset + falignorg-1) & ~(falignorg-1);
- byteoffset = (byteoffset + falign-1) & ~(falign-1);
-
- if (byteoffsetorg != byteoffset) {
- ct->ct_flags |= CT_WITH_PACKED_CHANGE;
- }
-
+ /* first, pad to the next byte,
+ * then pad to 'falign' or 'falignorg' bytes */
+ byteoffset = ROUNDUP_BYTES(byteoffset, bitoffset);
+ bitoffset = 0;
+ byteoffsetorg = (byteoffset + falignorg-1) & ~(falignorg-1);
+ byteoffset = (byteoffset + falign-1) & ~(falign-1);
+
+ if (byteoffsetorg != byteoffset) {
+ ct->ct_flags |= CT_WITH_PACKED_CHANGE;
+ }
+
if (foffset >= 0) {
/* a forced field position: ignore the offset just computed,
except to know if we must set CT_CUSTOM_FIELD_POS */
- if (detect_custom_layout(ct, sflags, byteoffset, foffset,
+ if (detect_custom_layout(ct, sflags, byteoffset, foffset,
"wrong offset for field '",
PyText_AS_UTF8(fname), "'") < 0)
goto error;
- byteoffset = foffset;
+ byteoffset = foffset;
}
if (PyText_GetSize(fname) == 0 &&
@@ -5270,7 +5270,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
*previous = _add_field(interned_fields,
get_field_name(ftype, cfsrc),
cfsrc->cf_type,
- byteoffset + cfsrc->cf_offset,
+ byteoffset + cfsrc->cf_offset,
cfsrc->cf_bitshift,
cfsrc->cf_bitsize,
cfsrc->cf_flags | fflags);
@@ -5283,13 +5283,13 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
}
else {
*previous = _add_field(interned_fields, fname, ftype,
- byteoffset, bs_flag, -1, fflags);
+ byteoffset, bs_flag, -1, fflags);
if (*previous == NULL)
goto error;
previous = &(*previous)->cf_next;
}
if (ftype->ct_size >= 0)
- byteoffset += ftype->ct_size;
+ byteoffset += ftype->ct_size;
prev_bitfield_size = 0;
}
else {
@@ -5326,7 +5326,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
/* compute the starting position of the theoretical field
that covers a complete 'ftype', inside of which we will
locate the real bitfield */
- field_offset_bytes = byteoffset;
+ field_offset_bytes = byteoffset;
field_offset_bytes &= ~(falign - 1);
if (fbitsize == 0) {
@@ -5339,13 +5339,13 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
if (!(sflags & SF_MSVC_BITFIELDS)) {
/* GCC's notion of "ftype :0;" */
- /* pad byteoffset to a value aligned for "ftype" */
- if (ROUNDUP_BYTES(byteoffset, bitoffset) > field_offset_bytes) {
+ /* pad byteoffset to a value aligned for "ftype" */
+ if (ROUNDUP_BYTES(byteoffset, bitoffset) > field_offset_bytes) {
field_offset_bytes += falign;
- assert(byteoffset < field_offset_bytes);
+ assert(byteoffset < field_offset_bytes);
}
- byteoffset = field_offset_bytes;
- bitoffset = 0;
+ byteoffset = field_offset_bytes;
+ bitoffset = 0;
}
else {
/* MSVC's notion of "ftype :0;" */
@@ -5362,8 +5362,8 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
/* Can the field start at the offset given by 'boffset'? It
can if it would entirely fit into an aligned ftype field. */
- bits_already_occupied = (byteoffset-field_offset_bytes) * 8
- + bitoffset;
+ bits_already_occupied = (byteoffset-field_offset_bytes) * 8
+ + bitoffset;
if (bits_already_occupied + fbitsize > 8 * ftype->ct_size) {
/* it would not fit, we need to start at the next
@@ -5377,18 +5377,18 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
goto error;
}
field_offset_bytes += falign;
- assert(byteoffset < field_offset_bytes);
- byteoffset = field_offset_bytes;
- bitoffset = 0;
+ assert(byteoffset < field_offset_bytes);
+ byteoffset = field_offset_bytes;
+ bitoffset = 0;
bitshift = 0;
}
else {
bitshift = bits_already_occupied;
assert(bitshift >= 0);
}
- bitoffset += fbitsize;
- byteoffset += (bitoffset >> 3);
- bitoffset &= 7;
+ bitoffset += fbitsize;
+ byteoffset += (bitoffset >> 3);
+ bitoffset &= 7;
}
else {
/* MSVC's algorithm */
@@ -5404,43 +5404,43 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
}
else {
/* no: start a new full field */
- byteoffset = ROUNDUP_BYTES(byteoffset, bitoffset);
- bitoffset = 0;
- /* align */
- byteoffset = (byteoffset + falign-1) & ~(falign-1);
- byteoffset += ftype->ct_size;
+ byteoffset = ROUNDUP_BYTES(byteoffset, bitoffset);
+ bitoffset = 0;
+ /* align */
+ byteoffset = (byteoffset + falign-1) & ~(falign-1);
+ byteoffset += ftype->ct_size;
bitshift = 0;
prev_bitfield_size = ftype->ct_size;
prev_bitfield_free = 8 * prev_bitfield_size;
}
prev_bitfield_free -= fbitsize;
- field_offset_bytes = byteoffset - ftype->ct_size;
+ field_offset_bytes = byteoffset - ftype->ct_size;
}
if (sflags & SF_GCC_BIG_ENDIAN)
bitshift = 8 * ftype->ct_size - fbitsize - bitshift;
- if (PyText_GetSize(fname) > 0) {
-
- *previous = _add_field(interned_fields, fname, ftype,
+ if (PyText_GetSize(fname) > 0) {
+
+ *previous = _add_field(interned_fields, fname, ftype,
field_offset_bytes, bitshift, fbitsize,
fflags);
- if (*previous == NULL)
- goto error;
- previous = &(*previous)->cf_next;
- }
+ if (*previous == NULL)
+ goto error;
+ previous = &(*previous)->cf_next;
+ }
}
}
- assert(bitoffset == (bitoffset & 7));
- if (ROUNDUP_BYTES(byteoffset, bitoffset) > byteoffsetmax)
- byteoffsetmax = ROUNDUP_BYTES(byteoffset, bitoffset);
+ assert(bitoffset == (bitoffset & 7));
+ if (ROUNDUP_BYTES(byteoffset, bitoffset) > byteoffsetmax)
+ byteoffsetmax = ROUNDUP_BYTES(byteoffset, bitoffset);
}
*previous = NULL;
/* Like C, if the size of this structure would be zero, we compute it
as 1 instead. But for ctypes support, we allow the manually-
specified totalsize to be zero in this case. */
- alignedsize = (byteoffsetmax + alignment - 1) & ~(alignment-1);
+ alignedsize = (byteoffsetmax + alignment - 1) & ~(alignment-1);
if (alignedsize == 0)
alignedsize = 1;
@@ -5451,10 +5451,10 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
if (detect_custom_layout(ct, sflags, alignedsize,
totalsize, "wrong total size", "", "") < 0)
goto error;
- if (totalsize < byteoffsetmax) {
+ if (totalsize < byteoffsetmax) {
PyErr_Format(PyExc_TypeError,
"%s cannot be of size %zd: there are fields at least "
- "up to %zd", ct->ct_name, totalsize, byteoffsetmax);
+ "up to %zd", ct->ct_name, totalsize, byteoffsetmax);
goto error;
}
}
@@ -5503,28 +5503,28 @@ static void *fb_alloc(struct funcbuilder_s *fb, Py_ssize_t size)
}
}
-#define SUPPORTED_IN_API_MODE \
- " are only supported as %s if the function is " \
- "'API mode' and non-variadic (i.e. declared inside ffibuilder" \
- ".cdef()+ffibuilder.set_source() and not taking a final '...' " \
- "argument)"
-
-static ffi_type *fb_unsupported(CTypeDescrObject *ct, const char *place,
- const char *detail)
-{
- PyErr_Format(PyExc_NotImplementedError,
- "ctype '%s' not supported as %s. %s. "
- "Such structs" SUPPORTED_IN_API_MODE,
- ct->ct_name, place, detail, place);
- return NULL;
-}
-
+#define SUPPORTED_IN_API_MODE \
+ " are only supported as %s if the function is " \
+ "'API mode' and non-variadic (i.e. declared inside ffibuilder" \
+ ".cdef()+ffibuilder.set_source() and not taking a final '...' " \
+ "argument)"
+
+static ffi_type *fb_unsupported(CTypeDescrObject *ct, const char *place,
+ const char *detail)
+{
+ PyErr_Format(PyExc_NotImplementedError,
+ "ctype '%s' not supported as %s. %s. "
+ "Such structs" SUPPORTED_IN_API_MODE,
+ ct->ct_name, place, detail, place);
+ return NULL;
+}
+
static ffi_type *fb_fill_type(struct funcbuilder_s *fb, CTypeDescrObject *ct,
int is_result_type)
{
const char *place = is_result_type ? "return value" : "argument";
- if (ct->ct_flags & (CT_PRIMITIVE_ANY & ~CT_PRIMITIVE_COMPLEX)) {
+ if (ct->ct_flags & (CT_PRIMITIVE_ANY & ~CT_PRIMITIVE_COMPLEX)) {
return (ffi_type *)ct->ct_extra;
}
else if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) {
@@ -5558,26 +5558,26 @@ static ffi_type *fb_fill_type(struct funcbuilder_s *fb, CTypeDescrObject *ct,
Another reason for CT_CUSTOM_FIELD_POS would be anonymous
nested structures: we lost the information about having it
here, so better safe (and forbid it) than sorry (and maybe
- crash). Note: it seems we only get in this case with
- ffi.verify().
+ crash). Note: it seems we only get in this case with
+ ffi.verify().
*/
if (force_lazy_struct(ct) < 0)
return NULL;
if (ct->ct_flags & CT_CUSTOM_FIELD_POS) {
/* these NotImplementedErrors may be caught and ignored until
a real call is made to a function of this type */
- return fb_unsupported(ct, place,
- "It is a struct declared with \"...;\", but the C "
- "calling convention may depend on the missing fields; "
- "or, it contains anonymous struct/unions");
- }
- /* Another reason: __attribute__((packed)) is not supported by libffi.
- */
- if (ct->ct_flags & CT_WITH_PACKED_CHANGE) {
- return fb_unsupported(ct, place,
- "It is a 'packed' structure, with a different layout than "
- "expected by libffi");
- }
+ return fb_unsupported(ct, place,
+ "It is a struct declared with \"...;\", but the C "
+ "calling convention may depend on the missing fields; "
+ "or, it contains anonymous struct/unions");
+ }
+ /* Another reason: __attribute__((packed)) is not supported by libffi.
+ */
+ if (ct->ct_flags & CT_WITH_PACKED_CHANGE) {
+ return fb_unsupported(ct, place,
+ "It is a 'packed' structure, with a different layout than "
+ "expected by libffi");
+ }
n = PyDict_Size(ct->ct_stuff);
nflat = 0;
@@ -5590,9 +5590,9 @@ static ffi_type *fb_fill_type(struct funcbuilder_s *fb, CTypeDescrObject *ct,
CTypeDescrObject *ct1;
assert(cf != NULL);
if (cf->cf_bitshift >= 0) {
- return fb_unsupported(ct, place,
- "It is a struct with bit fields, which libffi does not "
- "support");
+ return fb_unsupported(ct, place,
+ "It is a struct with bit fields, which libffi does not "
+ "support");
}
flat = 1;
ct1 = cf->cf_type;
@@ -5601,9 +5601,9 @@ static ffi_type *fb_fill_type(struct funcbuilder_s *fb, CTypeDescrObject *ct,
ct1 = ct1->ct_itemdescr;
}
if (flat <= 0) {
- return fb_unsupported(ct, place,
- "It is a struct with a zero-length array, which libffi "
- "does not support");
+ return fb_unsupported(ct, place,
+ "It is a struct with a zero-length array, which libffi "
+ "does not support");
}
nflat += flat;
cf = cf->cf_next;
@@ -5642,24 +5642,24 @@ static ffi_type *fb_fill_type(struct funcbuilder_s *fb, CTypeDescrObject *ct,
}
return ffistruct;
}
- else if (ct->ct_flags & CT_UNION) {
- PyErr_Format(PyExc_NotImplementedError,
- "ctype '%s' not supported as %s by libffi. "
- "Unions" SUPPORTED_IN_API_MODE,
- ct->ct_name, place, place);
- return NULL;
- }
+ else if (ct->ct_flags & CT_UNION) {
+ PyErr_Format(PyExc_NotImplementedError,
+ "ctype '%s' not supported as %s by libffi. "
+ "Unions" SUPPORTED_IN_API_MODE,
+ ct->ct_name, place, place);
+ return NULL;
+ }
else {
- char *extra = "";
- if (ct->ct_flags & CT_PRIMITIVE_COMPLEX)
- extra = " (the support for complex types inside libffi "
- "is mostly missing at this point, so CFFI only "
- "supports complex types as arguments or return "
- "value in API-mode functions)";
-
+ char *extra = "";
+ if (ct->ct_flags & CT_PRIMITIVE_COMPLEX)
+ extra = " (the support for complex types inside libffi "
+ "is mostly missing at this point, so CFFI only "
+ "supports complex types as arguments or return "
+ "value in API-mode functions)";
+
PyErr_Format(PyExc_NotImplementedError,
- "ctype '%s' (size %zd) not supported as %s%s",
- ct->ct_name, ct->ct_size, place, extra);
+ "ctype '%s' (size %zd) not supported as %s%s",
+ ct->ct_name, ct->ct_size, place, extra);
return NULL;
}
}
@@ -5850,14 +5850,14 @@ static CTypeDescrObject *fb_prepare_ctype(struct funcbuilder_s *fb,
static cif_description_t *fb_prepare_cif(PyObject *fargs,
CTypeDescrObject *fresult,
- Py_ssize_t variadic_nargs_declared,
+ Py_ssize_t variadic_nargs_declared,
ffi_abi fabi)
-
+
{
char *buffer;
cif_description_t *cif_descr;
struct funcbuilder_s funcbuffer;
- ffi_status status = (ffi_status)-1;
+ ffi_status status = (ffi_status)-1;
funcbuffer.nb_bytes = 0;
funcbuffer.bufferp = NULL;
@@ -5880,24 +5880,24 @@ static cif_description_t *fb_prepare_cif(PyObject *fargs,
assert(funcbuffer.bufferp == buffer + funcbuffer.nb_bytes);
cif_descr = (cif_description_t *)buffer;
-
- /* use `ffi_prep_cif_var` if necessary and available */
-#if CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE
- if (variadic_nargs_declared >= 0) {
- if (CFFI_CHECK_FFI_PREP_CIF_VAR) {
- status = ffi_prep_cif_var(&cif_descr->cif, fabi,
- variadic_nargs_declared, funcbuffer.nargs,
- funcbuffer.rtype, funcbuffer.atypes);
- }
- }
-#endif
-
- if (status == (ffi_status)-1) {
- status = ffi_prep_cif(&cif_descr->cif, fabi, funcbuffer.nargs,
- funcbuffer.rtype, funcbuffer.atypes);
- }
-
- if (status != FFI_OK) {
+
+ /* use `ffi_prep_cif_var` if necessary and available */
+#if CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE
+ if (variadic_nargs_declared >= 0) {
+ if (CFFI_CHECK_FFI_PREP_CIF_VAR) {
+ status = ffi_prep_cif_var(&cif_descr->cif, fabi,
+ variadic_nargs_declared, funcbuffer.nargs,
+ funcbuffer.rtype, funcbuffer.atypes);
+ }
+ }
+#endif
+
+ if (status == (ffi_status)-1) {
+ status = ffi_prep_cif(&cif_descr->cif, fabi, funcbuffer.nargs,
+ funcbuffer.rtype, funcbuffer.atypes);
+ }
+
+ if (status != FFI_OK) {
PyErr_SetString(PyExc_SystemError,
"libffi failed to build this function type");
goto error;
@@ -5941,7 +5941,7 @@ static PyObject *new_function_type(PyObject *fargs, /* tuple */
is computed here. */
cif_description_t *cif_descr;
- cif_descr = fb_prepare_cif(fargs, fresult, -1, fabi);
+ cif_descr = fb_prepare_cif(fargs, fresult, -1, fabi);
if (cif_descr == NULL) {
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) {
PyErr_Clear(); /* will get the exception if we see an
@@ -6046,14 +6046,14 @@ static int convert_from_object_fficallback(char *result,
return 0;
}
else if (ctype->ct_flags & (CT_PRIMITIVE_CHAR | CT_PRIMITIVE_SIGNED |
- CT_PRIMITIVE_UNSIGNED |
- CT_POINTER | CT_FUNCTIONPTR)) {
+ CT_PRIMITIVE_UNSIGNED |
+ CT_POINTER | CT_FUNCTIONPTR)) {
/* zero extension: fill the '*result' with zeros, and (on big-
- endian machines) correct the 'result' pointer to write to.
- We also do that for pointers, even though we're normally not
- in this branch because ctype->ct_size == sizeof(ffi_arg) for
- pointers---except on some architectures like x32 (issue #372).
- */
+ endian machines) correct the 'result' pointer to write to.
+ We also do that for pointers, even though we're normally not
+ in this branch because ctype->ct_size == sizeof(ffi_arg) for
+ pointers---except on some architectures like x32 (issue #372).
+ */
memset(result, 0, sizeof(ffi_arg));
#ifdef WORDS_BIGENDIAN
result += (sizeof(ffi_arg) - ctype->ct_size);
@@ -6064,49 +6064,49 @@ static int convert_from_object_fficallback(char *result,
return convert_from_object(result, ctype, pyobj);
}
-static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
- char *objdescr, PyObject *obj,
+static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
+ char *objdescr, PyObject *obj,
char *extra_error_line)
{
/* like PyErr_WriteUnraisable(), but write a full traceback */
-#ifdef USE_WRITEUNRAISABLEMSG
-
- /* PyErr_WriteUnraisable actually writes the full traceback anyway
- from Python 3.4, but we can't really get the formatting of the
- custom text to be what we want. We can do better from Python
- 3.8 by calling the new _PyErr_WriteUnraisableMsg().
- Luckily it's also Python 3.8 that adds new functionality that
- people might want: the new sys.unraisablehook().
- */
- PyObject *s;
- int first_char;
- assert(objdescr != NULL && objdescr[0] != 0); /* non-empty */
- first_char = objdescr[0];
- if (first_char >= 'A' && first_char <= 'Z')
- first_char += 'a' - 'A'; /* lower() the very first character */
- if (extra_error_line == NULL)
- extra_error_line = "";
-
- if (obj != NULL)
- s = PyUnicode_FromFormat("%c%s%R%s",
- first_char, objdescr + 1, obj, extra_error_line);
- else
- s = PyUnicode_FromFormat("%c%s%s",
- first_char, objdescr + 1, extra_error_line);
-
- PyErr_Restore(t, v, tb);
- if (s != NULL) {
- _PyErr_WriteUnraisableMsg(PyText_AS_UTF8(s), NULL);
- Py_DECREF(s);
- }
- else
- PyErr_WriteUnraisable(obj); /* best effort */
- PyErr_Clear();
-
-#else
-
- /* version for Python 2.7 and < 3.8 */
- PyObject *f;
+#ifdef USE_WRITEUNRAISABLEMSG
+
+ /* PyErr_WriteUnraisable actually writes the full traceback anyway
+ from Python 3.4, but we can't really get the formatting of the
+ custom text to be what we want. We can do better from Python
+ 3.8 by calling the new _PyErr_WriteUnraisableMsg().
+ Luckily it's also Python 3.8 that adds new functionality that
+ people might want: the new sys.unraisablehook().
+ */
+ PyObject *s;
+ int first_char;
+ assert(objdescr != NULL && objdescr[0] != 0); /* non-empty */
+ first_char = objdescr[0];
+ if (first_char >= 'A' && first_char <= 'Z')
+ first_char += 'a' - 'A'; /* lower() the very first character */
+ if (extra_error_line == NULL)
+ extra_error_line = "";
+
+ if (obj != NULL)
+ s = PyUnicode_FromFormat("%c%s%R%s",
+ first_char, objdescr + 1, obj, extra_error_line);
+ else
+ s = PyUnicode_FromFormat("%c%s%s",
+ first_char, objdescr + 1, extra_error_line);
+
+ PyErr_Restore(t, v, tb);
+ if (s != NULL) {
+ _PyErr_WriteUnraisableMsg(PyText_AS_UTF8(s), NULL);
+ Py_DECREF(s);
+ }
+ else
+ PyErr_WriteUnraisable(obj); /* best effort */
+ PyErr_Clear();
+
+#else
+
+ /* version for Python 2.7 and < 3.8 */
+ PyObject *f;
#if PY_MAJOR_VERSION >= 3
/* jump through hoops to ensure the tb is attached to v, on Python 3 */
PyErr_NormalizeException(&t, &v, &tb);
@@ -6130,8 +6130,8 @@ static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
Py_XDECREF(t);
Py_XDECREF(v);
Py_XDECREF(tb);
-
-#endif
+
+#endif
}
static void general_invoke_callback(int decode_args_from_libffi,
@@ -6181,11 +6181,11 @@ static void general_invoke_callback(int decode_args_from_libffi,
goto error;
if (convert_from_object_fficallback(result, SIGNATURE(1), py_res,
decode_args_from_libffi) < 0) {
-#ifdef USE_WRITEUNRAISABLEMSG
- extra_error_line = ", trying to convert the result back to C";
-#else
+#ifdef USE_WRITEUNRAISABLEMSG
+ extra_error_line = ", trying to convert the result back to C";
+#else
extra_error_line = "Trying to convert the result back to C:\n";
-#endif
+#endif
goto error;
}
done:
@@ -6202,12 +6202,12 @@ static void general_invoke_callback(int decode_args_from_libffi,
}
onerror_cb = PyTuple_GET_ITEM(cb_args, 3);
if (onerror_cb == Py_None) {
- PyObject *ecap, *t, *v, *tb;
- PyErr_Fetch(&t, &v, &tb);
- ecap = _cffi_start_error_capture();
- _my_PyErr_WriteUnraisable(t, v, tb, "From cffi callback ", py_ob,
+ PyObject *ecap, *t, *v, *tb;
+ PyErr_Fetch(&t, &v, &tb);
+ ecap = _cffi_start_error_capture();
+ _my_PyErr_WriteUnraisable(t, v, tb, "From cffi callback ", py_ob,
extra_error_line);
- _cffi_stop_error_capture(ecap);
+ _cffi_stop_error_capture(ecap);
}
else {
PyObject *exc1, *val1, *tb1, *res1, *exc2, *val2, *tb2;
@@ -6231,23 +6231,23 @@ static void general_invoke_callback(int decode_args_from_libffi,
}
else {
/* double exception! print a double-traceback... */
- PyObject *ecap;
+ PyObject *ecap;
PyErr_Fetch(&exc2, &val2, &tb2);
- ecap = _cffi_start_error_capture();
- _my_PyErr_WriteUnraisable(exc1, val1, tb1,
- "From cffi callback ", py_ob,
+ ecap = _cffi_start_error_capture();
+ _my_PyErr_WriteUnraisable(exc1, val1, tb1,
+ "From cffi callback ", py_ob,
extra_error_line);
-#ifdef USE_WRITEUNRAISABLEMSG
- _my_PyErr_WriteUnraisable(exc2, val2, tb2,
- "during handling of the above exception by 'onerror'",
- NULL, NULL);
-#else
+#ifdef USE_WRITEUNRAISABLEMSG
+ _my_PyErr_WriteUnraisable(exc2, val2, tb2,
+ "during handling of the above exception by 'onerror'",
+ NULL, NULL);
+#else
extra_error_line = ("\nDuring the call to 'onerror', "
"another exception occurred:\n\n");
- _my_PyErr_WriteUnraisable(exc2, val2, tb2,
- NULL, NULL, extra_error_line);
-#endif
- _cffi_stop_error_capture(ecap);
+ _my_PyErr_WriteUnraisable(exc2, val2, tb2,
+ NULL, NULL, extra_error_line);
+#endif
+ _cffi_stop_error_capture(ecap);
}
}
goto done;
@@ -6314,38 +6314,38 @@ static PyObject *prepare_callback_info_tuple(CTypeDescrObject *ct,
infotuple = Py_BuildValue("OOOO", ct, ob, py_rawerr, onerror_ob);
Py_DECREF(py_rawerr);
-#if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000
+#if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000
/* We must setup the GIL here, in case the callback is invoked in
- some other non-Pythonic thread. This is the same as ctypes.
- But PyEval_InitThreads() is always a no-op from CPython 3.7
- (the call from ctypes was removed some time later I think). */
+ some other non-Pythonic thread. This is the same as ctypes.
+ But PyEval_InitThreads() is always a no-op from CPython 3.7
+ (the call from ctypes was removed some time later I think). */
PyEval_InitThreads();
#endif
return infotuple;
}
-/* messily try to silence a gcc/clang deprecation warning for
- ffi_prep_closure. Don't miss the "pragma pop" after the function.
- This is done around the whole function because very old GCCs don't
- support it inside a function. */
-#if defined(__clang__)
-# pragma clang diagnostic push
-# pragma clang diagnostic ignored "-Wdeprecated-declarations"
-#elif defined(__GNUC__)
-# pragma GCC diagnostic push
-# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-#endif
+/* messily try to silence a gcc/clang deprecation warning for
+ ffi_prep_closure. Don't miss the "pragma pop" after the function.
+ This is done around the whole function because very old GCCs don't
+ support it inside a function. */
+#if defined(__clang__)
+# pragma clang diagnostic push
+# pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#elif defined(__GNUC__)
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
static PyObject *b_callback(PyObject *self, PyObject *args)
{
CTypeDescrObject *ct;
- CDataObject_closure *cd;
+ CDataObject_closure *cd;
PyObject *ob, *error_ob = Py_None, *onerror_ob = Py_None;
PyObject *infotuple;
cif_description_t *cif_descr;
ffi_closure *closure;
- ffi_status status;
- void *closure_exec;
+ ffi_status status;
+ void *closure_exec;
if (!PyArg_ParseTuple(args, "O!O|OO:callback", &CTypeDescr_Type, &ct, &ob,
&error_ob, &onerror_ob))
@@ -6355,34 +6355,34 @@ static PyObject *b_callback(PyObject *self, PyObject *args)
if (infotuple == NULL)
return NULL;
-#if CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE
- if (CFFI_CHECK_FFI_CLOSURE_ALLOC) {
- closure = ffi_closure_alloc(sizeof(ffi_closure), &closure_exec);
- } else
-#endif
- {
- closure = cffi_closure_alloc();
- closure_exec = closure;
- }
-
- if (closure == NULL) {
- Py_DECREF(infotuple);
- PyErr_SetString(PyExc_MemoryError,
- "Cannot allocate write+execute memory for ffi.callback(). "
- "You might be running on a system that prevents this. "
- "For more information, see "
- "https://cffi.readthedocs.io/en/latest/using.html#callbacks");
- return NULL;
- }
- cd = PyObject_GC_New(CDataObject_closure, &CDataOwningGC_Type);
+#if CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE
+ if (CFFI_CHECK_FFI_CLOSURE_ALLOC) {
+ closure = ffi_closure_alloc(sizeof(ffi_closure), &closure_exec);
+ } else
+#endif
+ {
+ closure = cffi_closure_alloc();
+ closure_exec = closure;
+ }
+
+ if (closure == NULL) {
+ Py_DECREF(infotuple);
+ PyErr_SetString(PyExc_MemoryError,
+ "Cannot allocate write+execute memory for ffi.callback(). "
+ "You might be running on a system that prevents this. "
+ "For more information, see "
+ "https://cffi.readthedocs.io/en/latest/using.html#callbacks");
+ return NULL;
+ }
+ cd = PyObject_GC_New(CDataObject_closure, &CDataOwningGC_Type);
if (cd == NULL)
goto error;
Py_INCREF(ct);
- cd->head.c_type = ct;
- cd->head.c_data = (char *)closure_exec;
- cd->head.c_weakreflist = NULL;
- closure->user_data = NULL;
- cd->closure = closure;
+ cd->head.c_type = ct;
+ cd->head.c_data = (char *)closure_exec;
+ cd->head.c_weakreflist = NULL;
+ closure->user_data = NULL;
+ cd->closure = closure;
cif_descr = (cif_description_t *)ct->ct_extra;
if (cif_descr == NULL) {
@@ -6391,30 +6391,30 @@ static PyObject *b_callback(PyObject *self, PyObject *args)
"return type or with '...'", ct->ct_name);
goto error;
}
-
-#if CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE
- if (CFFI_CHECK_FFI_PREP_CLOSURE_LOC) {
- status = ffi_prep_closure_loc(closure, &cif_descr->cif,
- invoke_callback, infotuple, closure_exec);
- }
- else
-#endif
- {
-#if defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE) && !FFI_LEGACY_CLOSURE_API
- PyErr_Format(PyExc_SystemError, "ffi_prep_closure_loc() is missing");
- goto error;
-#else
- status = ffi_prep_closure(closure, &cif_descr->cif,
- invoke_callback, infotuple);
-#endif
- }
-
- if (status != FFI_OK) {
+
+#if CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE
+ if (CFFI_CHECK_FFI_PREP_CLOSURE_LOC) {
+ status = ffi_prep_closure_loc(closure, &cif_descr->cif,
+ invoke_callback, infotuple, closure_exec);
+ }
+ else
+#endif
+ {
+#if defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE) && !FFI_LEGACY_CLOSURE_API
+ PyErr_Format(PyExc_SystemError, "ffi_prep_closure_loc() is missing");
+ goto error;
+#else
+ status = ffi_prep_closure(closure, &cif_descr->cif,
+ invoke_callback, infotuple);
+#endif
+ }
+
+ if (status != FFI_OK) {
PyErr_SetString(PyExc_SystemError,
"libffi failed to build this callback");
goto error;
}
-
+
if (closure->user_data != infotuple) {
/* Issue #266. Should not occur, but could, if we are using
at runtime a version of libffi compiled with a different
@@ -6429,30 +6429,30 @@ static PyObject *b_callback(PyObject *self, PyObject *args)
"different from the 'ffi.h' file seen at compile-time)");
goto error;
}
- PyObject_GC_Track(cd);
+ PyObject_GC_Track(cd);
return (PyObject *)cd;
error:
closure->user_data = NULL;
- if (cd == NULL) {
-#if CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE
- if (CFFI_CHECK_FFI_CLOSURE_ALLOC) {
- ffi_closure_free(closure);
- }
- else
-#endif
- cffi_closure_free(closure);
- }
+ if (cd == NULL) {
+#if CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE
+ if (CFFI_CHECK_FFI_CLOSURE_ALLOC) {
+ ffi_closure_free(closure);
+ }
+ else
+#endif
+ cffi_closure_free(closure);
+ }
else
Py_DECREF(cd);
Py_XDECREF(infotuple);
return NULL;
}
-#if defined(__clang__)
-# pragma clang diagnostic pop
-#elif defined(__GNUC__)
-# pragma GCC diagnostic pop
-#endif
+#if defined(__clang__)
+# pragma clang diagnostic pop
+#elif defined(__GNUC__)
+# pragma GCC diagnostic pop
+#endif
static PyObject *b_new_enum_type(PyObject *self, PyObject *args)
{
@@ -6498,7 +6498,7 @@ static PyObject *b_new_enum_type(PyObject *self, PyObject *args)
if (!PyText_Check(tmpkey)) {
#if PY_MAJOR_VERSION < 3
if (PyUnicode_Check(tmpkey)) {
- const char *text = PyText_AsUTF8(tmpkey);
+ const char *text = PyText_AsUTF8(tmpkey);
if (text == NULL)
goto error;
Py_DECREF(tmpkey);
@@ -6758,8 +6758,8 @@ static PyObject *b_string(PyObject *self, PyObject *args, PyObject *kwds)
if (cd->c_type->ct_itemdescr != NULL &&
cd->c_type->ct_itemdescr->ct_flags & (CT_PRIMITIVE_CHAR |
CT_PRIMITIVE_SIGNED |
- CT_PRIMITIVE_UNSIGNED) &&
- !(cd->c_type->ct_itemdescr->ct_flags & CT_IS_BOOL)) {
+ CT_PRIMITIVE_UNSIGNED) &&
+ !(cd->c_type->ct_itemdescr->ct_flags & CT_IS_BOOL)) {
Py_ssize_t length = maxlen;
if (cd->c_data == NULL) {
PyObject *s = cdata_repr(cd);
@@ -6791,44 +6791,44 @@ static PyObject *b_string(PyObject *self, PyObject *args, PyObject *kwds)
return PyBytes_FromStringAndSize(start, length);
}
else if (cd->c_type->ct_itemdescr->ct_flags & CT_PRIMITIVE_CHAR) {
- switch (cd->c_type->ct_itemdescr->ct_size) {
- case 2: {
- const cffi_char16_t *start = (cffi_char16_t *)cd->c_data;
- if (length < 0) {
- /*READ(start, 2)*/
- length = 0;
- while (start[length])
- length++;
- /*READ(start, 2 * length)*/
- }
- else {
- /*READ(start, 2 * length)*/
- maxlen = length;
- length = 0;
- while (length < maxlen && start[length])
- length++;
- }
- return _my_PyUnicode_FromChar16(start, length);
+ switch (cd->c_type->ct_itemdescr->ct_size) {
+ case 2: {
+ const cffi_char16_t *start = (cffi_char16_t *)cd->c_data;
+ if (length < 0) {
+ /*READ(start, 2)*/
+ length = 0;
+ while (start[length])
+ length++;
+ /*READ(start, 2 * length)*/
+ }
+ else {
+ /*READ(start, 2 * length)*/
+ maxlen = length;
+ length = 0;
+ while (length < maxlen && start[length])
+ length++;
+ }
+ return _my_PyUnicode_FromChar16(start, length);
+ }
+ case 4: {
+ const cffi_char32_t *start = (cffi_char32_t *)cd->c_data;
+ if (length < 0) {
+ /*READ(start, 4)*/
+ length = 0;
+ while (start[length])
+ length++;
+ /*READ(start, 4 * length)*/
+ }
+ else {
+ /*READ(start, 4 * length)*/
+ maxlen = length;
+ length = 0;
+ while (length < maxlen && start[length])
+ length++;
+ }
+ return _my_PyUnicode_FromChar32(start, length);
}
- case 4: {
- const cffi_char32_t *start = (cffi_char32_t *)cd->c_data;
- if (length < 0) {
- /*READ(start, 4)*/
- length = 0;
- while (start[length])
- length++;
- /*READ(start, 4 * length)*/
- }
- else {
- /*READ(start, 4 * length)*/
- maxlen = length;
- length = 0;
- while (length < maxlen && start[length])
- length++;
- }
- return _my_PyUnicode_FromChar32(start, length);
}
- }
}
}
else if (cd->c_type->ct_flags & CT_IS_ENUM) {
@@ -6844,12 +6844,12 @@ static PyObject *b_string(PyObject *self, PyObject *args, PyObject *kwds)
if (cd->c_type->ct_size == sizeof(char))
return PyBytes_FromStringAndSize(cd->c_data, 1);
else if (cd->c_type->ct_flags & CT_PRIMITIVE_CHAR) {
- switch (cd->c_type->ct_size) {
- case 2:
- return _my_PyUnicode_FromChar16((cffi_char16_t *)cd->c_data, 1);
- case 4:
- return _my_PyUnicode_FromChar32((cffi_char32_t *)cd->c_data, 1);
- }
+ switch (cd->c_type->ct_size) {
+ case 2:
+ return _my_PyUnicode_FromChar16((cffi_char16_t *)cd->c_data, 1);
+ case 4:
+ return _my_PyUnicode_FromChar32((cffi_char32_t *)cd->c_data, 1);
+ }
}
}
PyErr_Format(PyExc_TypeError, "string(): unexpected cdata '%s' argument",
@@ -6895,14 +6895,14 @@ static PyObject *b_unpack(PyObject *self, PyObject *args, PyObject *kwds)
/* byte- and unicode strings */
ctitem = cd->c_type->ct_itemdescr;
if (ctitem->ct_flags & CT_PRIMITIVE_CHAR) {
- switch (ctitem->ct_size) {
- case sizeof(char):
+ switch (ctitem->ct_size) {
+ case sizeof(char):
return PyBytes_FromStringAndSize(cd->c_data, length);
- case 2:
- return _my_PyUnicode_FromChar16((cffi_char16_t *)cd->c_data,length);
- case 4:
- return _my_PyUnicode_FromChar32((cffi_char32_t *)cd->c_data,length);
- }
+ case 2:
+ return _my_PyUnicode_FromChar16((cffi_char16_t *)cd->c_data,length);
+ case 4:
+ return _my_PyUnicode_FromChar32((cffi_char32_t *)cd->c_data,length);
+ }
}
/* else, the result is a list. This implementation should be
@@ -6918,7 +6918,7 @@ static PyObject *b_unpack(PyObject *self, PyObject *args, PyObject *kwds)
src = cd->c_data;
itemsize = ctitem->ct_size;
if (itemsize < 0) {
- Py_DECREF(result);
+ Py_DECREF(result);
PyErr_Format(PyExc_ValueError, "'%s' points to items of unknown size",
cd->c_type->ct_name);
return NULL;
@@ -6951,8 +6951,8 @@ static PyObject *b_unpack(PyObject *self, PyObject *args, PyObject *kwds)
/* Note: we never pick case 6 if sizeof(int) == sizeof(long),
so that case 6 below can assume that the 'unsigned int' result
would always fit in a 'signed long'. */
- if (ctitem->ct_flags & CT_IS_BOOL) casenum = 11;
- else if (itemsize == sizeof(unsigned long)) casenum = 7;
+ if (ctitem->ct_flags & CT_IS_BOOL) casenum = 11;
+ else if (itemsize == sizeof(unsigned long)) casenum = 7;
else if (itemsize == sizeof(unsigned int)) casenum = 6;
else if (itemsize == sizeof(unsigned short)) casenum = 5;
else if (itemsize == sizeof(unsigned char)) casenum = 4;
@@ -6985,13 +6985,13 @@ static PyObject *b_unpack(PyObject *self, PyObject *args, PyObject *kwds)
case 8: x = PyFloat_FromDouble(*(float *)src); break;
case 9: x = PyFloat_FromDouble(*(double *)src); break;
case 10: x = new_simple_cdata(*(char **)src, ctitem); break;
- case 11:
- switch (*(unsigned char *)src) {
- case 0: x = Py_False; Py_INCREF(x); break;
- case 1: x = Py_True; Py_INCREF(x); break;
- default: x = convert_to_object(src, ctitem); /* error */
- }
- break;
+ case 11:
+ switch (*(unsigned char *)src) {
+ case 0: x = Py_False; Py_INCREF(x); break;
+ case 1: x = Py_True; Py_INCREF(x); break;
+ default: x = convert_to_object(src, ctitem); /* error */
+ }
+ break;
}
if (x == NULL) {
Py_DECREF(result);
@@ -7003,10 +7003,10 @@ static PyObject *b_unpack(PyObject *self, PyObject *args, PyObject *kwds)
return result;
}
-static PyObject *
-b_buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+static PyObject *
+b_buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- /* this is the constructor of the type implemented in minibuffer.h */
+ /* this is the constructor of the type implemented in minibuffer.h */
CDataObject *cd;
Py_ssize_t size = -1;
static char *keywords[] = {"cdata", "size", NULL};
@@ -7074,7 +7074,7 @@ static PyObject *newp_handle(CTypeDescrObject *ct_voidp, PyObject *x)
&CDataOwningGC_Type);
if (cd == NULL)
return NULL;
- Py_INCREF(ct_voidp); /* must be "void *" */
+ Py_INCREF(ct_voidp); /* must be "void *" */
cd->head.c_type = ct_voidp;
cd->head.c_data = (char *)cd;
cd->head.c_weakreflist = NULL;
@@ -7187,27 +7187,27 @@ static int _my_PyObject_GetContiguousBuffer(PyObject *x, Py_buffer *view,
return 0;
}
-static PyObject *direct_from_buffer(CTypeDescrObject *ct, PyObject *x,
- int require_writable)
+static PyObject *direct_from_buffer(CTypeDescrObject *ct, PyObject *x,
+ int require_writable)
{
- CDataObject *cd;
- Py_buffer *view;
- Py_ssize_t arraylength, minimumlength = 0;
+ CDataObject *cd;
+ Py_buffer *view;
+ Py_ssize_t arraylength, minimumlength = 0;
- if (!(ct->ct_flags & (CT_ARRAY | CT_POINTER))) {
- PyErr_Format(PyExc_TypeError,
- "expected a pointer or array ctype, got '%s'",
- ct->ct_name);
- return NULL;
+ if (!(ct->ct_flags & (CT_ARRAY | CT_POINTER))) {
+ PyErr_Format(PyExc_TypeError,
+ "expected a pointer or array ctype, got '%s'",
+ ct->ct_name);
+ return NULL;
}
- /* PyPy 5.7 can obtain buffers for string (python 2)
- or bytes (python 3). from_buffer(u"foo") is disallowed.
- */
- if (PyUnicode_Check(x)) {
+ /* PyPy 5.7 can obtain buffers for string (python 2)
+ or bytes (python 3). from_buffer(u"foo") is disallowed.
+ */
+ if (PyUnicode_Check(x)) {
PyErr_SetString(PyExc_TypeError,
- "from_buffer() cannot return the address "
- "of a unicode object");
+ "from_buffer() cannot return the address "
+ "of a unicode object");
return NULL;
}
@@ -7216,54 +7216,54 @@ static PyObject *direct_from_buffer(CTypeDescrObject *ct, PyObject *x,
PyErr_NoMemory();
return NULL;
}
- if (_my_PyObject_GetContiguousBuffer(x, view, require_writable) < 0)
+ if (_my_PyObject_GetContiguousBuffer(x, view, require_writable) < 0)
goto error1;
- if (ct->ct_flags & CT_POINTER)
- {
- arraylength = view->len; /* number of bytes, not used so far */
- }
- else {
- /* ct->ct_flags & CT_ARRAY */
- if (ct->ct_length >= 0) {
- /* it's an array with a fixed length; make sure that the
- buffer contains enough bytes. */
- minimumlength = ct->ct_size;
- arraylength = ct->ct_length;
- }
- else {
- /* it's an open 'array[]' */
- if (ct->ct_itemdescr->ct_size == 1) {
- /* fast path, performance only */
- arraylength = view->len;
- }
- else if (ct->ct_itemdescr->ct_size > 0) {
- /* give it as many items as fit the buffer. Ignore a
- partial last element. */
- arraylength = view->len / ct->ct_itemdescr->ct_size;
- }
- else {
- /* it's an array 'empty[]'. Unsupported obscure case:
- the problem is that setting the length of the result
- to anything large (like SSIZE_T_MAX) is dangerous,
- because if someone tries to loop over it, it will
- turn effectively into an infinite loop. */
- PyErr_Format(PyExc_ZeroDivisionError,
- "from_buffer('%s', ..): the actual length of the array "
- "cannot be computed", ct->ct_name);
- goto error2;
- }
- }
- }
- if (view->len < minimumlength) {
- PyErr_Format(PyExc_ValueError,
- "buffer is too small (%zd bytes) for '%s' (%zd bytes)",
- view->len, ct->ct_name, minimumlength);
- goto error2;
- }
-
- cd = (CDataObject *)PyObject_GC_New(CDataObject_frombuf,
- &CDataFromBuf_Type);
+ if (ct->ct_flags & CT_POINTER)
+ {
+ arraylength = view->len; /* number of bytes, not used so far */
+ }
+ else {
+ /* ct->ct_flags & CT_ARRAY */
+ if (ct->ct_length >= 0) {
+ /* it's an array with a fixed length; make sure that the
+ buffer contains enough bytes. */
+ minimumlength = ct->ct_size;
+ arraylength = ct->ct_length;
+ }
+ else {
+ /* it's an open 'array[]' */
+ if (ct->ct_itemdescr->ct_size == 1) {
+ /* fast path, performance only */
+ arraylength = view->len;
+ }
+ else if (ct->ct_itemdescr->ct_size > 0) {
+ /* give it as many items as fit the buffer. Ignore a
+ partial last element. */
+ arraylength = view->len / ct->ct_itemdescr->ct_size;
+ }
+ else {
+ /* it's an array 'empty[]'. Unsupported obscure case:
+ the problem is that setting the length of the result
+ to anything large (like SSIZE_T_MAX) is dangerous,
+ because if someone tries to loop over it, it will
+ turn effectively into an infinite loop. */
+ PyErr_Format(PyExc_ZeroDivisionError,
+ "from_buffer('%s', ..): the actual length of the array "
+ "cannot be computed", ct->ct_name);
+ goto error2;
+ }
+ }
+ }
+ if (view->len < minimumlength) {
+ PyErr_Format(PyExc_ValueError,
+ "buffer is too small (%zd bytes) for '%s' (%zd bytes)",
+ view->len, ct->ct_name, minimumlength);
+ goto error2;
+ }
+
+ cd = (CDataObject *)PyObject_GC_New(CDataObject_frombuf,
+ &CDataFromBuf_Type);
if (cd == NULL)
goto error2;
@@ -7271,8 +7271,8 @@ static PyObject *direct_from_buffer(CTypeDescrObject *ct, PyObject *x,
cd->c_type = ct;
cd->c_data = view->buf;
cd->c_weakreflist = NULL;
- ((CDataObject_frombuf *)cd)->length = arraylength;
- ((CDataObject_frombuf *)cd)->bufferview = view;
+ ((CDataObject_frombuf *)cd)->length = arraylength;
+ ((CDataObject_frombuf *)cd)->bufferview = view;
PyObject_GC_Track(cd);
return (PyObject *)cd;
@@ -7287,13 +7287,13 @@ static PyObject *b_from_buffer(PyObject *self, PyObject *args)
{
CTypeDescrObject *ct;
PyObject *x;
- int require_writable = 0;
+ int require_writable = 0;
- if (!PyArg_ParseTuple(args, "O!O|i", &CTypeDescr_Type, &ct, &x,
- &require_writable))
+ if (!PyArg_ParseTuple(args, "O!O|i", &CTypeDescr_Type, &ct, &x,
+ &require_writable))
return NULL;
- return direct_from_buffer(ct, x, require_writable);
+ return direct_from_buffer(ct, x, require_writable);
}
static int _fetch_as_buffer(PyObject *x, Py_buffer *view, int writable_only)
@@ -7360,12 +7360,12 @@ static PyObject *b_gcp(PyObject *self, PyObject *args, PyObject *kwds)
CDataObject *cd;
CDataObject *origobj;
PyObject *destructor;
- Py_ssize_t ignored; /* for pypy */
- static char *keywords[] = {"cdata", "destructor", "size", NULL};
+ Py_ssize_t ignored; /* for pypy */
+ static char *keywords[] = {"cdata", "destructor", "size", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O|n:gc", keywords,
- &CData_Type, &origobj, &destructor,
- &ignored))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O|n:gc", keywords,
+ &CData_Type, &origobj, &destructor,
+ &ignored))
return NULL;
if (destructor == Py_None) {
@@ -7383,15 +7383,15 @@ static PyObject *b_gcp(PyObject *self, PyObject *args, PyObject *kwds)
return (PyObject *)cd;
}
-static PyObject *b_release(PyObject *self, PyObject *arg)
-{
- if (!CData_Check(arg)) {
- PyErr_SetString(PyExc_TypeError, "expected a 'cdata' object");
- return NULL;
- }
- return cdata_exit(arg, NULL);
-}
-
+static PyObject *b_release(PyObject *self, PyObject *arg)
+{
+ if (!CData_Check(arg)) {
+ PyErr_SetString(PyExc_TypeError, "expected a 'cdata' object");
+ return NULL;
+ }
+ return cdata_exit(arg, NULL);
+}
+
/************************************************************/
static char _testfunc0(char a, char b)
@@ -7566,20 +7566,20 @@ static int _testfunc23(char *p)
return -42;
}
-#if 0 /* libffi doesn't properly support complexes currently */
- /* also, MSVC might not support _Complex... */
- /* if this is enabled one day, remember to also add _Complex
- * arguments in addition to return values. */
-static float _Complex _testfunc24(float a, float b)
-{
- return a + I*2.0*b;
-}
-static double _Complex _testfunc25(double a, double b)
-{
- return a + I*2.0*b;
-}
-#endif
-
+#if 0 /* libffi doesn't properly support complexes currently */
+ /* also, MSVC might not support _Complex... */
+ /* if this is enabled one day, remember to also add _Complex
+ * arguments in addition to return values. */
+static float _Complex _testfunc24(float a, float b)
+{
+ return a + I*2.0*b;
+}
+static double _Complex _testfunc25(double a, double b)
+{
+ return a + I*2.0*b;
+}
+#endif
+
static PyObject *b__testfunc(PyObject *self, PyObject *args)
{
/* for testing only */
@@ -7612,10 +7612,10 @@ static PyObject *b__testfunc(PyObject *self, PyObject *args)
case 21: f = &_testfunc21; break;
case 22: f = &_testfunc22; break;
case 23: f = &_testfunc23; break;
-#if 0
- case 24: f = &_testfunc24; break;
- case 25: f = &_testfunc25; break;
-#endif
+#if 0
+ case 24: f = &_testfunc24; break;
+ case 25: f = &_testfunc25; break;
+#endif
default:
PyErr_SetNone(PyExc_ValueError);
return NULL;
@@ -7717,7 +7717,7 @@ static PyMethodDef FFIBackendMethods[] = {
{"from_buffer", b_from_buffer, METH_VARARGS},
{"memmove", (PyCFunction)b_memmove, METH_VARARGS | METH_KEYWORDS},
{"gcp", (PyCFunction)b_gcp, METH_VARARGS | METH_KEYWORDS},
- {"release", b_release, METH_O},
+ {"release", b_release, METH_O},
#ifdef MS_WIN32
{"getwinerror", (PyCFunction)b_getwinerror, METH_VARARGS | METH_KEYWORDS},
#endif
@@ -7832,51 +7832,51 @@ static PyObject *_cffi_from_c_char(char x) {
return PyBytes_FromStringAndSize(&x, 1);
}
-/* backward-compatibility hack: instead of _cffi_to_c_char16_t() and
- * _cffi_to_c_char32_t(), we have _cffi_to_c_wchar_t() handling whatever
- * size is wchar_t, and _cffi_to_c_wchar3216_t() handling the opposite.
- */
+/* backward-compatibility hack: instead of _cffi_to_c_char16_t() and
+ * _cffi_to_c_char32_t(), we have _cffi_to_c_wchar_t() handling whatever
+ * size is wchar_t, and _cffi_to_c_wchar3216_t() handling the opposite.
+ */
#ifdef HAVE_WCHAR_H
-typedef wchar_t cffi_wchar_t;
-#else
-typedef uint16_t cffi_wchar_t; /* random pick... */
+typedef wchar_t cffi_wchar_t;
+#else
+typedef uint16_t cffi_wchar_t; /* random pick... */
#endif
-static cffi_wchar_t _cffi_to_c_wchar_t(PyObject *init)
-{
- if (sizeof(cffi_wchar_t) == 2)
- return (cffi_wchar_t)_convert_to_char16_t(init);
- else
- return (cffi_wchar_t)_convert_to_char32_t(init);
-}
-static PyObject *_cffi_from_c_wchar_t(cffi_wchar_t x) {
- if (sizeof(cffi_wchar_t) == 2) {
- cffi_char16_t input = x;
- return _my_PyUnicode_FromChar16(&input, 1);
- }
- else {
- cffi_char32_t input = x;
- return _my_PyUnicode_FromChar32(&input, 1);
- }
-}
-static int _cffi_to_c_wchar3216_t(PyObject *init)
-{
- if (sizeof(cffi_wchar_t) == 4)
- return (int)_convert_to_char16_t(init);
- else
- return (int)_convert_to_char32_t(init);
-}
-static PyObject *_cffi_from_c_wchar3216_t(int x) {
- if (sizeof(cffi_wchar_t) == 4) {
- cffi_char16_t input = x;
- return _my_PyUnicode_FromChar16(&input, 1);
- }
- else {
- cffi_char32_t input = x;
- return _my_PyUnicode_FromChar32(&input, 1);
- }
-}
-
+static cffi_wchar_t _cffi_to_c_wchar_t(PyObject *init)
+{
+ if (sizeof(cffi_wchar_t) == 2)
+ return (cffi_wchar_t)_convert_to_char16_t(init);
+ else
+ return (cffi_wchar_t)_convert_to_char32_t(init);
+}
+static PyObject *_cffi_from_c_wchar_t(cffi_wchar_t x) {
+ if (sizeof(cffi_wchar_t) == 2) {
+ cffi_char16_t input = x;
+ return _my_PyUnicode_FromChar16(&input, 1);
+ }
+ else {
+ cffi_char32_t input = x;
+ return _my_PyUnicode_FromChar32(&input, 1);
+ }
+}
+static int _cffi_to_c_wchar3216_t(PyObject *init)
+{
+ if (sizeof(cffi_wchar_t) == 4)
+ return (int)_convert_to_char16_t(init);
+ else
+ return (int)_convert_to_char32_t(init);
+}
+static PyObject *_cffi_from_c_wchar3216_t(int x) {
+ if (sizeof(cffi_wchar_t) == 4) {
+ cffi_char16_t input = x;
+ return _my_PyUnicode_FromChar16(&input, 1);
+ }
+ else {
+ cffi_char32_t input = x;
+ return _my_PyUnicode_FromChar32(&input, 1);
+ }
+}
+
struct _cffi_externpy_s; /* forward declaration */
static void cffi_call_python(struct _cffi_externpy_s *, char *args);
@@ -7900,15 +7900,15 @@ static void *cffi_exports[] = {
convert_to_object,
convert_from_object,
convert_struct_to_owning_object,
- _cffi_to_c_wchar_t,
+ _cffi_to_c_wchar_t,
_cffi_from_c_wchar_t,
_cffi_to_c_long_double,
_cffi_to_c__Bool,
_prepare_pointer_call_argument,
convert_array_from_object,
cffi_call_python,
- _cffi_to_c_wchar3216_t,
- _cffi_from_c_wchar3216_t,
+ _cffi_to_c_wchar3216_t,
+ _cffi_from_c_wchar3216_t,
};
static struct { const char *name; int value; } all_dlopen_flags[] = {
@@ -7962,22 +7962,22 @@ init_cffi_backend(void)
PyObject *m, *v;
int i;
static char init_done = 0;
- static PyTypeObject *all_types[] = {
- &dl_type,
- &CTypeDescr_Type,
- &CField_Type,
- &CData_Type,
- &CDataOwning_Type,
- &CDataOwningGC_Type,
- &CDataFromBuf_Type,
- &CDataGCP_Type,
- &CDataIter_Type,
- &MiniBuffer_Type,
- &FFI_Type,
- &Lib_Type,
- &GlobSupport_Type,
- NULL
- };
+ static PyTypeObject *all_types[] = {
+ &dl_type,
+ &CTypeDescr_Type,
+ &CField_Type,
+ &CData_Type,
+ &CDataOwning_Type,
+ &CDataOwningGC_Type,
+ &CDataFromBuf_Type,
+ &CDataGCP_Type,
+ &CDataIter_Type,
+ &MiniBuffer_Type,
+ &FFI_Type,
+ &Lib_Type,
+ &GlobSupport_Type,
+ NULL
+ };
v = PySys_GetObject("version");
if (v == NULL || !PyText_Check(v) ||
@@ -8003,23 +8003,23 @@ init_cffi_backend(void)
INITERROR;
}
- /* readify all types and add them to the module */
- for (i = 0; all_types[i] != NULL; i++) {
- PyTypeObject *tp = all_types[i];
- PyObject *tpo = (PyObject *)tp;
- if (strncmp(tp->tp_name, "_cffi_backend.", 14) != 0) {
- PyErr_Format(PyExc_ImportError,
- "'%s' is an ill-formed type name", tp->tp_name);
- INITERROR;
- }
- if (PyType_Ready(tp) < 0)
- INITERROR;
-
- Py_INCREF(tpo);
- if (PyModule_AddObject(m, tp->tp_name + 14, tpo) < 0)
- INITERROR;
- }
-
+ /* readify all types and add them to the module */
+ for (i = 0; all_types[i] != NULL; i++) {
+ PyTypeObject *tp = all_types[i];
+ PyObject *tpo = (PyObject *)tp;
+ if (strncmp(tp->tp_name, "_cffi_backend.", 14) != 0) {
+ PyErr_Format(PyExc_ImportError,
+ "'%s' is an ill-formed type name", tp->tp_name);
+ INITERROR;
+ }
+ if (PyType_Ready(tp) < 0)
+ INITERROR;
+
+ Py_INCREF(tpo);
+ if (PyModule_AddObject(m, tp->tp_name + 14, tpo) < 0)
+ INITERROR;
+ }
+
if (!init_done) {
v = PyText_FromString("_cffi_backend");
if (v == NULL || PyDict_SetItemString(CData_Type.tp_dict,
@@ -8067,9 +8067,9 @@ init_cffi_backend(void)
init_cffi_tls();
if (PyErr_Occurred())
INITERROR;
- init_cffi_tls_zombie();
- if (PyErr_Occurred())
- INITERROR;
+ init_cffi_tls_zombie();
+ if (PyErr_Occurred())
+ INITERROR;
if (init_ffi_lib(m) < 0)
INITERROR;
diff --git a/contrib/python/cffi/c/call_python.c b/contrib/python/cffi/c/call_python.c
index 520e026cbf..d3d2e17810 100644
--- a/contrib/python/cffi/c/call_python.c
+++ b/contrib/python/cffi/c/call_python.c
@@ -1,18 +1,18 @@
-#if PY_VERSION_HEX >= 0x03080000
-# define HAVE_PYINTERPSTATE_GETDICT
-#endif
-
-
-static PyObject *_current_interp_key(void)
-{
- PyInterpreterState *interp = PyThreadState_GET()->interp;
-#ifdef HAVE_PYINTERPSTATE_GETDICT
- return PyInterpreterState_GetDict(interp); /* shared reference */
-#else
- return interp->modules;
-#endif
-}
-
+#if PY_VERSION_HEX >= 0x03080000
+# define HAVE_PYINTERPSTATE_GETDICT
+#endif
+
+
+static PyObject *_current_interp_key(void)
+{
+ PyInterpreterState *interp = PyThreadState_GET()->interp;
+#ifdef HAVE_PYINTERPSTATE_GETDICT
+ return PyInterpreterState_GetDict(interp); /* shared reference */
+#else
+ return interp->modules;
+#endif
+}
+
static PyObject *_get_interpstate_dict(void)
{
/* Hack around to return a dict that is subinterpreter-local.
@@ -22,9 +22,9 @@ static PyObject *_get_interpstate_dict(void)
*/
static PyObject *attr_name = NULL;
PyThreadState *tstate;
- PyObject *d, *interpdict;
+ PyObject *d, *interpdict;
int err;
- PyInterpreterState *interp;
+ PyInterpreterState *interp;
tstate = PyThreadState_GET();
if (tstate == NULL) {
@@ -32,13 +32,13 @@ static PyObject *_get_interpstate_dict(void)
return NULL;
}
- interp = tstate->interp;
-#ifdef HAVE_PYINTERPSTATE_GETDICT
- interpdict = PyInterpreterState_GetDict(interp); /* shared reference */
-#else
- interpdict = interp->builtins;
-#endif
- if (interpdict == NULL) {
+ interp = tstate->interp;
+#ifdef HAVE_PYINTERPSTATE_GETDICT
+ interpdict = PyInterpreterState_GetDict(interp); /* shared reference */
+#else
+ interpdict = interp->builtins;
+#endif
+ if (interpdict == NULL) {
/* subinterpreter was cleared already, or is being cleared right now,
to a point that is too much for us to continue */
return NULL;
@@ -52,13 +52,13 @@ static PyObject *_get_interpstate_dict(void)
goto error;
}
- d = PyDict_GetItem(interpdict, attr_name);
+ d = PyDict_GetItem(interpdict, attr_name);
if (d == NULL) {
d = PyDict_New();
if (d == NULL)
goto error;
- err = PyDict_SetItem(interpdict, attr_name, d);
- Py_DECREF(d); /* if successful, there is one ref left in interpdict */
+ err = PyDict_SetItem(interpdict, attr_name, d);
+ Py_DECREF(d); /* if successful, there is one ref left in interpdict */
if (err < 0)
goto error;
}
@@ -71,7 +71,7 @@ static PyObject *_get_interpstate_dict(void)
static PyObject *_ffi_def_extern_decorator(PyObject *outer_args, PyObject *fn)
{
- const char *s;
+ const char *s;
PyObject *error, *onerror, *infotuple, *old1;
int index, err;
const struct _cffi_global_s *g;
@@ -177,7 +177,7 @@ static int _update_cache_to_call_python(struct _cffi_externpy_s *externpy)
if (infotuple == NULL)
return 3; /* no ffi.def_extern() from this subinterpreter */
- new1 = _current_interp_key();
+ new1 = _current_interp_key();
Py_INCREF(new1);
Py_INCREF(infotuple);
old1 = (PyObject *)externpy->reserved1;
@@ -197,20 +197,20 @@ static int _update_cache_to_call_python(struct _cffi_externpy_s *externpy)
#if (defined(WITH_THREAD) && !defined(_MSC_VER) && \
!defined(__amd64__) && !defined(__x86_64__) && \
!defined(__i386__) && !defined(__i386))
-# if defined(HAVE_SYNC_SYNCHRONIZE)
-# define read_barrier() __sync_synchronize()
-# elif defined(_AIX)
-# define read_barrier() __lwsync()
-# elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
-# include <mbarrier.h>
-# define read_barrier() __compiler_barrier()
-# elif defined(__hpux)
-# define read_barrier() _Asm_mf()
-# else
-# define read_barrier() /* missing */
-# warning "no definition for read_barrier(), missing synchronization for\
- multi-thread initialization in embedded mode"
-# endif
+# if defined(HAVE_SYNC_SYNCHRONIZE)
+# define read_barrier() __sync_synchronize()
+# elif defined(_AIX)
+# define read_barrier() __lwsync()
+# elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
+# include <mbarrier.h>
+# define read_barrier() __compiler_barrier()
+# elif defined(__hpux)
+# define read_barrier() _Asm_mf()
+# else
+# define read_barrier() /* missing */
+# warning "no definition for read_barrier(), missing synchronization for\
+ multi-thread initialization in embedded mode"
+# endif
#else
# define read_barrier() (void)0
#endif
@@ -254,10 +254,10 @@ static void cffi_call_python(struct _cffi_externpy_s *externpy, char *args)
save_errno();
/* We need the infotuple here. We could always go through
- _update_cache_to_call_python(), but to avoid the extra dict
+ _update_cache_to_call_python(), but to avoid the extra dict
lookups, we cache in (reserved1, reserved2) the last seen pair
- (interp->modules, infotuple). The first item in this tuple is
- a random PyObject that identifies the subinterpreter.
+ (interp->modules, infotuple). The first item in this tuple is
+ a random PyObject that identifies the subinterpreter.
*/
if (externpy->reserved1 == NULL) {
/* Not initialized! We didn't call @ffi.def_extern() on this
@@ -266,7 +266,7 @@ static void cffi_call_python(struct _cffi_externpy_s *externpy, char *args)
}
else {
PyGILState_STATE state = gil_ensure();
- if (externpy->reserved1 != _current_interp_key()) {
+ if (externpy->reserved1 != _current_interp_key()) {
/* Update the (reserved1, reserved2) cache. This will fail
if we didn't call @ffi.def_extern() in this particular
subinterpreter. */
diff --git a/contrib/python/cffi/c/cdlopen.c b/contrib/python/cffi/c/cdlopen.c
index 9dc542590f..0ed319b8b5 100644
--- a/contrib/python/cffi/c/cdlopen.c
+++ b/contrib/python/cffi/c/cdlopen.c
@@ -1,7 +1,7 @@
/* ffi.dlopen() interface with dlopen()/dlsym()/dlclose() */
-static void *cdlopen_fetch(PyObject *libname, void *libhandle,
- const char *symbol)
+static void *cdlopen_fetch(PyObject *libname, void *libhandle,
+ const char *symbol)
{
void *address;
@@ -40,19 +40,19 @@ static int cdlopen_close(PyObject *libname, void *libhandle)
static PyObject *ffi_dlopen(PyObject *self, PyObject *args)
{
- const char *modname;
- PyObject *temp, *result = NULL;
+ const char *modname;
+ PyObject *temp, *result = NULL;
void *handle;
- int auto_close;
+ int auto_close;
- handle = b_do_dlopen(args, &modname, &temp, &auto_close);
- if (handle != NULL)
- {
- result = (PyObject *)lib_internal_new((FFIObject *)self,
- modname, handle, auto_close);
+ handle = b_do_dlopen(args, &modname, &temp, &auto_close);
+ if (handle != NULL)
+ {
+ result = (PyObject *)lib_internal_new((FFIObject *)self,
+ modname, handle, auto_close);
}
- Py_XDECREF(temp);
- return result;
+ Py_XDECREF(temp);
+ return result;
}
static PyObject *ffi_dlclose(PyObject *self, PyObject *args)
@@ -63,16 +63,16 @@ static PyObject *ffi_dlclose(PyObject *self, PyObject *args)
return NULL;
libhandle = lib->l_libhandle;
- if (libhandle != NULL)
- {
- lib->l_libhandle = NULL;
-
- /* Clear the dict to force further accesses to do cdlopen_fetch()
- again, and fail because the library was closed. */
- PyDict_Clear(lib->l_dict);
-
- if (cdlopen_close(lib->l_libname, libhandle) < 0)
- return NULL;
+ if (libhandle != NULL)
+ {
+ lib->l_libhandle = NULL;
+
+ /* Clear the dict to force further accesses to do cdlopen_fetch()
+ again, and fail because the library was closed. */
+ PyDict_Clear(lib->l_dict);
+
+ if (cdlopen_close(lib->l_libname, libhandle) < 0)
+ return NULL;
}
Py_INCREF(Py_None);
return Py_None;
diff --git a/contrib/python/cffi/c/cffi1_module.c b/contrib/python/cffi/c/cffi1_module.c
index 2c00c5f57a..06a84fea37 100644
--- a/contrib/python/cffi/c/cffi1_module.c
+++ b/contrib/python/cffi/c/cffi1_module.c
@@ -2,9 +2,9 @@
#include "parse_c_type.c"
#include "realize_c_type.c"
-#define CFFI_VERSION_MIN 0x2601
-#define CFFI_VERSION_CHAR16CHAR32 0x2801
-#define CFFI_VERSION_MAX 0x28FF
+#define CFFI_VERSION_MIN 0x2601
+#define CFFI_VERSION_CHAR16CHAR32 0x2801
+#define CFFI_VERSION_MAX 0x28FF
typedef struct FFIObject_s FFIObject;
typedef struct LibObject_s LibObject;
@@ -41,9 +41,9 @@ static int init_ffi_lib(PyObject *m)
if (PyDict_SetItemString(FFI_Type.tp_dict, "CData",
(PyObject *)&CData_Type) < 0)
return -1;
- if (PyDict_SetItemString(FFI_Type.tp_dict, "buffer",
- (PyObject *)&MiniBuffer_Type) < 0)
- return -1;
+ if (PyDict_SetItemString(FFI_Type.tp_dict, "buffer",
+ (PyObject *)&MiniBuffer_Type) < 0)
+ return -1;
for (i = 0; all_dlopen_flags[i].name != NULL; i++) {
x = PyInt_FromLong(all_dlopen_flags[i].value);
@@ -169,8 +169,8 @@ static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
num_exports = 25;
if (ctx->flags & 1) /* set to mean that 'extern "Python"' is used */
num_exports = 26;
- if (version >= CFFI_VERSION_CHAR16CHAR32)
- num_exports = 28;
+ if (version >= CFFI_VERSION_CHAR16CHAR32)
+ num_exports = 28;
memcpy(exports, (char *)cffi_exports, num_exports * sizeof(void *));
/* make the module object */
@@ -184,7 +184,7 @@ static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
if (ffi == NULL || PyModule_AddObject(m, "ffi", (PyObject *)ffi) < 0)
return NULL;
- lib = lib_internal_new(ffi, module_name, NULL, 0);
+ lib = lib_internal_new(ffi, module_name, NULL, 0);
if (lib == NULL || PyModule_AddObject(m, "lib", (PyObject *)lib) < 0)
return NULL;
diff --git a/contrib/python/cffi/c/cglob.c b/contrib/python/cffi/c/cglob.c
index 0d708b8aa3..e97767c9d7 100644
--- a/contrib/python/cffi/c/cglob.c
+++ b/contrib/python/cffi/c/cglob.c
@@ -20,7 +20,7 @@ static void glob_support_dealloc(GlobSupportObject *gs)
static PyTypeObject GlobSupport_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.__FFIGlobSupport",
+ "_cffi_backend.__FFIGlobSupport",
sizeof(GlobSupportObject),
0,
(destructor)glob_support_dealloc, /* tp_dealloc */
diff --git a/contrib/python/cffi/c/ffi_obj.c b/contrib/python/cffi/c/ffi_obj.c
index cb5f3355a1..f154146621 100644
--- a/contrib/python/cffi/c/ffi_obj.c
+++ b/contrib/python/cffi/c/ffi_obj.c
@@ -92,7 +92,7 @@ static PyObject *ffiobj_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
/* forward, declared in cdlopen.c because it's mostly useful for this case */
static int ffiobj_init(PyObject *self, PyObject *args, PyObject *kwds);
-static PyObject *ffi_fetch_int_constant(FFIObject *ffi, const char *name,
+static PyObject *ffi_fetch_int_constant(FFIObject *ffi, const char *name,
int recursion)
{
int index;
@@ -145,7 +145,7 @@ static PyObject *ffi_fetch_int_constant(FFIObject *ffi, const char *name,
#define ACCEPT_ALL (ACCEPT_STRING | ACCEPT_CTYPE | ACCEPT_CDATA)
#define CONSIDER_FN_AS_FNPTR 8
-static CTypeDescrObject *_ffi_bad_type(FFIObject *ffi, const char *input_text)
+static CTypeDescrObject *_ffi_bad_type(FFIObject *ffi, const char *input_text)
{
size_t length = strlen(input_text);
char *extra;
@@ -188,7 +188,7 @@ static CTypeDescrObject *_ffi_type(FFIObject *ffi, PyObject *arg,
PyObject *x = PyDict_GetItem(types_dict, arg);
if (x == NULL) {
- const char *input_text = PyText_AS_UTF8(arg);
+ const char *input_text = PyText_AS_UTF8(arg);
int err, index = parse_c_type(&ffi->info, input_text);
if (index < 0)
return _ffi_bad_type(ffi, input_text);
@@ -697,40 +697,40 @@ PyDoc_STRVAR(ffi_from_buffer_doc,
"containing large quantities of raw data in some other format, like\n"
"'array.array' or numpy arrays.");
-static PyObject *ffi_from_buffer(FFIObject *self, PyObject *args,
- PyObject *kwds)
+static PyObject *ffi_from_buffer(FFIObject *self, PyObject *args,
+ PyObject *kwds)
{
- PyObject *cdecl1, *python_buf = NULL;
- CTypeDescrObject *ct;
- int require_writable = 0;
- static char *keywords[] = {"cdecl", "python_buffer",
- "require_writable", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:from_buffer", keywords,
- &cdecl1, &python_buf, &require_writable))
- return NULL;
-
- if (python_buf == NULL) {
- python_buf = cdecl1;
- ct = g_ct_chararray;
- }
- else {
- ct = _ffi_type(self, cdecl1, ACCEPT_STRING|ACCEPT_CTYPE);
- if (ct == NULL)
- return NULL;
- }
- return direct_from_buffer(ct, python_buf, require_writable);
+ PyObject *cdecl1, *python_buf = NULL;
+ CTypeDescrObject *ct;
+ int require_writable = 0;
+ static char *keywords[] = {"cdecl", "python_buffer",
+ "require_writable", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:from_buffer", keywords,
+ &cdecl1, &python_buf, &require_writable))
+ return NULL;
+
+ if (python_buf == NULL) {
+ python_buf = cdecl1;
+ ct = g_ct_chararray;
+ }
+ else {
+ ct = _ffi_type(self, cdecl1, ACCEPT_STRING|ACCEPT_CTYPE);
+ if (ct == NULL)
+ return NULL;
+ }
+ return direct_from_buffer(ct, python_buf, require_writable);
}
PyDoc_STRVAR(ffi_gc_doc,
"Return a new cdata object that points to the same data.\n"
"Later, when this new cdata object is garbage-collected,\n"
-"'destructor(old_cdata_object)' will be called.\n"
-"\n"
-"The optional 'size' gives an estimate of the size, used to\n"
-"trigger the garbage collection more eagerly. So far only used\n"
-"on PyPy. It tells the GC that the returned object keeps alive\n"
-"roughly 'size' bytes of external memory.");
+"'destructor(old_cdata_object)' will be called.\n"
+"\n"
+"The optional 'size' gives an estimate of the size, used to\n"
+"trigger the garbage collection more eagerly. So far only used\n"
+"on PyPy. It tells the GC that the returned object keeps alive\n"
+"roughly 'size' bytes of external memory.");
#define ffi_gc b_gcp /* ffi_gc() => b_gcp()
from _cffi_backend.c */
@@ -1073,7 +1073,7 @@ static PyObject *ffi_init_once(FFIObject *self, PyObject *args, PyObject *kwds)
Py_DECREF(res);
res = NULL;
}
- Py_XDECREF(tup);
+ Py_XDECREF(tup);
}
}
@@ -1082,22 +1082,22 @@ static PyObject *ffi_init_once(FFIObject *self, PyObject *args, PyObject *kwds)
return res;
}
-PyDoc_STRVAR(ffi_release_doc,
-"Release now the resources held by a 'cdata' object from ffi.new(),\n"
-"ffi.gc() or ffi.from_buffer(). The cdata object must not be used\n"
-"afterwards.\n"
-"\n"
-"'ffi.release(cdata)' is equivalent to 'cdata.__exit__()'.\n"
-"\n"
-"Note that on CPython this method has no effect (so far) on objects\n"
-"returned by ffi.new(), because the memory is allocated inline with the\n"
-"cdata object and cannot be freed independently. It might be fixed in\n"
-"future releases of cffi.");
-
-#define ffi_release b_release /* ffi_release() => b_release()
- from _cffi_backend.c */
-
-
+PyDoc_STRVAR(ffi_release_doc,
+"Release now the resources held by a 'cdata' object from ffi.new(),\n"
+"ffi.gc() or ffi.from_buffer(). The cdata object must not be used\n"
+"afterwards.\n"
+"\n"
+"'ffi.release(cdata)' is equivalent to 'cdata.__exit__()'.\n"
+"\n"
+"Note that on CPython this method has no effect (so far) on objects\n"
+"returned by ffi.new(), because the memory is allocated inline with the\n"
+"cdata object and cannot be freed independently. It might be fixed in\n"
+"future releases of cffi.");
+
+#define ffi_release b_release /* ffi_release() => b_release()
+ from _cffi_backend.c */
+
+
#define METH_VKW (METH_VARARGS | METH_KEYWORDS)
static PyMethodDef ffi_methods[] = {
{"addressof", (PyCFunction)ffi_addressof, METH_VARARGS, ffi_addressof_doc},
@@ -1107,7 +1107,7 @@ static PyMethodDef ffi_methods[] = {
{"cast", (PyCFunction)ffi_cast, METH_VARARGS, ffi_cast_doc},
{"dlclose", (PyCFunction)ffi_dlclose, METH_VARARGS, ffi_dlclose_doc},
{"dlopen", (PyCFunction)ffi_dlopen, METH_VARARGS, ffi_dlopen_doc},
- {"from_buffer",(PyCFunction)ffi_from_buffer,METH_VKW, ffi_from_buffer_doc},
+ {"from_buffer",(PyCFunction)ffi_from_buffer,METH_VKW, ffi_from_buffer_doc},
{"from_handle",(PyCFunction)ffi_from_handle,METH_O, ffi_from_handle_doc},
{"gc", (PyCFunction)ffi_gc, METH_VKW, ffi_gc_doc},
{"getctype", (PyCFunction)ffi_getctype, METH_VKW, ffi_getctype_doc},
@@ -1122,7 +1122,7 @@ static PyMethodDef ffi_methods[] = {
{"new_allocator",(PyCFunction)ffi_new_allocator,METH_VKW,ffi_new_allocator_doc},
{"new_handle", (PyCFunction)ffi_new_handle, METH_O, ffi_new_handle_doc},
{"offsetof", (PyCFunction)ffi_offsetof, METH_VARARGS, ffi_offsetof_doc},
- {"release", (PyCFunction)ffi_release, METH_O, ffi_release_doc},
+ {"release", (PyCFunction)ffi_release, METH_O, ffi_release_doc},
{"sizeof", (PyCFunction)ffi_sizeof, METH_O, ffi_sizeof_doc},
{"string", (PyCFunction)ffi_string, METH_VKW, ffi_string_doc},
{"typeof", (PyCFunction)ffi_typeof, METH_O, ffi_typeof_doc},
@@ -1137,7 +1137,7 @@ static PyGetSetDef ffi_getsets[] = {
static PyTypeObject FFI_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.FFI",
+ "_cffi_backend.FFI",
sizeof(FFIObject),
0,
(destructor)ffi_dealloc, /* tp_dealloc */
diff --git a/contrib/python/cffi/c/file_emulator.h b/contrib/python/cffi/c/file_emulator.h
index 93b9d82ac0..82a34c0c72 100644
--- a/contrib/python/cffi/c/file_emulator.h
+++ b/contrib/python/cffi/c/file_emulator.h
@@ -33,7 +33,7 @@ static FILE *PyFile_AsFile(PyObject *ob_file)
PyObject *ob, *ob_capsule = NULL, *ob_mode = NULL;
FILE *f;
int fd;
- const char *mode;
+ const char *mode;
ob = PyObject_CallMethod(ob_file, "flush", NULL);
if (ob == NULL)
diff --git a/contrib/python/cffi/c/lib_obj.c b/contrib/python/cffi/c/lib_obj.c
index 4f37f5811f..38bf3d5510 100644
--- a/contrib/python/cffi/c/lib_obj.c
+++ b/contrib/python/cffi/c/lib_obj.c
@@ -29,7 +29,7 @@ struct LibObject_s {
PyObject *l_libname; /* some string that gives the name of the lib */
FFIObject *l_ffi; /* reference back to the ffi object */
void *l_libhandle; /* the dlopen()ed handle, if any */
- int l_auto_close; /* if we must dlclose() this handle */
+ int l_auto_close; /* if we must dlclose() this handle */
};
static struct CPyExtFunc_s *_cpyextfunc_get(PyObject *x)
@@ -86,14 +86,14 @@ static PyObject *_cpyextfunc_type_index(PyObject *x)
}
static void cdlopen_close_ignore_errors(void *libhandle); /* forward */
-static void *cdlopen_fetch(PyObject *libname, void *libhandle,
- const char *symbol);
+static void *cdlopen_fetch(PyObject *libname, void *libhandle,
+ const char *symbol);
static void lib_dealloc(LibObject *lib)
{
- PyObject_GC_UnTrack(lib);
- if (lib->l_auto_close)
- cdlopen_close_ignore_errors(lib->l_libhandle);
+ PyObject_GC_UnTrack(lib);
+ if (lib->l_auto_close)
+ cdlopen_close_ignore_errors(lib->l_libhandle);
Py_DECREF(lib->l_dict);
Py_DECREF(lib->l_libname);
Py_DECREF(lib->l_ffi);
@@ -130,7 +130,7 @@ static PyObject *lib_build_cpython_func(LibObject *lib,
int i, type_index = _CFFI_GETARG(g->type_op);
_cffi_opcode_t *opcodes = lib->l_types_builder->ctx.types;
static const char *const format = ";\n\nCFFI C function from %s.lib";
- const char *libname = PyText_AS_UTF8(lib->l_libname);
+ const char *libname = PyText_AS_UTF8(lib->l_libname);
struct funcbuilder_s funcbuilder;
/* return type: */
@@ -161,14 +161,14 @@ static PyObject *lib_build_cpython_func(LibObject *lib,
if (fb_build_name(&funcbuilder, g->name, pfargs, nargs, fresult, 0) < 0)
goto error;
- /* The few bytes of memory we allocate here appear to leak, but
- this is not a real leak. Indeed, CPython never unloads its C
- extension modules. There is only one PyMem_Malloc() per real
- C function in a CFFI C extension module. That means that this
- PyMem_Malloc() could also have been written with a static
- global variable generated for each CPYTHON_BLTN defined in the
- C extension, and the effect would be the same (but a bit more
- complicated).
+ /* The few bytes of memory we allocate here appear to leak, but
+ this is not a real leak. Indeed, CPython never unloads its C
+ extension modules. There is only one PyMem_Malloc() per real
+ C function in a CFFI C extension module. That means that this
+ PyMem_Malloc() could also have been written with a static
+ global variable generated for each CPYTHON_BLTN defined in the
+ C extension, and the effect would be the same (but a bit more
+ complicated).
*/
xfunc = PyMem_Malloc(sizeof(struct CPyExtFunc_s) +
funcbuilder.nb_bytes +
@@ -213,7 +213,7 @@ static PyObject *lib_build_and_cache_attr(LibObject *lib, PyObject *name,
const struct _cffi_global_s *g;
CTypeDescrObject *ct;
builder_c_t *types_builder = lib->l_types_builder;
- const char *s = PyText_AsUTF8(name);
+ const char *s = PyText_AsUTF8(name);
if (s == NULL)
return NULL;
@@ -320,20 +320,20 @@ static PyObject *lib_build_and_cache_attr(LibObject *lib, PyObject *name,
return NULL;
}
else {
- /* The few bytes of memory we allocate here appear to leak, but
- this is not a real leak. Indeed, CPython never unloads its C
- extension modules. There is only one PyMem_Malloc() per real
- non-integer C constant in a CFFI C extension module. That
- means that this PyMem_Malloc() could also have been written
- with a static global variable generated for each OP_CONSTANT
- defined in the C extension, and the effect would be the same
- (but a bit more complicated).
-
- Note that we used to do alloca(), but see issue #198. We
- could still do alloca(), or explicit PyMem_Free(), in some
- cases; but there is no point and it only makes the remaining
- less-common cases more suspicious.
- */
+ /* The few bytes of memory we allocate here appear to leak, but
+ this is not a real leak. Indeed, CPython never unloads its C
+ extension modules. There is only one PyMem_Malloc() per real
+ non-integer C constant in a CFFI C extension module. That
+ means that this PyMem_Malloc() could also have been written
+ with a static global variable generated for each OP_CONSTANT
+ defined in the C extension, and the effect would be the same
+ (but a bit more complicated).
+
+ Note that we used to do alloca(), but see issue #198. We
+ could still do alloca(), or explicit PyMem_Free(), in some
+ cases; but there is no point and it only makes the remaining
+ less-common cases more suspicious.
+ */
assert(_CFFI_GETOP(g->type_op) == _CFFI_OP_CONSTANT);
data = PyMem_Malloc(ct->ct_size);
if (data == NULL) {
@@ -508,7 +508,7 @@ static PyObject *_lib_dict(LibObject *lib)
static PyObject *lib_getattr(LibObject *lib, PyObject *name)
{
- const char *p;
+ const char *p;
PyObject *x;
LIB_GET_OR_CACHE_ADDR(x, lib, name, goto missing);
@@ -519,7 +519,7 @@ static PyObject *lib_getattr(LibObject *lib, PyObject *name)
return x;
missing:
- /*** ATTRIBUTEERROR IS SET HERE ***/
+ /*** ATTRIBUTEERROR IS SET HERE ***/
p = PyText_AsUTF8(name);
if (p == NULL)
return NULL;
@@ -546,14 +546,14 @@ static PyObject *lib_getattr(LibObject *lib, PyObject *name)
PyErr_Clear();
return PyText_FromFormat("%s.lib", PyText_AS_UTF8(lib->l_libname));
}
-#if PY_MAJOR_VERSION >= 3
- if (strcmp(p, "__loader__") == 0 || strcmp(p, "__spec__") == 0) {
- /* some more module-like behavior hacks */
- PyErr_Clear();
- Py_INCREF(Py_None);
- return Py_None;
- }
-#endif
+#if PY_MAJOR_VERSION >= 3
+ if (strcmp(p, "__loader__") == 0 || strcmp(p, "__spec__") == 0) {
+ /* some more module-like behavior hacks */
+ PyErr_Clear();
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+#endif
return NULL;
}
@@ -589,7 +589,7 @@ static PyMethodDef lib_methods[] = {
static PyTypeObject Lib_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.Lib",
+ "_cffi_backend.Lib",
sizeof(LibObject),
0,
(destructor)lib_dealloc, /* tp_dealloc */
@@ -625,8 +625,8 @@ static PyTypeObject Lib_Type = {
offsetof(LibObject, l_dict), /* tp_dictoffset */
};
-static LibObject *lib_internal_new(FFIObject *ffi, const char *module_name,
- void *dlopen_libhandle, int auto_close)
+static LibObject *lib_internal_new(FFIObject *ffi, const char *module_name,
+ void *dlopen_libhandle, int auto_close)
{
LibObject *lib;
PyObject *libname, *dict;
@@ -649,7 +649,7 @@ static LibObject *lib_internal_new(FFIObject *ffi, const char *module_name,
Py_INCREF(ffi);
lib->l_ffi = ffi;
lib->l_libhandle = dlopen_libhandle;
- lib->l_auto_close = auto_close;
+ lib->l_auto_close = auto_close;
return lib;
err3:
@@ -657,8 +657,8 @@ static LibObject *lib_internal_new(FFIObject *ffi, const char *module_name,
err2:
Py_DECREF(libname);
err1:
- if (auto_close)
- cdlopen_close_ignore_errors(dlopen_libhandle);
+ if (auto_close)
+ cdlopen_close_ignore_errors(dlopen_libhandle);
return NULL;
}
diff --git a/contrib/python/cffi/c/libffi_msvc/ffi.c b/contrib/python/cffi/c/libffi_msvc/ffi.c
index 4fe84f16b3..581d659982 100644
--- a/contrib/python/cffi/c/libffi_msvc/ffi.c
+++ b/contrib/python/cffi/c/libffi_msvc/ffi.c
@@ -103,7 +103,7 @@ void ffi_prep_args(char *stack, extended_cif *ecif)
}
}
#ifdef _WIN64
- else if (z != 1 && z != 2 && z != 4 && z != 8)
+ else if (z != 1 && z != 2 && z != 4 && z != 8)
{
/* On Win64, if a single argument takes more than 8 bytes,
then it is always passed by reference. */
@@ -144,11 +144,11 @@ ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
/* MSVC returns small structures in registers. Put in cif->flags
the value FFI_TYPE_STRUCT only if the structure is big enough;
otherwise, put the 4- or 8-bytes integer type. */
- if (cif->rtype->size == 1 ||
- cif->rtype->size == 2 ||
- cif->rtype->size == 4)
+ if (cif->rtype->size == 1 ||
+ cif->rtype->size == 2 ||
+ cif->rtype->size == 4)
cif->flags = FFI_TYPE_INT;
- else if (cif->rtype->size == 8)
+ else if (cif->rtype->size == 8)
cif->flags = FFI_TYPE_SINT64;
else
cif->flags = FFI_TYPE_STRUCT;
@@ -289,7 +289,7 @@ ffi_closure_SYSV (ffi_closure *closure, char *argp)
_asm fld DWORD PTR [eax] ;
// asm ("flds (%0)" : : "r" (resp) : "st" );
}
- else if (rtype == FFI_TYPE_DOUBLE || rtype == FFI_TYPE_LONGDOUBLE)
+ else if (rtype == FFI_TYPE_DOUBLE || rtype == FFI_TYPE_LONGDOUBLE)
{
_asm mov eax, resp ;
_asm fld QWORD PTR [eax] ;
@@ -305,10 +305,10 @@ ffi_closure_SYSV (ffi_closure *closure, char *argp)
// : : "r"(resp)
// : "eax", "edx");
}
- else if (rtype == FFI_TYPE_STRUCT)
- {
- _asm mov eax, resp ;
- }
+ else if (rtype == FFI_TYPE_STRUCT)
+ {
+ _asm mov eax, resp ;
+ }
#else
/* now, do a generic return based on the value of rtype */
if (rtype == FFI_TYPE_INT)
@@ -319,7 +319,7 @@ ffi_closure_SYSV (ffi_closure *closure, char *argp)
{
asm ("flds (%0)" : : "r" (resp) : "st" );
}
- else if (rtype == FFI_TYPE_DOUBLE || rtype == FFI_TYPE_LONGDOUBLE)
+ else if (rtype == FFI_TYPE_DOUBLE || rtype == FFI_TYPE_LONGDOUBLE)
{
asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" );
}
@@ -330,10 +330,10 @@ ffi_closure_SYSV (ffi_closure *closure, char *argp)
: : "r"(resp)
: "eax", "edx");
}
- else if (rtype == FFI_TYPE_STRUCT)
- {
- asm ("movl %0,%%eax" : : "r" (resp) : "eax");
- }
+ else if (rtype == FFI_TYPE_STRUCT)
+ {
+ asm ("movl %0,%%eax" : : "r" (resp) : "eax");
+ }
#endif
#endif
@@ -342,8 +342,8 @@ ffi_closure_SYSV (ffi_closure *closure, char *argp)
result types except for floats; we have to 'mov xmm0, rax' in the
caller to correct this.
*/
- if (rtype == FFI_TYPE_STRUCT)
- return resp;
+ if (rtype == FFI_TYPE_STRUCT)
+ return resp;
return *(void **)resp;
#endif
}
@@ -382,7 +382,7 @@ ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
/* because we're little endian, this is what it turns into. */
#ifdef _WIN64
- if (z != 1 && z != 2 && z != 4 && z != 8)
+ if (z != 1 && z != 2 && z != 4 && z != 8)
{
/* On Win64, if a single argument takes more than 8 bytes,
then it is always passed by reference. */
@@ -451,11 +451,11 @@ ffi_prep_closure_loc (ffi_closure* closure,
|| cif->arg_types[3]->type == FFI_TYPE_DOUBLE))
mask |= 8;
- /* if we return a non-small struct, then the first argument is a pointer
- * to the return area, and all real arguments are shifted by one */
- if (cif->flags == FFI_TYPE_STRUCT)
- mask = (mask & ~8) << 1;
-
+ /* if we return a non-small struct, then the first argument is a pointer
+ * to the return area, and all real arguments are shifted by one */
+ if (cif->flags == FFI_TYPE_STRUCT)
+ mask = (mask & ~8) << 1;
+
/* 41 BB ---- mov r11d,mask */
BYTES("\x41\xBB"); INT(mask);
diff --git a/contrib/python/cffi/c/libffi_msvc/prep_cif.c b/contrib/python/cffi/c/libffi_msvc/prep_cif.c
index 5ee83a9f0d..df94a98875 100644
--- a/contrib/python/cffi/c/libffi_msvc/prep_cif.c
+++ b/contrib/python/cffi/c/libffi_msvc/prep_cif.c
@@ -117,10 +117,10 @@ ffi_status ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif *cif,
/* Make space for the return structure pointer */
if (cif->rtype->type == FFI_TYPE_STRUCT
#ifdef _WIN32
- && (cif->rtype->size != 1) /* MSVC returns small structs in registers */
- && (cif->rtype->size != 2)
- && (cif->rtype->size != 4)
- && (cif->rtype->size != 8)
+ && (cif->rtype->size != 1) /* MSVC returns small structs in registers */
+ && (cif->rtype->size != 2)
+ && (cif->rtype->size != 4)
+ && (cif->rtype->size != 8)
#endif
#ifdef SPARC
&& (cif->abi != FFI_V9 || cif->rtype->size > 32)
diff --git a/contrib/python/cffi/c/minibuffer.h b/contrib/python/cffi/c/minibuffer.h
index e92141fa16..f3f5ca15c4 100644
--- a/contrib/python/cffi/c/minibuffer.h
+++ b/contrib/python/cffi/c/minibuffer.h
@@ -56,17 +56,17 @@ static int mb_ass_item(MiniBufferObj *self, Py_ssize_t idx, PyObject *other)
}
}
-/* forward: from _cffi_backend.c */
-static int _fetch_as_buffer(PyObject *x, Py_buffer *view, int writable_only);
-
+/* forward: from _cffi_backend.c */
+static int _fetch_as_buffer(PyObject *x, Py_buffer *view, int writable_only);
+
static int mb_ass_slice(MiniBufferObj *self,
Py_ssize_t left, Py_ssize_t right, PyObject *other)
{
- Py_ssize_t count;
+ Py_ssize_t count;
Py_ssize_t size = self->mb_size;
- Py_buffer src_view;
+ Py_buffer src_view;
- if (_fetch_as_buffer(other, &src_view, 0) < 0)
+ if (_fetch_as_buffer(other, &src_view, 0) < 0)
return -1;
if (left < 0) left = 0;
@@ -74,14 +74,14 @@ static int mb_ass_slice(MiniBufferObj *self,
if (left > right) left = right;
count = right - left;
- if (count != src_view.len) {
- PyBuffer_Release(&src_view);
+ if (count != src_view.len) {
+ PyBuffer_Release(&src_view);
PyErr_SetString(PyExc_ValueError,
"right operand length must match slice length");
return -1;
}
- memcpy(self->mb_data + left, src_view.buf, count);
- PyBuffer_Release(&src_view);
+ memcpy(self->mb_data + left, src_view.buf, count);
+ PyBuffer_Release(&src_view);
return 0;
}
@@ -160,91 +160,91 @@ mb_clear(MiniBufferObj *ob)
return 0;
}
-static PyObject *
-mb_richcompare(PyObject *self, PyObject *other, int op)
-{
- Py_ssize_t self_size, other_size;
- Py_buffer self_bytes, other_bytes;
- PyObject *res;
- Py_ssize_t minsize;
- int cmp, rc;
-
- /* Bytes can be compared to anything that supports the (binary)
- buffer API. Except that a comparison with Unicode is always an
- error, even if the comparison is for equality. */
- rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
- if (!rc)
- rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
- if (rc < 0)
- return NULL;
- if (rc) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
- PyErr_Clear();
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
-
- }
- self_size = self_bytes.len;
-
- if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
- PyErr_Clear();
- PyBuffer_Release(&self_bytes);
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
-
- }
- other_size = other_bytes.len;
-
- if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
- /* Shortcut: if the lengths differ, the objects differ */
- cmp = (op == Py_NE);
- }
- else {
- minsize = self_size;
- if (other_size < minsize)
- minsize = other_size;
-
- cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
- /* In ISO C, memcmp() guarantees to use unsigned bytes! */
-
- if (cmp == 0) {
- if (self_size < other_size)
- cmp = -1;
- else if (self_size > other_size)
- cmp = 1;
- }
-
- switch (op) {
- case Py_LT: cmp = cmp < 0; break;
- case Py_LE: cmp = cmp <= 0; break;
- case Py_EQ: cmp = cmp == 0; break;
- case Py_NE: cmp = cmp != 0; break;
- case Py_GT: cmp = cmp > 0; break;
- case Py_GE: cmp = cmp >= 0; break;
- }
- }
-
- res = cmp ? Py_True : Py_False;
- PyBuffer_Release(&self_bytes);
- PyBuffer_Release(&other_bytes);
- Py_INCREF(res);
- return res;
-}
-
+static PyObject *
+mb_richcompare(PyObject *self, PyObject *other, int op)
+{
+ Py_ssize_t self_size, other_size;
+ Py_buffer self_bytes, other_bytes;
+ PyObject *res;
+ Py_ssize_t minsize;
+ int cmp, rc;
+
+ /* Bytes can be compared to anything that supports the (binary)
+ buffer API. Except that a comparison with Unicode is always an
+ error, even if the comparison is for equality. */
+ rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
+ if (!rc)
+ rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
+ if (rc < 0)
+ return NULL;
+ if (rc) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
+ PyErr_Clear();
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+
+ }
+ self_size = self_bytes.len;
+
+ if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
+ PyErr_Clear();
+ PyBuffer_Release(&self_bytes);
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+
+ }
+ other_size = other_bytes.len;
+
+ if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
+ /* Shortcut: if the lengths differ, the objects differ */
+ cmp = (op == Py_NE);
+ }
+ else {
+ minsize = self_size;
+ if (other_size < minsize)
+ minsize = other_size;
+
+ cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
+ /* In ISO C, memcmp() guarantees to use unsigned bytes! */
+
+ if (cmp == 0) {
+ if (self_size < other_size)
+ cmp = -1;
+ else if (self_size > other_size)
+ cmp = 1;
+ }
+
+ switch (op) {
+ case Py_LT: cmp = cmp < 0; break;
+ case Py_LE: cmp = cmp <= 0; break;
+ case Py_EQ: cmp = cmp == 0; break;
+ case Py_NE: cmp = cmp != 0; break;
+ case Py_GT: cmp = cmp > 0; break;
+ case Py_GE: cmp = cmp >= 0; break;
+ }
+ }
+
+ res = cmp ? Py_True : Py_False;
+ PyBuffer_Release(&self_bytes);
+ PyBuffer_Release(&other_bytes);
+ Py_INCREF(res);
+ return res;
+}
+
#if PY_MAJOR_VERSION >= 3
/* pfffffffffffff pages of copy-paste from listobject.c */
-
-/* pfffffffffffff#2: the PySlice_GetIndicesEx() *macro* should not
- be called, because C extension modules compiled with it differ
- on ABI between 3.6.0, 3.6.1 and 3.6.2. */
-#if PY_VERSION_HEX < 0x03070000 && defined(PySlice_GetIndicesEx) && !defined(PYPY_VERSION)
-#undef PySlice_GetIndicesEx
-#endif
-
+
+/* pfffffffffffff#2: the PySlice_GetIndicesEx() *macro* should not
+ be called, because C extension modules compiled with it differ
+ on ABI between 3.6.0, 3.6.1 and 3.6.2. */
+#if PY_VERSION_HEX < 0x03070000 && defined(PySlice_GetIndicesEx) && !defined(PYPY_VERSION)
+#undef PySlice_GetIndicesEx
+#endif
+
static PyObject *mb_subscript(MiniBufferObj *self, PyObject *item)
{
if (PyIndex_Check(item)) {
@@ -326,22 +326,22 @@ static PyMappingMethods mb_as_mapping = {
# define MINIBUF_TPFLAGS (Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER)
#endif
-PyDoc_STRVAR(ffi_buffer_doc,
-"ffi.buffer(cdata[, byte_size]):\n"
-"Return a read-write buffer object that references the raw C data\n"
-"pointed to by the given 'cdata'. The 'cdata' must be a pointer or an\n"
-"array. Can be passed to functions expecting a buffer, or directly\n"
-"manipulated with:\n"
-"\n"
-" buf[:] get a copy of it in a regular string, or\n"
-" buf[idx] as a single character\n"
-" buf[:] = ...\n"
-" buf[idx] = ... change the content");
-
-static PyObject * /* forward, implemented in _cffi_backend.c */
-b_buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
-
-
+PyDoc_STRVAR(ffi_buffer_doc,
+"ffi.buffer(cdata[, byte_size]):\n"
+"Return a read-write buffer object that references the raw C data\n"
+"pointed to by the given 'cdata'. The 'cdata' must be a pointer or an\n"
+"array. Can be passed to functions expecting a buffer, or directly\n"
+"manipulated with:\n"
+"\n"
+" buf[:] get a copy of it in a regular string, or\n"
+" buf[idx] as a single character\n"
+" buf[:] = ...\n"
+" buf[idx] = ... change the content");
+
+static PyObject * /* forward, implemented in _cffi_backend.c */
+b_buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
+
+
static PyTypeObject MiniBuffer_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_cffi_backend.buffer",
@@ -372,25 +372,25 @@ static PyTypeObject MiniBuffer_Type = {
&mb_as_buffer, /* tp_as_buffer */
(Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
MINIBUF_TPFLAGS), /* tp_flags */
- ffi_buffer_doc, /* tp_doc */
+ ffi_buffer_doc, /* tp_doc */
(traverseproc)mb_traverse, /* tp_traverse */
(inquiry)mb_clear, /* tp_clear */
- (richcmpfunc)mb_richcompare, /* tp_richcompare */
+ (richcmpfunc)mb_richcompare, /* tp_richcompare */
offsetof(MiniBufferObj, mb_weakreflist), /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- b_buffer_new, /* tp_new */
- 0, /* tp_free */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ b_buffer_new, /* tp_new */
+ 0, /* tp_free */
};
static PyObject *minibuffer_new(char *data, Py_ssize_t size,
diff --git a/contrib/python/cffi/c/misc_thread_common.h b/contrib/python/cffi/c/misc_thread_common.h
index 7c8a543f26..66e283545b 100644
--- a/contrib/python/cffi/c/misc_thread_common.h
+++ b/contrib/python/cffi/c/misc_thread_common.h
@@ -1,14 +1,14 @@
#ifndef WITH_THREAD
# error "xxx no-thread configuration not tested, please report if you need that"
#endif
-#include "pythread.h"
+#include "pythread.h"
struct cffi_tls_s {
- /* The current thread's ThreadCanaryObj. This is only non-null in
- case cffi builds the thread state here. It remains null if this
- thread had already a thread state provided by CPython. */
- struct thread_canary_s *local_thread_canary;
+ /* The current thread's ThreadCanaryObj. This is only non-null in
+ case cffi builds the thread state here. It remains null if this
+ thread had already a thread state provided by CPython. */
+ struct thread_canary_s *local_thread_canary;
#ifndef USE__THREAD
/* The saved errno. If the C compiler supports '__thread', then
@@ -25,247 +25,247 @@ struct cffi_tls_s {
static struct cffi_tls_s *get_cffi_tls(void); /* in misc_thread_posix.h
or misc_win32.h */
-
-/* We try to keep the PyThreadState around in a thread not started by
- * Python but where cffi callbacks occur. If we didn't do that, then
- * the standard logic in PyGILState_Ensure() and PyGILState_Release()
- * would create a new PyThreadState and completely free it for every
- * single call. For some applications, this is a huge slow-down.
- *
- * As shown by issue #362, it is quite messy to do. The current
- * solution is to keep the PyThreadState alive by incrementing its
- * 'gilstate_counter'. We detect thread shut-down, and we put the
- * PyThreadState inside a list of zombies (we can't free it
- * immediately because we don't have the GIL at that point in time).
- * We also detect other pieces of code (notably Py_Finalize()) which
- * clear and free PyThreadStates under our feet, using ThreadCanaryObj.
- */
-
-#define TLS_ZOM_LOCK() PyThread_acquire_lock(cffi_zombie_lock, WAIT_LOCK)
-#define TLS_ZOM_UNLOCK() PyThread_release_lock(cffi_zombie_lock)
-static PyThread_type_lock cffi_zombie_lock = NULL;
-
-
-/* A 'canary' object is created in a thread when there is a callback
- invoked, and that thread has no PyThreadState so far. It is an
- object of reference count equal to 1, which is stored in the
- PyThreadState->dict. Two things can occur then:
-
- 1. The PyThreadState can be forcefully cleared by Py_Finalize().
- Then thread_canary_dealloc() is called, and we have to cancel
- the hacks we did to keep the PyThreadState alive.
-
- 2. The thread finishes. In that case, we put the canary in a list
- of zombies, and at some convenient time later when we have the
- GIL, we free all PyThreadStates in the zombie list.
-
- Some more fun comes from the fact that thread_canary_dealloc() can
- be called at a point where the canary is in the zombie list already.
- Also, the various pieces are freed at specific points in time, and
- we must make sure not to access already-freed structures:
-
- - the struct cffi_tls_s is valid until the thread shuts down, and
- then it is freed by cffi_thread_shutdown().
-
- - the canary is a normal Python object, but we have a borrowed
- reference to it from cffi_tls_s.local_thread_canary.
- */
-
-typedef struct thread_canary_s {
- PyObject_HEAD
- struct thread_canary_s *zombie_prev, *zombie_next;
- PyThreadState *tstate;
- struct cffi_tls_s *tls;
-} ThreadCanaryObj;
-
-static PyTypeObject ThreadCanary_Type; /* forward */
-static ThreadCanaryObj cffi_zombie_head;
-
-static void
-_thread_canary_detach_with_lock(ThreadCanaryObj *ob)
-{
- /* must be called with both the GIL and TLS_ZOM_LOCK. */
- ThreadCanaryObj *p, *n;
- p = ob->zombie_prev;
- n = ob->zombie_next;
- p->zombie_next = n;
- n->zombie_prev = p;
- ob->zombie_prev = NULL;
- ob->zombie_next = NULL;
-}
-
-static void
-thread_canary_dealloc(ThreadCanaryObj *ob)
-{
- /* this ThreadCanaryObj is being freed: if it is in the zombie
- chained list, remove it. Thread-safety: 'zombie_next' amd
- 'local_thread_canary' accesses need to be protected with
- the TLS_ZOM_LOCK.
- */
- TLS_ZOM_LOCK();
- if (ob->zombie_next != NULL) {
- //fprintf(stderr, "thread_canary_dealloc(%p): ZOMBIE\n", ob);
- _thread_canary_detach_with_lock(ob);
- }
- else {
- //fprintf(stderr, "thread_canary_dealloc(%p): not a zombie\n", ob);
- }
-
- if (ob->tls != NULL) {
- //fprintf(stderr, "thread_canary_dealloc(%p): was local_thread_canary\n", ob);
- assert(ob->tls->local_thread_canary == ob);
- ob->tls->local_thread_canary = NULL;
- }
- TLS_ZOM_UNLOCK();
-
- PyObject_Del((PyObject *)ob);
-}
-
-static void
-thread_canary_make_zombie(ThreadCanaryObj *ob)
-{
- /* This must be called without the GIL, but with the TLS_ZOM_LOCK.
- It must be called at most once for a given ThreadCanaryObj. */
- ThreadCanaryObj *last;
-
- //fprintf(stderr, "thread_canary_make_zombie(%p)\n", ob);
- if (ob->zombie_next)
- Py_FatalError("cffi: ThreadCanaryObj is already a zombie");
- last = cffi_zombie_head.zombie_prev;
- ob->zombie_next = &cffi_zombie_head;
- ob->zombie_prev = last;
- last->zombie_next = ob;
- cffi_zombie_head.zombie_prev = ob;
-}
-
-static void
-thread_canary_free_zombies(void)
-{
- /* This must be called with the GIL. */
- if (cffi_zombie_head.zombie_next == &cffi_zombie_head)
- return; /* fast path */
-
- while (1) {
- ThreadCanaryObj *ob;
- PyThreadState *tstate = NULL;
-
- TLS_ZOM_LOCK();
- ob = cffi_zombie_head.zombie_next;
- if (ob != &cffi_zombie_head) {
- tstate = ob->tstate;
- //fprintf(stderr, "thread_canary_free_zombie(%p) tstate=%p\n", ob, tstate);
- _thread_canary_detach_with_lock(ob);
- if (tstate == NULL)
- Py_FatalError("cffi: invalid ThreadCanaryObj->tstate");
- }
- TLS_ZOM_UNLOCK();
-
- if (tstate == NULL)
- break;
- PyThreadState_Clear(tstate); /* calls thread_canary_dealloc on 'ob',
- but now ob->zombie_next == NULL. */
- PyThreadState_Delete(tstate);
- //fprintf(stderr, "thread_canary_free_zombie: cleared and deleted tstate=%p\n", tstate);
- }
- //fprintf(stderr, "thread_canary_free_zombie: end\n");
-}
-
-static void
-thread_canary_register(PyThreadState *tstate)
-{
- /* called with the GIL; 'tstate' is the current PyThreadState. */
- ThreadCanaryObj *canary;
- PyObject *tdict;
- struct cffi_tls_s *tls;
- int err;
-
- /* first free the zombies, if any */
- thread_canary_free_zombies();
-
- tls = get_cffi_tls();
- if (tls == NULL)
- goto ignore_error;
-
- tdict = PyThreadState_GetDict();
- if (tdict == NULL)
- goto ignore_error;
-
- canary = PyObject_New(ThreadCanaryObj, &ThreadCanary_Type);
- //fprintf(stderr, "thread_canary_register(%p): tstate=%p tls=%p\n", canary, tstate, tls);
- if (canary == NULL)
- goto ignore_error;
- canary->zombie_prev = NULL;
- canary->zombie_next = NULL;
- canary->tstate = tstate;
- canary->tls = tls;
-
- err = PyDict_SetItemString(tdict, "cffi.thread.canary", (PyObject *)canary);
- Py_DECREF(canary);
- if (err < 0)
- goto ignore_error;
-
- /* thread-safety: we have the GIL here, and 'tstate' is the one that
- corresponds to our own thread. We are allocating a new 'canary'
- and setting it up for our own thread, both in 'tdict' (which owns
- the reference) and in 'tls->local_thread_canary' (which doesn't). */
- assert(Py_REFCNT(canary) == 1);
- tls->local_thread_canary = canary;
- tstate->gilstate_counter++;
- /* ^^^ this means 'tstate' will never be automatically freed by
- PyGILState_Release() */
- return;
-
- ignore_error:
- PyErr_Clear();
-}
-
-static PyTypeObject ThreadCanary_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_cffi_backend.thread_canary",
- sizeof(ThreadCanaryObj),
- 0,
- (destructor)thread_canary_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_compare */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
-};
-
-static void init_cffi_tls_zombie(void)
-{
- cffi_zombie_head.zombie_next = &cffi_zombie_head;
- cffi_zombie_head.zombie_prev = &cffi_zombie_head;
- cffi_zombie_lock = PyThread_allocate_lock();
- if (cffi_zombie_lock == NULL)
- PyErr_SetString(PyExc_SystemError, "can't allocate cffi_zombie_lock");
-}
-
+
+/* We try to keep the PyThreadState around in a thread not started by
+ * Python but where cffi callbacks occur. If we didn't do that, then
+ * the standard logic in PyGILState_Ensure() and PyGILState_Release()
+ * would create a new PyThreadState and completely free it for every
+ * single call. For some applications, this is a huge slow-down.
+ *
+ * As shown by issue #362, it is quite messy to do. The current
+ * solution is to keep the PyThreadState alive by incrementing its
+ * 'gilstate_counter'. We detect thread shut-down, and we put the
+ * PyThreadState inside a list of zombies (we can't free it
+ * immediately because we don't have the GIL at that point in time).
+ * We also detect other pieces of code (notably Py_Finalize()) which
+ * clear and free PyThreadStates under our feet, using ThreadCanaryObj.
+ */
+
+#define TLS_ZOM_LOCK() PyThread_acquire_lock(cffi_zombie_lock, WAIT_LOCK)
+#define TLS_ZOM_UNLOCK() PyThread_release_lock(cffi_zombie_lock)
+static PyThread_type_lock cffi_zombie_lock = NULL;
+
+
+/* A 'canary' object is created in a thread when there is a callback
+ invoked, and that thread has no PyThreadState so far. It is an
+ object of reference count equal to 1, which is stored in the
+ PyThreadState->dict. Two things can occur then:
+
+ 1. The PyThreadState can be forcefully cleared by Py_Finalize().
+ Then thread_canary_dealloc() is called, and we have to cancel
+ the hacks we did to keep the PyThreadState alive.
+
+ 2. The thread finishes. In that case, we put the canary in a list
+ of zombies, and at some convenient time later when we have the
+ GIL, we free all PyThreadStates in the zombie list.
+
+ Some more fun comes from the fact that thread_canary_dealloc() can
+ be called at a point where the canary is in the zombie list already.
+ Also, the various pieces are freed at specific points in time, and
+ we must make sure not to access already-freed structures:
+
+ - the struct cffi_tls_s is valid until the thread shuts down, and
+ then it is freed by cffi_thread_shutdown().
+
+ - the canary is a normal Python object, but we have a borrowed
+ reference to it from cffi_tls_s.local_thread_canary.
+ */
+
+typedef struct thread_canary_s {
+ PyObject_HEAD
+ struct thread_canary_s *zombie_prev, *zombie_next;
+ PyThreadState *tstate;
+ struct cffi_tls_s *tls;
+} ThreadCanaryObj;
+
+static PyTypeObject ThreadCanary_Type; /* forward */
+static ThreadCanaryObj cffi_zombie_head;
+
+static void
+_thread_canary_detach_with_lock(ThreadCanaryObj *ob)
+{
+ /* must be called with both the GIL and TLS_ZOM_LOCK. */
+ ThreadCanaryObj *p, *n;
+ p = ob->zombie_prev;
+ n = ob->zombie_next;
+ p->zombie_next = n;
+ n->zombie_prev = p;
+ ob->zombie_prev = NULL;
+ ob->zombie_next = NULL;
+}
+
+static void
+thread_canary_dealloc(ThreadCanaryObj *ob)
+{
+ /* this ThreadCanaryObj is being freed: if it is in the zombie
+ chained list, remove it. Thread-safety: 'zombie_next' amd
+ 'local_thread_canary' accesses need to be protected with
+ the TLS_ZOM_LOCK.
+ */
+ TLS_ZOM_LOCK();
+ if (ob->zombie_next != NULL) {
+ //fprintf(stderr, "thread_canary_dealloc(%p): ZOMBIE\n", ob);
+ _thread_canary_detach_with_lock(ob);
+ }
+ else {
+ //fprintf(stderr, "thread_canary_dealloc(%p): not a zombie\n", ob);
+ }
+
+ if (ob->tls != NULL) {
+ //fprintf(stderr, "thread_canary_dealloc(%p): was local_thread_canary\n", ob);
+ assert(ob->tls->local_thread_canary == ob);
+ ob->tls->local_thread_canary = NULL;
+ }
+ TLS_ZOM_UNLOCK();
+
+ PyObject_Del((PyObject *)ob);
+}
+
+static void
+thread_canary_make_zombie(ThreadCanaryObj *ob)
+{
+ /* This must be called without the GIL, but with the TLS_ZOM_LOCK.
+ It must be called at most once for a given ThreadCanaryObj. */
+ ThreadCanaryObj *last;
+
+ //fprintf(stderr, "thread_canary_make_zombie(%p)\n", ob);
+ if (ob->zombie_next)
+ Py_FatalError("cffi: ThreadCanaryObj is already a zombie");
+ last = cffi_zombie_head.zombie_prev;
+ ob->zombie_next = &cffi_zombie_head;
+ ob->zombie_prev = last;
+ last->zombie_next = ob;
+ cffi_zombie_head.zombie_prev = ob;
+}
+
+static void
+thread_canary_free_zombies(void)
+{
+ /* This must be called with the GIL. */
+ if (cffi_zombie_head.zombie_next == &cffi_zombie_head)
+ return; /* fast path */
+
+ while (1) {
+ ThreadCanaryObj *ob;
+ PyThreadState *tstate = NULL;
+
+ TLS_ZOM_LOCK();
+ ob = cffi_zombie_head.zombie_next;
+ if (ob != &cffi_zombie_head) {
+ tstate = ob->tstate;
+ //fprintf(stderr, "thread_canary_free_zombie(%p) tstate=%p\n", ob, tstate);
+ _thread_canary_detach_with_lock(ob);
+ if (tstate == NULL)
+ Py_FatalError("cffi: invalid ThreadCanaryObj->tstate");
+ }
+ TLS_ZOM_UNLOCK();
+
+ if (tstate == NULL)
+ break;
+ PyThreadState_Clear(tstate); /* calls thread_canary_dealloc on 'ob',
+ but now ob->zombie_next == NULL. */
+ PyThreadState_Delete(tstate);
+ //fprintf(stderr, "thread_canary_free_zombie: cleared and deleted tstate=%p\n", tstate);
+ }
+ //fprintf(stderr, "thread_canary_free_zombie: end\n");
+}
+
+static void
+thread_canary_register(PyThreadState *tstate)
+{
+ /* called with the GIL; 'tstate' is the current PyThreadState. */
+ ThreadCanaryObj *canary;
+ PyObject *tdict;
+ struct cffi_tls_s *tls;
+ int err;
+
+ /* first free the zombies, if any */
+ thread_canary_free_zombies();
+
+ tls = get_cffi_tls();
+ if (tls == NULL)
+ goto ignore_error;
+
+ tdict = PyThreadState_GetDict();
+ if (tdict == NULL)
+ goto ignore_error;
+
+ canary = PyObject_New(ThreadCanaryObj, &ThreadCanary_Type);
+ //fprintf(stderr, "thread_canary_register(%p): tstate=%p tls=%p\n", canary, tstate, tls);
+ if (canary == NULL)
+ goto ignore_error;
+ canary->zombie_prev = NULL;
+ canary->zombie_next = NULL;
+ canary->tstate = tstate;
+ canary->tls = tls;
+
+ err = PyDict_SetItemString(tdict, "cffi.thread.canary", (PyObject *)canary);
+ Py_DECREF(canary);
+ if (err < 0)
+ goto ignore_error;
+
+ /* thread-safety: we have the GIL here, and 'tstate' is the one that
+ corresponds to our own thread. We are allocating a new 'canary'
+ and setting it up for our own thread, both in 'tdict' (which owns
+ the reference) and in 'tls->local_thread_canary' (which doesn't). */
+ assert(Py_REFCNT(canary) == 1);
+ tls->local_thread_canary = canary;
+ tstate->gilstate_counter++;
+ /* ^^^ this means 'tstate' will never be automatically freed by
+ PyGILState_Release() */
+ return;
+
+ ignore_error:
+ PyErr_Clear();
+}
+
+static PyTypeObject ThreadCanary_Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_cffi_backend.thread_canary",
+ sizeof(ThreadCanaryObj),
+ 0,
+ (destructor)thread_canary_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_compare */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+};
+
+static void init_cffi_tls_zombie(void)
+{
+ cffi_zombie_head.zombie_next = &cffi_zombie_head;
+ cffi_zombie_head.zombie_prev = &cffi_zombie_head;
+ cffi_zombie_lock = PyThread_allocate_lock();
+ if (cffi_zombie_lock == NULL)
+ PyErr_SetString(PyExc_SystemError, "can't allocate cffi_zombie_lock");
+}
+
static void cffi_thread_shutdown(void *p)
{
- /* this function is called from misc_thread_posix or misc_win32
- when a thread is about to end. */
+ /* this function is called from misc_thread_posix or misc_win32
+ when a thread is about to end. */
struct cffi_tls_s *tls = (struct cffi_tls_s *)p;
- /* thread-safety: this field 'local_thread_canary' can be reset
- to NULL in parallel, protected by TLS_ZOM_LOCK. */
- TLS_ZOM_LOCK();
- if (tls->local_thread_canary != NULL) {
- tls->local_thread_canary->tls = NULL;
- thread_canary_make_zombie(tls->local_thread_canary);
+ /* thread-safety: this field 'local_thread_canary' can be reset
+ to NULL in parallel, protected by TLS_ZOM_LOCK. */
+ TLS_ZOM_LOCK();
+ if (tls->local_thread_canary != NULL) {
+ tls->local_thread_canary->tls = NULL;
+ thread_canary_make_zombie(tls->local_thread_canary);
}
- TLS_ZOM_UNLOCK();
- //fprintf(stderr, "thread_shutdown(%p)\n", tls);
+ TLS_ZOM_UNLOCK();
+ //fprintf(stderr, "thread_shutdown(%p)\n", tls);
free(tls);
}
@@ -297,33 +297,33 @@ static void restore_errno_only(void)
#endif
-/* MESS. We can't use PyThreadState_GET(), because that calls
- PyThreadState_Get() which fails an assert if the result is NULL.
-
- * in Python 2.7 and <= 3.4, the variable _PyThreadState_Current
- is directly available, so use that.
-
- * in Python 3.5, the variable is available too, but it might be
- the case that the headers don't define it (this changed in 3.5.1).
- In case we're compiling with 3.5.x with x >= 1, we need to
- manually define this variable.
-
- * in Python >= 3.6 there is _PyThreadState_UncheckedGet().
- It was added in 3.5.2 but should never be used in 3.5.x
- because it is not available in 3.5.0 or 3.5.1.
-*/
-#if PY_VERSION_HEX >= 0x03050100 && PY_VERSION_HEX < 0x03060000
-PyAPI_DATA(void *volatile) _PyThreadState_Current;
+/* MESS. We can't use PyThreadState_GET(), because that calls
+ PyThreadState_Get() which fails an assert if the result is NULL.
+
+ * in Python 2.7 and <= 3.4, the variable _PyThreadState_Current
+ is directly available, so use that.
+
+ * in Python 3.5, the variable is available too, but it might be
+ the case that the headers don't define it (this changed in 3.5.1).
+ In case we're compiling with 3.5.x with x >= 1, we need to
+ manually define this variable.
+
+ * in Python >= 3.6 there is _PyThreadState_UncheckedGet().
+ It was added in 3.5.2 but should never be used in 3.5.x
+ because it is not available in 3.5.0 or 3.5.1.
+*/
+#if PY_VERSION_HEX >= 0x03050100 && PY_VERSION_HEX < 0x03060000
+PyAPI_DATA(void *volatile) _PyThreadState_Current;
#endif
static PyThreadState *get_current_ts(void)
{
-#if PY_VERSION_HEX >= 0x03060000
- return _PyThreadState_UncheckedGet();
-#elif defined(_Py_atomic_load_relaxed)
+#if PY_VERSION_HEX >= 0x03060000
+ return _PyThreadState_UncheckedGet();
+#elif defined(_Py_atomic_load_relaxed)
return (PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current);
#else
- return (PyThreadState*)_PyThreadState_Current; /* assume atomic read */
+ return (PyThreadState*)_PyThreadState_Current; /* assume atomic read */
#endif
}
@@ -357,9 +357,9 @@ static PyGILState_STATE gil_ensure(void)
assert(ts == get_current_ts());
assert(ts->gilstate_counter >= 1);
- /* Use the ThreadCanary mechanism to keep 'ts' alive until the
- thread really shuts down */
- thread_canary_register(ts);
+ /* Use the ThreadCanary mechanism to keep 'ts' alive until the
+ thread really shuts down */
+ thread_canary_register(ts);
return result;
}
diff --git a/contrib/python/cffi/c/misc_win32.h b/contrib/python/cffi/c/misc_win32.h
index f184885080..2946721801 100644
--- a/contrib/python/cffi/c/misc_win32.h
+++ b/contrib/python/cffi/c/misc_win32.h
@@ -100,10 +100,10 @@ static PyObject *b_getwinerror(PyObject *self, PyObject *args, PyObject *kwds)
s_buf[--len] = L'\0';
message = PyUnicode_FromWideChar(s_buf, len);
}
- if (message != NULL) {
+ if (message != NULL) {
v = Py_BuildValue("(iO)", err, message);
- Py_DECREF(message);
- }
+ Py_DECREF(message);
+ }
else
v = NULL;
LocalFree(s_buf);
@@ -169,14 +169,14 @@ static PyObject *b_getwinerror(PyObject *self, PyObject *args, PyObject *kwds)
static void *dlopen(const char *filename, int flag)
{
- return (void *)LoadLibraryA(filename);
+ return (void *)LoadLibraryA(filename);
+}
+
+static void *dlopenW(const wchar_t *filename)
+{
+ return (void *)LoadLibraryW(filename);
}
-static void *dlopenW(const wchar_t *filename)
-{
- return (void *)LoadLibraryW(filename);
-}
-
static void *dlsym(void *handle, const char *symbol)
{
void *address = GetProcAddress((HMODULE)handle, symbol);
diff --git a/contrib/python/cffi/c/parse_c_type.c b/contrib/python/cffi/c/parse_c_type.c
index c2bff61e77..698ef6451e 100644
--- a/contrib/python/cffi/c/parse_c_type.c
+++ b/contrib/python/cffi/c/parse_c_type.c
@@ -25,7 +25,7 @@ enum token_e {
/* keywords */
TOK__BOOL,
TOK_CHAR,
- TOK__COMPLEX,
+ TOK__COMPLEX,
TOK_CONST,
TOK_DOUBLE,
TOK_ENUM,
@@ -159,7 +159,7 @@ static void next_token(token_t *tok)
if (tok->size == 5 && !memcmp(p, "_Bool", 5)) tok->kind = TOK__BOOL;
if (tok->size == 7 && !memcmp(p,"__cdecl",7)) tok->kind = TOK_CDECL;
if (tok->size == 9 && !memcmp(p,"__stdcall",9))tok->kind = TOK_STDCALL;
- if (tok->size == 8 && !memcmp(p,"_Complex",8)) tok->kind = TOK__COMPLEX;
+ if (tok->size == 8 && !memcmp(p,"_Complex",8)) tok->kind = TOK__COMPLEX;
break;
case 'c':
if (tok->size == 4 && !memcmp(p, "char", 4)) tok->kind = TOK_CHAR;
@@ -493,7 +493,7 @@ int search_standard_typename(const char *p, size_t size)
case '1':
if (size == 8 && !memcmp(p, "uint16", 6)) return _CFFI_PRIM_UINT16;
- if (size == 8 && !memcmp(p, "char16", 6)) return _CFFI_PRIM_CHAR16;
+ if (size == 8 && !memcmp(p, "char16", 6)) return _CFFI_PRIM_CHAR16;
break;
case '2':
@@ -502,7 +502,7 @@ int search_standard_typename(const char *p, size_t size)
case '3':
if (size == 8 && !memcmp(p, "uint32", 6)) return _CFFI_PRIM_UINT32;
- if (size == 8 && !memcmp(p, "char32", 6)) return _CFFI_PRIM_CHAR32;
+ if (size == 8 && !memcmp(p, "char32", 6)) return _CFFI_PRIM_CHAR32;
break;
case '4':
@@ -604,7 +604,7 @@ static int parse_complete(token_t *tok)
{
unsigned int t0;
_cffi_opcode_t t1;
- _cffi_opcode_t t1complex;
+ _cffi_opcode_t t1complex;
int modifiers_length, modifiers_sign;
qualifiers:
@@ -660,8 +660,8 @@ static int parse_complete(token_t *tok)
break;
}
- t1complex = 0;
-
+ t1complex = 0;
+
if (modifiers_length || modifiers_sign) {
switch (tok->kind) {
@@ -672,7 +672,7 @@ static int parse_complete(token_t *tok)
case TOK_STRUCT:
case TOK_UNION:
case TOK_ENUM:
- case TOK__COMPLEX:
+ case TOK__COMPLEX:
return parse_error(tok, "invalid combination of types");
case TOK_DOUBLE:
@@ -726,11 +726,11 @@ static int parse_complete(token_t *tok)
break;
case TOK_FLOAT:
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_FLOAT);
- t1complex = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_FLOATCOMPLEX);
+ t1complex = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_FLOATCOMPLEX);
break;
case TOK_DOUBLE:
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_DOUBLE);
- t1complex = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_DOUBLECOMPLEX);
+ t1complex = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_DOUBLECOMPLEX);
break;
case TOK_IDENTIFIER:
{
@@ -797,13 +797,13 @@ static int parse_complete(token_t *tok)
}
next_token(tok);
}
- if (tok->kind == TOK__COMPLEX)
- {
- if (t1complex == 0)
- return parse_error(tok, "_Complex type combination unsupported");
- t1 = t1complex;
- next_token(tok);
- }
+ if (tok->kind == TOK__COMPLEX)
+ {
+ if (t1complex == 0)
+ return parse_error(tok, "_Complex type combination unsupported");
+ t1 = t1complex;
+ next_token(tok);
+ }
return parse_sequel(tok, write_ds(tok, t1));
}
diff --git a/contrib/python/cffi/c/realize_c_type.c b/contrib/python/cffi/c/realize_c_type.c
index 6c70c14cf3..82629b7ef1 100644
--- a/contrib/python/cffi/c/realize_c_type.c
+++ b/contrib/python/cffi/c/realize_c_type.c
@@ -151,10 +151,10 @@ static PyObject *build_primitive_type(int num)
"uint_fast64_t",
"intmax_t",
"uintmax_t",
- "float _Complex",
- "double _Complex",
- "char16_t",
- "char32_t",
+ "float _Complex",
+ "double _Complex",
+ "char16_t",
+ "char32_t",
};
PyObject *x;
@@ -269,11 +269,11 @@ realize_c_type(builder_c_t *builder, _cffi_opcode_t opcodes[], int index)
PyObject *x = realize_c_type_or_func(builder, opcodes, index);
if (x == NULL || CTypeDescr_Check(x))
return (CTypeDescrObject *)x;
- else {
- unexpected_fn_type(x);
- Py_DECREF(x);
- return NULL;
- }
+ else {
+ unexpected_fn_type(x);
+ Py_DECREF(x);
+ return NULL;
+ }
}
static void _realize_name(char *target, const char *prefix, const char *srcname)
@@ -413,8 +413,8 @@ _realize_c_struct_or_union(builder_c_t *builder, int sindex)
}
static PyObject *
-realize_c_type_or_func_now(builder_c_t *builder, _cffi_opcode_t op,
- _cffi_opcode_t opcodes[], int index)
+realize_c_type_or_func_now(builder_c_t *builder, _cffi_opcode_t op,
+ _cffi_opcode_t opcodes[], int index)
{
PyObject *x, *y, *z;
Py_ssize_t length = -1;
@@ -636,36 +636,36 @@ realize_c_type_or_func_now(builder_c_t *builder, _cffi_opcode_t op,
return NULL;
}
- return x;
-}
-
-static int _realize_recursion_level;
-
-static PyObject *
-realize_c_type_or_func(builder_c_t *builder,
- _cffi_opcode_t opcodes[], int index)
-{
- PyObject *x;
- _cffi_opcode_t op = opcodes[index];
-
- if ((((uintptr_t)op) & 1) == 0) {
- x = (PyObject *)op;
- Py_INCREF(x);
- return x;
- }
-
- if (_realize_recursion_level >= 1000) {
- PyErr_Format(PyExc_RuntimeError,
- "type-building recursion too deep or infinite. "
- "This is known to occur e.g. in ``struct s { void(*callable)"
- "(struct s); }''. Please report if you get this error and "
- "really need support for your case.");
- return NULL;
- }
- _realize_recursion_level++;
- x = realize_c_type_or_func_now(builder, op, opcodes, index);
- _realize_recursion_level--;
-
+ return x;
+}
+
+static int _realize_recursion_level;
+
+static PyObject *
+realize_c_type_or_func(builder_c_t *builder,
+ _cffi_opcode_t opcodes[], int index)
+{
+ PyObject *x;
+ _cffi_opcode_t op = opcodes[index];
+
+ if ((((uintptr_t)op) & 1) == 0) {
+ x = (PyObject *)op;
+ Py_INCREF(x);
+ return x;
+ }
+
+ if (_realize_recursion_level >= 1000) {
+ PyErr_Format(PyExc_RuntimeError,
+ "type-building recursion too deep or infinite. "
+ "This is known to occur e.g. in ``struct s { void(*callable)"
+ "(struct s); }''. Please report if you get this error and "
+ "really need support for your case.");
+ return NULL;
+ }
+ _realize_recursion_level++;
+ x = realize_c_type_or_func_now(builder, op, opcodes, index);
+ _realize_recursion_level--;
+
if (x != NULL && opcodes == builder->ctx.types && opcodes[index] != x) {
assert((((uintptr_t)x) & 1) == 0);
assert((((uintptr_t)opcodes[index]) & 1) == 1);
@@ -673,7 +673,7 @@ realize_c_type_or_func(builder_c_t *builder,
opcodes[index] = x;
}
return x;
-}
+}
static CTypeDescrObject *
realize_c_func_return_type(builder_c_t *builder,
@@ -760,13 +760,13 @@ static int do_realize_lazy_struct(CTypeDescrObject *ct)
return -1;
}
- if (ctf != NULL && fld->field_offset == (size_t)-1) {
+ if (ctf != NULL && fld->field_offset == (size_t)-1) {
/* unnamed struct, with field positions and sizes entirely
determined by complete_struct_or_union() and not checked.
Or, bitfields (field_size >= 0), similarly not checked. */
assert(fld->field_size == (size_t)-1 || fbitsize >= 0);
}
- else if (ctf == NULL || detect_custom_layout(ct, SF_STD_FIELD_POS,
+ else if (ctf == NULL || detect_custom_layout(ct, SF_STD_FIELD_POS,
ctf->ct_size, fld->field_size,
"wrong size for field '",
fld->name, "'") < 0) {
diff --git a/contrib/python/cffi/c/test_c.py b/contrib/python/cffi/c/test_c.py
index ba577e28b9..654584d924 100644
--- a/contrib/python/cffi/c/test_c.py
+++ b/contrib/python/cffi/c/test_c.py
@@ -1,25 +1,25 @@
import py
-import pytest
-
+import pytest
+
def _setup_path():
import os, sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
_setup_path()
from _cffi_backend import *
-from _cffi_backend import _get_types, _get_common_types
-try:
- from _cffi_backend import _testfunc
-except ImportError:
- def _testfunc(num):
- pytest.skip("_testunc() not available")
-from _cffi_backend import __version__
+from _cffi_backend import _get_types, _get_common_types
+try:
+ from _cffi_backend import _testfunc
+except ImportError:
+ def _testfunc(num):
+ pytest.skip("_testunc() not available")
+from _cffi_backend import __version__
# ____________________________________________________________
import sys
-assert __version__ == "1.15.0", ("This test_c.py file is for testing a version"
- " of cffi that differs from the one that we"
- " get from 'import _cffi_backend'")
+assert __version__ == "1.15.0", ("This test_c.py file is for testing a version"
+ " of cffi that differs from the one that we"
+ " get from 'import _cffi_backend'")
if sys.version_info < (3,):
type_or_class = "type"
mandatory_b_prefix = ''
@@ -32,7 +32,7 @@ if sys.version_info < (3,):
.replace(r'\\U', r'\U'))
u = U()
str2bytes = str
- strict_compare = False
+ strict_compare = False
else:
type_or_class = "class"
long = int
@@ -44,7 +44,7 @@ else:
bitem2bchr = bytechr
u = ""
str2bytes = lambda s: bytes(s, "ascii")
- strict_compare = True
+ strict_compare = True
def size_of_int():
BInt = new_primitive_type("int")
@@ -66,12 +66,12 @@ def find_and_load_library(name, flags=RTLD_NOW):
path = None
else:
path = ctypes.util.find_library(name)
- if path is None and name == 'c':
- assert sys.platform == 'win32'
- assert (sys.version_info >= (3,) or
- '__pypy__' in sys.builtin_module_names)
- py.test.skip("dlopen(None) cannot work on Windows "
- "with PyPy or Python 3")
+ if path is None and name == 'c':
+ assert sys.platform == 'win32'
+ assert (sys.version_info >= (3,) or
+ '__pypy__' in sys.builtin_module_names)
+ py.test.skip("dlopen(None) cannot work on Windows "
+ "with PyPy or Python 3")
return load_library(path, flags)
def test_load_library():
@@ -114,16 +114,16 @@ def test_cast_to_signed_char():
p = new_primitive_type("signed char")
x = cast(p, -65 + 17*256)
assert repr(x) == "<cdata 'signed char' -65>"
- assert repr(type(x)) == "<%s '_cffi_backend._CDataBase'>" % type_or_class
+ assert repr(type(x)) == "<%s '_cffi_backend._CDataBase'>" % type_or_class
assert int(x) == -65
x = cast(p, -66 + (1<<199)*256)
assert repr(x) == "<cdata 'signed char' -66>"
assert int(x) == -66
- assert (x == cast(p, -66)) is True
- assert (x != cast(p, -66)) is False
+ assert (x == cast(p, -66)) is True
+ assert (x != cast(p, -66)) is False
q = new_primitive_type("short")
- assert (x == cast(q, -66)) is True
- assert (x != cast(q, -66)) is False
+ assert (x == cast(q, -66)) is True
+ assert (x != cast(q, -66)) is False
def test_sizeof_type():
py.test.raises(TypeError, sizeof, 42.5)
@@ -188,7 +188,7 @@ def test_float_types():
assert float(cast(p, 1.1)) != 1.1 # rounding error
assert float(cast(p, 1E200)) == INF # limited range
- assert cast(p, -1.1) == cast(p, -1.1)
+ assert cast(p, -1.1) == cast(p, -1.1)
assert repr(float(cast(p, -0.0))) == '-0.0'
assert float(cast(p, b'\x09')) == 9.0
assert float(cast(p, u+'\x09')) == 9.0
@@ -198,60 +198,60 @@ def test_float_types():
def test_complex_types():
INF = 1E200 * 1E200
for name in ["float", "double"]:
- p = new_primitive_type(name + " _Complex")
- assert bool(cast(p, 0)) is False
+ p = new_primitive_type(name + " _Complex")
+ assert bool(cast(p, 0)) is False
assert bool(cast(p, INF))
assert bool(cast(p, -INF))
- assert bool(cast(p, 0j)) is False
+ assert bool(cast(p, 0j)) is False
assert bool(cast(p, INF*1j))
assert bool(cast(p, -INF*1j))
- # "can't convert complex to float", like CPython's "float(0j)"
+ # "can't convert complex to float", like CPython's "float(0j)"
py.test.raises(TypeError, int, cast(p, -150))
py.test.raises(TypeError, long, cast(p, -150))
py.test.raises(TypeError, float, cast(p, -150))
assert complex(cast(p, 1.25)) == 1.25
assert complex(cast(p, 1.25j)) == 1.25j
- assert complex(cast(p, complex(0,INF))) == complex(0,INF)
- assert complex(cast(p, -INF)) == -INF
+ assert complex(cast(p, complex(0,INF))) == complex(0,INF)
+ assert complex(cast(p, -INF)) == -INF
if name == "float":
assert complex(cast(p, 1.1j)) != 1.1j # rounding error
assert complex(cast(p, 1E200+3j)) == INF+3j # limited range
- assert complex(cast(p, complex(3,1E200))) == complex(3,INF) # limited range
+ assert complex(cast(p, complex(3,1E200))) == complex(3,INF) # limited range
- assert cast(p, -1.1j) == cast(p, -1.1j)
+ assert cast(p, -1.1j) == cast(p, -1.1j)
assert repr(complex(cast(p, -0.0)).real) == '-0.0'
- #assert repr(complex(cast(p, -0j))) == '-0j' # http://bugs.python.org/issue29602
- assert complex(cast(p, b'\x09')) == 9.0 + 0j
- assert complex(cast(p, u+'\x09')) == 9.0 + 0j
- assert complex(cast(p, True)) == 1.0 + 0j
+ #assert repr(complex(cast(p, -0j))) == '-0j' # http://bugs.python.org/issue29602
+ assert complex(cast(p, b'\x09')) == 9.0 + 0j
+ assert complex(cast(p, u+'\x09')) == 9.0 + 0j
+ assert complex(cast(p, True)) == 1.0 + 0j
py.test.raises(TypeError, cast, p, None)
#
- py.test.raises(TypeError, cast, new_primitive_type(name), 1+0j)
- #
- for basetype in ["char", "int", "uint64_t", "float",
- "double", "long double"]:
- baseobj = cast(new_primitive_type(basetype), 65)
- py.test.raises(TypeError, complex, baseobj)
- #
- BArray = new_array_type(new_pointer_type(p), 10)
- x = newp(BArray, None)
- x[5] = 12.34 + 56.78j
- assert type(x[5]) is complex
- assert abs(x[5] - (12.34 + 56.78j)) < 1e-5
- assert (x[5] == 12.34 + 56.78j) == (name == "double") # rounding error
- #
- class Foo:
- def __complex__(self):
- return 2 + 3j
- assert complex(Foo()) == 2 + 3j
- assert complex(cast(p, Foo())) == 2 + 3j
- py.test.raises(TypeError, cast, new_primitive_type("int"), 1+0j)
+ py.test.raises(TypeError, cast, new_primitive_type(name), 1+0j)
+ #
+ for basetype in ["char", "int", "uint64_t", "float",
+ "double", "long double"]:
+ baseobj = cast(new_primitive_type(basetype), 65)
+ py.test.raises(TypeError, complex, baseobj)
+ #
+ BArray = new_array_type(new_pointer_type(p), 10)
+ x = newp(BArray, None)
+ x[5] = 12.34 + 56.78j
+ assert type(x[5]) is complex
+ assert abs(x[5] - (12.34 + 56.78j)) < 1e-5
+ assert (x[5] == 12.34 + 56.78j) == (name == "double") # rounding error
+ #
+ class Foo:
+ def __complex__(self):
+ return 2 + 3j
+ assert complex(Foo()) == 2 + 3j
+ assert complex(cast(p, Foo())) == 2 + 3j
+ py.test.raises(TypeError, cast, new_primitive_type("int"), 1+0j)
def test_character_type():
p = new_primitive_type("char")
assert bool(cast(p, 'A')) is True
assert bool(cast(p, '\x00')) is False # since 1.7
- assert cast(p, '\x00') == cast(p, -17*256)
+ assert cast(p, '\x00') == cast(p, -17*256)
assert int(cast(p, 'A')) == 65
assert long(cast(p, 'A')) == 65
assert type(int(cast(p, 'A'))) is int
@@ -296,9 +296,9 @@ def test_pointer_to_int():
assert repr(q).startswith("<cdata 'int *' 0x")
assert p == q
assert hash(p) == hash(q)
- e = py.test.raises(TypeError, newp, new_array_type(BPtr, None), None)
- assert str(e.value) == (
- "expected new array length or list/tuple/str, not NoneType")
+ e = py.test.raises(TypeError, newp, new_array_type(BPtr, None), None)
+ assert str(e.value) == (
+ "expected new array length or list/tuple/str, not NoneType")
def test_pointer_bool():
BInt = new_primitive_type("int")
@@ -322,10 +322,10 @@ def test_reading_pointer_to_int():
assert p[0] == 0
p = newp(BPtr, 5000)
assert p[0] == 5000
- with pytest.raises(IndexError):
- p[1]
- with pytest.raises(IndexError):
- p[-1]
+ with pytest.raises(IndexError):
+ p[1]
+ with pytest.raises(IndexError):
+ p[-1]
def test_reading_pointer_to_float():
BFloat = new_primitive_type("float")
@@ -354,18 +354,18 @@ def test_newp_integer_types():
max = (1 << (8*size-1)) - 1
assert newp(pp, min)[0] == min
assert newp(pp, max)[0] == max
- py.test.raises(OverflowError, newp, pp, min - 2 ** 32)
- py.test.raises(OverflowError, newp, pp, min - 2 ** 64)
- py.test.raises(OverflowError, newp, pp, max + 2 ** 32)
- py.test.raises(OverflowError, newp, pp, max + 2 ** 64)
+ py.test.raises(OverflowError, newp, pp, min - 2 ** 32)
+ py.test.raises(OverflowError, newp, pp, min - 2 ** 64)
+ py.test.raises(OverflowError, newp, pp, max + 2 ** 32)
+ py.test.raises(OverflowError, newp, pp, max + 2 ** 64)
py.test.raises(OverflowError, newp, pp, min - 1)
py.test.raises(OverflowError, newp, pp, max + 1)
- py.test.raises(OverflowError, newp, pp, min - 1 - 2 ** 32)
- py.test.raises(OverflowError, newp, pp, min - 1 - 2 ** 64)
- py.test.raises(OverflowError, newp, pp, max + 1)
- py.test.raises(OverflowError, newp, pp, max + 1 + 2 ** 32)
- py.test.raises(OverflowError, newp, pp, max + 1 + 2 ** 64)
- py.test.raises(TypeError, newp, pp, 1.0)
+ py.test.raises(OverflowError, newp, pp, min - 1 - 2 ** 32)
+ py.test.raises(OverflowError, newp, pp, min - 1 - 2 ** 64)
+ py.test.raises(OverflowError, newp, pp, max + 1)
+ py.test.raises(OverflowError, newp, pp, max + 1 + 2 ** 32)
+ py.test.raises(OverflowError, newp, pp, max + 1 + 2 ** 64)
+ py.test.raises(TypeError, newp, pp, 1.0)
for name in ['char', 'short', 'int', 'long', 'long long']:
p = new_primitive_type('unsigned ' + name)
pp = new_pointer_type(p)
@@ -392,9 +392,9 @@ def test_reading_pointer_to_char():
assert int(c) == ord(b'A')
py.test.raises(TypeError, cast, BChar, b'foo')
py.test.raises(TypeError, cast, BChar, u+'foo')
- e = py.test.raises(TypeError, newp, new_array_type(BPtr, None), 12.3)
- assert str(e.value) == (
- "expected new array length or list/tuple/str, not float")
+ e = py.test.raises(TypeError, newp, new_array_type(BPtr, None), 12.3)
+ assert str(e.value) == (
+ "expected new array length or list/tuple/str, not float")
def test_reading_pointer_to_pointer():
BVoidP = new_pointer_type(new_void_type())
@@ -426,15 +426,15 @@ def test_load_standard_library():
x = find_and_load_library(None)
BVoidP = new_pointer_type(new_void_type())
assert x.load_function(BVoidP, 'strcpy')
- py.test.raises(AttributeError, x.load_function,
+ py.test.raises(AttributeError, x.load_function,
BVoidP, 'xxx_this_function_does_not_exist')
# the next one is from 'libm', not 'libc', but we assume
# that it is already loaded too, so it should work
assert x.load_function(BVoidP, 'sqrt')
- #
- x.close_lib()
- py.test.raises(ValueError, x.load_function, BVoidP, 'sqrt')
- x.close_lib()
+ #
+ x.close_lib()
+ py.test.raises(ValueError, x.load_function, BVoidP, 'sqrt')
+ x.close_lib()
def test_no_len_on_nonarray():
p = new_primitive_type("int")
@@ -453,8 +453,8 @@ def test_cmp_none():
def test_invalid_indexing():
p = new_primitive_type("int")
x = cast(p, 42)
- with pytest.raises(TypeError):
- x[0]
+ with pytest.raises(TypeError):
+ x[0]
def test_default_str():
BChar = new_primitive_type("char")
@@ -547,16 +547,16 @@ def test_array_instance():
assert len(a) == LENGTH
for i in range(LENGTH):
assert a[i] == 0
- with pytest.raises(IndexError):
- a[LENGTH]
- with pytest.raises(IndexError):
- a[-1]
+ with pytest.raises(IndexError):
+ a[LENGTH]
+ with pytest.raises(IndexError):
+ a[-1]
for i in range(LENGTH):
a[i] = i * i + 1
for i in range(LENGTH):
assert a[i] == i * i + 1
- with pytest.raises(IndexError) as e:
- a[LENGTH+100] = 500
+ with pytest.raises(IndexError) as e:
+ a[LENGTH+100] = 500
assert ('(expected %d < %d)' % (LENGTH+100, LENGTH)) in str(e.value)
py.test.raises(TypeError, int, a)
@@ -571,14 +571,14 @@ def test_array_of_unknown_length_instance():
a[i] -= i
for i in range(42):
assert a[i] == -i
- with pytest.raises(IndexError):
- a[42]
- with pytest.raises(IndexError):
- a[-1]
- with pytest.raises(IndexError):
- a[42] = 123
- with pytest.raises(IndexError):
- a[-1] = 456
+ with pytest.raises(IndexError):
+ a[42]
+ with pytest.raises(IndexError):
+ a[-1]
+ with pytest.raises(IndexError):
+ a[42] = 123
+ with pytest.raises(IndexError):
+ a[-1] = 456
def test_array_of_unknown_length_instance_with_initializer():
p = new_primitive_type("int")
@@ -626,14 +626,14 @@ def test_array_sub():
assert a == (p - 1)
BPtr = new_pointer_type(new_primitive_type("short"))
q = newp(BPtr, None)
- with pytest.raises(TypeError):
- p - q
- with pytest.raises(TypeError):
- q - p
- with pytest.raises(TypeError):
- a - q
- with pytest.raises(TypeError) as e:
- q - a
+ with pytest.raises(TypeError):
+ p - q
+ with pytest.raises(TypeError):
+ q - p
+ with pytest.raises(TypeError):
+ a - q
+ with pytest.raises(TypeError) as e:
+ q - a
assert str(e.value) == "cannot subtract cdata 'short *' and cdata 'int *'"
def test_ptr_sub_unaligned():
@@ -646,10 +646,10 @@ def test_ptr_sub_unaligned():
assert b - a == (bi - 1240) // size_of_int()
assert a - b == (1240 - bi) // size_of_int()
else:
- with pytest.raises(ValueError):
- b - a
- with pytest.raises(ValueError):
- a - b
+ with pytest.raises(ValueError):
+ b - a
+ with pytest.raises(ValueError):
+ a - b
def test_cast_primitive_from_cdata():
p = new_primitive_type("int")
@@ -799,16 +799,16 @@ def test_struct_instance():
BInt = new_primitive_type("int")
BStruct = new_struct_type("struct foo")
BStructPtr = new_pointer_type(BStruct)
- p = cast(BStructPtr, 42)
- with pytest.raises(AttributeError) as e:
- p.a1 # opaque
- assert str(e.value) == ("cdata 'struct foo *' points to an opaque type: "
- "cannot read fields")
- with pytest.raises(AttributeError) as e:
- p.a1 = 10 # opaque
- assert str(e.value) == ("cdata 'struct foo *' points to an opaque type: "
- "cannot write fields")
-
+ p = cast(BStructPtr, 42)
+ with pytest.raises(AttributeError) as e:
+ p.a1 # opaque
+ assert str(e.value) == ("cdata 'struct foo *' points to an opaque type: "
+ "cannot read fields")
+ with pytest.raises(AttributeError) as e:
+ p.a1 = 10 # opaque
+ assert str(e.value) == ("cdata 'struct foo *' points to an opaque type: "
+ "cannot write fields")
+
complete_struct_or_union(BStruct, [('a1', BInt, -1),
('a2', BInt, -1)])
p = newp(BStructPtr, None)
@@ -817,42 +817,42 @@ def test_struct_instance():
s.a2 = 123
assert s.a1 == 0
assert s.a2 == 123
- with pytest.raises(OverflowError):
- s.a1 = sys.maxsize+1
+ with pytest.raises(OverflowError):
+ s.a1 = sys.maxsize+1
assert s.a1 == 0
- with pytest.raises(AttributeError) as e:
- p.foobar
- assert str(e.value) == "cdata 'struct foo *' has no field 'foobar'"
- with pytest.raises(AttributeError) as e:
- p.foobar = 42
- assert str(e.value) == "cdata 'struct foo *' has no field 'foobar'"
- with pytest.raises(AttributeError) as e:
- s.foobar
- assert str(e.value) == "cdata 'struct foo' has no field 'foobar'"
- with pytest.raises(AttributeError) as e:
- s.foobar = 42
- assert str(e.value) == "cdata 'struct foo' has no field 'foobar'"
- j = cast(BInt, 42)
- with pytest.raises(AttributeError) as e:
- j.foobar
- assert str(e.value) == "cdata 'int' has no attribute 'foobar'"
- with pytest.raises(AttributeError) as e:
- j.foobar = 42
- assert str(e.value) == "cdata 'int' has no attribute 'foobar'"
- j = cast(new_pointer_type(BInt), 42)
- with pytest.raises(AttributeError) as e:
- j.foobar
- assert str(e.value) == "cdata 'int *' has no attribute 'foobar'"
- with pytest.raises(AttributeError) as e:
- j.foobar = 42
- assert str(e.value) == "cdata 'int *' has no attribute 'foobar'"
- pp = newp(new_pointer_type(BStructPtr), p)
- with pytest.raises(AttributeError) as e:
- pp.a1
- assert str(e.value) == "cdata 'struct foo * *' has no attribute 'a1'"
- with pytest.raises(AttributeError) as e:
- pp.a1 = 42
- assert str(e.value) == "cdata 'struct foo * *' has no attribute 'a1'"
+ with pytest.raises(AttributeError) as e:
+ p.foobar
+ assert str(e.value) == "cdata 'struct foo *' has no field 'foobar'"
+ with pytest.raises(AttributeError) as e:
+ p.foobar = 42
+ assert str(e.value) == "cdata 'struct foo *' has no field 'foobar'"
+ with pytest.raises(AttributeError) as e:
+ s.foobar
+ assert str(e.value) == "cdata 'struct foo' has no field 'foobar'"
+ with pytest.raises(AttributeError) as e:
+ s.foobar = 42
+ assert str(e.value) == "cdata 'struct foo' has no field 'foobar'"
+ j = cast(BInt, 42)
+ with pytest.raises(AttributeError) as e:
+ j.foobar
+ assert str(e.value) == "cdata 'int' has no attribute 'foobar'"
+ with pytest.raises(AttributeError) as e:
+ j.foobar = 42
+ assert str(e.value) == "cdata 'int' has no attribute 'foobar'"
+ j = cast(new_pointer_type(BInt), 42)
+ with pytest.raises(AttributeError) as e:
+ j.foobar
+ assert str(e.value) == "cdata 'int *' has no attribute 'foobar'"
+ with pytest.raises(AttributeError) as e:
+ j.foobar = 42
+ assert str(e.value) == "cdata 'int *' has no attribute 'foobar'"
+ pp = newp(new_pointer_type(BStructPtr), p)
+ with pytest.raises(AttributeError) as e:
+ pp.a1
+ assert str(e.value) == "cdata 'struct foo * *' has no attribute 'a1'"
+ with pytest.raises(AttributeError) as e:
+ pp.a1 = 42
+ assert str(e.value) == "cdata 'struct foo * *' has no attribute 'a1'"
def test_union_instance():
BInt = new_primitive_type("int")
@@ -987,15 +987,15 @@ def test_call_function_0():
py.test.raises(OverflowError, f, 128, 0)
py.test.raises(OverflowError, f, 0, 128)
-def test_call_function_0_pretend_bool_result():
- BSignedChar = new_primitive_type("signed char")
- BBool = new_primitive_type("_Bool")
- BFunc0 = new_function_type((BSignedChar, BSignedChar), BBool, False)
- f = cast(BFunc0, _testfunc(0))
- assert f(40, -39) is True
- assert f(40, -40) is False
- py.test.raises(ValueError, f, 40, 2)
-
+def test_call_function_0_pretend_bool_result():
+ BSignedChar = new_primitive_type("signed char")
+ BBool = new_primitive_type("_Bool")
+ BFunc0 = new_function_type((BSignedChar, BSignedChar), BBool, False)
+ f = cast(BFunc0, _testfunc(0))
+ assert f(40, -39) is True
+ assert f(40, -40) is False
+ py.test.raises(ValueError, f, 40, 2)
+
def test_call_function_1():
BInt = new_primitive_type("int")
BLong = new_primitive_type("long")
@@ -1158,17 +1158,17 @@ def test_call_function_23_bis():
res = f(b"foo")
assert res == 1000 * ord(b'f')
-def test_call_function_23_bool_array():
- # declaring the function as int(_Bool*)
- BBool = new_primitive_type("_Bool")
- BBoolP = new_pointer_type(BBool)
- BInt = new_primitive_type("int")
- BFunc23 = new_function_type((BBoolP,), BInt, False)
- f = cast(BFunc23, _testfunc(23))
- res = f(b"\x01\x01")
- assert res == 1000
- py.test.raises(ValueError, f, b"\x02\x02")
-
+def test_call_function_23_bool_array():
+ # declaring the function as int(_Bool*)
+ BBool = new_primitive_type("_Bool")
+ BBoolP = new_pointer_type(BBool)
+ BInt = new_primitive_type("int")
+ BFunc23 = new_function_type((BBoolP,), BInt, False)
+ f = cast(BFunc23, _testfunc(23))
+ res = f(b"\x01\x01")
+ assert res == 1000
+ py.test.raises(ValueError, f, b"\x02\x02")
+
def test_cannot_pass_struct_with_array_of_length_0():
BInt = new_primitive_type("int")
BArray0 = new_array_type(new_pointer_type(BInt), 0)
@@ -1195,34 +1195,34 @@ def test_call_function_9():
BSShort = new_primitive_type("short")
assert f(3, cast(BSChar, -3), cast(BUChar, 200), cast(BSShort, -5)) == 192
-def test_call_function_24():
- BFloat = new_primitive_type("float")
- BFloatComplex = new_primitive_type("float _Complex")
- BFunc3 = new_function_type((BFloat, BFloat), BFloatComplex, False)
- if 0: # libffi returning nonsense silently, so logic disabled for now
- f = cast(BFunc3, _testfunc(24))
- result = f(1.25, 5.1)
- assert type(result) == complex
- assert result.real == 1.25 # exact
- assert (result.imag != 2*5.1) and (abs(result.imag - 2*5.1) < 1e-5) # inexact
- else:
- f = cast(BFunc3, _testfunc(9))
- py.test.raises(NotImplementedError, f, 12.3, 34.5)
-
-def test_call_function_25():
- BDouble = new_primitive_type("double")
- BDoubleComplex = new_primitive_type("double _Complex")
- BFunc3 = new_function_type((BDouble, BDouble), BDoubleComplex, False)
- if 0: # libffi returning nonsense silently, so logic disabled for now
- f = cast(BFunc3, _testfunc(25))
- result = f(1.25, 5.1)
- assert type(result) == complex
- assert result.real == 1.25 # exact
- assert (result.imag != 2*5.1) and (abs(result.imag - 2*5.1) < 1e-10) # inexact
- else:
- f = cast(BFunc3, _testfunc(9))
- py.test.raises(NotImplementedError, f, 12.3, 34.5)
-
+def test_call_function_24():
+ BFloat = new_primitive_type("float")
+ BFloatComplex = new_primitive_type("float _Complex")
+ BFunc3 = new_function_type((BFloat, BFloat), BFloatComplex, False)
+ if 0: # libffi returning nonsense silently, so logic disabled for now
+ f = cast(BFunc3, _testfunc(24))
+ result = f(1.25, 5.1)
+ assert type(result) == complex
+ assert result.real == 1.25 # exact
+ assert (result.imag != 2*5.1) and (abs(result.imag - 2*5.1) < 1e-5) # inexact
+ else:
+ f = cast(BFunc3, _testfunc(9))
+ py.test.raises(NotImplementedError, f, 12.3, 34.5)
+
+def test_call_function_25():
+ BDouble = new_primitive_type("double")
+ BDoubleComplex = new_primitive_type("double _Complex")
+ BFunc3 = new_function_type((BDouble, BDouble), BDoubleComplex, False)
+ if 0: # libffi returning nonsense silently, so logic disabled for now
+ f = cast(BFunc3, _testfunc(25))
+ result = f(1.25, 5.1)
+ assert type(result) == complex
+ assert result.real == 1.25 # exact
+ assert (result.imag != 2*5.1) and (abs(result.imag - 2*5.1) < 1e-10) # inexact
+ else:
+ f = cast(BFunc3, _testfunc(9))
+ py.test.raises(NotImplementedError, f, 12.3, 34.5)
+
def test_cannot_call_with_a_autocompleted_struct():
BSChar = new_primitive_type("signed char")
BDouble = new_primitive_type("double")
@@ -1234,13 +1234,13 @@ def test_cannot_call_with_a_autocompleted_struct():
BFunc = new_function_type((BStruct,), BDouble) # internally not callable
dummy_func = cast(BFunc, 42)
e = py.test.raises(NotImplementedError, dummy_func, "?")
- msg = ("ctype 'struct foo' not supported as argument. It is a struct "
- 'declared with "...;", but the C calling convention may depend '
- "on the missing fields; or, it contains anonymous struct/unions. "
- "Such structs are only supported as argument if the function is "
- "'API mode' and non-variadic (i.e. declared inside ffibuilder."
- "cdef()+ffibuilder.set_source() and not taking a final '...' "
- "argument)")
+ msg = ("ctype 'struct foo' not supported as argument. It is a struct "
+ 'declared with "...;", but the C calling convention may depend '
+ "on the missing fields; or, it contains anonymous struct/unions. "
+ "Such structs are only supported as argument if the function is "
+ "'API mode' and non-variadic (i.e. declared inside ffibuilder."
+ "cdef()+ffibuilder.set_source() and not taking a final '...' "
+ "argument)")
assert str(e.value) == msg
def test_new_charp():
@@ -1277,9 +1277,9 @@ def test_read_variable():
ll = find_and_load_library('c')
stderr = ll.read_variable(BVoidP, "stderr")
assert stderr == cast(BVoidP, _testfunc(8))
- #
- ll.close_lib()
- py.test.raises(ValueError, ll.read_variable, BVoidP, "stderr")
+ #
+ ll.close_lib()
+ py.test.raises(ValueError, ll.read_variable, BVoidP, "stderr")
def test_read_variable_as_unknown_length_array():
## FIXME: this test assumes glibc specific behavior, it's not compliant with C standard
@@ -1306,9 +1306,9 @@ def test_write_variable():
assert not ll.read_variable(BVoidP, "stderr")
ll.write_variable(BVoidP, "stderr", stderr)
assert ll.read_variable(BVoidP, "stderr") == stderr
- #
- ll.close_lib()
- py.test.raises(ValueError, ll.write_variable, BVoidP, "stderr", stderr)
+ #
+ ll.close_lib()
+ py.test.raises(ValueError, ll.write_variable, BVoidP, "stderr", stderr)
def test_callback():
BInt = new_primitive_type("int")
@@ -1331,9 +1331,9 @@ def test_callback_exception():
except ImportError:
import io as cStringIO # Python 3
import linecache
- def matches(istr, ipattern, ipattern38):
- if sys.version_info >= (3, 8):
- ipattern = ipattern38
+ def matches(istr, ipattern, ipattern38):
+ if sys.version_info >= (3, 8):
+ ipattern = ipattern38
str, pattern = istr, ipattern
while '$' in pattern:
i = pattern.index('$')
@@ -1366,8 +1366,8 @@ def test_callback_exception():
try:
linecache.getline = lambda *args: 'LINE' # hack: speed up PyPy tests
sys.stderr = cStringIO.StringIO()
- if hasattr(sys, '__unraisablehook__'): # work around pytest
- sys.unraisablehook = sys.__unraisablehook__ # on recent CPythons
+ if hasattr(sys, '__unraisablehook__'): # work around pytest
+ sys.unraisablehook = sys.__unraisablehook__ # on recent CPythons
assert f(100) == 300
assert sys.stderr.getvalue() == ''
assert f(10000) == -42
@@ -1379,14 +1379,14 @@ Traceback (most recent call last):
File "$", line $, in check_value
$
ValueError: 42
-""", """\
-Exception ignored from cffi callback <function$Zcb1 at 0x$>:
-Traceback (most recent call last):
- File "$", line $, in Zcb1
- $
- File "$", line $, in check_value
- $
-ValueError: 42
+""", """\
+Exception ignored from cffi callback <function$Zcb1 at 0x$>:
+Traceback (most recent call last):
+ File "$", line $, in Zcb1
+ $
+ File "$", line $, in check_value
+ $
+ValueError: 42
""")
sys.stderr = cStringIO.StringIO()
bigvalue = 20000
@@ -1395,12 +1395,12 @@ ValueError: 42
From cffi callback <function$Zcb1 at 0x$>:
Trying to convert the result back to C:
OverflowError: integer 60000 does not fit 'short'
-""", """\
-Exception ignored from cffi callback <function$Zcb1 at 0x$>, trying to convert the result back to C:
-Traceback (most recent call last):
- File "$", line $, in test_callback_exception
- $
-OverflowError: integer 60000 does not fit 'short'
+""", """\
+Exception ignored from cffi callback <function$Zcb1 at 0x$>, trying to convert the result back to C:
+Traceback (most recent call last):
+ File "$", line $, in test_callback_exception
+ $
+OverflowError: integer 60000 does not fit 'short'
""")
sys.stderr = cStringIO.StringIO()
bigvalue = 20000
@@ -1438,24 +1438,24 @@ OverflowError: integer 60000 does not fit 'short'
During the call to 'onerror', another exception occurred:
TypeError: $integer$
-""", """\
-Exception ignored from cffi callback <function$Zcb1 at 0x$>, trying to convert the result back to C:
-Traceback (most recent call last):
- File "$", line $, in test_callback_exception
- $
-OverflowError: integer 60000 does not fit 'short'
-Exception ignored during handling of the above exception by 'onerror':
-Traceback (most recent call last):
- File "$", line $, in test_callback_exception
- $
-TypeError: $integer$
+""", """\
+Exception ignored from cffi callback <function$Zcb1 at 0x$>, trying to convert the result back to C:
+Traceback (most recent call last):
+ File "$", line $, in test_callback_exception
+ $
+OverflowError: integer 60000 does not fit 'short'
+Exception ignored during handling of the above exception by 'onerror':
+Traceback (most recent call last):
+ File "$", line $, in test_callback_exception
+ $
+TypeError: $integer$
""")
#
sys.stderr = cStringIO.StringIO()
seen = "not a list" # this makes the oops() function crash
assert ff(bigvalue) == -42
- # the $ after the AttributeError message are for the suggestions that
- # will be added in Python 3.10
+ # the $ after the AttributeError message are for the suggestions that
+ # will be added in Python 3.10
assert matches(sys.stderr.getvalue(), """\
From cffi callback <function$Zcb1 at 0x$>:
Trying to convert the result back to C:
@@ -1466,18 +1466,18 @@ During the call to 'onerror', another exception occurred:
Traceback (most recent call last):
File "$", line $, in oops
$
-AttributeError: 'str' object has no attribute 'append$
-""", """\
-Exception ignored from cffi callback <function$Zcb1 at 0x$>, trying to convert the result back to C:
-Traceback (most recent call last):
- File "$", line $, in test_callback_exception
- $
-OverflowError: integer 60000 does not fit 'short'
-Exception ignored during handling of the above exception by 'onerror':
-Traceback (most recent call last):
- File "$", line $, in oops
- $
-AttributeError: 'str' object has no attribute 'append$
+AttributeError: 'str' object has no attribute 'append$
+""", """\
+Exception ignored from cffi callback <function$Zcb1 at 0x$>, trying to convert the result back to C:
+Traceback (most recent call last):
+ File "$", line $, in test_callback_exception
+ $
+OverflowError: integer 60000 does not fit 'short'
+Exception ignored during handling of the above exception by 'onerror':
+Traceback (most recent call last):
+ File "$", line $, in oops
+ $
+AttributeError: 'str' object has no attribute 'append$
""")
finally:
sys.stderr = orig_stderr
@@ -1512,7 +1512,7 @@ def test_a_lot_of_callbacks():
def make_callback(m):
def cb(n):
return n + m
- return callback(BFunc, cb, 42) # 'cb' goes out of scope
+ return callback(BFunc, cb, 42) # 'cb' goes out of scope
#
flist = [make_callback(i) for i in range(BIGNUM)]
for i, f in enumerate(flist):
@@ -1714,8 +1714,8 @@ def test_enum_in_struct():
assert ("an integer is required" in msg or # CPython
"unsupported operand type for int(): 'NoneType'" in msg or # old PyPys
"expected integer, got NoneType object" in msg) # newer PyPys
- with pytest.raises(TypeError):
- p.a1 = "def"
+ with pytest.raises(TypeError):
+ p.a1 = "def"
if sys.version_info < (3,):
BEnum2 = new_enum_type(unicode("foo"), (unicode('abc'),), (5,), BInt)
assert string(cast(BEnum2, 5)) == 'abc'
@@ -1845,17 +1845,17 @@ def test_bitfield_instance():
p.a1 = -1
assert p.a1 == -1
p.a1 = 0
- with pytest.raises(OverflowError):
- p.a1 = 2
+ with pytest.raises(OverflowError):
+ p.a1 = 2
assert p.a1 == 0
#
p.a1 = -1
p.a2 = 3
p.a3 = -4
- with pytest.raises(OverflowError):
- p.a3 = 4
- with pytest.raises(OverflowError) as e:
- p.a3 = -5
+ with pytest.raises(OverflowError):
+ p.a3 = 4
+ with pytest.raises(OverflowError) as e:
+ p.a3 = -5
assert str(e.value) == ("value -5 outside the range allowed by the "
"bit field width: -4 <= x <= 3")
assert p.a1 == -1 and p.a2 == 3 and p.a3 == -4
@@ -1864,8 +1864,8 @@ def test_bitfield_instance():
# allows also setting the value "1" (it still gets read back as -1)
p.a1 = 1
assert p.a1 == -1
- with pytest.raises(OverflowError) as e:
- p.a1 = -2
+ with pytest.raises(OverflowError) as e:
+ p.a1 = -2
assert str(e.value) == ("value -2 outside the range allowed by the "
"bit field width: -1 <= x <= 1")
@@ -1925,17 +1925,17 @@ def test_assign_string():
assert string(a[2]) == b"."
a[2] = b"12345"
assert string(a[2]) == b"12345"
- with pytest.raises(IndexError) as e:
- a[2] = b"123456"
+ with pytest.raises(IndexError) as e:
+ a[2] = b"123456"
assert 'char[5]' in str(e.value)
assert 'got 6 characters' in str(e.value)
def test_add_error():
x = cast(new_primitive_type("int"), 42)
- with pytest.raises(TypeError):
- x + 1
- with pytest.raises(TypeError):
- x - 1
+ with pytest.raises(TypeError):
+ x + 1
+ with pytest.raises(TypeError):
+ x - 1
def test_void_errors():
py.test.raises(ValueError, alignof, new_void_type())
@@ -1969,7 +1969,7 @@ def test_more_overflow_errors():
def test_newp_copying():
"""Test that we can do newp(<type>, <cdata of the given type>) for most
- types, including same-type arrays.
+ types, including same-type arrays.
"""
BInt = new_primitive_type("int")
p = newp(new_pointer_type(BInt), cast(BInt, 42))
@@ -1998,9 +1998,9 @@ def test_newp_copying():
a1 = newp(BArray, [1, 2, 3, 4])
py.test.raises(TypeError, newp, BArray, a1)
BArray6 = new_array_type(new_pointer_type(BInt), 6)
- a1 = newp(BArray6, [10, 20, 30])
- a2 = newp(BArray6, a1)
- assert list(a2) == [10, 20, 30, 0, 0, 0]
+ a1 = newp(BArray6, [10, 20, 30])
+ a2 = newp(BArray6, a1)
+ assert list(a2) == [10, 20, 30, 0, 0, 0]
#
s1 = newp(BStructPtr, [42])
s2 = newp(BStructPtr, s1[0])
@@ -2053,11 +2053,11 @@ def test_string_byte():
assert string(a, 8).startswith(b'ABC') # may contain additional garbage
def test_string_wchar():
- for typename in ["wchar_t", "char16_t", "char32_t"]:
- _test_string_wchar_variant(typename)
-
-def _test_string_wchar_variant(typename):
- BWChar = new_primitive_type(typename)
+ for typename in ["wchar_t", "char16_t", "char32_t"]:
+ _test_string_wchar_variant(typename)
+
+def _test_string_wchar_variant(typename):
+ BWChar = new_primitive_type(typename)
assert string(cast(BWChar, 42)) == u+'*'
assert string(cast(BWChar, 0x4253)) == u+'\u4253'
assert string(cast(BWChar, 0)) == u+'\x00'
@@ -2219,45 +2219,45 @@ def test_cast_with_functionptr():
py.test.raises(TypeError, newp, BStructPtr, [cast(BFunc2, 0)])
def test_wchar():
- _test_wchar_variant("wchar_t")
- if sys.platform.startswith("linux"):
- BWChar = new_primitive_type("wchar_t")
- assert sizeof(BWChar) == 4
- # wchar_t is often signed on Linux, but not always (e.g. on ARM)
- assert int(cast(BWChar, -1)) in (-1, 4294967295)
-
-def test_char16():
- BChar16 = new_primitive_type("char16_t")
- assert sizeof(BChar16) == 2
- _test_wchar_variant("char16_t")
- assert int(cast(BChar16, -1)) == 0xffff # always unsigned
-
-def test_char32():
- BChar32 = new_primitive_type("char32_t")
- assert sizeof(BChar32) == 4
- _test_wchar_variant("char32_t")
- assert int(cast(BChar32, -1)) == 0xffffffff # always unsigned
-
-def _test_wchar_variant(typename):
- BWChar = new_primitive_type(typename)
+ _test_wchar_variant("wchar_t")
+ if sys.platform.startswith("linux"):
+ BWChar = new_primitive_type("wchar_t")
+ assert sizeof(BWChar) == 4
+ # wchar_t is often signed on Linux, but not always (e.g. on ARM)
+ assert int(cast(BWChar, -1)) in (-1, 4294967295)
+
+def test_char16():
+ BChar16 = new_primitive_type("char16_t")
+ assert sizeof(BChar16) == 2
+ _test_wchar_variant("char16_t")
+ assert int(cast(BChar16, -1)) == 0xffff # always unsigned
+
+def test_char32():
+ BChar32 = new_primitive_type("char32_t")
+ assert sizeof(BChar32) == 4
+ _test_wchar_variant("char32_t")
+ assert int(cast(BChar32, -1)) == 0xffffffff # always unsigned
+
+def _test_wchar_variant(typename):
+ BWChar = new_primitive_type(typename)
BInt = new_primitive_type("int")
pyuni4 = {1: True, 2: False}[len(u+'\U00012345')]
wchar4 = {2: False, 4: True}[sizeof(BWChar)]
- assert str(cast(BWChar, 0x45)) == "<cdata '%s' %s'E'>" % (
- typename, mandatory_u_prefix)
- assert str(cast(BWChar, 0x1234)) == "<cdata '%s' %s'\u1234'>" % (
- typename, mandatory_u_prefix)
- if not _hacked_pypy_uni4():
- if wchar4:
+ assert str(cast(BWChar, 0x45)) == "<cdata '%s' %s'E'>" % (
+ typename, mandatory_u_prefix)
+ assert str(cast(BWChar, 0x1234)) == "<cdata '%s' %s'\u1234'>" % (
+ typename, mandatory_u_prefix)
+ if not _hacked_pypy_uni4():
+ if wchar4:
x = cast(BWChar, 0x12345)
- assert str(x) == "<cdata '%s' %s'\U00012345'>" % (
- typename, mandatory_u_prefix)
+ assert str(x) == "<cdata '%s' %s'\U00012345'>" % (
+ typename, mandatory_u_prefix)
assert int(x) == 0x12345
- else:
- x = cast(BWChar, 0x18345)
- assert str(x) == "<cdata '%s' %s'\u8345'>" % (
- typename, mandatory_u_prefix)
- assert int(x) == 0x8345
+ else:
+ x = cast(BWChar, 0x18345)
+ assert str(x) == "<cdata '%s' %s'\u8345'>" % (
+ typename, mandatory_u_prefix)
+ assert int(x) == 0x8345
#
BWCharP = new_pointer_type(BWChar)
BStruct = new_struct_type("struct foo_s")
@@ -2267,16 +2267,16 @@ def _test_wchar_variant(typename):
s = newp(BStructPtr)
s.a1 = u+'\x00'
assert s.a1 == u+'\x00'
- with pytest.raises(TypeError):
- s.a1 = b'a'
- with pytest.raises(TypeError):
- s.a1 = bytechr(0xFF)
+ with pytest.raises(TypeError):
+ s.a1 = b'a'
+ with pytest.raises(TypeError):
+ s.a1 = bytechr(0xFF)
s.a1 = u+'\u1234'
assert s.a1 == u+'\u1234'
if pyuni4:
- if wchar4:
- s.a1 = u+'\U00012345'
- assert s.a1 == u+'\U00012345'
+ if wchar4:
+ s.a1 = u+'\U00012345'
+ assert s.a1 == u+'\U00012345'
elif wchar4:
if not _hacked_pypy_uni4():
s.a1 = cast(BWChar, 0x12345)
@@ -2284,8 +2284,8 @@ def _test_wchar_variant(typename):
s.a1 = u+'\ud807\udf44'
assert s.a1 == u+'\U00011f44'
else:
- with pytest.raises(TypeError):
- s.a1 = u+'\U00012345'
+ with pytest.raises(TypeError):
+ s.a1 = u+'\U00012345'
#
BWCharArray = new_array_type(BWCharP, None)
a = newp(BWCharArray, u+'hello \u1234 world')
@@ -2309,21 +2309,21 @@ def _test_wchar_variant(typename):
assert list(a) == expected
got = [a[i] for i in range(4)]
assert got == expected
- with pytest.raises(IndexError):
- a[4]
+ with pytest.raises(IndexError):
+ a[4]
#
w = cast(BWChar, 'a')
- assert repr(w) == "<cdata '%s' %s'a'>" % (typename, mandatory_u_prefix)
+ assert repr(w) == "<cdata '%s' %s'a'>" % (typename, mandatory_u_prefix)
assert str(w) == repr(w)
assert string(w) == u+'a'
assert int(w) == ord('a')
w = cast(BWChar, 0x1234)
- assert repr(w) == "<cdata '%s' %s'\u1234'>" % (typename, mandatory_u_prefix)
+ assert repr(w) == "<cdata '%s' %s'\u1234'>" % (typename, mandatory_u_prefix)
assert str(w) == repr(w)
assert string(w) == u+'\u1234'
assert int(w) == 0x1234
w = cast(BWChar, u+'\u8234')
- assert repr(w) == "<cdata '%s' %s'\u8234'>" % (typename, mandatory_u_prefix)
+ assert repr(w) == "<cdata '%s' %s'\u8234'>" % (typename, mandatory_u_prefix)
assert str(w) == repr(w)
assert string(w) == u+'\u8234'
assert int(w) == 0x8234
@@ -2331,8 +2331,8 @@ def _test_wchar_variant(typename):
assert repr(w) == "<cdata 'int' 4660>"
if wchar4 and not _hacked_pypy_uni4():
w = cast(BWChar, u+'\U00012345')
- assert repr(w) == "<cdata '%s' %s'\U00012345'>" % (
- typename, mandatory_u_prefix)
+ assert repr(w) == "<cdata '%s' %s'\U00012345'>" % (
+ typename, mandatory_u_prefix)
assert str(w) == repr(w)
assert string(w) == u+'\U00012345'
assert int(w) == 0x12345
@@ -2359,7 +2359,7 @@ def _test_wchar_variant(typename):
py.test.raises(RuntimeError, string, q)
#
def cb(p):
- assert repr(p).startswith("<cdata '%s *' 0x" % typename)
+ assert repr(p).startswith("<cdata '%s *' 0x" % typename)
return len(string(p))
BFunc = new_function_type((BWCharP,), BInt, False)
f = callback(BFunc, cb, -42)
@@ -2372,27 +2372,27 @@ def _test_wchar_variant(typename):
x = cast(BWChar, -1)
py.test.raises(ValueError, string, x)
-def test_wchar_variants_mix():
- BWChar = new_primitive_type("wchar_t")
- BChar16 = new_primitive_type("char16_t")
- BChar32 = new_primitive_type("char32_t")
- assert int(cast(BChar32, cast(BChar16, -2))) == 0xfffe
- assert int(cast(BWChar, cast(BChar16, -2))) == 0xfffe
- assert int(cast(BChar16, cast(BChar32, 0x0001f345))) == 0xf345
- assert int(cast(BChar16, cast(BWChar, 0x0001f345))) == 0xf345
- #
- BChar16A = new_array_type(new_pointer_type(BChar16), None)
- BChar32A = new_array_type(new_pointer_type(BChar32), None)
- x = cast(BChar32, 'A')
- py.test.raises(TypeError, newp, BChar16A, [x])
- x = cast(BChar16, 'A')
- py.test.raises(TypeError, newp, BChar32A, [x])
- #
- a = newp(BChar16A, u+'\U00012345')
- assert len(a) == 3
- a = newp(BChar32A, u+'\U00012345')
- assert len(a) == 2 # even if the Python unicode string above is 2 chars
-
+def test_wchar_variants_mix():
+ BWChar = new_primitive_type("wchar_t")
+ BChar16 = new_primitive_type("char16_t")
+ BChar32 = new_primitive_type("char32_t")
+ assert int(cast(BChar32, cast(BChar16, -2))) == 0xfffe
+ assert int(cast(BWChar, cast(BChar16, -2))) == 0xfffe
+ assert int(cast(BChar16, cast(BChar32, 0x0001f345))) == 0xf345
+ assert int(cast(BChar16, cast(BWChar, 0x0001f345))) == 0xf345
+ #
+ BChar16A = new_array_type(new_pointer_type(BChar16), None)
+ BChar32A = new_array_type(new_pointer_type(BChar32), None)
+ x = cast(BChar32, 'A')
+ py.test.raises(TypeError, newp, BChar16A, [x])
+ x = cast(BChar16, 'A')
+ py.test.raises(TypeError, newp, BChar32A, [x])
+ #
+ a = newp(BChar16A, u+'\U00012345')
+ assert len(a) == 3
+ a = newp(BChar32A, u+'\U00012345')
+ assert len(a) == 2 # even if the Python unicode string above is 2 chars
+
def test_keepalive_struct():
# exception to the no-keepalive rule: p=newp(BStructPtr) returns a
# pointer owning the memory, and p[0] returns a pointer to the
@@ -2442,11 +2442,11 @@ def test_owning_repr():
def test_cannot_dereference_void():
BVoidP = new_pointer_type(new_void_type())
p = cast(BVoidP, 123456)
- with pytest.raises(TypeError):
- p[0]
+ with pytest.raises(TypeError):
+ p[0]
p = cast(BVoidP, 0)
- with pytest.raises((TypeError, RuntimeError)):
- p[0]
+ with pytest.raises((TypeError, RuntimeError)):
+ p[0]
def test_iter():
BInt = new_primitive_type("int")
@@ -2466,15 +2466,15 @@ def test_cmp():
q = cast(BInt, 124)
assert (p == q) is False
assert (p != q) is True
- assert (q == p) is False
- assert (q != p) is True
- if strict_compare:
- with pytest.raises(TypeError): p < q
- with pytest.raises(TypeError): p <= q
- with pytest.raises(TypeError): q < p
- with pytest.raises(TypeError): q <= p
- with pytest.raises(TypeError): p > q
- with pytest.raises(TypeError): p >= q
+ assert (q == p) is False
+ assert (q != p) is True
+ if strict_compare:
+ with pytest.raises(TypeError): p < q
+ with pytest.raises(TypeError): p <= q
+ with pytest.raises(TypeError): q < p
+ with pytest.raises(TypeError): q <= p
+ with pytest.raises(TypeError): p > q
+ with pytest.raises(TypeError): p >= q
r = cast(BVoidP, p)
assert (p < r) is False
assert (p <= r) is True
@@ -2507,7 +2507,7 @@ def test_buffer():
buf = buffer(c)
assert repr(buf).startswith('<_cffi_backend.buffer object at 0x')
assert bytes(buf) == b"hi there\x00"
- assert type(buf) is buffer
+ assert type(buf) is buffer
if sys.version_info < (3,):
assert str(buf) == "hi there\x00"
assert unicode(buf) == u+"hi there\x00"
@@ -2520,8 +2520,8 @@ def test_buffer():
try:
expected = b"hi there\x00"[i]
except IndexError:
- with pytest.raises(IndexError):
- buf[i]
+ with pytest.raises(IndexError):
+ buf[i]
else:
assert buf[i] == bitem2bchr(expected)
# --mb_slice--
@@ -2548,18 +2548,18 @@ def test_buffer():
try:
expected[i] = bytechr(i & 0xff)
except IndexError:
- with pytest.raises(IndexError):
- buf[i] = bytechr(i & 0xff)
+ with pytest.raises(IndexError):
+ buf[i] = bytechr(i & 0xff)
else:
buf[i] = bytechr(i & 0xff)
assert list(buf) == expected
# --mb_ass_slice--
buf[:] = b"hi there\x00"
assert list(buf) == list(c) == list(map(bitem2bchr, b"hi there\x00"))
- with pytest.raises(ValueError):
- buf[:] = b"shorter"
- with pytest.raises(ValueError):
- buf[:] = b"this is much too long!"
+ with pytest.raises(ValueError):
+ buf[:] = b"shorter"
+ with pytest.raises(ValueError):
+ buf[:] = b"this is much too long!"
buf[4:2] = b"" # no effect, but should work
assert buf[:] == b"hi there\x00"
buf[:2] = b"HI"
@@ -2595,8 +2595,8 @@ def test_errno():
assert get_errno() == 95
def test_errno_callback():
- if globals().get('PY_DOT_PY'):
- py.test.skip("cannot run this test on py.py (e.g. fails on Windows)")
+ if globals().get('PY_DOT_PY'):
+ py.test.skip("cannot run this test on py.py (e.g. fails on Windows)")
set_errno(95)
def cb():
e = get_errno()
@@ -2633,16 +2633,16 @@ def test_bug_delitem():
BChar = new_primitive_type("char")
BCharP = new_pointer_type(BChar)
x = newp(BCharP)
- with pytest.raises(TypeError):
- del x[0]
+ with pytest.raises(TypeError):
+ del x[0]
def test_bug_delattr():
BLong = new_primitive_type("long")
BStruct = new_struct_type("struct foo")
complete_struct_or_union(BStruct, [('a1', BLong, -1)])
x = newp(new_pointer_type(BStruct))
- with pytest.raises(AttributeError):
- del x.a1
+ with pytest.raises(AttributeError):
+ del x.a1
def test_variable_length_struct():
py.test.skip("later")
@@ -2660,8 +2660,8 @@ def test_variable_length_struct():
assert sizeof(x) == 6 * size_of_long()
x[4] = 123
assert x[4] == 123
- with pytest.raises(IndexError):
- x[5]
+ with pytest.raises(IndexError):
+ x[5]
assert len(x.a2) == 5
#
py.test.raises(TypeError, newp, BStructP, [123])
@@ -2891,40 +2891,40 @@ def test_bool():
py.test.raises(OverflowError, newp, BBoolP, 2)
py.test.raises(OverflowError, newp, BBoolP, -1)
BCharP = new_pointer_type(new_primitive_type("char"))
- p = newp(BCharP, b'\x01')
+ p = newp(BCharP, b'\x01')
+ q = cast(BBoolP, p)
+ assert q[0] is True
+ p = newp(BCharP, b'\x00')
q = cast(BBoolP, p)
- assert q[0] is True
- p = newp(BCharP, b'\x00')
- q = cast(BBoolP, p)
- assert q[0] is False
+ assert q[0] is False
py.test.raises(TypeError, string, cast(BBool, False))
BDouble = new_primitive_type("double")
assert int(cast(BBool, cast(BDouble, 0.1))) == 1
assert int(cast(BBool, cast(BDouble, 0.0))) == 0
- BBoolA = new_array_type(BBoolP, None)
- p = newp(BBoolA, b'\x01\x00')
- assert p[0] is True
- assert p[1] is False
-
-def test_bool_forbidden_cases():
- BBool = new_primitive_type("_Bool")
- BBoolP = new_pointer_type(BBool)
- BBoolA = new_array_type(BBoolP, None)
- BCharP = new_pointer_type(new_primitive_type("char"))
- p = newp(BCharP, b'X')
- q = cast(BBoolP, p)
- with pytest.raises(ValueError):
- q[0]
- py.test.raises(TypeError, newp, BBoolP, b'\x00')
- assert newp(BBoolP, 0)[0] is False
- assert newp(BBoolP, 1)[0] is True
- py.test.raises(OverflowError, newp, BBoolP, 2)
- py.test.raises(OverflowError, newp, BBoolP, -1)
- py.test.raises(ValueError, newp, BBoolA, b'\x00\x01\x02')
- py.test.raises(OverflowError, newp, BBoolA, [0, 1, 2])
- py.test.raises(TypeError, string, newp(BBoolP, 1))
- py.test.raises(TypeError, string, newp(BBoolA, [1]))
-
+ BBoolA = new_array_type(BBoolP, None)
+ p = newp(BBoolA, b'\x01\x00')
+ assert p[0] is True
+ assert p[1] is False
+
+def test_bool_forbidden_cases():
+ BBool = new_primitive_type("_Bool")
+ BBoolP = new_pointer_type(BBool)
+ BBoolA = new_array_type(BBoolP, None)
+ BCharP = new_pointer_type(new_primitive_type("char"))
+ p = newp(BCharP, b'X')
+ q = cast(BBoolP, p)
+ with pytest.raises(ValueError):
+ q[0]
+ py.test.raises(TypeError, newp, BBoolP, b'\x00')
+ assert newp(BBoolP, 0)[0] is False
+ assert newp(BBoolP, 1)[0] is True
+ py.test.raises(OverflowError, newp, BBoolP, 2)
+ py.test.raises(OverflowError, newp, BBoolP, -1)
+ py.test.raises(ValueError, newp, BBoolA, b'\x00\x01\x02')
+ py.test.raises(OverflowError, newp, BBoolA, [0, 1, 2])
+ py.test.raises(TypeError, string, newp(BBoolP, 1))
+ py.test.raises(TypeError, string, newp(BBoolA, [1]))
+
def test_typeoffsetof():
BChar = new_primitive_type("char")
BStruct = new_struct_type("struct foo")
@@ -3214,10 +3214,10 @@ def test_slice():
assert c[1] == 123
assert c[3] == 456
assert d[2] == 456
- with pytest.raises(IndexError):
- d[3]
- with pytest.raises(IndexError):
- d[-1]
+ with pytest.raises(IndexError):
+ d[3]
+ with pytest.raises(IndexError):
+ d[-1]
def test_slice_ptr():
BIntP = new_pointer_type(new_primitive_type("int"))
@@ -3235,8 +3235,8 @@ def test_slice_array_checkbounds():
c = newp(BIntArray, 5)
c[0:5]
assert len(c[5:5]) == 0
- with pytest.raises(IndexError):
- c[-1:1]
+ with pytest.raises(IndexError):
+ c[-1:1]
cp = c + 0
cp[-1:1]
@@ -3244,23 +3244,23 @@ def test_nonstandard_slice():
BIntP = new_pointer_type(new_primitive_type("int"))
BIntArray = new_array_type(BIntP, None)
c = newp(BIntArray, 5)
- with pytest.raises(IndexError) as e:
- c[:5]
+ with pytest.raises(IndexError) as e:
+ c[:5]
assert str(e.value) == "slice start must be specified"
- with pytest.raises(IndexError) as e:
- c[4:]
+ with pytest.raises(IndexError) as e:
+ c[4:]
assert str(e.value) == "slice stop must be specified"
- with pytest.raises(IndexError) as e:
- c[1:2:3]
+ with pytest.raises(IndexError) as e:
+ c[1:2:3]
assert str(e.value) == "slice with step not supported"
- with pytest.raises(IndexError) as e:
- c[1:2:1]
+ with pytest.raises(IndexError) as e:
+ c[1:2:1]
assert str(e.value) == "slice with step not supported"
- with pytest.raises(IndexError) as e:
- c[4:2]
+ with pytest.raises(IndexError) as e:
+ c[4:2]
assert str(e.value) == "slice start > stop"
- with pytest.raises(IndexError) as e:
- c[6:6]
+ with pytest.raises(IndexError) as e:
+ c[6:6]
assert str(e.value) == "index too large (expected 6 <= 5)"
def test_setslice():
@@ -3274,11 +3274,11 @@ def test_setslice():
assert list(c) == [0, 100, 300, 400, 0]
cp[-1:1] = iter([500, 600])
assert list(c) == [0, 100, 500, 600, 0]
- with pytest.raises(ValueError):
- cp[-1:1] = [1000]
+ with pytest.raises(ValueError):
+ cp[-1:1] = [1000]
assert list(c) == [0, 100, 1000, 600, 0]
- with pytest.raises(ValueError):
- cp[-1:1] = (700, 800, 900)
+ with pytest.raises(ValueError):
+ cp[-1:1] = (700, 800, 900)
assert list(c) == [0, 100, 700, 800, 0]
def test_setslice_array():
@@ -3538,14 +3538,14 @@ def test_struct_array_no_length():
assert sizeof(q[0]) == sizeof(BStruct)
#
# error cases
- with pytest.raises(IndexError):
- p.y[4]
- with pytest.raises(TypeError):
- p.y = cast(BIntP, 0)
- with pytest.raises(TypeError):
- p.y = 15
- with pytest.raises(TypeError):
- p.y = None
+ with pytest.raises(IndexError):
+ p.y[4]
+ with pytest.raises(TypeError):
+ p.y = cast(BIntP, 0)
+ with pytest.raises(TypeError):
+ p.y = 15
+ with pytest.raises(TypeError):
+ p.y = None
#
# accepting this may be specified by the C99 standard,
# or a GCC strangeness...
@@ -3567,16 +3567,16 @@ def test_struct_array_no_length():
assert p.a[1] == 20
assert p.a[2] == 30
assert p.a[3] == 0
- #
- # struct of struct of varsized array
- BStruct2 = new_struct_type("bar")
- complete_struct_or_union(BStruct2, [('head', BInt),
- ('tail', BStruct)])
- for i in range(2): # try to detect heap overwrites
- p = newp(new_pointer_type(BStruct2), [100, [200, list(range(50))]])
- assert p.tail.y[49] == 49
-
-
+ #
+ # struct of struct of varsized array
+ BStruct2 = new_struct_type("bar")
+ complete_struct_or_union(BStruct2, [('head', BInt),
+ ('tail', BStruct)])
+ for i in range(2): # try to detect heap overwrites
+ p = newp(new_pointer_type(BStruct2), [100, [200, list(range(50))]])
+ assert p.tail.y[49] == 49
+
+
def test_struct_array_no_length_explicit_position():
BInt = new_primitive_type("int")
BIntP = new_pointer_type(BInt)
@@ -3650,22 +3650,22 @@ def test_ass_slice():
p[2:5] = [b"*", b"Z", b"T"]
p[1:3] = b"XY"
assert list(p) == [b"f", b"X", b"Y", b"Z", b"T", b"r", b"\x00"]
- with pytest.raises(TypeError):
- p[1:5] = u+'XYZT'
- with pytest.raises(TypeError):
- p[1:5] = [1, 2, 3, 4]
- #
- for typename in ["wchar_t", "char16_t", "char32_t"]:
- BUniChar = new_primitive_type(typename)
- BArray = new_array_type(new_pointer_type(BUniChar), None)
- p = newp(BArray, u+"foobar")
- p[2:5] = [u+"*", u+"Z", u+"T"]
- p[1:3] = u+"XY"
- assert list(p) == [u+"f", u+"X", u+"Y", u+"Z", u+"T", u+"r", u+"\x00"]
- with pytest.raises(TypeError):
- p[1:5] = b'XYZT'
- with pytest.raises(TypeError):
- p[1:5] = [1, 2, 3, 4]
+ with pytest.raises(TypeError):
+ p[1:5] = u+'XYZT'
+ with pytest.raises(TypeError):
+ p[1:5] = [1, 2, 3, 4]
+ #
+ for typename in ["wchar_t", "char16_t", "char32_t"]:
+ BUniChar = new_primitive_type(typename)
+ BArray = new_array_type(new_pointer_type(BUniChar), None)
+ p = newp(BArray, u+"foobar")
+ p[2:5] = [u+"*", u+"Z", u+"T"]
+ p[1:3] = u+"XY"
+ assert list(p) == [u+"f", u+"X", u+"Y", u+"Z", u+"T", u+"r", u+"\x00"]
+ with pytest.raises(TypeError):
+ p[1:5] = b'XYZT'
+ with pytest.raises(TypeError):
+ p[1:5] = [1, 2, 3, 4]
def test_void_p_arithmetic():
BVoid = new_void_type()
@@ -3676,14 +3676,14 @@ def test_void_p_arithmetic():
assert int(cast(BInt, p - (-42))) == 100042
assert (p + 42) - p == 42
q = cast(new_pointer_type(new_primitive_type("char")), 100000)
- with pytest.raises(TypeError):
- p - q
- with pytest.raises(TypeError):
- q - p
- with pytest.raises(TypeError):
- p + cast(new_primitive_type('int'), 42)
- with pytest.raises(TypeError):
- p - cast(new_primitive_type('int'), 42)
+ with pytest.raises(TypeError):
+ p - q
+ with pytest.raises(TypeError):
+ q - p
+ with pytest.raises(TypeError):
+ p + cast(new_primitive_type('int'), 42)
+ with pytest.raises(TypeError):
+ p - cast(new_primitive_type('int'), 42)
def test_sizeof_sliced_array():
BInt = new_primitive_type("int")
@@ -3695,50 +3695,50 @@ def test_packed():
BLong = new_primitive_type("long")
BChar = new_primitive_type("char")
BShort = new_primitive_type("short")
- for extra_args in [(SF_PACKED,), (0, 1)]:
- BStruct = new_struct_type("struct foo")
- complete_struct_or_union(BStruct, [('a1', BLong, -1),
- ('a2', BChar, -1),
- ('a3', BShort, -1)],
- None, -1, -1, *extra_args)
- d = BStruct.fields
- assert len(d) == 3
- assert d[0][0] == 'a1'
- assert d[0][1].type is BLong
- assert d[0][1].offset == 0
- assert d[0][1].bitshift == -1
- assert d[0][1].bitsize == -1
- assert d[1][0] == 'a2'
- assert d[1][1].type is BChar
- assert d[1][1].offset == sizeof(BLong)
- assert d[1][1].bitshift == -1
- assert d[1][1].bitsize == -1
- assert d[2][0] == 'a3'
- assert d[2][1].type is BShort
- assert d[2][1].offset == sizeof(BLong) + sizeof(BChar)
- assert d[2][1].bitshift == -1
- assert d[2][1].bitsize == -1
- assert sizeof(BStruct) == sizeof(BLong) + sizeof(BChar) + sizeof(BShort)
- assert alignof(BStruct) == 1
- #
- BStruct2 = new_struct_type("struct foo")
- complete_struct_or_union(BStruct2, [('b1', BChar, -1),
- ('b2', BLong, -1)],
- None, -1, -1, 0, 2)
- d = BStruct2.fields
- assert len(d) == 2
- assert d[0][0] == 'b1'
- assert d[0][1].type is BChar
+ for extra_args in [(SF_PACKED,), (0, 1)]:
+ BStruct = new_struct_type("struct foo")
+ complete_struct_or_union(BStruct, [('a1', BLong, -1),
+ ('a2', BChar, -1),
+ ('a3', BShort, -1)],
+ None, -1, -1, *extra_args)
+ d = BStruct.fields
+ assert len(d) == 3
+ assert d[0][0] == 'a1'
+ assert d[0][1].type is BLong
+ assert d[0][1].offset == 0
+ assert d[0][1].bitshift == -1
+ assert d[0][1].bitsize == -1
+ assert d[1][0] == 'a2'
+ assert d[1][1].type is BChar
+ assert d[1][1].offset == sizeof(BLong)
+ assert d[1][1].bitshift == -1
+ assert d[1][1].bitsize == -1
+ assert d[2][0] == 'a3'
+ assert d[2][1].type is BShort
+ assert d[2][1].offset == sizeof(BLong) + sizeof(BChar)
+ assert d[2][1].bitshift == -1
+ assert d[2][1].bitsize == -1
+ assert sizeof(BStruct) == sizeof(BLong) + sizeof(BChar) + sizeof(BShort)
+ assert alignof(BStruct) == 1
+ #
+ BStruct2 = new_struct_type("struct foo")
+ complete_struct_or_union(BStruct2, [('b1', BChar, -1),
+ ('b2', BLong, -1)],
+ None, -1, -1, 0, 2)
+ d = BStruct2.fields
+ assert len(d) == 2
+ assert d[0][0] == 'b1'
+ assert d[0][1].type is BChar
assert d[0][1].offset == 0
assert d[0][1].bitshift == -1
assert d[0][1].bitsize == -1
- assert d[1][0] == 'b2'
- assert d[1][1].type is BLong
- assert d[1][1].offset == 2
+ assert d[1][0] == 'b2'
+ assert d[1][1].type is BLong
+ assert d[1][1].offset == 2
assert d[1][1].bitshift == -1
assert d[1][1].bitsize == -1
- assert sizeof(BStruct2) == 2 + sizeof(BLong)
- assert alignof(BStruct2) == 2
+ assert sizeof(BStruct2) == 2 + sizeof(BLong)
+ assert alignof(BStruct2) == 2
def test_packed_with_bitfields():
if sys.platform == "win32":
@@ -3780,27 +3780,27 @@ def test_from_buffer_not_str_unicode():
except ImportError:
pass
else:
- # Python 2 only
- contents = from_buffer(BCharA, buffer(b"foo"))
- assert len(contents) == len(p1)
- for i in range(len(contents)):
- assert contents[i] == p1[i]
- p4 = buffer(u+"foo")
- contents = from_buffer(BCharA, buffer(u+"foo"))
- assert len(contents) == len(p4)
- for i in range(len(contents)):
- assert contents[i] == p4[i]
+ # Python 2 only
+ contents = from_buffer(BCharA, buffer(b"foo"))
+ assert len(contents) == len(p1)
+ for i in range(len(contents)):
+ assert contents[i] == p1[i]
+ p4 = buffer(u+"foo")
+ contents = from_buffer(BCharA, buffer(u+"foo"))
+ assert len(contents) == len(p4)
+ for i in range(len(contents)):
+ assert contents[i] == p4[i]
try:
from __builtin__ import memoryview
except ImportError:
pass
else:
- contents = from_buffer(BCharA, memoryview(b"foo"))
- assert len(contents) == len(p1)
- for i in range(len(contents)):
- assert contents[i] == p1[i]
+ contents = from_buffer(BCharA, memoryview(b"foo"))
+ assert len(contents) == len(p1)
+ for i in range(len(contents)):
+ assert contents[i] == p1[i]
+
-
def test_from_buffer_bytearray():
a = bytearray(b"xyz")
BChar = new_primitive_type("char")
@@ -3873,163 +3873,163 @@ def test_from_buffer_more_cases():
check(4 | 8, "CHB", "GTB")
check(4 | 16, "CHB", "ROB")
-def test_from_buffer_require_writable():
- BChar = new_primitive_type("char")
- BCharP = new_pointer_type(BChar)
- BCharA = new_array_type(BCharP, None)
- p1 = from_buffer(BCharA, b"foo", False)
- assert p1 == from_buffer(BCharA, b"foo", False)
- py.test.raises((TypeError, BufferError), from_buffer, BCharA, b"foo", True)
- ba = bytearray(b"foo")
- p1 = from_buffer(BCharA, ba, True)
- p1[0] = b"g"
- assert ba == b"goo"
-
-def test_from_buffer_types():
- BInt = new_primitive_type("int")
- BIntP = new_pointer_type(BInt)
- BIntA = new_array_type(BIntP, None)
- lst = [-12345678, 87654321, 489148]
- bytestring = bytearray(buffer(newp(BIntA, lst))[:] + b'XYZ')
- lst2 = lst + [42, -999999999]
- bytestring2 = bytearray(buffer(newp(BIntA, lst2))[:] + b'XYZ')
- #
- p1 = from_buffer(BIntA, bytestring) # int[]
- assert typeof(p1) is BIntA
- assert len(p1) == 3
- assert p1[0] == lst[0]
- assert p1[1] == lst[1]
- assert p1[2] == lst[2]
- with pytest.raises(IndexError):
- p1[3]
- with pytest.raises(IndexError):
- p1[-1]
- #
- py.test.raises(TypeError, from_buffer, BInt, bytestring)
- #
- p2 = from_buffer(BIntP, bytestring) # int *
- assert p2 == p1 or 'PY_DOT_PY' in globals()
- # note: on py.py ^^^, bytearray buffers are not emulated well enough
- assert typeof(p2) is BIntP
- assert p2[0] == lst[0]
- assert p2[1] == lst[1]
- assert p2[2] == lst[2]
- # hopefully does not crash, but doesn't raise an exception:
- p2[3]
- p2[-1]
- # not enough data even for one, but this is not enforced:
- from_buffer(BIntP, b"")
- #
- BIntA2 = new_array_type(BIntP, 2)
- p2 = from_buffer(BIntA2, bytestring) # int[2]
- assert typeof(p2) is BIntA2
- assert len(p2) == 2
- assert p2[0] == lst[0]
- assert p2[1] == lst[1]
- with pytest.raises(IndexError):
- p2[2]
- with pytest.raises(IndexError):
- p2[-1]
- assert p2 == p1 or 'PY_DOT_PY' in globals()
- #
- BIntA4 = new_array_type(BIntP, 4) # int[4]: too big
- py.test.raises(ValueError, from_buffer, BIntA4, bytestring)
- #
- BStruct = new_struct_type("foo")
- complete_struct_or_union(BStruct, [('a1', BInt, -1),
- ('a2', BInt, -1)])
- BStructP = new_pointer_type(BStruct)
- BStructA = new_array_type(BStructP, None)
- p1 = from_buffer(BStructA, bytestring2) # struct[]
- assert len(p1) == 2
- assert typeof(p1) is BStructA
- assert p1[0].a1 == lst2[0]
- assert p1[0].a2 == lst2[1]
- assert p1[1].a1 == lst2[2]
- assert p1[1].a2 == lst2[3]
- with pytest.raises(IndexError):
- p1[2]
- with pytest.raises(IndexError):
- p1[-1]
- assert repr(p1) == "<cdata 'foo[]' buffer len 2 from 'bytearray' object>"
- #
- p2 = from_buffer(BStructP, bytestring2) # 'struct *'
- assert p2 == p1 or 'PY_DOT_PY' in globals()
- assert typeof(p2) is BStructP
- assert p2.a1 == lst2[0]
- assert p2.a2 == lst2[1]
- assert p2[0].a1 == lst2[0]
- assert p2[0].a2 == lst2[1]
- assert p2[1].a1 == lst2[2]
- assert p2[1].a2 == lst2[3]
- # does not crash:
- p2[2]
- p2[-1]
- # not enough data even for one, but this is not enforced:
- from_buffer(BStructP, b"")
- from_buffer(BStructP, b"1234567")
- #
- release(p1)
- assert repr(p1) == "<cdata 'foo[]' buffer RELEASED>"
- #
- BEmptyStruct = new_struct_type("empty")
- complete_struct_or_union(BEmptyStruct, [], Ellipsis, 0)
- assert sizeof(BEmptyStruct) == 0
- BEmptyStructP = new_pointer_type(BEmptyStruct)
- BEmptyStructA = new_array_type(BEmptyStructP, None)
- py.test.raises(ZeroDivisionError, from_buffer, # empty[]
- BEmptyStructA, bytestring)
- #
- BEmptyStructA5 = new_array_type(BEmptyStructP, 5)
- p1 = from_buffer(BEmptyStructA5, bytestring) # struct empty[5]
- assert typeof(p1) is BEmptyStructA5
- assert len(p1) == 5
- assert (cast(BIntP, p1) == from_buffer(BIntA, bytestring)
- or 'PY_DOT_PY' in globals())
- #
- BVarStruct = new_struct_type("varfoo")
- BVarStructP = new_pointer_type(BVarStruct)
- complete_struct_or_union(BVarStruct, [('a1', BInt, -1),
- ('va', BIntA, -1)])
- with pytest.raises(TypeError):
- from_buffer(BVarStruct, bytestring)
- pv = from_buffer(BVarStructP, bytestring) # varfoo *
- assert pv.a1 == lst[0]
- assert pv.va[0] == lst[1]
- assert pv.va[1] == lst[2]
- assert sizeof(pv[0]) == 1 * size_of_int()
- with pytest.raises(TypeError):
- len(pv.va)
- # hopefully does not crash, but doesn't raise an exception:
- pv.va[2]
- pv.va[-1]
- # not enough data even for one, but this is not enforced:
- from_buffer(BVarStructP, b"")
- assert repr(pv) == "<cdata 'varfoo *' buffer from 'bytearray' object>"
- assert repr(pv[0]).startswith("<cdata 'varfoo &' ")
- #
- release(pv)
- assert repr(pv) == "<cdata 'varfoo *' buffer RELEASED>"
- assert repr(pv[0]).startswith("<cdata 'varfoo &' ")
- #
- pv = from_buffer(BVarStructP, bytestring) # make a fresh one
- with pytest.raises(ValueError):
- release(pv[0])
-
-def test_issue483():
- BInt = new_primitive_type("int")
- BIntP = new_pointer_type(BInt)
- BIntA = new_array_type(BIntP, None)
- lst = list(range(25))
- bytestring = bytearray(buffer(newp(BIntA, lst))[:] + b'XYZ')
- p1 = from_buffer(BIntA, bytestring) # int[]
- assert len(buffer(p1)) == 25 * size_of_int()
- assert sizeof(p1) == 25 * size_of_int()
- #
- p2 = from_buffer(BIntP, bytestring)
- assert sizeof(p2) == size_of_ptr()
- assert len(buffer(p2)) == size_of_int() # first element only, by default
-
+def test_from_buffer_require_writable():
+ BChar = new_primitive_type("char")
+ BCharP = new_pointer_type(BChar)
+ BCharA = new_array_type(BCharP, None)
+ p1 = from_buffer(BCharA, b"foo", False)
+ assert p1 == from_buffer(BCharA, b"foo", False)
+ py.test.raises((TypeError, BufferError), from_buffer, BCharA, b"foo", True)
+ ba = bytearray(b"foo")
+ p1 = from_buffer(BCharA, ba, True)
+ p1[0] = b"g"
+ assert ba == b"goo"
+
+def test_from_buffer_types():
+ BInt = new_primitive_type("int")
+ BIntP = new_pointer_type(BInt)
+ BIntA = new_array_type(BIntP, None)
+ lst = [-12345678, 87654321, 489148]
+ bytestring = bytearray(buffer(newp(BIntA, lst))[:] + b'XYZ')
+ lst2 = lst + [42, -999999999]
+ bytestring2 = bytearray(buffer(newp(BIntA, lst2))[:] + b'XYZ')
+ #
+ p1 = from_buffer(BIntA, bytestring) # int[]
+ assert typeof(p1) is BIntA
+ assert len(p1) == 3
+ assert p1[0] == lst[0]
+ assert p1[1] == lst[1]
+ assert p1[2] == lst[2]
+ with pytest.raises(IndexError):
+ p1[3]
+ with pytest.raises(IndexError):
+ p1[-1]
+ #
+ py.test.raises(TypeError, from_buffer, BInt, bytestring)
+ #
+ p2 = from_buffer(BIntP, bytestring) # int *
+ assert p2 == p1 or 'PY_DOT_PY' in globals()
+ # note: on py.py ^^^, bytearray buffers are not emulated well enough
+ assert typeof(p2) is BIntP
+ assert p2[0] == lst[0]
+ assert p2[1] == lst[1]
+ assert p2[2] == lst[2]
+ # hopefully does not crash, but doesn't raise an exception:
+ p2[3]
+ p2[-1]
+ # not enough data even for one, but this is not enforced:
+ from_buffer(BIntP, b"")
+ #
+ BIntA2 = new_array_type(BIntP, 2)
+ p2 = from_buffer(BIntA2, bytestring) # int[2]
+ assert typeof(p2) is BIntA2
+ assert len(p2) == 2
+ assert p2[0] == lst[0]
+ assert p2[1] == lst[1]
+ with pytest.raises(IndexError):
+ p2[2]
+ with pytest.raises(IndexError):
+ p2[-1]
+ assert p2 == p1 or 'PY_DOT_PY' in globals()
+ #
+ BIntA4 = new_array_type(BIntP, 4) # int[4]: too big
+ py.test.raises(ValueError, from_buffer, BIntA4, bytestring)
+ #
+ BStruct = new_struct_type("foo")
+ complete_struct_or_union(BStruct, [('a1', BInt, -1),
+ ('a2', BInt, -1)])
+ BStructP = new_pointer_type(BStruct)
+ BStructA = new_array_type(BStructP, None)
+ p1 = from_buffer(BStructA, bytestring2) # struct[]
+ assert len(p1) == 2
+ assert typeof(p1) is BStructA
+ assert p1[0].a1 == lst2[0]
+ assert p1[0].a2 == lst2[1]
+ assert p1[1].a1 == lst2[2]
+ assert p1[1].a2 == lst2[3]
+ with pytest.raises(IndexError):
+ p1[2]
+ with pytest.raises(IndexError):
+ p1[-1]
+ assert repr(p1) == "<cdata 'foo[]' buffer len 2 from 'bytearray' object>"
+ #
+ p2 = from_buffer(BStructP, bytestring2) # 'struct *'
+ assert p2 == p1 or 'PY_DOT_PY' in globals()
+ assert typeof(p2) is BStructP
+ assert p2.a1 == lst2[0]
+ assert p2.a2 == lst2[1]
+ assert p2[0].a1 == lst2[0]
+ assert p2[0].a2 == lst2[1]
+ assert p2[1].a1 == lst2[2]
+ assert p2[1].a2 == lst2[3]
+ # does not crash:
+ p2[2]
+ p2[-1]
+ # not enough data even for one, but this is not enforced:
+ from_buffer(BStructP, b"")
+ from_buffer(BStructP, b"1234567")
+ #
+ release(p1)
+ assert repr(p1) == "<cdata 'foo[]' buffer RELEASED>"
+ #
+ BEmptyStruct = new_struct_type("empty")
+ complete_struct_or_union(BEmptyStruct, [], Ellipsis, 0)
+ assert sizeof(BEmptyStruct) == 0
+ BEmptyStructP = new_pointer_type(BEmptyStruct)
+ BEmptyStructA = new_array_type(BEmptyStructP, None)
+ py.test.raises(ZeroDivisionError, from_buffer, # empty[]
+ BEmptyStructA, bytestring)
+ #
+ BEmptyStructA5 = new_array_type(BEmptyStructP, 5)
+ p1 = from_buffer(BEmptyStructA5, bytestring) # struct empty[5]
+ assert typeof(p1) is BEmptyStructA5
+ assert len(p1) == 5
+ assert (cast(BIntP, p1) == from_buffer(BIntA, bytestring)
+ or 'PY_DOT_PY' in globals())
+ #
+ BVarStruct = new_struct_type("varfoo")
+ BVarStructP = new_pointer_type(BVarStruct)
+ complete_struct_or_union(BVarStruct, [('a1', BInt, -1),
+ ('va', BIntA, -1)])
+ with pytest.raises(TypeError):
+ from_buffer(BVarStruct, bytestring)
+ pv = from_buffer(BVarStructP, bytestring) # varfoo *
+ assert pv.a1 == lst[0]
+ assert pv.va[0] == lst[1]
+ assert pv.va[1] == lst[2]
+ assert sizeof(pv[0]) == 1 * size_of_int()
+ with pytest.raises(TypeError):
+ len(pv.va)
+ # hopefully does not crash, but doesn't raise an exception:
+ pv.va[2]
+ pv.va[-1]
+ # not enough data even for one, but this is not enforced:
+ from_buffer(BVarStructP, b"")
+ assert repr(pv) == "<cdata 'varfoo *' buffer from 'bytearray' object>"
+ assert repr(pv[0]).startswith("<cdata 'varfoo &' ")
+ #
+ release(pv)
+ assert repr(pv) == "<cdata 'varfoo *' buffer RELEASED>"
+ assert repr(pv[0]).startswith("<cdata 'varfoo &' ")
+ #
+ pv = from_buffer(BVarStructP, bytestring) # make a fresh one
+ with pytest.raises(ValueError):
+ release(pv[0])
+
+def test_issue483():
+ BInt = new_primitive_type("int")
+ BIntP = new_pointer_type(BInt)
+ BIntA = new_array_type(BIntP, None)
+ lst = list(range(25))
+ bytestring = bytearray(buffer(newp(BIntA, lst))[:] + b'XYZ')
+ p1 = from_buffer(BIntA, bytestring) # int[]
+ assert len(buffer(p1)) == 25 * size_of_int()
+ assert sizeof(p1) == 25 * size_of_int()
+ #
+ p2 = from_buffer(BIntP, bytestring)
+ assert sizeof(p2) == size_of_ptr()
+ assert len(buffer(p2)) == size_of_int() # first element only, by default
+
def test_memmove():
Short = new_primitive_type("short")
ShortA = new_array_type(new_pointer_type(Short), None)
@@ -4106,14 +4106,14 @@ def test_dereference_null_ptr():
BInt = new_primitive_type("int")
BIntPtr = new_pointer_type(BInt)
p = cast(BIntPtr, 0)
- with pytest.raises(RuntimeError):
- p[0]
- with pytest.raises(RuntimeError):
- p[0] = 42
- with pytest.raises(RuntimeError):
- p[42]
- with pytest.raises(RuntimeError):
- p[42] = -1
+ with pytest.raises(RuntimeError):
+ p[0]
+ with pytest.raises(RuntimeError):
+ p[0] = 42
+ with pytest.raises(RuntimeError):
+ p[42]
+ with pytest.raises(RuntimeError):
+ p[42] = -1
def test_mixup():
BStruct1 = new_struct_type("foo")
@@ -4129,12 +4129,12 @@ def test_mixup():
pp2 = newp(BStruct2PtrPtr)
pp3 = newp(BStruct3PtrPtr)
pp1[0] = pp1[0]
- with pytest.raises(TypeError) as e:
- pp3[0] = pp1[0]
+ with pytest.raises(TypeError) as e:
+ pp3[0] = pp1[0]
assert str(e.value).startswith("initializer for ctype 'bar *' must be a ")
assert str(e.value).endswith(", not cdata 'foo *'")
- with pytest.raises(TypeError) as e:
- pp2[0] = pp1[0]
+ with pytest.raises(TypeError) as e:
+ pp2[0] = pp1[0]
assert str(e.value) == ("initializer for ctype 'foo *' appears indeed to "
"be 'foo *', but the types are different (check "
"that you are not e.g. mixing up different ffi "
@@ -4166,12 +4166,12 @@ def test_unpack():
assert unpack(p, 10) == b"abc\x00def\x00\x00\x00"
assert unpack(p+1, 5) == b"bc\x00de"
- for typename in ["wchar_t", "char16_t", "char32_t"]:
- BWChar = new_primitive_type(typename)
- BArray = new_array_type(new_pointer_type(BWChar), 10) # wchar_t[10]
- p = newp(BArray, u"abc\x00def")
- assert unpack(p, 10) == u"abc\x00def\x00\x00\x00"
-
+ for typename in ["wchar_t", "char16_t", "char32_t"]:
+ BWChar = new_primitive_type(typename)
+ BArray = new_array_type(new_pointer_type(BWChar), 10) # wchar_t[10]
+ p = newp(BArray, u"abc\x00def")
+ assert unpack(p, 10) == u"abc\x00def\x00\x00\x00"
+
for typename, samples in [
("uint8_t", [0, 2**8-1]),
("uint16_t", [0, 2**16-1]),
@@ -4181,7 +4181,7 @@ def test_unpack():
("int16_t", [-2**15, 2**15-1]),
("int32_t", [-2**31, 2**31-1]),
("int64_t", [-2**63, 2**63-1]),
- ("_Bool", [False, True]),
+ ("_Bool", [False, True]),
("float", [0.0, 10.5]),
("double", [12.34, 56.78]),
]:
@@ -4192,7 +4192,7 @@ def test_unpack():
assert result == samples
for i in range(len(samples)):
assert result[i] == p[i] and type(result[i]) is type(p[i])
- assert (type(result[i]) is bool) == (type(samples[i]) is bool)
+ assert (type(result[i]) is bool) == (type(samples[i]) is bool)
#
BInt = new_primitive_type("int")
py.test.raises(TypeError, unpack, p)
@@ -4252,18 +4252,18 @@ def test_cdata_dir():
def test_char_pointer_conversion():
import warnings
- assert __version__.startswith("1."), (
- "the warning will be an error if we ever release cffi 2.x")
+ assert __version__.startswith("1."), (
+ "the warning will be an error if we ever release cffi 2.x")
BCharP = new_pointer_type(new_primitive_type("char"))
BIntP = new_pointer_type(new_primitive_type("int"))
BVoidP = new_pointer_type(new_void_type())
- BUCharP = new_pointer_type(new_primitive_type("unsigned char"))
+ BUCharP = new_pointer_type(new_primitive_type("unsigned char"))
z1 = cast(BCharP, 0)
z2 = cast(BIntP, 0)
z3 = cast(BVoidP, 0)
- z4 = cast(BUCharP, 0)
+ z4 = cast(BUCharP, 0)
with warnings.catch_warnings(record=True) as w:
- warnings.simplefilter("always")
+ warnings.simplefilter("always")
newp(new_pointer_type(BIntP), z1) # warn
assert len(w) == 1
newp(new_pointer_type(BVoidP), z1) # fine
@@ -4276,300 +4276,300 @@ def test_char_pointer_conversion():
assert len(w) == 2
newp(new_pointer_type(BIntP), z3) # fine
assert len(w) == 2
- newp(new_pointer_type(BCharP), z4) # fine (ignore signedness here)
- assert len(w) == 2
- newp(new_pointer_type(BUCharP), z1) # fine (ignore signedness here)
- assert len(w) == 2
- newp(new_pointer_type(BUCharP), z3) # fine
- assert len(w) == 2
+ newp(new_pointer_type(BCharP), z4) # fine (ignore signedness here)
+ assert len(w) == 2
+ newp(new_pointer_type(BUCharP), z1) # fine (ignore signedness here)
+ assert len(w) == 2
+ newp(new_pointer_type(BUCharP), z3) # fine
+ assert len(w) == 2
# check that the warnings are associated with lines in this file
assert w[1].lineno == w[0].lineno + 4
-
-def test_primitive_comparison():
- def assert_eq(a, b):
- assert (a == b) is True
- assert (b == a) is True
- assert (a != b) is False
- assert (b != a) is False
- assert (a < b) is False
- assert (a <= b) is True
- assert (a > b) is False
- assert (a >= b) is True
- assert (b < a) is False
- assert (b <= a) is True
- assert (b > a) is False
- assert (b >= a) is True
- assert hash(a) == hash(b)
- def assert_lt(a, b, check_hash=True):
- assert (a == b) is False
- assert (b == a) is False
- assert (a != b) is True
- assert (b != a) is True
- assert (a < b) is True
- assert (a <= b) is True
- assert (a > b) is False
- assert (a >= b) is False
- assert (b < a) is False
- assert (b <= a) is False
- assert (b > a) is True
- assert (b >= a) is True
- if check_hash:
- assert hash(a) != hash(b) # (or at least, it is unlikely)
- def assert_gt(a, b, check_hash=True):
- assert_lt(b, a, check_hash)
- def assert_ne(a, b):
- assert (a == b) is False
- assert (b == a) is False
- assert (a != b) is True
- assert (b != a) is True
- if strict_compare:
- with pytest.raises(TypeError): a < b
- with pytest.raises(TypeError): a <= b
- with pytest.raises(TypeError): a > b
- with pytest.raises(TypeError): a >= b
- with pytest.raises(TypeError): b < a
- with pytest.raises(TypeError): b <= a
- with pytest.raises(TypeError): b > a
- with pytest.raises(TypeError): b >= a
- elif a < b:
- assert_lt(a, b)
- else:
- assert_lt(b, a)
- assert_eq(5, 5)
- assert_lt(3, 5)
- assert_ne('5', 5)
- #
- t1 = new_primitive_type("char")
- t2 = new_primitive_type("int")
- t3 = new_primitive_type("unsigned char")
- t4 = new_primitive_type("unsigned int")
- t5 = new_primitive_type("float")
- t6 = new_primitive_type("double")
- assert_eq(cast(t1, 65), b'A')
- assert_lt(cast(t1, 64), b'\x99')
- assert_gt(cast(t1, 200), b'A')
- assert_ne(cast(t1, 65), 65)
- assert_eq(cast(t2, -25), -25)
- assert_lt(cast(t2, -25), -24)
- assert_gt(cast(t2, -25), -26)
- assert_eq(cast(t3, 65), 65)
- assert_ne(cast(t3, 65), b'A')
- assert_ne(cast(t3, 65), cast(t1, 65))
- assert_gt(cast(t4, -1), -1, check_hash=False)
- assert_gt(cast(t4, -1), cast(t2, -1), check_hash=False)
- assert_gt(cast(t4, -1), 99999)
- assert_eq(cast(t4, -1), 256 ** size_of_int() - 1)
- assert_eq(cast(t5, 3.0), 3)
- assert_eq(cast(t5, 3.5), 3.5)
- assert_lt(cast(t5, 3.3), 3.3) # imperfect rounding
- assert_eq(cast(t6, 3.3), 3.3)
- assert_eq(cast(t5, 3.5), cast(t6, 3.5))
- assert_lt(cast(t5, 3.1), cast(t6, 3.1)) # imperfect rounding
- assert_eq(cast(t5, 7.0), cast(t3, 7))
- assert_lt(cast(t5, 3.1), 3.101)
- assert_gt(cast(t5, 3.1), 3)
-
-def test_explicit_release_new():
- # release() on a ffi.new() object has no effect on CPython, but
- # really releases memory on PyPy. We can't test that effect
- # though, because a released cdata is not marked.
- BIntP = new_pointer_type(new_primitive_type("int"))
- p = newp(BIntP)
- p[0] = 42
- with pytest.raises(IndexError):
- p[1]
- release(p)
- # here, reading p[0] might give garbage or segfault...
- release(p) # no effect
- #
- BStruct = new_struct_type("struct foo")
- BStructP = new_pointer_type(BStruct)
- complete_struct_or_union(BStruct, [('p', BIntP, -1)])
- pstruct = newp(BStructP)
- assert pstruct.p == cast(BIntP, 0)
- release(pstruct)
- # here, reading pstruct.p might give garbage or segfault...
- release(pstruct) # no effect
-
-def test_explicit_release_new_contextmgr():
- BIntP = new_pointer_type(new_primitive_type("int"))
- with newp(BIntP) as p:
- p[0] = 42
- assert p[0] == 42
- # here, reading p[0] might give garbage or segfault...
- release(p) # no effect
-
-def test_explicit_release_badtype():
- BIntP = new_pointer_type(new_primitive_type("int"))
- p = cast(BIntP, 12345)
- py.test.raises(ValueError, release, p)
- py.test.raises(ValueError, release, p)
- BStruct = new_struct_type("struct foo")
- BStructP = new_pointer_type(BStruct)
- complete_struct_or_union(BStruct, [('p', BIntP, -1)])
- pstruct = newp(BStructP)
- py.test.raises(ValueError, release, pstruct[0])
-
-def test_explicit_release_badtype_contextmgr():
- BIntP = new_pointer_type(new_primitive_type("int"))
- p = cast(BIntP, 12345)
- with pytest.raises(ValueError):
- with p:
- pass
- with pytest.raises(ValueError):
- with p:
- pass
-
-def test_explicit_release_gc():
- BIntP = new_pointer_type(new_primitive_type("int"))
- seen = []
- intp1 = newp(BIntP, 12345)
- p1 = cast(BIntP, intp1)
- p = gcp(p1, seen.append)
- assert seen == []
- release(p)
- assert seen == [p1]
- assert p1[0] == 12345
- assert p[0] == 12345 # true so far, but might change to raise RuntimeError
- release(p) # no effect
-
-def test_explicit_release_gc_contextmgr():
- BIntP = new_pointer_type(new_primitive_type("int"))
- seen = []
- intp1 = newp(BIntP, 12345)
- p1 = cast(BIntP, intp1)
- p = gcp(p1, seen.append)
- with p:
- assert p[0] == 12345
- assert seen == []
- assert seen == [p1]
- assert p1[0] == 12345
- assert p[0] == 12345 # true so far, but might change to raise RuntimeError
- release(p) # no effect
-
-def test_explicit_release_from_buffer():
- a = bytearray(b"xyz")
- BChar = new_primitive_type("char")
- BCharP = new_pointer_type(BChar)
- BCharA = new_array_type(BCharP, None)
- p = from_buffer(BCharA, a)
- assert p[2] == b"z"
- assert repr(p) == "<cdata 'char[]' buffer len 3 from 'bytearray' object>"
- release(p)
- assert p[2] == b"z" # true so far, but might change to raise RuntimeError
- assert repr(p) == "<cdata 'char[]' buffer RELEASED>"
- release(p) # no effect
-
-def test_explicit_release_from_buffer_contextmgr():
- a = bytearray(b"xyz")
- BChar = new_primitive_type("char")
- BCharP = new_pointer_type(BChar)
- BCharA = new_array_type(BCharP, None)
- p = from_buffer(BCharA, a)
- with p:
- assert p[2] == b"z"
- assert p[2] == b"z" # true so far, but might change to raise RuntimeError
- assert repr(p) == "<cdata 'char[]' buffer RELEASED>"
- release(p) # no effect
-
-def test_explicit_release_bytearray_on_cpython():
- if '__pypy__' in sys.builtin_module_names:
- py.test.skip("pypy's bytearray are never locked")
- a = bytearray(b"xyz")
- BChar = new_primitive_type("char")
- BCharP = new_pointer_type(BChar)
- BCharA = new_array_type(BCharP, None)
- a += b't' * 10
- p = from_buffer(BCharA, a)
- with pytest.raises(BufferError):
- a += b'u' * 100
- release(p)
- a += b'v' * 100
- release(p) # no effect
- a += b'w' * 1000
- assert a == bytearray(b"xyz" + b't' * 10 + b'v' * 100 + b'w' * 1000)
-
-def test_int_doesnt_give_bool():
- BBool = new_primitive_type("_Bool")
- x = int(cast(BBool, 42))
- assert type(x) is int and x == 1
- x = long(cast(BBool, 42))
- assert type(x) is long and x == 1
- with pytest.raises(TypeError):
- float(cast(BBool, 42))
- with pytest.raises(TypeError):
- complex(cast(BBool, 42))
-
-def test_cannot_call_null_function_pointer():
- BInt = new_primitive_type("int")
- BFunc = new_function_type((BInt, BInt), BInt, False)
- f = cast(BFunc, 0)
- with pytest.raises(RuntimeError):
- f(40, 2)
-
-def test_huge_structure():
- BChar = new_primitive_type("char")
- BArray = new_array_type(new_pointer_type(BChar), sys.maxsize)
- assert sizeof(BArray) == sys.maxsize
- BStruct = new_struct_type("struct foo")
- complete_struct_or_union(BStruct, [('a1', BArray, -1)])
- assert sizeof(BStruct) == sys.maxsize
-
-def test_get_types():
- import _cffi_backend
- CData, CType = _get_types()
- assert CData is _cffi_backend._CDataBase
- assert CType is _cffi_backend.CType
-
-def test_type_available_with_correct_names():
- import _cffi_backend
- check_names = [
- 'CType',
- 'CField',
- 'CLibrary',
- '_CDataBase',
- 'FFI',
- 'Lib',
- 'buffer',
- ]
- if '__pypy__' in sys.builtin_module_names:
- check_names += [
- '__CData_iterator',
- '__FFIGlobSupport',
- '__FFIAllocator',
- '__FFIFunctionWrapper',
- ]
- else:
- check_names += [
- '__CDataOwn',
- '__CDataOwnGC',
- '__CDataFromBuf',
- '__CDataGCP',
- '__CData_iterator',
- '__FFIGlobSupport',
- ]
- for name in check_names:
- tp = getattr(_cffi_backend, name)
- assert isinstance(tp, type)
- assert (tp.__module__, tp.__name__) == ('_cffi_backend', name)
-
-def test_unaligned_types():
- BByteArray = new_array_type(
- new_pointer_type(new_primitive_type("unsigned char")), None)
- pbuf = newp(BByteArray, 40)
- buf = buffer(pbuf)
- #
- for name in ['short', 'int', 'long', 'long long', 'float', 'double',
- 'float _Complex', 'double _Complex']:
- p = new_primitive_type(name)
- if name.endswith(' _Complex'):
- num = cast(p, 1.23 - 4.56j)
- else:
- num = cast(p, 0x0123456789abcdef)
- size = sizeof(p)
- buf[0:40] = b"\x00" * 40
- pbuf1 = cast(new_pointer_type(p), pbuf + 1)
- pbuf1[0] = num
- assert pbuf1[0] == num
- assert buf[0] == b'\x00'
- assert buf[1 + size] == b'\x00'
+
+def test_primitive_comparison():
+ def assert_eq(a, b):
+ assert (a == b) is True
+ assert (b == a) is True
+ assert (a != b) is False
+ assert (b != a) is False
+ assert (a < b) is False
+ assert (a <= b) is True
+ assert (a > b) is False
+ assert (a >= b) is True
+ assert (b < a) is False
+ assert (b <= a) is True
+ assert (b > a) is False
+ assert (b >= a) is True
+ assert hash(a) == hash(b)
+ def assert_lt(a, b, check_hash=True):
+ assert (a == b) is False
+ assert (b == a) is False
+ assert (a != b) is True
+ assert (b != a) is True
+ assert (a < b) is True
+ assert (a <= b) is True
+ assert (a > b) is False
+ assert (a >= b) is False
+ assert (b < a) is False
+ assert (b <= a) is False
+ assert (b > a) is True
+ assert (b >= a) is True
+ if check_hash:
+ assert hash(a) != hash(b) # (or at least, it is unlikely)
+ def assert_gt(a, b, check_hash=True):
+ assert_lt(b, a, check_hash)
+ def assert_ne(a, b):
+ assert (a == b) is False
+ assert (b == a) is False
+ assert (a != b) is True
+ assert (b != a) is True
+ if strict_compare:
+ with pytest.raises(TypeError): a < b
+ with pytest.raises(TypeError): a <= b
+ with pytest.raises(TypeError): a > b
+ with pytest.raises(TypeError): a >= b
+ with pytest.raises(TypeError): b < a
+ with pytest.raises(TypeError): b <= a
+ with pytest.raises(TypeError): b > a
+ with pytest.raises(TypeError): b >= a
+ elif a < b:
+ assert_lt(a, b)
+ else:
+ assert_lt(b, a)
+ assert_eq(5, 5)
+ assert_lt(3, 5)
+ assert_ne('5', 5)
+ #
+ t1 = new_primitive_type("char")
+ t2 = new_primitive_type("int")
+ t3 = new_primitive_type("unsigned char")
+ t4 = new_primitive_type("unsigned int")
+ t5 = new_primitive_type("float")
+ t6 = new_primitive_type("double")
+ assert_eq(cast(t1, 65), b'A')
+ assert_lt(cast(t1, 64), b'\x99')
+ assert_gt(cast(t1, 200), b'A')
+ assert_ne(cast(t1, 65), 65)
+ assert_eq(cast(t2, -25), -25)
+ assert_lt(cast(t2, -25), -24)
+ assert_gt(cast(t2, -25), -26)
+ assert_eq(cast(t3, 65), 65)
+ assert_ne(cast(t3, 65), b'A')
+ assert_ne(cast(t3, 65), cast(t1, 65))
+ assert_gt(cast(t4, -1), -1, check_hash=False)
+ assert_gt(cast(t4, -1), cast(t2, -1), check_hash=False)
+ assert_gt(cast(t4, -1), 99999)
+ assert_eq(cast(t4, -1), 256 ** size_of_int() - 1)
+ assert_eq(cast(t5, 3.0), 3)
+ assert_eq(cast(t5, 3.5), 3.5)
+ assert_lt(cast(t5, 3.3), 3.3) # imperfect rounding
+ assert_eq(cast(t6, 3.3), 3.3)
+ assert_eq(cast(t5, 3.5), cast(t6, 3.5))
+ assert_lt(cast(t5, 3.1), cast(t6, 3.1)) # imperfect rounding
+ assert_eq(cast(t5, 7.0), cast(t3, 7))
+ assert_lt(cast(t5, 3.1), 3.101)
+ assert_gt(cast(t5, 3.1), 3)
+
+def test_explicit_release_new():
+ # release() on a ffi.new() object has no effect on CPython, but
+ # really releases memory on PyPy. We can't test that effect
+ # though, because a released cdata is not marked.
+ BIntP = new_pointer_type(new_primitive_type("int"))
+ p = newp(BIntP)
+ p[0] = 42
+ with pytest.raises(IndexError):
+ p[1]
+ release(p)
+ # here, reading p[0] might give garbage or segfault...
+ release(p) # no effect
+ #
+ BStruct = new_struct_type("struct foo")
+ BStructP = new_pointer_type(BStruct)
+ complete_struct_or_union(BStruct, [('p', BIntP, -1)])
+ pstruct = newp(BStructP)
+ assert pstruct.p == cast(BIntP, 0)
+ release(pstruct)
+ # here, reading pstruct.p might give garbage or segfault...
+ release(pstruct) # no effect
+
+def test_explicit_release_new_contextmgr():
+ BIntP = new_pointer_type(new_primitive_type("int"))
+ with newp(BIntP) as p:
+ p[0] = 42
+ assert p[0] == 42
+ # here, reading p[0] might give garbage or segfault...
+ release(p) # no effect
+
+def test_explicit_release_badtype():
+ BIntP = new_pointer_type(new_primitive_type("int"))
+ p = cast(BIntP, 12345)
+ py.test.raises(ValueError, release, p)
+ py.test.raises(ValueError, release, p)
+ BStruct = new_struct_type("struct foo")
+ BStructP = new_pointer_type(BStruct)
+ complete_struct_or_union(BStruct, [('p', BIntP, -1)])
+ pstruct = newp(BStructP)
+ py.test.raises(ValueError, release, pstruct[0])
+
+def test_explicit_release_badtype_contextmgr():
+ BIntP = new_pointer_type(new_primitive_type("int"))
+ p = cast(BIntP, 12345)
+ with pytest.raises(ValueError):
+ with p:
+ pass
+ with pytest.raises(ValueError):
+ with p:
+ pass
+
+def test_explicit_release_gc():
+ BIntP = new_pointer_type(new_primitive_type("int"))
+ seen = []
+ intp1 = newp(BIntP, 12345)
+ p1 = cast(BIntP, intp1)
+ p = gcp(p1, seen.append)
+ assert seen == []
+ release(p)
+ assert seen == [p1]
+ assert p1[0] == 12345
+ assert p[0] == 12345 # true so far, but might change to raise RuntimeError
+ release(p) # no effect
+
+def test_explicit_release_gc_contextmgr():
+ BIntP = new_pointer_type(new_primitive_type("int"))
+ seen = []
+ intp1 = newp(BIntP, 12345)
+ p1 = cast(BIntP, intp1)
+ p = gcp(p1, seen.append)
+ with p:
+ assert p[0] == 12345
+ assert seen == []
+ assert seen == [p1]
+ assert p1[0] == 12345
+ assert p[0] == 12345 # true so far, but might change to raise RuntimeError
+ release(p) # no effect
+
+def test_explicit_release_from_buffer():
+ a = bytearray(b"xyz")
+ BChar = new_primitive_type("char")
+ BCharP = new_pointer_type(BChar)
+ BCharA = new_array_type(BCharP, None)
+ p = from_buffer(BCharA, a)
+ assert p[2] == b"z"
+ assert repr(p) == "<cdata 'char[]' buffer len 3 from 'bytearray' object>"
+ release(p)
+ assert p[2] == b"z" # true so far, but might change to raise RuntimeError
+ assert repr(p) == "<cdata 'char[]' buffer RELEASED>"
+ release(p) # no effect
+
+def test_explicit_release_from_buffer_contextmgr():
+ a = bytearray(b"xyz")
+ BChar = new_primitive_type("char")
+ BCharP = new_pointer_type(BChar)
+ BCharA = new_array_type(BCharP, None)
+ p = from_buffer(BCharA, a)
+ with p:
+ assert p[2] == b"z"
+ assert p[2] == b"z" # true so far, but might change to raise RuntimeError
+ assert repr(p) == "<cdata 'char[]' buffer RELEASED>"
+ release(p) # no effect
+
+def test_explicit_release_bytearray_on_cpython():
+ if '__pypy__' in sys.builtin_module_names:
+ py.test.skip("pypy's bytearray are never locked")
+ a = bytearray(b"xyz")
+ BChar = new_primitive_type("char")
+ BCharP = new_pointer_type(BChar)
+ BCharA = new_array_type(BCharP, None)
+ a += b't' * 10
+ p = from_buffer(BCharA, a)
+ with pytest.raises(BufferError):
+ a += b'u' * 100
+ release(p)
+ a += b'v' * 100
+ release(p) # no effect
+ a += b'w' * 1000
+ assert a == bytearray(b"xyz" + b't' * 10 + b'v' * 100 + b'w' * 1000)
+
+def test_int_doesnt_give_bool():
+ BBool = new_primitive_type("_Bool")
+ x = int(cast(BBool, 42))
+ assert type(x) is int and x == 1
+ x = long(cast(BBool, 42))
+ assert type(x) is long and x == 1
+ with pytest.raises(TypeError):
+ float(cast(BBool, 42))
+ with pytest.raises(TypeError):
+ complex(cast(BBool, 42))
+
+def test_cannot_call_null_function_pointer():
+ BInt = new_primitive_type("int")
+ BFunc = new_function_type((BInt, BInt), BInt, False)
+ f = cast(BFunc, 0)
+ with pytest.raises(RuntimeError):
+ f(40, 2)
+
+def test_huge_structure():
+ BChar = new_primitive_type("char")
+ BArray = new_array_type(new_pointer_type(BChar), sys.maxsize)
+ assert sizeof(BArray) == sys.maxsize
+ BStruct = new_struct_type("struct foo")
+ complete_struct_or_union(BStruct, [('a1', BArray, -1)])
+ assert sizeof(BStruct) == sys.maxsize
+
+def test_get_types():
+ import _cffi_backend
+ CData, CType = _get_types()
+ assert CData is _cffi_backend._CDataBase
+ assert CType is _cffi_backend.CType
+
+def test_type_available_with_correct_names():
+ import _cffi_backend
+ check_names = [
+ 'CType',
+ 'CField',
+ 'CLibrary',
+ '_CDataBase',
+ 'FFI',
+ 'Lib',
+ 'buffer',
+ ]
+ if '__pypy__' in sys.builtin_module_names:
+ check_names += [
+ '__CData_iterator',
+ '__FFIGlobSupport',
+ '__FFIAllocator',
+ '__FFIFunctionWrapper',
+ ]
+ else:
+ check_names += [
+ '__CDataOwn',
+ '__CDataOwnGC',
+ '__CDataFromBuf',
+ '__CDataGCP',
+ '__CData_iterator',
+ '__FFIGlobSupport',
+ ]
+ for name in check_names:
+ tp = getattr(_cffi_backend, name)
+ assert isinstance(tp, type)
+ assert (tp.__module__, tp.__name__) == ('_cffi_backend', name)
+
+def test_unaligned_types():
+ BByteArray = new_array_type(
+ new_pointer_type(new_primitive_type("unsigned char")), None)
+ pbuf = newp(BByteArray, 40)
+ buf = buffer(pbuf)
+ #
+ for name in ['short', 'int', 'long', 'long long', 'float', 'double',
+ 'float _Complex', 'double _Complex']:
+ p = new_primitive_type(name)
+ if name.endswith(' _Complex'):
+ num = cast(p, 1.23 - 4.56j)
+ else:
+ num = cast(p, 0x0123456789abcdef)
+ size = sizeof(p)
+ buf[0:40] = b"\x00" * 40
+ pbuf1 = cast(new_pointer_type(p), pbuf + 1)
+ pbuf1[0] = num
+ assert pbuf1[0] == num
+ assert buf[0] == b'\x00'
+ assert buf[1 + size] == b'\x00'
diff --git a/contrib/python/cffi/c/wchar_helper.h b/contrib/python/cffi/c/wchar_helper.h
index 57c1b2a2bf..399b55130b 100644
--- a/contrib/python/cffi/c/wchar_helper.h
+++ b/contrib/python/cffi/c/wchar_helper.h
@@ -2,27 +2,27 @@
* wchar_t helpers
*/
-typedef uint16_t cffi_char16_t;
-typedef uint32_t cffi_char32_t;
+typedef uint16_t cffi_char16_t;
+typedef uint32_t cffi_char32_t;
-#if Py_UNICODE_SIZE == 2
+#if Py_UNICODE_SIZE == 2
/* Before Python 2.7, PyUnicode_FromWideChar is not able to convert
wchar_t values greater than 65535 into two-unicode-characters surrogates.
But even the Python 2.7 version doesn't detect wchar_t values that are
out of range(1114112), and just returns nonsense.
-
- From cffi 1.11 we can't use it anyway, because we need a version
- with char32_t input types.
+
+ From cffi 1.11 we can't use it anyway, because we need a version
+ with char32_t input types.
*/
static PyObject *
-_my_PyUnicode_FromChar32(const cffi_char32_t *w, Py_ssize_t size)
+_my_PyUnicode_FromChar32(const cffi_char32_t *w, Py_ssize_t size)
{
PyObject *unicode;
Py_ssize_t i;
Py_ssize_t alloc;
- const cffi_char32_t *orig_w;
+ const cffi_char32_t *orig_w;
alloc = size;
orig_w = w;
@@ -41,11 +41,11 @@ _my_PyUnicode_FromChar32(const cffi_char32_t *w, Py_ssize_t size)
Py_UNICODE *u;
u = PyUnicode_AS_UNICODE(unicode);
for (i = size; i > 0; i--) {
- if (*w > 0xFFFF) {
- cffi_char32_t ordinal;
- if (*w > 0x10FFFF) {
+ if (*w > 0xFFFF) {
+ cffi_char32_t ordinal;
+ if (*w > 0x10FFFF) {
PyErr_Format(PyExc_ValueError,
- "char32_t out of range for "
+ "char32_t out of range for "
"conversion to unicode: 0x%x", (int)*w);
Py_DECREF(unicode);
return NULL;
@@ -62,56 +62,56 @@ _my_PyUnicode_FromChar32(const cffi_char32_t *w, Py_ssize_t size)
return unicode;
}
-static PyObject *
-_my_PyUnicode_FromChar16(const cffi_char16_t *w, Py_ssize_t size)
-{
- return PyUnicode_FromUnicode((const Py_UNICODE *)w, size);
-}
-
-#else /* Py_UNICODE_SIZE == 4 */
-
-static PyObject *
-_my_PyUnicode_FromChar32(const cffi_char32_t *w, Py_ssize_t size)
-{
- return PyUnicode_FromUnicode((const Py_UNICODE *)w, size);
-}
-
-static PyObject *
-_my_PyUnicode_FromChar16(const cffi_char16_t *w, Py_ssize_t size)
-{
- /* 'size' is the length of the 'w' array */
- PyObject *result = PyUnicode_FromUnicode(NULL, size);
-
- if (result != NULL) {
- Py_UNICODE *u_base = PyUnicode_AS_UNICODE(result);
- Py_UNICODE *u = u_base;
-
- if (size == 1) { /* performance only */
- *u = (cffi_char32_t)*w;
- }
- else {
- while (size > 0) {
- cffi_char32_t ch = *w++;
- size--;
- if (0xD800 <= ch && ch <= 0xDBFF && size > 0) {
- cffi_char32_t ch2 = *w;
- if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {
- ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000;
- w++;
- size--;
- }
- }
- *u++ = ch;
- }
- if (PyUnicode_Resize(&result, u - u_base) < 0) {
- Py_DECREF(result);
- return NULL;
- }
- }
- }
- return result;
-}
-
+static PyObject *
+_my_PyUnicode_FromChar16(const cffi_char16_t *w, Py_ssize_t size)
+{
+ return PyUnicode_FromUnicode((const Py_UNICODE *)w, size);
+}
+
+#else /* Py_UNICODE_SIZE == 4 */
+
+static PyObject *
+_my_PyUnicode_FromChar32(const cffi_char32_t *w, Py_ssize_t size)
+{
+ return PyUnicode_FromUnicode((const Py_UNICODE *)w, size);
+}
+
+static PyObject *
+_my_PyUnicode_FromChar16(const cffi_char16_t *w, Py_ssize_t size)
+{
+ /* 'size' is the length of the 'w' array */
+ PyObject *result = PyUnicode_FromUnicode(NULL, size);
+
+ if (result != NULL) {
+ Py_UNICODE *u_base = PyUnicode_AS_UNICODE(result);
+ Py_UNICODE *u = u_base;
+
+ if (size == 1) { /* performance only */
+ *u = (cffi_char32_t)*w;
+ }
+ else {
+ while (size > 0) {
+ cffi_char32_t ch = *w++;
+ size--;
+ if (0xD800 <= ch && ch <= 0xDBFF && size > 0) {
+ cffi_char32_t ch2 = *w;
+ if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {
+ ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000;
+ w++;
+ size--;
+ }
+ }
+ *u++ = ch;
+ }
+ if (PyUnicode_Resize(&result, u - u_base) < 0) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ }
+ return result;
+}
+
#endif
@@ -120,73 +120,73 @@ _my_PyUnicode_FromChar16(const cffi_char16_t *w, Py_ssize_t size)
#define AS_SURROGATE(u) (0x10000 + (((u)[0] - 0xD800) << 10) + \
((u)[1] - 0xDC00))
-static int
-_my_PyUnicode_AsSingleChar16(PyObject *unicode, cffi_char16_t *result,
- char *err_got)
+static int
+_my_PyUnicode_AsSingleChar16(PyObject *unicode, cffi_char16_t *result,
+ char *err_got)
+{
+ Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode);
+ if (PyUnicode_GET_SIZE(unicode) != 1) {
+ sprintf(err_got, "unicode string of length %zd",
+ PyUnicode_GET_SIZE(unicode));
+ return -1;
+ }
+#if Py_UNICODE_SIZE == 4
+ if (((unsigned int)u[0]) > 0xFFFF)
+ {
+ sprintf(err_got, "larger-than-0xFFFF character");
+ return -1;
+ }
+#endif
+ *result = (cffi_char16_t)u[0];
+ return 0;
+}
+
+static int
+_my_PyUnicode_AsSingleChar32(PyObject *unicode, cffi_char32_t *result,
+ char *err_got)
{
Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode);
- if (PyUnicode_GET_SIZE(unicode) != 1) {
- sprintf(err_got, "unicode string of length %zd",
- PyUnicode_GET_SIZE(unicode));
- return -1;
- }
-#if Py_UNICODE_SIZE == 4
- if (((unsigned int)u[0]) > 0xFFFF)
- {
- sprintf(err_got, "larger-than-0xFFFF character");
- return -1;
- }
-#endif
- *result = (cffi_char16_t)u[0];
- return 0;
-}
-
-static int
-_my_PyUnicode_AsSingleChar32(PyObject *unicode, cffi_char32_t *result,
- char *err_got)
-{
- Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode);
if (PyUnicode_GET_SIZE(unicode) == 1) {
- *result = (cffi_char32_t)u[0];
+ *result = (cffi_char32_t)u[0];
return 0;
}
-#if Py_UNICODE_SIZE == 2
+#if Py_UNICODE_SIZE == 2
if (PyUnicode_GET_SIZE(unicode) == 2 && IS_SURROGATE(u)) {
*result = AS_SURROGATE(u);
return 0;
}
#endif
- sprintf(err_got, "unicode string of length %zd",
- PyUnicode_GET_SIZE(unicode));
+ sprintf(err_got, "unicode string of length %zd",
+ PyUnicode_GET_SIZE(unicode));
return -1;
}
-static Py_ssize_t _my_PyUnicode_SizeAsChar16(PyObject *unicode)
+static Py_ssize_t _my_PyUnicode_SizeAsChar16(PyObject *unicode)
+{
+ Py_ssize_t length = PyUnicode_GET_SIZE(unicode);
+ Py_ssize_t result = length;
+
+#if Py_UNICODE_SIZE == 4
+ Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode);
+ Py_ssize_t i;
+
+ for (i=0; i<length; i++) {
+ if (u[i] > 0xFFFF)
+ result++;
+ }
+#endif
+ return result;
+}
+
+static Py_ssize_t _my_PyUnicode_SizeAsChar32(PyObject *unicode)
{
Py_ssize_t length = PyUnicode_GET_SIZE(unicode);
Py_ssize_t result = length;
-#if Py_UNICODE_SIZE == 4
+#if Py_UNICODE_SIZE == 2
Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode);
Py_ssize_t i;
- for (i=0; i<length; i++) {
- if (u[i] > 0xFFFF)
- result++;
- }
-#endif
- return result;
-}
-
-static Py_ssize_t _my_PyUnicode_SizeAsChar32(PyObject *unicode)
-{
- Py_ssize_t length = PyUnicode_GET_SIZE(unicode);
- Py_ssize_t result = length;
-
-#if Py_UNICODE_SIZE == 2
- Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode);
- Py_ssize_t i;
-
for (i=0; i<length-1; i++) {
if (IS_SURROGATE(u+i))
result--;
@@ -195,45 +195,45 @@ static Py_ssize_t _my_PyUnicode_SizeAsChar32(PyObject *unicode)
return result;
}
-static int _my_PyUnicode_AsChar16(PyObject *unicode,
- cffi_char16_t *result,
- Py_ssize_t resultlen)
+static int _my_PyUnicode_AsChar16(PyObject *unicode,
+ cffi_char16_t *result,
+ Py_ssize_t resultlen)
+{
+ Py_ssize_t len = PyUnicode_GET_SIZE(unicode);
+ Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode);
+ Py_ssize_t i;
+ for (i=0; i<len; i++) {
+#if Py_UNICODE_SIZE == 2
+ cffi_char16_t ordinal = u[i];
+#else
+ cffi_char32_t ordinal = u[i];
+ if (ordinal > 0xFFFF) {
+ if (ordinal > 0x10FFFF) {
+ PyErr_Format(PyExc_ValueError,
+ "unicode character out of range for "
+ "conversion to char16_t: 0x%x", (int)ordinal);
+ return -1;
+ }
+ ordinal -= 0x10000;
+ *result++ = 0xD800 | (ordinal >> 10);
+ *result++ = 0xDC00 | (ordinal & 0x3FF);
+ continue;
+ }
+#endif
+ *result++ = ordinal;
+ }
+ return 0;
+}
+
+static int _my_PyUnicode_AsChar32(PyObject *unicode,
+ cffi_char32_t *result,
+ Py_ssize_t resultlen)
{
- Py_ssize_t len = PyUnicode_GET_SIZE(unicode);
Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode);
Py_ssize_t i;
- for (i=0; i<len; i++) {
-#if Py_UNICODE_SIZE == 2
- cffi_char16_t ordinal = u[i];
-#else
- cffi_char32_t ordinal = u[i];
- if (ordinal > 0xFFFF) {
- if (ordinal > 0x10FFFF) {
- PyErr_Format(PyExc_ValueError,
- "unicode character out of range for "
- "conversion to char16_t: 0x%x", (int)ordinal);
- return -1;
- }
- ordinal -= 0x10000;
- *result++ = 0xD800 | (ordinal >> 10);
- *result++ = 0xDC00 | (ordinal & 0x3FF);
- continue;
- }
-#endif
- *result++ = ordinal;
- }
- return 0;
-}
-
-static int _my_PyUnicode_AsChar32(PyObject *unicode,
- cffi_char32_t *result,
- Py_ssize_t resultlen)
-{
- Py_UNICODE *u = PyUnicode_AS_UNICODE(unicode);
- Py_ssize_t i;
for (i=0; i<resultlen; i++) {
- cffi_char32_t ordinal = *u;
-#if Py_UNICODE_SIZE == 2
+ cffi_char32_t ordinal = *u;
+#if Py_UNICODE_SIZE == 2
if (IS_SURROGATE(u)) {
ordinal = AS_SURROGATE(u);
u++;
@@ -242,5 +242,5 @@ static int _my_PyUnicode_AsChar32(PyObject *unicode,
result[i] = ordinal;
u++;
}
- return 0;
+ return 0;
}
diff --git a/contrib/python/cffi/c/wchar_helper_3.h b/contrib/python/cffi/c/wchar_helper_3.h
index cfcfeede29..f15464ef60 100644
--- a/contrib/python/cffi/c/wchar_helper_3.h
+++ b/contrib/python/cffi/c/wchar_helper_3.h
@@ -1,149 +1,149 @@
-/*
- * wchar_t helpers, version CPython >= 3.3.
- *
- * CPython 3.3 added support for sys.maxunicode == 0x10FFFF on all
- * platforms, even ones with wchar_t limited to 2 bytes. As such,
- * this code here works from the outside like wchar_helper.h in the
- * case Py_UNICODE_SIZE == 4, but the implementation is very different.
- */
-
-typedef uint16_t cffi_char16_t;
-typedef uint32_t cffi_char32_t;
-
-
-static PyObject *
-_my_PyUnicode_FromChar32(const cffi_char32_t *w, Py_ssize_t size)
-{
- return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, w, size);
-}
-
-static PyObject *
-_my_PyUnicode_FromChar16(const cffi_char16_t *w, Py_ssize_t size)
-{
- /* are there any surrogate pairs, and if so, how many? */
- Py_ssize_t i, count_surrogates = 0;
- for (i = 0; i < size - 1; i++) {
- if (0xD800 <= w[i] && w[i] <= 0xDBFF &&
- 0xDC00 <= w[i+1] && w[i+1] <= 0xDFFF)
- count_surrogates++;
- }
- if (count_surrogates == 0) {
- /* no, fast path */
- return PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, w, size);
- }
- else
- {
- PyObject *result = PyUnicode_New(size - count_surrogates, 0x10FFFF);
- Py_UCS4 *data;
- assert(PyUnicode_KIND(result) == PyUnicode_4BYTE_KIND);
- data = PyUnicode_4BYTE_DATA(result);
-
- for (i = 0; i < size; i++)
- {
- cffi_char32_t ch = w[i];
- if (0xD800 <= ch && ch <= 0xDBFF && i < size - 1) {
- cffi_char32_t ch2 = w[i + 1];
- if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {
- ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000;
- i++;
- }
- }
- *data++ = ch;
- }
- return result;
- }
-}
-
-static int
-_my_PyUnicode_AsSingleChar16(PyObject *unicode, cffi_char16_t *result,
- char *err_got)
-{
- cffi_char32_t ch;
- if (PyUnicode_GET_LENGTH(unicode) != 1) {
- sprintf(err_got, "unicode string of length %zd",
- PyUnicode_GET_LENGTH(unicode));
- return -1;
- }
- ch = PyUnicode_READ_CHAR(unicode, 0);
-
- if (ch > 0xFFFF)
- {
- sprintf(err_got, "larger-than-0xFFFF character");
- return -1;
- }
- *result = (cffi_char16_t)ch;
- return 0;
-}
-
-static int
-_my_PyUnicode_AsSingleChar32(PyObject *unicode, cffi_char32_t *result,
- char *err_got)
-{
- if (PyUnicode_GET_LENGTH(unicode) != 1) {
- sprintf(err_got, "unicode string of length %zd",
- PyUnicode_GET_LENGTH(unicode));
- return -1;
- }
- *result = PyUnicode_READ_CHAR(unicode, 0);
- return 0;
-}
-
-static Py_ssize_t _my_PyUnicode_SizeAsChar16(PyObject *unicode)
-{
- Py_ssize_t length = PyUnicode_GET_LENGTH(unicode);
- Py_ssize_t result = length;
- unsigned int kind = PyUnicode_KIND(unicode);
-
- if (kind == PyUnicode_4BYTE_KIND)
- {
- Py_UCS4 *data = PyUnicode_4BYTE_DATA(unicode);
- Py_ssize_t i;
- for (i = 0; i < length; i++) {
- if (data[i] > 0xFFFF)
- result++;
- }
- }
- return result;
-}
-
-static Py_ssize_t _my_PyUnicode_SizeAsChar32(PyObject *unicode)
-{
- return PyUnicode_GET_LENGTH(unicode);
-}
-
-static int _my_PyUnicode_AsChar16(PyObject *unicode,
- cffi_char16_t *result,
- Py_ssize_t resultlen)
-{
- Py_ssize_t len = PyUnicode_GET_LENGTH(unicode);
- unsigned int kind = PyUnicode_KIND(unicode);
- void *data = PyUnicode_DATA(unicode);
- Py_ssize_t i;
-
- for (i = 0; i < len; i++) {
- cffi_char32_t ordinal = PyUnicode_READ(kind, data, i);
- if (ordinal > 0xFFFF) {
- if (ordinal > 0x10FFFF) {
- PyErr_Format(PyExc_ValueError,
- "unicode character out of range for "
- "conversion to char16_t: 0x%x", (int)ordinal);
- return -1;
- }
- ordinal -= 0x10000;
- *result++ = 0xD800 | (ordinal >> 10);
- *result++ = 0xDC00 | (ordinal & 0x3FF);
- }
- else
- *result++ = ordinal;
- }
- return 0;
-}
-
-static int _my_PyUnicode_AsChar32(PyObject *unicode,
- cffi_char32_t *result,
- Py_ssize_t resultlen)
-{
- if (PyUnicode_AsUCS4(unicode, (Py_UCS4 *)result, resultlen, 0) == NULL)
- return -1;
- return 0;
-}
+/*
+ * wchar_t helpers, version CPython >= 3.3.
+ *
+ * CPython 3.3 added support for sys.maxunicode == 0x10FFFF on all
+ * platforms, even ones with wchar_t limited to 2 bytes. As such,
+ * this code here works from the outside like wchar_helper.h in the
+ * case Py_UNICODE_SIZE == 4, but the implementation is very different.
+ */
+
+typedef uint16_t cffi_char16_t;
+typedef uint32_t cffi_char32_t;
+
+
+static PyObject *
+_my_PyUnicode_FromChar32(const cffi_char32_t *w, Py_ssize_t size)
+{
+ return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, w, size);
+}
+
+static PyObject *
+_my_PyUnicode_FromChar16(const cffi_char16_t *w, Py_ssize_t size)
+{
+ /* are there any surrogate pairs, and if so, how many? */
+ Py_ssize_t i, count_surrogates = 0;
+ for (i = 0; i < size - 1; i++) {
+ if (0xD800 <= w[i] && w[i] <= 0xDBFF &&
+ 0xDC00 <= w[i+1] && w[i+1] <= 0xDFFF)
+ count_surrogates++;
+ }
+ if (count_surrogates == 0) {
+ /* no, fast path */
+ return PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, w, size);
+ }
+ else
+ {
+ PyObject *result = PyUnicode_New(size - count_surrogates, 0x10FFFF);
+ Py_UCS4 *data;
+ assert(PyUnicode_KIND(result) == PyUnicode_4BYTE_KIND);
+ data = PyUnicode_4BYTE_DATA(result);
+
+ for (i = 0; i < size; i++)
+ {
+ cffi_char32_t ch = w[i];
+ if (0xD800 <= ch && ch <= 0xDBFF && i < size - 1) {
+ cffi_char32_t ch2 = w[i + 1];
+ if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {
+ ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000;
+ i++;
+ }
+ }
+ *data++ = ch;
+ }
+ return result;
+ }
+}
+
+static int
+_my_PyUnicode_AsSingleChar16(PyObject *unicode, cffi_char16_t *result,
+ char *err_got)
+{
+ cffi_char32_t ch;
+ if (PyUnicode_GET_LENGTH(unicode) != 1) {
+ sprintf(err_got, "unicode string of length %zd",
+ PyUnicode_GET_LENGTH(unicode));
+ return -1;
+ }
+ ch = PyUnicode_READ_CHAR(unicode, 0);
+
+ if (ch > 0xFFFF)
+ {
+ sprintf(err_got, "larger-than-0xFFFF character");
+ return -1;
+ }
+ *result = (cffi_char16_t)ch;
+ return 0;
+}
+
+static int
+_my_PyUnicode_AsSingleChar32(PyObject *unicode, cffi_char32_t *result,
+ char *err_got)
+{
+ if (PyUnicode_GET_LENGTH(unicode) != 1) {
+ sprintf(err_got, "unicode string of length %zd",
+ PyUnicode_GET_LENGTH(unicode));
+ return -1;
+ }
+ *result = PyUnicode_READ_CHAR(unicode, 0);
+ return 0;
+}
+
+static Py_ssize_t _my_PyUnicode_SizeAsChar16(PyObject *unicode)
+{
+ Py_ssize_t length = PyUnicode_GET_LENGTH(unicode);
+ Py_ssize_t result = length;
+ unsigned int kind = PyUnicode_KIND(unicode);
+
+ if (kind == PyUnicode_4BYTE_KIND)
+ {
+ Py_UCS4 *data = PyUnicode_4BYTE_DATA(unicode);
+ Py_ssize_t i;
+ for (i = 0; i < length; i++) {
+ if (data[i] > 0xFFFF)
+ result++;
+ }
+ }
+ return result;
+}
+
+static Py_ssize_t _my_PyUnicode_SizeAsChar32(PyObject *unicode)
+{
+ return PyUnicode_GET_LENGTH(unicode);
+}
+
+static int _my_PyUnicode_AsChar16(PyObject *unicode,
+ cffi_char16_t *result,
+ Py_ssize_t resultlen)
+{
+ Py_ssize_t len = PyUnicode_GET_LENGTH(unicode);
+ unsigned int kind = PyUnicode_KIND(unicode);
+ void *data = PyUnicode_DATA(unicode);
+ Py_ssize_t i;
+
+ for (i = 0; i < len; i++) {
+ cffi_char32_t ordinal = PyUnicode_READ(kind, data, i);
+ if (ordinal > 0xFFFF) {
+ if (ordinal > 0x10FFFF) {
+ PyErr_Format(PyExc_ValueError,
+ "unicode character out of range for "
+ "conversion to char16_t: 0x%x", (int)ordinal);
+ return -1;
+ }
+ ordinal -= 0x10000;
+ *result++ = 0xD800 | (ordinal >> 10);
+ *result++ = 0xDC00 | (ordinal & 0x3FF);
+ }
+ else
+ *result++ = ordinal;
+ }
+ return 0;
+}
+
+static int _my_PyUnicode_AsChar32(PyObject *unicode,
+ cffi_char32_t *result,
+ Py_ssize_t resultlen)
+{
+ if (PyUnicode_AsUCS4(unicode, (Py_UCS4 *)result, resultlen, 0) == NULL)
+ return -1;
+ return 0;
+}
diff --git a/contrib/python/cffi/cffi/__init__.py b/contrib/python/cffi/cffi/__init__.py
index 0a9aa3bd58..82a961832c 100644
--- a/contrib/python/cffi/cffi/__init__.py
+++ b/contrib/python/cffi/cffi/__init__.py
@@ -1,12 +1,12 @@
__all__ = ['FFI', 'VerificationError', 'VerificationMissing', 'CDefError',
'FFIError']
-from .api import FFI
-from .error import CDefError, FFIError, VerificationError, VerificationMissing
-from .error import PkgConfigError
+from .api import FFI
+from .error import CDefError, FFIError, VerificationError, VerificationMissing
+from .error import PkgConfigError
-__version__ = "1.15.0"
-__version_info__ = (1, 15, 0)
+__version__ = "1.15.0"
+__version_info__ = (1, 15, 0)
# The verifier module file names are based on the CRC32 of a string that
# contains the following version number. It may be older than __version__
diff --git a/contrib/python/cffi/cffi/_cffi_errors.h b/contrib/python/cffi/cffi/_cffi_errors.h
index 7093c19034..158e059034 100644
--- a/contrib/python/cffi/cffi/_cffi_errors.h
+++ b/contrib/python/cffi/cffi/_cffi_errors.h
@@ -1,149 +1,149 @@
-#ifndef CFFI_MESSAGEBOX
-# ifdef _MSC_VER
-# define CFFI_MESSAGEBOX 1
-# else
-# define CFFI_MESSAGEBOX 0
-# endif
-#endif
-
-
-#if CFFI_MESSAGEBOX
-/* Windows only: logic to take the Python-CFFI embedding logic
- initialization errors and display them in a background thread
- with MessageBox. The idea is that if the whole program closes
- as a result of this problem, then likely it is already a console
- program and you can read the stderr output in the console too.
- If it is not a console program, then it will likely show its own
- dialog to complain, or generally not abruptly close, and for this
- case the background thread should stay alive.
-*/
-static void *volatile _cffi_bootstrap_text;
-
-static PyObject *_cffi_start_error_capture(void)
-{
- PyObject *result = NULL;
- PyObject *x, *m, *bi;
-
- if (InterlockedCompareExchangePointer(&_cffi_bootstrap_text,
- (void *)1, NULL) != NULL)
- return (PyObject *)1;
-
- m = PyImport_AddModule("_cffi_error_capture");
- if (m == NULL)
- goto error;
-
- result = PyModule_GetDict(m);
- if (result == NULL)
- goto error;
-
-#if PY_MAJOR_VERSION >= 3
- bi = PyImport_ImportModule("builtins");
-#else
- bi = PyImport_ImportModule("__builtin__");
-#endif
- if (bi == NULL)
- goto error;
- PyDict_SetItemString(result, "__builtins__", bi);
- Py_DECREF(bi);
-
- x = PyRun_String(
- "import sys\n"
- "class FileLike:\n"
- " def write(self, x):\n"
- " try:\n"
- " of.write(x)\n"
- " except: pass\n"
- " self.buf += x\n"
- " def flush(self):\n"
- " pass\n"
- "fl = FileLike()\n"
- "fl.buf = ''\n"
- "of = sys.stderr\n"
- "sys.stderr = fl\n"
- "def done():\n"
- " sys.stderr = of\n"
- " return fl.buf\n", /* make sure the returned value stays alive */
- Py_file_input,
- result, result);
- Py_XDECREF(x);
-
- error:
- if (PyErr_Occurred())
- {
- PyErr_WriteUnraisable(Py_None);
- PyErr_Clear();
- }
- return result;
-}
-
-#pragma comment(lib, "user32.lib")
-
-static DWORD WINAPI _cffi_bootstrap_dialog(LPVOID ignored)
-{
- Sleep(666); /* may be interrupted if the whole process is closing */
-#if PY_MAJOR_VERSION >= 3
- MessageBoxW(NULL, (wchar_t *)_cffi_bootstrap_text,
- L"Python-CFFI error",
- MB_OK | MB_ICONERROR);
-#else
- MessageBoxA(NULL, (char *)_cffi_bootstrap_text,
- "Python-CFFI error",
- MB_OK | MB_ICONERROR);
-#endif
- _cffi_bootstrap_text = NULL;
- return 0;
-}
-
-static void _cffi_stop_error_capture(PyObject *ecap)
-{
- PyObject *s;
- void *text;
-
- if (ecap == (PyObject *)1)
- return;
-
- if (ecap == NULL)
- goto error;
-
- s = PyRun_String("done()", Py_eval_input, ecap, ecap);
- if (s == NULL)
- goto error;
-
- /* Show a dialog box, but in a background thread, and
- never show multiple dialog boxes at once. */
-#if PY_MAJOR_VERSION >= 3
- text = PyUnicode_AsWideCharString(s, NULL);
-#else
- text = PyString_AsString(s);
-#endif
-
- _cffi_bootstrap_text = text;
-
- if (text != NULL)
- {
- HANDLE h;
- h = CreateThread(NULL, 0, _cffi_bootstrap_dialog,
- NULL, 0, NULL);
- if (h != NULL)
- CloseHandle(h);
- }
- /* decref the string, but it should stay alive as 'fl.buf'
- in the small module above. It will really be freed only if
- we later get another similar error. So it's a leak of at
- most one copy of the small module. That's fine for this
- situation which is usually a "fatal error" anyway. */
- Py_DECREF(s);
- PyErr_Clear();
- return;
-
- error:
- _cffi_bootstrap_text = NULL;
- PyErr_Clear();
-}
-
-#else
-
-static PyObject *_cffi_start_error_capture(void) { return NULL; }
-static void _cffi_stop_error_capture(PyObject *ecap) { }
-
-#endif
+#ifndef CFFI_MESSAGEBOX
+# ifdef _MSC_VER
+# define CFFI_MESSAGEBOX 1
+# else
+# define CFFI_MESSAGEBOX 0
+# endif
+#endif
+
+
+#if CFFI_MESSAGEBOX
+/* Windows only: logic to take the Python-CFFI embedding logic
+ initialization errors and display them in a background thread
+ with MessageBox. The idea is that if the whole program closes
+ as a result of this problem, then likely it is already a console
+ program and you can read the stderr output in the console too.
+ If it is not a console program, then it will likely show its own
+ dialog to complain, or generally not abruptly close, and for this
+ case the background thread should stay alive.
+*/
+static void *volatile _cffi_bootstrap_text;
+
+static PyObject *_cffi_start_error_capture(void)
+{
+ PyObject *result = NULL;
+ PyObject *x, *m, *bi;
+
+ if (InterlockedCompareExchangePointer(&_cffi_bootstrap_text,
+ (void *)1, NULL) != NULL)
+ return (PyObject *)1;
+
+ m = PyImport_AddModule("_cffi_error_capture");
+ if (m == NULL)
+ goto error;
+
+ result = PyModule_GetDict(m);
+ if (result == NULL)
+ goto error;
+
+#if PY_MAJOR_VERSION >= 3
+ bi = PyImport_ImportModule("builtins");
+#else
+ bi = PyImport_ImportModule("__builtin__");
+#endif
+ if (bi == NULL)
+ goto error;
+ PyDict_SetItemString(result, "__builtins__", bi);
+ Py_DECREF(bi);
+
+ x = PyRun_String(
+ "import sys\n"
+ "class FileLike:\n"
+ " def write(self, x):\n"
+ " try:\n"
+ " of.write(x)\n"
+ " except: pass\n"
+ " self.buf += x\n"
+ " def flush(self):\n"
+ " pass\n"
+ "fl = FileLike()\n"
+ "fl.buf = ''\n"
+ "of = sys.stderr\n"
+ "sys.stderr = fl\n"
+ "def done():\n"
+ " sys.stderr = of\n"
+ " return fl.buf\n", /* make sure the returned value stays alive */
+ Py_file_input,
+ result, result);
+ Py_XDECREF(x);
+
+ error:
+ if (PyErr_Occurred())
+ {
+ PyErr_WriteUnraisable(Py_None);
+ PyErr_Clear();
+ }
+ return result;
+}
+
+#pragma comment(lib, "user32.lib")
+
+static DWORD WINAPI _cffi_bootstrap_dialog(LPVOID ignored)
+{
+ Sleep(666); /* may be interrupted if the whole process is closing */
+#if PY_MAJOR_VERSION >= 3
+ MessageBoxW(NULL, (wchar_t *)_cffi_bootstrap_text,
+ L"Python-CFFI error",
+ MB_OK | MB_ICONERROR);
+#else
+ MessageBoxA(NULL, (char *)_cffi_bootstrap_text,
+ "Python-CFFI error",
+ MB_OK | MB_ICONERROR);
+#endif
+ _cffi_bootstrap_text = NULL;
+ return 0;
+}
+
+static void _cffi_stop_error_capture(PyObject *ecap)
+{
+ PyObject *s;
+ void *text;
+
+ if (ecap == (PyObject *)1)
+ return;
+
+ if (ecap == NULL)
+ goto error;
+
+ s = PyRun_String("done()", Py_eval_input, ecap, ecap);
+ if (s == NULL)
+ goto error;
+
+ /* Show a dialog box, but in a background thread, and
+ never show multiple dialog boxes at once. */
+#if PY_MAJOR_VERSION >= 3
+ text = PyUnicode_AsWideCharString(s, NULL);
+#else
+ text = PyString_AsString(s);
+#endif
+
+ _cffi_bootstrap_text = text;
+
+ if (text != NULL)
+ {
+ HANDLE h;
+ h = CreateThread(NULL, 0, _cffi_bootstrap_dialog,
+ NULL, 0, NULL);
+ if (h != NULL)
+ CloseHandle(h);
+ }
+ /* decref the string, but it should stay alive as 'fl.buf'
+ in the small module above. It will really be freed only if
+ we later get another similar error. So it's a leak of at
+ most one copy of the small module. That's fine for this
+ situation which is usually a "fatal error" anyway. */
+ Py_DECREF(s);
+ PyErr_Clear();
+ return;
+
+ error:
+ _cffi_bootstrap_text = NULL;
+ PyErr_Clear();
+}
+
+#else
+
+static PyObject *_cffi_start_error_capture(void) { return NULL; }
+static void _cffi_stop_error_capture(PyObject *ecap) { }
+
+#endif
diff --git a/contrib/python/cffi/cffi/_cffi_include.h b/contrib/python/cffi/cffi/_cffi_include.h
index bca47b5402..e4c0a67240 100644
--- a/contrib/python/cffi/cffi/_cffi_include.h
+++ b/contrib/python/cffi/cffi/_cffi_include.h
@@ -7,50 +7,50 @@
we can learn about Py_DEBUG from pyconfig.h, but it is unclear if
the same works for the other two macros. Py_DEBUG implies them,
but not the other way around.
-
- The implementation is messy (issue #350): on Windows, with _MSC_VER,
- we have to define Py_LIMITED_API even before including pyconfig.h.
- In that case, we guess what pyconfig.h will do to the macros above,
- and check our guess after the #include.
-
- Note that on Windows, with CPython 3.x, you need >= 3.5 and virtualenv
- version >= 16.0.0. With older versions of either, you don't get a
- copy of PYTHON3.DLL in the virtualenv. We can't check the version of
- CPython *before* we even include pyconfig.h. ffi.set_source() puts
- a ``#define _CFFI_NO_LIMITED_API'' at the start of this file if it is
- running on Windows < 3.5, as an attempt at fixing it, but that's
- arguably wrong because it may not be the target version of Python.
- Still better than nothing I guess. As another workaround, you can
- remove the definition of Py_LIMITED_API here.
-
- See also 'py_limited_api' in cffi/setuptools_ext.py.
+
+ The implementation is messy (issue #350): on Windows, with _MSC_VER,
+ we have to define Py_LIMITED_API even before including pyconfig.h.
+ In that case, we guess what pyconfig.h will do to the macros above,
+ and check our guess after the #include.
+
+ Note that on Windows, with CPython 3.x, you need >= 3.5 and virtualenv
+ version >= 16.0.0. With older versions of either, you don't get a
+ copy of PYTHON3.DLL in the virtualenv. We can't check the version of
+ CPython *before* we even include pyconfig.h. ffi.set_source() puts
+ a ``#define _CFFI_NO_LIMITED_API'' at the start of this file if it is
+ running on Windows < 3.5, as an attempt at fixing it, but that's
+ arguably wrong because it may not be the target version of Python.
+ Still better than nothing I guess. As another workaround, you can
+ remove the definition of Py_LIMITED_API here.
+
+ See also 'py_limited_api' in cffi/setuptools_ext.py.
*/
-#if !defined(_CFFI_USE_EMBEDDING) && !defined(Py_LIMITED_API)
-# ifdef _MSC_VER
-# if !defined(_DEBUG) && !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) && !defined(_CFFI_NO_LIMITED_API)
-# define Py_LIMITED_API
-# endif
-# include <pyconfig.h>
- /* sanity-check: Py_LIMITED_API will cause crashes if any of these
- are also defined. Normally, the Python file PC/pyconfig.h does not
- cause any of these to be defined, with the exception that _DEBUG
- causes Py_DEBUG. Double-check that. */
-# ifdef Py_LIMITED_API
-# if defined(Py_DEBUG)
-# error "pyconfig.h unexpectedly defines Py_DEBUG, but Py_LIMITED_API is set"
-# endif
-# if defined(Py_TRACE_REFS)
-# error "pyconfig.h unexpectedly defines Py_TRACE_REFS, but Py_LIMITED_API is set"
-# endif
-# if defined(Py_REF_DEBUG)
-# error "pyconfig.h unexpectedly defines Py_REF_DEBUG, but Py_LIMITED_API is set"
-# endif
-# endif
-# else
-# include <pyconfig.h>
-# if !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) && !defined(_CFFI_NO_LIMITED_API)
-# define Py_LIMITED_API
-# endif
+#if !defined(_CFFI_USE_EMBEDDING) && !defined(Py_LIMITED_API)
+# ifdef _MSC_VER
+# if !defined(_DEBUG) && !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) && !defined(_CFFI_NO_LIMITED_API)
+# define Py_LIMITED_API
+# endif
+# include <pyconfig.h>
+ /* sanity-check: Py_LIMITED_API will cause crashes if any of these
+ are also defined. Normally, the Python file PC/pyconfig.h does not
+ cause any of these to be defined, with the exception that _DEBUG
+ causes Py_DEBUG. Double-check that. */
+# ifdef Py_LIMITED_API
+# if defined(Py_DEBUG)
+# error "pyconfig.h unexpectedly defines Py_DEBUG, but Py_LIMITED_API is set"
+# endif
+# if defined(Py_TRACE_REFS)
+# error "pyconfig.h unexpectedly defines Py_TRACE_REFS, but Py_LIMITED_API is set"
+# endif
+# if defined(Py_REF_DEBUG)
+# error "pyconfig.h unexpectedly defines Py_REF_DEBUG, but Py_LIMITED_API is set"
+# endif
+# endif
+# else
+# include <pyconfig.h>
+# if !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) && !defined(_CFFI_NO_LIMITED_API)
+# define Py_LIMITED_API
+# endif
# endif
#endif
@@ -134,7 +134,7 @@ extern "C" {
#define _cffi_from_c_ulong PyLong_FromUnsignedLong
#define _cffi_from_c_longlong PyLong_FromLongLong
#define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
-#define _cffi_from_c__Bool PyBool_FromLong
+#define _cffi_from_c__Bool PyBool_FromLong
#define _cffi_to_c_double PyFloat_AsDouble
#define _cffi_to_c_float PyFloat_AsDouble
@@ -199,9 +199,9 @@ extern "C" {
#define _cffi_from_c_struct \
((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[18])
#define _cffi_to_c_wchar_t \
- ((_cffi_wchar_t(*)(PyObject *))_cffi_exports[19])
+ ((_cffi_wchar_t(*)(PyObject *))_cffi_exports[19])
#define _cffi_from_c_wchar_t \
- ((PyObject *(*)(_cffi_wchar_t))_cffi_exports[20])
+ ((PyObject *(*)(_cffi_wchar_t))_cffi_exports[20])
#define _cffi_to_c_long_double \
((long double(*)(PyObject *))_cffi_exports[21])
#define _cffi_to_c__Bool \
@@ -214,11 +214,11 @@ extern "C" {
#define _CFFI_CPIDX 25
#define _cffi_call_python \
((void(*)(struct _cffi_externpy_s *, char *))_cffi_exports[_CFFI_CPIDX])
-#define _cffi_to_c_wchar3216_t \
- ((int(*)(PyObject *))_cffi_exports[26])
-#define _cffi_from_c_wchar3216_t \
- ((PyObject *(*)(int))_cffi_exports[27])
-#define _CFFI_NUM_EXPORTS 28
+#define _cffi_to_c_wchar3216_t \
+ ((int(*)(PyObject *))_cffi_exports[26])
+#define _cffi_from_c_wchar3216_t \
+ ((PyObject *(*)(int))_cffi_exports[27])
+#define _CFFI_NUM_EXPORTS 28
struct _cffi_ctypedescr;
@@ -259,94 +259,94 @@ static PyObject *_cffi_init(const char *module_name, Py_ssize_t version,
return NULL;
}
-
-#ifdef HAVE_WCHAR_H
-typedef wchar_t _cffi_wchar_t;
-#else
-typedef uint16_t _cffi_wchar_t; /* same random pick as _cffi_backend.c */
-#endif
-
-_CFFI_UNUSED_FN static uint16_t _cffi_to_c_char16_t(PyObject *o)
-{
- if (sizeof(_cffi_wchar_t) == 2)
- return (uint16_t)_cffi_to_c_wchar_t(o);
- else
- return (uint16_t)_cffi_to_c_wchar3216_t(o);
-}
-
-_CFFI_UNUSED_FN static PyObject *_cffi_from_c_char16_t(uint16_t x)
-{
- if (sizeof(_cffi_wchar_t) == 2)
- return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
- else
- return _cffi_from_c_wchar3216_t((int)x);
-}
-
-_CFFI_UNUSED_FN static int _cffi_to_c_char32_t(PyObject *o)
-{
- if (sizeof(_cffi_wchar_t) == 4)
- return (int)_cffi_to_c_wchar_t(o);
- else
- return (int)_cffi_to_c_wchar3216_t(o);
-}
-
-_CFFI_UNUSED_FN static PyObject *_cffi_from_c_char32_t(unsigned int x)
-{
- if (sizeof(_cffi_wchar_t) == 4)
- return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
- else
- return _cffi_from_c_wchar3216_t((int)x);
-}
-
-union _cffi_union_alignment_u {
- unsigned char m_char;
- unsigned short m_short;
- unsigned int m_int;
- unsigned long m_long;
- unsigned long long m_longlong;
- float m_float;
- double m_double;
- long double m_longdouble;
-};
-
-struct _cffi_freeme_s {
- struct _cffi_freeme_s *next;
- union _cffi_union_alignment_u alignment;
-};
-
-_CFFI_UNUSED_FN static int
-_cffi_convert_array_argument(struct _cffi_ctypedescr *ctptr, PyObject *arg,
- char **output_data, Py_ssize_t datasize,
- struct _cffi_freeme_s **freeme)
-{
- char *p;
- if (datasize < 0)
- return -1;
-
- p = *output_data;
- if (p == NULL) {
- struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc(
- offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize);
- if (fp == NULL)
- return -1;
- fp->next = *freeme;
- *freeme = fp;
- p = *output_data = (char *)&fp->alignment;
- }
- memset((void *)p, 0, (size_t)datasize);
- return _cffi_convert_array_from_object(p, ctptr, arg);
-}
-
-_CFFI_UNUSED_FN static void
-_cffi_free_array_arguments(struct _cffi_freeme_s *freeme)
-{
- do {
- void *p = (void *)freeme;
- freeme = freeme->next;
- PyObject_Free(p);
- } while (freeme != NULL);
-}
-
+
+#ifdef HAVE_WCHAR_H
+typedef wchar_t _cffi_wchar_t;
+#else
+typedef uint16_t _cffi_wchar_t; /* same random pick as _cffi_backend.c */
+#endif
+
+_CFFI_UNUSED_FN static uint16_t _cffi_to_c_char16_t(PyObject *o)
+{
+ if (sizeof(_cffi_wchar_t) == 2)
+ return (uint16_t)_cffi_to_c_wchar_t(o);
+ else
+ return (uint16_t)_cffi_to_c_wchar3216_t(o);
+}
+
+_CFFI_UNUSED_FN static PyObject *_cffi_from_c_char16_t(uint16_t x)
+{
+ if (sizeof(_cffi_wchar_t) == 2)
+ return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
+ else
+ return _cffi_from_c_wchar3216_t((int)x);
+}
+
+_CFFI_UNUSED_FN static int _cffi_to_c_char32_t(PyObject *o)
+{
+ if (sizeof(_cffi_wchar_t) == 4)
+ return (int)_cffi_to_c_wchar_t(o);
+ else
+ return (int)_cffi_to_c_wchar3216_t(o);
+}
+
+_CFFI_UNUSED_FN static PyObject *_cffi_from_c_char32_t(unsigned int x)
+{
+ if (sizeof(_cffi_wchar_t) == 4)
+ return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
+ else
+ return _cffi_from_c_wchar3216_t((int)x);
+}
+
+union _cffi_union_alignment_u {
+ unsigned char m_char;
+ unsigned short m_short;
+ unsigned int m_int;
+ unsigned long m_long;
+ unsigned long long m_longlong;
+ float m_float;
+ double m_double;
+ long double m_longdouble;
+};
+
+struct _cffi_freeme_s {
+ struct _cffi_freeme_s *next;
+ union _cffi_union_alignment_u alignment;
+};
+
+_CFFI_UNUSED_FN static int
+_cffi_convert_array_argument(struct _cffi_ctypedescr *ctptr, PyObject *arg,
+ char **output_data, Py_ssize_t datasize,
+ struct _cffi_freeme_s **freeme)
+{
+ char *p;
+ if (datasize < 0)
+ return -1;
+
+ p = *output_data;
+ if (p == NULL) {
+ struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc(
+ offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize);
+ if (fp == NULL)
+ return -1;
+ fp->next = *freeme;
+ *freeme = fp;
+ p = *output_data = (char *)&fp->alignment;
+ }
+ memset((void *)p, 0, (size_t)datasize);
+ return _cffi_convert_array_from_object(p, ctptr, arg);
+}
+
+_CFFI_UNUSED_FN static void
+_cffi_free_array_arguments(struct _cffi_freeme_s *freeme)
+{
+ do {
+ void *p = (void *)freeme;
+ freeme = freeme->next;
+ PyObject_Free(p);
+ } while (freeme != NULL);
+}
+
/********** end CPython-specific section **********/
#else
_CFFI_UNUSED_FN
diff --git a/contrib/python/cffi/cffi/_embedding.h b/contrib/python/cffi/cffi/_embedding.h
index 702c426053..e863d8541a 100644
--- a/contrib/python/cffi/cffi/_embedding.h
+++ b/contrib/python/cffi/cffi/_embedding.h
@@ -1,12 +1,12 @@
/***** Support code for embedding *****/
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-#if defined(_WIN32)
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#if defined(_WIN32)
# define CFFI_DLLEXPORT __declspec(dllexport)
#elif defined(__GNUC__)
# define CFFI_DLLEXPORT __attribute__((visibility("default")))
@@ -114,9 +114,9 @@ static void _cffi_release_reentrant_mutex(void)
/********** CPython-specific section **********/
#ifndef PYPY_VERSION
-#include "_cffi_errors.h"
+#include "_cffi_errors.h"
+
-
#define _cffi_call_python_org _cffi_exports[_CFFI_CPIDX]
PyMODINIT_FUNC _CFFI_PYTHON_STARTUP_FUNC(void); /* forward */
@@ -145,7 +145,7 @@ static int _cffi_initialize_python(void)
int result;
PyGILState_STATE state;
PyObject *pycode=NULL, *global_dict=NULL, *x;
- PyObject *builtins;
+ PyObject *builtins;
state = PyGILState_Ensure();
@@ -170,11 +170,11 @@ static int _cffi_initialize_python(void)
global_dict = PyDict_New();
if (global_dict == NULL)
goto error;
- builtins = PyEval_GetBuiltins();
- if (builtins == NULL)
+ builtins = PyEval_GetBuiltins();
+ if (builtins == NULL)
+ goto error;
+ if (PyDict_SetItemString(global_dict, "__builtins__", builtins) < 0)
goto error;
- if (PyDict_SetItemString(global_dict, "__builtins__", builtins) < 0)
- goto error;
x = PyEval_EvalCode(
#if PY_MAJOR_VERSION < 3
(PyCodeObject *)
@@ -204,16 +204,16 @@ static int _cffi_initialize_python(void)
/* Print as much information as potentially useful.
Debugging load-time failures with embedding is not fun
*/
- PyObject *ecap;
+ PyObject *ecap;
PyObject *exception, *v, *tb, *f, *modules, *mod;
PyErr_Fetch(&exception, &v, &tb);
- ecap = _cffi_start_error_capture();
- f = PySys_GetObject((char *)"stderr");
- if (f != NULL && f != Py_None) {
- PyFile_WriteString(
- "Failed to initialize the Python-CFFI embedding logic:\n\n", f);
- }
-
+ ecap = _cffi_start_error_capture();
+ f = PySys_GetObject((char *)"stderr");
+ if (f != NULL && f != Py_None) {
+ PyFile_WriteString(
+ "Failed to initialize the Python-CFFI embedding logic:\n\n", f);
+ }
+
if (exception != NULL) {
PyErr_NormalizeException(&exception, &v, &tb);
PyErr_Display(exception, v, tb);
@@ -224,7 +224,7 @@ static int _cffi_initialize_python(void)
if (f != NULL && f != Py_None) {
PyFile_WriteString("\nFrom: " _CFFI_MODULE_NAME
- "\ncompiled with cffi version: 1.15.0"
+ "\ncompiled with cffi version: 1.15.0"
"\n_cffi_backend module: ", f);
modules = PyImport_GetModuleDict();
mod = PyDict_GetItemString(modules, "_cffi_backend");
@@ -240,15 +240,15 @@ static int _cffi_initialize_python(void)
PyFile_WriteObject(PySys_GetObject((char *)"path"), f, 0);
PyFile_WriteString("\n\n", f);
}
- _cffi_stop_error_capture(ecap);
+ _cffi_stop_error_capture(ecap);
}
result = -1;
goto done;
}
-#if PY_VERSION_HEX < 0x03080000
+#if PY_VERSION_HEX < 0x03080000
PyAPI_DATA(char *) _PyParser_TokenNames[]; /* from CPython */
-#endif
+#endif
static int _cffi_carefully_make_gil(void)
{
@@ -257,44 +257,44 @@ static int _cffi_carefully_make_gil(void)
that we don't hold the GIL before (if it exists), and we don't
hold it afterwards.
- (What it really does used to be completely different in Python 2
- and Python 3, with the Python 2 solution avoiding the spin-lock
- around the Py_InitializeEx() call. However, after recent changes
- to CPython 2.7 (issue #358) it no longer works. So we use the
- Python 3 solution everywhere.)
+ (What it really does used to be completely different in Python 2
+ and Python 3, with the Python 2 solution avoiding the spin-lock
+ around the Py_InitializeEx() call. However, after recent changes
+ to CPython 2.7 (issue #358) it no longer works. So we use the
+ Python 3 solution everywhere.)
- This initializes Python by calling Py_InitializeEx().
- Important: this must not be called concurrently at all.
+ This initializes Python by calling Py_InitializeEx().
+ Important: this must not be called concurrently at all.
So we use a global variable as a simple spin lock. This global
variable must be from 'libpythonX.Y.so', not from this
cffi-based extension module, because it must be shared from
- different cffi-based extension modules.
-
- In Python < 3.8, we choose
+ different cffi-based extension modules.
+
+ In Python < 3.8, we choose
_PyParser_TokenNames[0] as a completely arbitrary pointer value
that is never written to. The default is to point to the
string "ENDMARKER". We change it temporarily to point to the
next character in that string. (Yes, I know it's REALLY
obscure.)
- In Python >= 3.8, this string array is no longer writable, so
- instead we pick PyCapsuleType.tp_version_tag. We can't change
- Python < 3.8 because someone might use a mixture of cffi
- embedded modules, some of which were compiled before this file
- changed.
+ In Python >= 3.8, this string array is no longer writable, so
+ instead we pick PyCapsuleType.tp_version_tag. We can't change
+ Python < 3.8 because someone might use a mixture of cffi
+ embedded modules, some of which were compiled before this file
+ changed.
*/
#ifdef WITH_THREAD
-# if PY_VERSION_HEX < 0x03080000
+# if PY_VERSION_HEX < 0x03080000
char *volatile *lock = (char *volatile *)_PyParser_TokenNames;
- char *old_value, *locked_value;
+ char *old_value, *locked_value;
while (1) { /* spin loop */
old_value = *lock;
- locked_value = old_value + 1;
+ locked_value = old_value + 1;
if (old_value[0] == 'E') {
assert(old_value[1] == 'N');
- if (cffi_compare_and_swap(lock, old_value, locked_value))
+ if (cffi_compare_and_swap(lock, old_value, locked_value))
break;
}
else {
@@ -305,51 +305,51 @@ static int _cffi_carefully_make_gil(void)
this is only run at start-up anyway. */
}
}
-# else
- int volatile *lock = (int volatile *)&PyCapsule_Type.tp_version_tag;
- int old_value, locked_value;
- assert(!(PyCapsule_Type.tp_flags & Py_TPFLAGS_HAVE_VERSION_TAG));
-
- while (1) { /* spin loop */
- old_value = *lock;
- locked_value = -42;
- if (old_value == 0) {
- if (cffi_compare_and_swap(lock, old_value, locked_value))
- break;
- }
- else {
- assert(old_value == locked_value);
- /* should ideally do a spin loop instruction here, but
- hard to do it portably and doesn't really matter I
- think: PyEval_InitThreads() should be very fast, and
- this is only run at start-up anyway. */
- }
- }
-# endif
+# else
+ int volatile *lock = (int volatile *)&PyCapsule_Type.tp_version_tag;
+ int old_value, locked_value;
+ assert(!(PyCapsule_Type.tp_flags & Py_TPFLAGS_HAVE_VERSION_TAG));
+
+ while (1) { /* spin loop */
+ old_value = *lock;
+ locked_value = -42;
+ if (old_value == 0) {
+ if (cffi_compare_and_swap(lock, old_value, locked_value))
+ break;
+ }
+ else {
+ assert(old_value == locked_value);
+ /* should ideally do a spin loop instruction here, but
+ hard to do it portably and doesn't really matter I
+ think: PyEval_InitThreads() should be very fast, and
+ this is only run at start-up anyway. */
+ }
+ }
+# endif
#endif
- /* call Py_InitializeEx() */
- if (!Py_IsInitialized()) {
- _cffi_py_initialize();
-#if PY_VERSION_HEX < 0x03070000
+ /* call Py_InitializeEx() */
+ if (!Py_IsInitialized()) {
+ _cffi_py_initialize();
+#if PY_VERSION_HEX < 0x03070000
+ PyEval_InitThreads();
+#endif
+ PyEval_SaveThread(); /* release the GIL */
+ /* the returned tstate must be the one that has been stored into the
+ autoTLSkey by _PyGILState_Init() called from Py_Initialize(). */
+ }
+ else {
+#if PY_VERSION_HEX < 0x03070000
+ /* PyEval_InitThreads() is always a no-op from CPython 3.7 */
+ PyGILState_STATE state = PyGILState_Ensure();
PyEval_InitThreads();
-#endif
- PyEval_SaveThread(); /* release the GIL */
- /* the returned tstate must be the one that has been stored into the
- autoTLSkey by _PyGILState_Init() called from Py_Initialize(). */
- }
- else {
-#if PY_VERSION_HEX < 0x03070000
- /* PyEval_InitThreads() is always a no-op from CPython 3.7 */
- PyGILState_STATE state = PyGILState_Ensure();
- PyEval_InitThreads();
PyGILState_Release(state);
-#endif
+#endif
}
#ifdef WITH_THREAD
/* release the lock */
- while (!cffi_compare_and_swap(lock, locked_value, old_value))
+ while (!cffi_compare_and_swap(lock, locked_value, old_value))
;
#endif
@@ -368,11 +368,11 @@ PyMODINIT_FUNC _CFFI_PYTHON_STARTUP_FUNC(const void *[]); /* forward */
static struct _cffi_pypy_init_s {
const char *name;
- void *func; /* function pointer */
+ void *func; /* function pointer */
const char *code;
} _cffi_pypy_init = {
_CFFI_MODULE_NAME,
- _CFFI_PYTHON_STARTUP_FUNC,
+ _CFFI_PYTHON_STARTUP_FUNC,
_CFFI_PYTHON_STARTUP_CODE,
};
@@ -521,7 +521,7 @@ static int cffi_start_python(void)
#undef cffi_compare_and_swap
#undef cffi_write_barrier
#undef cffi_read_barrier
-
-#ifdef __cplusplus
-}
-#endif
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/contrib/python/cffi/cffi/api.py b/contrib/python/cffi/cffi/api.py
index 32bc71c5a3..999a8aefc4 100644
--- a/contrib/python/cffi/cffi/api.py
+++ b/contrib/python/cffi/cffi/api.py
@@ -1,7 +1,7 @@
import sys, types
from .lock import allocate_lock
-from .error import CDefError
-from . import model
+from .error import CDefError
+from . import model
try:
callable
@@ -16,7 +16,7 @@ except NameError:
# Python 3.x
basestring = str
-_unspecified = object()
+_unspecified = object()
@@ -47,22 +47,22 @@ class FFI(object):
# _cffi_backend.so compiled.
import _cffi_backend as backend
from . import __version__
- if backend.__version__ != __version__:
- # bad version! Try to be as explicit as possible.
- if hasattr(backend, '__file__'):
- # CPython
- raise Exception("Version mismatch: this is the 'cffi' package version %s, located in %r. When we import the top-level '_cffi_backend' extension module, we get version %s, located in %r. The two versions should be equal; check your installation." % (
- __version__, __file__,
- backend.__version__, backend.__file__))
- else:
- # PyPy
- raise Exception("Version mismatch: this is the 'cffi' package version %s, located in %r. This interpreter comes with a built-in '_cffi_backend' module, which is version %s. The two versions should be equal; check your installation." % (
- __version__, __file__, backend.__version__))
+ if backend.__version__ != __version__:
+ # bad version! Try to be as explicit as possible.
+ if hasattr(backend, '__file__'):
+ # CPython
+ raise Exception("Version mismatch: this is the 'cffi' package version %s, located in %r. When we import the top-level '_cffi_backend' extension module, we get version %s, located in %r. The two versions should be equal; check your installation." % (
+ __version__, __file__,
+ backend.__version__, backend.__file__))
+ else:
+ # PyPy
+ raise Exception("Version mismatch: this is the 'cffi' package version %s, located in %r. This interpreter comes with a built-in '_cffi_backend' module, which is version %s. The two versions should be equal; check your installation." % (
+ __version__, __file__, backend.__version__))
# (If you insist you can also try to pass the option
# 'backend=backend_ctypes.CTypesBackend()', but don't
# rely on it! It's probably not going to work well.)
- from . import cparser
+ from . import cparser
self._backend = backend
self._lock = allocate_lock()
self._parser = cparser.Parser()
@@ -77,10 +77,10 @@ class FFI(object):
self._init_once_cache = {}
self._cdef_version = None
self._embedding = None
- self._typecache = model.get_typecache(backend)
+ self._typecache = model.get_typecache(backend)
if hasattr(backend, 'set_ffi'):
backend.set_ffi(self)
- for name in list(backend.__dict__):
+ for name in list(backend.__dict__):
if name.startswith('RTLD_'):
setattr(self, name, getattr(backend, name))
#
@@ -96,23 +96,23 @@ class FFI(object):
# ctypes backend: attach these constants to the instance
self.NULL = self.cast(self.BVoidP, 0)
self.CData, self.CType = backend._get_types()
- self.buffer = backend.buffer
+ self.buffer = backend.buffer
- def cdef(self, csource, override=False, packed=False, pack=None):
+ def cdef(self, csource, override=False, packed=False, pack=None):
"""Parse the given C source. This registers all declared functions,
types, and global variables. The functions and global variables can
then be accessed via either 'ffi.dlopen()' or 'ffi.verify()'.
The types can be used in 'ffi.new()' and other functions.
If 'packed' is specified as True, all structs declared inside this
cdef are packed, i.e. laid out without any field alignment at all.
- Alternatively, 'pack' can be a small integer, and requests for
- alignment greater than that are ignored (pack=1 is equivalent to
- packed=True).
+ Alternatively, 'pack' can be a small integer, and requests for
+ alignment greater than that are ignored (pack=1 is equivalent to
+ packed=True).
"""
- self._cdef(csource, override=override, packed=packed, pack=pack)
+ self._cdef(csource, override=override, packed=packed, pack=pack)
- def embedding_api(self, csource, packed=False, pack=None):
- self._cdef(csource, packed=packed, pack=pack, dllexport=True)
+ def embedding_api(self, csource, packed=False, pack=None):
+ self._cdef(csource, packed=packed, pack=pack, dllexport=True)
if self._embedding is None:
self._embedding = ''
@@ -141,24 +141,24 @@ class FFI(object):
linked to a particular library, just like C headers; in the
library we only look for the actual (untyped) symbols.
"""
- if not (isinstance(name, basestring) or
- name is None or
- isinstance(name, self.CData)):
- raise TypeError("dlopen(name): name must be a file name, None, "
- "or an already-opened 'void *' handle")
+ if not (isinstance(name, basestring) or
+ name is None or
+ isinstance(name, self.CData)):
+ raise TypeError("dlopen(name): name must be a file name, None, "
+ "or an already-opened 'void *' handle")
with self._lock:
lib, function_cache = _make_ffi_library(self, name, flags)
self._function_caches.append(function_cache)
self._libraries.append(lib)
return lib
- def dlclose(self, lib):
- """Close a library obtained with ffi.dlopen(). After this call,
- access to functions or variables from the library will fail
- (possibly with a segmentation fault).
- """
- type(lib).__cffi_close__(lib)
-
+ def dlclose(self, lib):
+ """Close a library obtained with ffi.dlopen(). After this call,
+ access to functions or variables from the library will fail
+ (possibly with a segmentation fault).
+ """
+ type(lib).__cffi_close__(lib)
+
def _typeof_locked(self, cdecl):
# call me with the lock!
key = cdecl
@@ -230,7 +230,7 @@ class FFI(object):
def offsetof(self, cdecl, *fields_or_indexes):
"""Return the offset of the named field inside the given
- structure or array, which must be given as a C type name.
+ structure or array, which must be given as a C type name.
You can give several field names in case of nested structures.
You can also give numeric values which correspond to array
items, in case of an array type.
@@ -318,7 +318,7 @@ class FFI(object):
return self._backend.string(cdata, maxlen)
def unpack(self, cdata, length):
- """Unpack an array of C data of the given length,
+ """Unpack an array of C data of the given length,
returning a Python string/unicode/list.
If 'cdata' is a pointer to 'char', returns a byte string.
@@ -334,36 +334,36 @@ class FFI(object):
"""
return self._backend.unpack(cdata, length)
- #def buffer(self, cdata, size=-1):
- # """Return a read-write buffer object that references the raw C data
- # pointed to by the given 'cdata'. The 'cdata' must be a pointer or
- # an array. Can be passed to functions expecting a buffer, or directly
- # manipulated with:
- #
- # buf[:] get a copy of it in a regular string, or
- # buf[idx] as a single character
- # buf[:] = ...
- # buf[idx] = ... change the content
- # """
- # note that 'buffer' is a type, set on this instance by __init__
-
- def from_buffer(self, cdecl, python_buffer=_unspecified,
- require_writable=False):
- """Return a cdata of the given type pointing to the data of the
+ #def buffer(self, cdata, size=-1):
+ # """Return a read-write buffer object that references the raw C data
+ # pointed to by the given 'cdata'. The 'cdata' must be a pointer or
+ # an array. Can be passed to functions expecting a buffer, or directly
+ # manipulated with:
+ #
+ # buf[:] get a copy of it in a regular string, or
+ # buf[idx] as a single character
+ # buf[:] = ...
+ # buf[idx] = ... change the content
+ # """
+ # note that 'buffer' is a type, set on this instance by __init__
+
+ def from_buffer(self, cdecl, python_buffer=_unspecified,
+ require_writable=False):
+ """Return a cdata of the given type pointing to the data of the
given Python object, which must support the buffer interface.
Note that this is not meant to be used on the built-in types
str or unicode (you can build 'char[]' arrays explicitly)
but only on objects containing large quantities of raw data
in some other format, like 'array.array' or numpy arrays.
-
- The first argument is optional and default to 'char[]'.
+
+ The first argument is optional and default to 'char[]'.
"""
- if python_buffer is _unspecified:
- cdecl, python_buffer = self.BCharA, cdecl
- elif isinstance(cdecl, basestring):
- cdecl = self._typeof(cdecl)
- return self._backend.from_buffer(cdecl, python_buffer,
- require_writable)
+ if python_buffer is _unspecified:
+ cdecl, python_buffer = self.BCharA, cdecl
+ elif isinstance(cdecl, basestring):
+ cdecl = self._typeof(cdecl)
+ return self._backend.from_buffer(cdecl, python_buffer,
+ require_writable)
def memmove(self, dest, src, n):
"""ffi.memmove(dest, src, n) copies n bytes of memory from src to dest.
@@ -418,17 +418,17 @@ class FFI(object):
replace_with = ' ' + replace_with
return self._backend.getcname(cdecl, replace_with)
- def gc(self, cdata, destructor, size=0):
+ def gc(self, cdata, destructor, size=0):
"""Return a new cdata object that points to the same
data. Later, when this new cdata object is garbage-collected,
'destructor(old_cdata_object)' will be called.
-
- The optional 'size' gives an estimate of the size, used to
- trigger the garbage collection more eagerly. So far only used
- on PyPy. It tells the GC that the returned object keeps alive
- roughly 'size' bytes of external memory.
+
+ The optional 'size' gives an estimate of the size, used to
+ trigger the garbage collection more eagerly. So far only used
+ on PyPy. It tells the GC that the returned object keeps alive
+ roughly 'size' bytes of external memory.
"""
- return self._backend.gcp(cdata, destructor, size)
+ return self._backend.gcp(cdata, destructor, size)
def _get_cached_btype(self, type):
assert self._lock.acquire(False) is False
@@ -492,12 +492,12 @@ class FFI(object):
field or array item in the structure or array, recursively in
case of nested structures.
"""
- try:
- ctype = self._backend.typeof(cdata)
- except TypeError:
- if '__addressof__' in type(cdata).__dict__:
- return type(cdata).__addressof__(cdata, *fields_or_indexes)
- raise
+ try:
+ ctype = self._backend.typeof(cdata)
+ except TypeError:
+ if '__addressof__' in type(cdata).__dict__:
+ return type(cdata).__addressof__(cdata, *fields_or_indexes)
+ raise
if fields_or_indexes:
ctype, offset = self._typeoffsetof(ctype, *fields_or_indexes)
else:
@@ -543,9 +543,9 @@ class FFI(object):
def from_handle(self, x):
return self._backend.from_handle(x)
- def release(self, x):
- self._backend.release(x)
-
+ def release(self, x):
+ self._backend.release(x)
+
def set_unicode(self, enabled_flag):
"""Windows: if 'enabled_flag' is True, enable the UNICODE and
_UNICODE defines in C, and declare the types like TCHAR and LPTCSTR
@@ -596,17 +596,17 @@ class FFI(object):
if sys.platform == "win32":
# we need 'libpypy-c.lib'. Current distributions of
# pypy (>= 4.1) contain it as 'libs/python27.lib'.
- pythonlib = "python{0[0]}{0[1]}".format(sys.version_info)
+ pythonlib = "python{0[0]}{0[1]}".format(sys.version_info)
if hasattr(sys, 'prefix'):
ensure('library_dirs', os.path.join(sys.prefix, 'libs'))
else:
# we need 'libpypy-c.{so,dylib}', which should be by
# default located in 'sys.prefix/bin' for installed
# systems.
- if sys.version_info < (3,):
- pythonlib = "pypy-c"
- else:
- pythonlib = "pypy3-c"
+ if sys.version_info < (3,):
+ pythonlib = "pypy-c"
+ else:
+ pythonlib = "pypy3-c"
if hasattr(sys, 'prefix'):
ensure('library_dirs', os.path.join(sys.prefix, 'bin'))
# On uninstalled pypy's, the libpypy-c is typically found in
@@ -635,28 +635,28 @@ class FFI(object):
ensure('extra_link_args', '/MANIFEST')
def set_source(self, module_name, source, source_extension='.c', **kwds):
- import os
+ import os
if hasattr(self, '_assigned_source'):
raise ValueError("set_source() cannot be called several times "
"per ffi object")
if not isinstance(module_name, basestring):
raise TypeError("'module_name' must be a string")
- if os.sep in module_name or (os.altsep and os.altsep in module_name):
- raise ValueError("'module_name' must not contain '/': use a dotted "
- "name to make a 'package.module' location")
+ if os.sep in module_name or (os.altsep and os.altsep in module_name):
+ raise ValueError("'module_name' must not contain '/': use a dotted "
+ "name to make a 'package.module' location")
self._assigned_source = (str(module_name), source,
source_extension, kwds)
- def set_source_pkgconfig(self, module_name, pkgconfig_libs, source,
- source_extension='.c', **kwds):
- from . import pkgconfig
- if not isinstance(pkgconfig_libs, list):
- raise TypeError("the pkgconfig_libs argument must be a list "
- "of package names")
- kwds2 = pkgconfig.flags_from_pkgconfig(pkgconfig_libs)
- pkgconfig.merge_flags(kwds, kwds2)
- self.set_source(module_name, source, source_extension, **kwds)
-
+ def set_source_pkgconfig(self, module_name, pkgconfig_libs, source,
+ source_extension='.c', **kwds):
+ from . import pkgconfig
+ if not isinstance(pkgconfig_libs, list):
+ raise TypeError("the pkgconfig_libs argument must be a list "
+ "of package names")
+ kwds2 = pkgconfig.flags_from_pkgconfig(pkgconfig_libs)
+ pkgconfig.merge_flags(kwds, kwds2)
+ self.set_source(module_name, source, source_extension, **kwds)
+
def distutils_extension(self, tmpdir='build', verbose=True):
from distutils.dir_util import mkpath
from .recompiler import recompile
@@ -802,30 +802,30 @@ class FFI(object):
def _load_backend_lib(backend, name, flags):
- import os
- if not isinstance(name, basestring):
- if sys.platform != "win32" or name is not None:
- return backend.load_library(name, flags)
+ import os
+ if not isinstance(name, basestring):
+ if sys.platform != "win32" or name is not None:
+ return backend.load_library(name, flags)
name = "c" # Windows: load_library(None) fails, but this works
- # on Python 2 (backward compatibility hack only)
- first_error = None
- if '.' in name or '/' in name or os.sep in name:
- try:
- return backend.load_library(name, flags)
- except OSError as e:
- first_error = e
- import ctypes.util
- path = ctypes.util.find_library(name)
- if path is None:
- if name == "c" and sys.platform == "win32" and sys.version_info >= (3,):
- raise OSError("dlopen(None) cannot work on Windows for Python 3 "
- "(see http://bugs.python.org/issue23606)")
- msg = ("ctypes.util.find_library() did not manage "
- "to locate a library called %r" % (name,))
- if first_error is not None:
- msg = "%s. Additionally, %s" % (first_error, msg)
- raise OSError(msg)
- return backend.load_library(path, flags)
+ # on Python 2 (backward compatibility hack only)
+ first_error = None
+ if '.' in name or '/' in name or os.sep in name:
+ try:
+ return backend.load_library(name, flags)
+ except OSError as e:
+ first_error = e
+ import ctypes.util
+ path = ctypes.util.find_library(name)
+ if path is None:
+ if name == "c" and sys.platform == "win32" and sys.version_info >= (3,):
+ raise OSError("dlopen(None) cannot work on Windows for Python 3 "
+ "(see http://bugs.python.org/issue23606)")
+ msg = ("ctypes.util.find_library() did not manage "
+ "to locate a library called %r" % (name,))
+ if first_error is not None:
+ msg = "%s. Additionally, %s" % (first_error, msg)
+ raise OSError(msg)
+ return backend.load_library(path, flags)
def _make_ffi_library(ffi, libname, flags):
backend = ffi._backend
@@ -835,7 +835,7 @@ def _make_ffi_library(ffi, libname, flags):
key = 'function ' + name
tp, _ = ffi._parser._declarations[key]
BType = ffi._get_cached_btype(tp)
- value = backendlib.load_function(BType, name)
+ value = backendlib.load_function(BType, name)
library.__dict__[name] = value
#
def accessor_variable(name):
@@ -848,21 +848,21 @@ def _make_ffi_library(ffi, libname, flags):
lambda self: read_variable(BType, name),
lambda self, value: write_variable(BType, name, value)))
#
- def addressof_var(name):
- try:
- return addr_variables[name]
- except KeyError:
- with ffi._lock:
- if name not in addr_variables:
- key = 'variable ' + name
- tp, _ = ffi._parser._declarations[key]
- BType = ffi._get_cached_btype(tp)
- if BType.kind != 'array':
- BType = model.pointer_cache(ffi, BType)
- p = backendlib.load_function(BType, name)
- addr_variables[name] = p
- return addr_variables[name]
- #
+ def addressof_var(name):
+ try:
+ return addr_variables[name]
+ except KeyError:
+ with ffi._lock:
+ if name not in addr_variables:
+ key = 'variable ' + name
+ tp, _ = ffi._parser._declarations[key]
+ BType = ffi._get_cached_btype(tp)
+ if BType.kind != 'array':
+ BType = model.pointer_cache(ffi, BType)
+ p = backendlib.load_function(BType, name)
+ addr_variables[name] = p
+ return addr_variables[name]
+ #
def accessor_constant(name):
raise NotImplementedError("non-integer constant '%s' cannot be "
"accessed from a dlopen() library" % (name,))
@@ -872,7 +872,7 @@ def _make_ffi_library(ffi, libname, flags):
#
accessors = {}
accessors_version = [False]
- addr_variables = {}
+ addr_variables = {}
#
def update_accessors():
if accessors_version[0] is ffi._cdef_version:
@@ -923,23 +923,23 @@ def _make_ffi_library(ffi, libname, flags):
with ffi._lock:
update_accessors()
return accessors.keys()
- def __addressof__(self, name):
- if name in library.__dict__:
- return library.__dict__[name]
- if name in FFILibrary.__dict__:
- return addressof_var(name)
- make_accessor(name)
- if name in library.__dict__:
- return library.__dict__[name]
- if name in FFILibrary.__dict__:
- return addressof_var(name)
- raise AttributeError("cffi library has no function or "
- "global variable named '%s'" % (name,))
- def __cffi_close__(self):
- backendlib.close_lib()
- self.__dict__.clear()
+ def __addressof__(self, name):
+ if name in library.__dict__:
+ return library.__dict__[name]
+ if name in FFILibrary.__dict__:
+ return addressof_var(name)
+ make_accessor(name)
+ if name in library.__dict__:
+ return library.__dict__[name]
+ if name in FFILibrary.__dict__:
+ return addressof_var(name)
+ raise AttributeError("cffi library has no function or "
+ "global variable named '%s'" % (name,))
+ def __cffi_close__(self):
+ backendlib.close_lib()
+ self.__dict__.clear()
#
- if isinstance(libname, basestring):
+ if isinstance(libname, basestring):
try:
if not isinstance(libname, str): # unicode, on Python 2
libname = libname.encode('utf-8')
diff --git a/contrib/python/cffi/cffi/backend_ctypes.py b/contrib/python/cffi/cffi/backend_ctypes.py
index 649ecc3751..e7956a79cf 100644
--- a/contrib/python/cffi/cffi/backend_ctypes.py
+++ b/contrib/python/cffi/cffi/backend_ctypes.py
@@ -112,20 +112,20 @@ class CTypesData(object):
def _make_cmp(name):
cmpfunc = getattr(operator, name)
def cmp(self, other):
- v_is_ptr = not isinstance(self, CTypesGenericPrimitive)
- w_is_ptr = (isinstance(other, CTypesData) and
- not isinstance(other, CTypesGenericPrimitive))
- if v_is_ptr and w_is_ptr:
+ v_is_ptr = not isinstance(self, CTypesGenericPrimitive)
+ w_is_ptr = (isinstance(other, CTypesData) and
+ not isinstance(other, CTypesGenericPrimitive))
+ if v_is_ptr and w_is_ptr:
return cmpfunc(self._convert_to_address(None),
other._convert_to_address(None))
- elif v_is_ptr or w_is_ptr:
- return NotImplemented
+ elif v_is_ptr or w_is_ptr:
+ return NotImplemented
else:
- if isinstance(self, CTypesGenericPrimitive):
- self = self._value
- if isinstance(other, CTypesGenericPrimitive):
- other = other._value
- return cmpfunc(self, other)
+ if isinstance(self, CTypesGenericPrimitive):
+ self = self._value
+ if isinstance(other, CTypesGenericPrimitive):
+ other = other._value
+ return cmpfunc(self, other)
cmp.func_name = name
return cmp
@@ -137,7 +137,7 @@ class CTypesData(object):
__ge__ = _make_cmp('__ge__')
def __hash__(self):
- return hash(self._convert_to_address(None))
+ return hash(self._convert_to_address(None))
def _to_string(self, maxlen):
raise TypeError("string(): %r" % (self,))
@@ -147,7 +147,7 @@ class CTypesGenericPrimitive(CTypesData):
__slots__ = []
def __hash__(self):
- return hash(self._value)
+ return hash(self._value)
def _get_own_repr(self):
return repr(self._from_ctypes(self._value))
@@ -403,7 +403,7 @@ class CTypesBackend(object):
source = _cast_source_to_int(source)
return cls(bool(source))
def __int__(self):
- return int(self._value)
+ return int(self._value)
if kind == 'char':
@classmethod
@@ -636,10 +636,10 @@ class CTypesBackend(object):
if isinstance(init, bytes):
init = [init[i:i+1] for i in range(len(init))]
else:
- if isinstance(init, CTypesGenericArray):
- if (len(init) != len(blob) or
- not isinstance(init, CTypesArray)):
- raise TypeError("length/type mismatch: %s" % (init,))
+ if isinstance(init, CTypesGenericArray):
+ if (len(init) != len(blob) or
+ not isinstance(init, CTypesArray)):
+ raise TypeError("length/type mismatch: %s" % (init,))
init = tuple(init)
if len(init) > len(blob):
raise IndexError("too many initializers")
@@ -734,8 +734,8 @@ class CTypesBackend(object):
return self._new_struct_or_union('union', name, ctypes.Union)
def complete_struct_or_union(self, CTypesStructOrUnion, fields, tp,
- totalsize=-1, totalalignment=-1, sflags=0,
- pack=0):
+ totalsize=-1, totalalignment=-1, sflags=0,
+ pack=0):
if totalsize >= 0 or totalalignment >= 0:
raise NotImplementedError("the ctypes backend of CFFI does not support "
"structures completed by verify(); please "
@@ -756,8 +756,8 @@ class CTypesBackend(object):
bfield_types[fname] = Ellipsis
if sflags & 8:
struct_or_union._pack_ = 1
- elif pack:
- struct_or_union._pack_ = pack
+ elif pack:
+ struct_or_union._pack_ = pack
struct_or_union._fields_ = cfields
CTypesStructOrUnion._bfield_types = bfield_types
#
@@ -1009,7 +1009,7 @@ class CTypesBackend(object):
_weakref_cache_ref = None
- def gcp(self, cdata, destructor, size=0):
+ def gcp(self, cdata, destructor, size=0):
if self._weakref_cache_ref is None:
import weakref
class MyRef(weakref.ref):
diff --git a/contrib/python/cffi/cffi/cffi_opcode.py b/contrib/python/cffi/cffi/cffi_opcode.py
index 43d386f3ec..a0df98d1c7 100644
--- a/contrib/python/cffi/cffi/cffi_opcode.py
+++ b/contrib/python/cffi/cffi/cffi_opcode.py
@@ -1,4 +1,4 @@
-from .error import VerificationError
+from .error import VerificationError
class CffiOp(object):
def __init__(self, op, arg):
@@ -105,12 +105,12 @@ PRIM_INT_FAST64 = 44
PRIM_UINT_FAST64 = 45
PRIM_INTMAX = 46
PRIM_UINTMAX = 47
-PRIM_FLOATCOMPLEX = 48
-PRIM_DOUBLECOMPLEX = 49
-PRIM_CHAR16 = 50
-PRIM_CHAR32 = 51
+PRIM_FLOATCOMPLEX = 48
+PRIM_DOUBLECOMPLEX = 49
+PRIM_CHAR16 = 50
+PRIM_CHAR32 = 51
-_NUM_PRIM = 52
+_NUM_PRIM = 52
_UNKNOWN_PRIM = -1
_UNKNOWN_FLOAT_PRIM = -2
_UNKNOWN_LONG_DOUBLE = -3
@@ -132,12 +132,12 @@ PRIMITIVE_TO_INDEX = {
'float': PRIM_FLOAT,
'double': PRIM_DOUBLE,
'long double': PRIM_LONGDOUBLE,
- 'float _Complex': PRIM_FLOATCOMPLEX,
- 'double _Complex': PRIM_DOUBLECOMPLEX,
+ 'float _Complex': PRIM_FLOATCOMPLEX,
+ 'double _Complex': PRIM_DOUBLECOMPLEX,
'_Bool': PRIM_BOOL,
'wchar_t': PRIM_WCHAR,
- 'char16_t': PRIM_CHAR16,
- 'char32_t': PRIM_CHAR32,
+ 'char16_t': PRIM_CHAR16,
+ 'char32_t': PRIM_CHAR32,
'int8_t': PRIM_INT8,
'uint8_t': PRIM_UINT8,
'int16_t': PRIM_INT16,
diff --git a/contrib/python/cffi/cffi/commontypes.py b/contrib/python/cffi/cffi/commontypes.py
index 4da767f179..8ec97c756a 100644
--- a/contrib/python/cffi/cffi/commontypes.py
+++ b/contrib/python/cffi/cffi/commontypes.py
@@ -1,6 +1,6 @@
import sys
-from . import model
-from .error import FFIError
+from . import model
+from .error import FFIError
COMMON_TYPES = {}
@@ -32,11 +32,11 @@ def resolve_common_type(parser, commontype):
elif cdecl in model.PrimitiveType.ALL_PRIMITIVE_TYPES:
result, quals = model.PrimitiveType(cdecl), 0
elif cdecl == 'set-unicode-needed':
- raise FFIError("The Windows type %r is only available after "
- "you call ffi.set_unicode()" % (commontype,))
+ raise FFIError("The Windows type %r is only available after "
+ "you call ffi.set_unicode()" % (commontype,))
else:
if commontype == cdecl:
- raise FFIError(
+ raise FFIError(
"Unsupported type: %r. Please look at "
"http://cffi.readthedocs.io/en/latest/cdef.html#ffi-cdef-limitations "
"and file an issue if you think this type should really "
diff --git a/contrib/python/cffi/cffi/cparser.py b/contrib/python/cffi/cffi/cparser.py
index 500e687da3..74830e913f 100644
--- a/contrib/python/cffi/cffi/cparser.py
+++ b/contrib/python/cffi/cffi/cparser.py
@@ -1,6 +1,6 @@
-from . import model
+from . import model
from .commontypes import COMMON_TYPES, resolve_common_type
-from .error import FFIError, CDefError
+from .error import FFIError, CDefError
try:
from . import _pycparser as pycparser
except ImportError:
@@ -16,20 +16,20 @@ try:
except ImportError:
lock = None
-def _workaround_for_static_import_finders():
- # Issue #392: packaging tools like cx_Freeze can not find these
- # because pycparser uses exec dynamic import. This is an obscure
- # workaround. This function is never called.
- import pycparser.yacctab
- import pycparser.lextab
-
-CDEF_SOURCE_STRING = "<cdef source string>"
+def _workaround_for_static_import_finders():
+ # Issue #392: packaging tools like cx_Freeze can not find these
+ # because pycparser uses exec dynamic import. This is an obscure
+ # workaround. This function is never called.
+ import pycparser.yacctab
+ import pycparser.lextab
+
+CDEF_SOURCE_STRING = "<cdef source string>"
_r_comment = re.compile(r"/\*.*?\*/|//([^\n\\]|\\.)*?$",
re.DOTALL | re.MULTILINE)
_r_define = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)"
r"\b((?:[^\n\\]|\\.)*?)$",
re.DOTALL | re.MULTILINE)
-_r_line_directive = re.compile(r"^[ \t]*#[ \t]*(?:line|\d+)\b.*$", re.MULTILINE)
+_r_line_directive = re.compile(r"^[ \t]*#[ \t]*(?:line|\d+)\b.*$", re.MULTILINE)
_r_partial_enum = re.compile(r"=\s*\.\.\.\s*[,}]|\.\.\.\s*\}")
_r_enum_dotdotdot = re.compile(r"__dotdotdot\d+__$")
_r_partial_array = re.compile(r"\[\s*\.\.\.\s*\]")
@@ -43,9 +43,9 @@ _r_extern_python = re.compile(r'\bextern\s*"'
r'(Python|Python\s*\+\s*C|C\s*\+\s*Python)"\s*.')
_r_star_const_space = re.compile( # matches "* const "
r"[*]\s*((const|volatile|restrict)\b\s*)+")
-_r_int_dotdotdot = re.compile(r"(\b(int|long|short|signed|unsigned|char)\s*)+"
- r"\.\.\.")
-_r_float_dotdotdot = re.compile(r"\b(double|float)\s*\.\.\.")
+_r_int_dotdotdot = re.compile(r"(\b(int|long|short|signed|unsigned|char)\s*)+"
+ r"\.\.\.")
+_r_float_dotdotdot = re.compile(r"\b(double|float)\s*\.\.\.")
def _get_parser():
global _parser_cache
@@ -126,7 +126,7 @@ def _preprocess_extern_python(csource):
# grouping variant
closing = csource.find('}', endpos)
if closing < 0:
- raise CDefError("'extern \"Python\" {': no '}' found")
+ raise CDefError("'extern \"Python\" {': no '}' found")
if csource.find('{', endpos + 1, closing) >= 0:
raise NotImplementedError("cannot use { } inside a block "
"'extern \"Python\" { ... }'")
@@ -136,7 +136,7 @@ def _preprocess_extern_python(csource):
# non-grouping variant
semicolon = csource.find(';', endpos)
if semicolon < 0:
- raise CDefError("'extern \"Python\": no ';' found")
+ raise CDefError("'extern \"Python\": no ';' found")
parts.append(csource[endpos:semicolon+1])
csource = csource[semicolon+1:]
parts.append(' void __cffi_extern_python_stop;')
@@ -145,56 +145,56 @@ def _preprocess_extern_python(csource):
parts.append(csource)
return ''.join(parts)
-def _warn_for_string_literal(csource):
- if '"' not in csource:
- return
- for line in csource.splitlines():
- if '"' in line and not line.lstrip().startswith('#'):
- import warnings
- warnings.warn("String literal found in cdef() or type source. "
- "String literals are ignored here, but you should "
- "remove them anyway because some character sequences "
- "confuse pre-parsing.")
- break
-
-def _warn_for_non_extern_non_static_global_variable(decl):
- if not decl.storage:
- import warnings
- warnings.warn("Global variable '%s' in cdef(): for consistency "
- "with C it should have a storage class specifier "
- "(usually 'extern')" % (decl.name,))
-
-def _remove_line_directives(csource):
- # _r_line_directive matches whole lines, without the final \n, if they
- # start with '#line' with some spacing allowed, or '#NUMBER'. This
- # function stores them away and replaces them with exactly the string
- # '#line@N', where N is the index in the list 'line_directives'.
- line_directives = []
- def replace(m):
- i = len(line_directives)
- line_directives.append(m.group())
- return '#line@%d' % i
- csource = _r_line_directive.sub(replace, csource)
- return csource, line_directives
-
-def _put_back_line_directives(csource, line_directives):
- def replace(m):
- s = m.group()
- if not s.startswith('#line@'):
- raise AssertionError("unexpected #line directive "
- "(should have been processed and removed")
- return line_directives[int(s[6:])]
- return _r_line_directive.sub(replace, csource)
-
+def _warn_for_string_literal(csource):
+ if '"' not in csource:
+ return
+ for line in csource.splitlines():
+ if '"' in line and not line.lstrip().startswith('#'):
+ import warnings
+ warnings.warn("String literal found in cdef() or type source. "
+ "String literals are ignored here, but you should "
+ "remove them anyway because some character sequences "
+ "confuse pre-parsing.")
+ break
+
+def _warn_for_non_extern_non_static_global_variable(decl):
+ if not decl.storage:
+ import warnings
+ warnings.warn("Global variable '%s' in cdef(): for consistency "
+ "with C it should have a storage class specifier "
+ "(usually 'extern')" % (decl.name,))
+
+def _remove_line_directives(csource):
+ # _r_line_directive matches whole lines, without the final \n, if they
+ # start with '#line' with some spacing allowed, or '#NUMBER'. This
+ # function stores them away and replaces them with exactly the string
+ # '#line@N', where N is the index in the list 'line_directives'.
+ line_directives = []
+ def replace(m):
+ i = len(line_directives)
+ line_directives.append(m.group())
+ return '#line@%d' % i
+ csource = _r_line_directive.sub(replace, csource)
+ return csource, line_directives
+
+def _put_back_line_directives(csource, line_directives):
+ def replace(m):
+ s = m.group()
+ if not s.startswith('#line@'):
+ raise AssertionError("unexpected #line directive "
+ "(should have been processed and removed")
+ return line_directives[int(s[6:])]
+ return _r_line_directive.sub(replace, csource)
+
def _preprocess(csource):
- # First, remove the lines of the form '#line N "filename"' because
- # the "filename" part could confuse the rest
- csource, line_directives = _remove_line_directives(csource)
+ # First, remove the lines of the form '#line N "filename"' because
+ # the "filename" part could confuse the rest
+ csource, line_directives = _remove_line_directives(csource)
# Remove comments. NOTE: this only work because the cdef() section
- # should not contain any string literals (except in line directives)!
- def replace_keeping_newlines(m):
- return ' ' + m.group().count('\n') * '\n'
- csource = _r_comment.sub(replace_keeping_newlines, csource)
+ # should not contain any string literals (except in line directives)!
+ def replace_keeping_newlines(m):
+ return ' ' + m.group().count('\n') * '\n'
+ csource = _r_comment.sub(replace_keeping_newlines, csource)
# Remove the "#define FOO x" lines
macros = {}
for match in _r_define.finditer(csource):
@@ -218,9 +218,9 @@ def _preprocess(csource):
# Replace `extern "Python"` with start/end markers
csource = _preprocess_extern_python(csource)
#
- # Now there should not be any string literal left; warn if we get one
- _warn_for_string_literal(csource)
- #
+ # Now there should not be any string literal left; warn if we get one
+ _warn_for_string_literal(csource)
+ #
# Replace "[...]" with "[__dotdotdotarray__]"
csource = _r_partial_array.sub('[__dotdotdotarray__]', csource)
#
@@ -241,16 +241,16 @@ def _preprocess(csource):
assert csource[p:p+3] == '...'
csource = '%s __dotdotdot%d__ %s' % (csource[:p], number,
csource[p+3:])
- # Replace "int ..." or "unsigned long int..." with "__dotdotdotint__"
- csource = _r_int_dotdotdot.sub(' __dotdotdotint__ ', csource)
- # Replace "float ..." or "double..." with "__dotdotdotfloat__"
- csource = _r_float_dotdotdot.sub(' __dotdotdotfloat__ ', csource)
+ # Replace "int ..." or "unsigned long int..." with "__dotdotdotint__"
+ csource = _r_int_dotdotdot.sub(' __dotdotdotint__ ', csource)
+ # Replace "float ..." or "double..." with "__dotdotdotfloat__"
+ csource = _r_float_dotdotdot.sub(' __dotdotdotfloat__ ', csource)
# Replace all remaining "..." with the same name, "__dotdotdot__",
# which is declared with a typedef for the purpose of C parsing.
- csource = csource.replace('...', ' __dotdotdot__ ')
- # Finally, put back the line directives
- csource = _put_back_line_directives(csource, line_directives)
- return csource, macros
+ csource = csource.replace('...', ' __dotdotdot__ ')
+ # Finally, put back the line directives
+ csource = _put_back_line_directives(csource, line_directives)
+ return csource, macros
def _common_type_names(csource):
# Look in the source for what looks like usages of types from the
@@ -319,21 +319,21 @@ class Parser(object):
ctn.discard(name)
typenames += sorted(ctn)
#
- csourcelines = []
- csourcelines.append('# 1 "<cdef automatic initialization code>"')
- for typename in typenames:
- csourcelines.append('typedef int %s;' % typename)
- csourcelines.append('typedef int __dotdotdotint__, __dotdotdotfloat__,'
- ' __dotdotdot__;')
- # this forces pycparser to consider the following in the file
- # called <cdef source string> from line 1
- csourcelines.append('# 1 "%s"' % (CDEF_SOURCE_STRING,))
+ csourcelines = []
+ csourcelines.append('# 1 "<cdef automatic initialization code>"')
+ for typename in typenames:
+ csourcelines.append('typedef int %s;' % typename)
+ csourcelines.append('typedef int __dotdotdotint__, __dotdotdotfloat__,'
+ ' __dotdotdot__;')
+ # this forces pycparser to consider the following in the file
+ # called <cdef source string> from line 1
+ csourcelines.append('# 1 "%s"' % (CDEF_SOURCE_STRING,))
csourcelines.append(csource)
- fullcsource = '\n'.join(csourcelines)
+ fullcsource = '\n'.join(csourcelines)
if lock is not None:
lock.acquire() # pycparser is not thread-safe...
try:
- ast = _get_parser().parse(fullcsource)
+ ast = _get_parser().parse(fullcsource)
except pycparser.c_parser.ParseError as e:
self.convert_pycparser_error(e, csource)
finally:
@@ -343,17 +343,17 @@ class Parser(object):
return ast, macros, csource
def _convert_pycparser_error(self, e, csource):
- # xxx look for "<cdef source string>:NUM:" at the start of str(e)
- # and interpret that as a line number. This will not work if
- # the user gives explicit ``# NUM "FILE"`` directives.
+ # xxx look for "<cdef source string>:NUM:" at the start of str(e)
+ # and interpret that as a line number. This will not work if
+ # the user gives explicit ``# NUM "FILE"`` directives.
line = None
msg = str(e)
- match = re.match(r"%s:(\d+):" % (CDEF_SOURCE_STRING,), msg)
- if match:
- linenum = int(match.group(1), 10)
- csourcelines = csource.splitlines()
- if 1 <= linenum <= len(csourcelines):
- line = csourcelines[linenum-1]
+ match = re.match(r"%s:(\d+):" % (CDEF_SOURCE_STRING,), msg)
+ if match:
+ linenum = int(match.group(1), 10)
+ csourcelines = csource.splitlines()
+ if 1 <= linenum <= len(csourcelines):
+ line = csourcelines[linenum-1]
return line
def convert_pycparser_error(self, e, csource):
@@ -364,27 +364,27 @@ class Parser(object):
msg = 'cannot parse "%s"\n%s' % (line.strip(), msg)
else:
msg = 'parse error\n%s' % (msg,)
- raise CDefError(msg)
-
- def parse(self, csource, override=False, packed=False, pack=None,
- dllexport=False):
- if packed:
- if packed != True:
- raise ValueError("'packed' should be False or True; use "
- "'pack' to give another value")
- if pack:
- raise ValueError("cannot give both 'pack' and 'packed'")
- pack = 1
- elif pack:
- if pack & (pack - 1):
- raise ValueError("'pack' must be a power of two, not %r" %
- (pack,))
- else:
- pack = 0
+ raise CDefError(msg)
+
+ def parse(self, csource, override=False, packed=False, pack=None,
+ dllexport=False):
+ if packed:
+ if packed != True:
+ raise ValueError("'packed' should be False or True; use "
+ "'pack' to give another value")
+ if pack:
+ raise ValueError("cannot give both 'pack' and 'packed'")
+ pack = 1
+ elif pack:
+ if pack & (pack - 1):
+ raise ValueError("'pack' must be a power of two, not %r" %
+ (pack,))
+ else:
+ pack = 0
prev_options = self._options
try:
self._options = {'override': override,
- 'packed': pack,
+ 'packed': pack,
'dllexport': dllexport}
self._internal_parse(csource)
finally:
@@ -400,46 +400,46 @@ class Parser(object):
for decl in iterator:
if decl.name == '__dotdotdot__':
break
- else:
- assert 0
- current_decl = None
+ else:
+ assert 0
+ current_decl = None
#
try:
self._inside_extern_python = '__cffi_extern_python_stop'
for decl in iterator:
- current_decl = decl
+ current_decl = decl
if isinstance(decl, pycparser.c_ast.Decl):
self._parse_decl(decl)
elif isinstance(decl, pycparser.c_ast.Typedef):
if not decl.name:
- raise CDefError("typedef does not declare any name",
- decl)
+ raise CDefError("typedef does not declare any name",
+ decl)
quals = 0
- if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType) and
- decl.type.type.names[-1].startswith('__dotdotdot')):
+ if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType) and
+ decl.type.type.names[-1].startswith('__dotdotdot')):
realtype = self._get_unknown_type(decl)
elif (isinstance(decl.type, pycparser.c_ast.PtrDecl) and
isinstance(decl.type.type, pycparser.c_ast.TypeDecl) and
isinstance(decl.type.type.type,
pycparser.c_ast.IdentifierType) and
- decl.type.type.type.names[-1].startswith('__dotdotdot')):
- realtype = self._get_unknown_ptr_type(decl)
+ decl.type.type.type.names[-1].startswith('__dotdotdot')):
+ realtype = self._get_unknown_ptr_type(decl)
else:
realtype, quals = self._get_type_and_quals(
- decl.type, name=decl.name, partial_length_ok=True,
- typedef_example="*(%s *)0" % (decl.name,))
+ decl.type, name=decl.name, partial_length_ok=True,
+ typedef_example="*(%s *)0" % (decl.name,))
self._declare('typedef ' + decl.name, realtype, quals=quals)
elif decl.__class__.__name__ == 'Pragma':
pass # skip pragma, only in pycparser 2.15
else:
- raise CDefError("unexpected <%s>: this construct is valid "
- "C but not valid in cdef()" %
- decl.__class__.__name__, decl)
- except CDefError as e:
- if len(e.args) == 1:
- e.args = e.args + (current_decl,)
- raise
- except FFIError as e:
+ raise CDefError("unexpected <%s>: this construct is valid "
+ "C but not valid in cdef()" %
+ decl.__class__.__name__, decl)
+ except CDefError as e:
+ if len(e.args) == 1:
+ e.args = e.args + (current_decl,)
+ raise
+ except FFIError as e:
msg = self._convert_pycparser_error(e, csource)
if msg:
e.args = (e.args[0] + "\n *** Err: %s" % msg,)
@@ -449,7 +449,7 @@ class Parser(object):
if key in self._int_constants:
if self._int_constants[key] == val:
return # ignore identical double declarations
- raise FFIError(
+ raise FFIError(
"multiple declarations of constant: %s" % (key,))
self._int_constants[key] = val
@@ -476,7 +476,7 @@ class Parser(object):
elif value == '...':
self._declare('macro ' + key, value)
else:
- raise CDefError(
+ raise CDefError(
'only supports one of the following syntax:\n'
' #define %s ... (literally dot-dot-dot)\n'
' #define %s NUMBER (with NUMBER an integer'
@@ -511,8 +511,8 @@ class Parser(object):
elif isinstance(node, pycparser.c_ast.Enum):
self._get_struct_union_enum_type('enum', node)
elif not decl.name:
- raise CDefError("construct does not declare any variable",
- decl)
+ raise CDefError("construct does not declare any variable",
+ decl)
#
if decl.name:
tp, quals = self._get_type_and_quals(node,
@@ -539,13 +539,13 @@ class Parser(object):
self._inside_extern_python = decl.name
else:
if self._inside_extern_python !='__cffi_extern_python_stop':
- raise CDefError(
+ raise CDefError(
"cannot declare constants or "
"variables with 'extern \"Python\"'")
if (quals & model.Q_CONST) and not tp.is_array_type:
self._declare('constant ' + decl.name, tp, quals=quals)
else:
- _warn_for_non_extern_non_static_global_variable(decl)
+ _warn_for_non_extern_non_static_global_variable(decl)
self._declare('variable ' + decl.name, tp, quals=quals)
def parse_type(self, cdecl):
@@ -556,7 +556,7 @@ class Parser(object):
assert not macros
exprnode = ast.ext[-1].type.args.params[0]
if isinstance(exprnode, pycparser.c_ast.ID):
- raise CDefError("unknown identifier '%s'" % (exprnode.name,))
+ raise CDefError("unknown identifier '%s'" % (exprnode.name,))
return self._get_type_and_quals(exprnode.type)
def _declare(self, name, obj, included=False, quals=0):
@@ -565,7 +565,7 @@ class Parser(object):
if prevobj is obj and prevquals == quals:
return
if not self._options.get('override'):
- raise FFIError(
+ raise FFIError(
"multiple declarations of %s (for interactive usage, "
"try cdef(xx, override=True))" % (name,))
assert '__dotdotdot__' not in name.split()
@@ -594,8 +594,8 @@ class Parser(object):
return model.NamedPointerType(type, declname, quals)
return model.PointerType(type, quals)
- def _get_type_and_quals(self, typenode, name=None, partial_length_ok=False,
- typedef_example=None):
+ def _get_type_and_quals(self, typenode, name=None, partial_length_ok=False,
+ typedef_example=None):
# first, dereference typedefs, if we have it already parsed, we're good
if (isinstance(typenode, pycparser.c_ast.TypeDecl) and
isinstance(typenode.type, pycparser.c_ast.IdentifierType) and
@@ -612,18 +612,18 @@ class Parser(object):
else:
length = self._parse_constant(
typenode.dim, partial_length_ok=partial_length_ok)
- # a hack: in 'typedef int foo_t[...][...];', don't use '...' as
- # the length but use directly the C expression that would be
- # generated by recompiler.py. This lets the typedef be used in
- # many more places within recompiler.py
- if typedef_example is not None:
- if length == '...':
- length = '_cffi_array_len(%s)' % (typedef_example,)
- typedef_example = "*" + typedef_example
- #
+ # a hack: in 'typedef int foo_t[...][...];', don't use '...' as
+ # the length but use directly the C expression that would be
+ # generated by recompiler.py. This lets the typedef be used in
+ # many more places within recompiler.py
+ if typedef_example is not None:
+ if length == '...':
+ length = '_cffi_array_len(%s)' % (typedef_example,)
+ typedef_example = "*" + typedef_example
+ #
tp, quals = self._get_type_and_quals(typenode.type,
- partial_length_ok=partial_length_ok,
- typedef_example=typedef_example)
+ partial_length_ok=partial_length_ok,
+ typedef_example=typedef_example)
return model.ArrayType(tp, length), quals
#
if isinstance(typenode, pycparser.c_ast.PtrDecl):
@@ -664,7 +664,7 @@ class Parser(object):
if ident == 'void':
return model.void_type, quals
if ident == '__dotdotdot__':
- raise FFIError(':%d: bad usage of "..."' %
+ raise FFIError(':%d: bad usage of "..."' %
typenode.coord.line)
tp0, quals0 = resolve_common_type(self, ident)
return tp0, (quals | quals0)
@@ -696,14 +696,14 @@ class Parser(object):
return self._get_struct_union_enum_type('union', typenode, name,
nested=True), 0
#
- raise FFIError(":%d: bad or unsupported type declaration" %
+ raise FFIError(":%d: bad or unsupported type declaration" %
typenode.coord.line)
def _parse_function_type(self, typenode, funcname=None):
params = list(getattr(typenode.args, 'params', []))
for i, arg in enumerate(params):
if not hasattr(arg, 'type'):
- raise CDefError("%s arg %d: unknown type '%s'"
+ raise CDefError("%s arg %d: unknown type '%s'"
" (if you meant to use the old C syntax of giving"
" untyped arguments, it is not supported)"
% (funcname or 'in expression', i + 1,
@@ -717,7 +717,7 @@ class Parser(object):
if ellipsis:
params.pop()
if not params:
- raise CDefError(
+ raise CDefError(
"%s: a function with only '(...)' as argument"
" is not correct C" % (funcname or 'in expression'))
args = [self._as_func_arg(*self._get_type_and_quals(argdeclnode.type))
@@ -818,7 +818,7 @@ class Parser(object):
return tp
#
if tp.fldnames is not None:
- raise CDefError("duplicate declaration of struct %s" % name)
+ raise CDefError("duplicate declaration of struct %s" % name)
fldnames = []
fldtypes = []
fldbitsize = []
@@ -862,7 +862,7 @@ class Parser(object):
def _make_partial(self, tp, nested):
if not isinstance(tp, model.StructOrUnion):
- raise CDefError("%s cannot be partial" % (tp,))
+ raise CDefError("%s cannot be partial" % (tp,))
if not tp.has_c_name() and not nested:
raise NotImplementedError("%s is partial but has no C name" %(tp,))
tp.partial = True
@@ -872,25 +872,25 @@ class Parser(object):
# or positive/negative number
if isinstance(exprnode, pycparser.c_ast.Constant):
s = exprnode.value
- if '0' <= s[0] <= '9':
- s = s.rstrip('uUlL')
- try:
- if s.startswith('0'):
- return int(s, 8)
- else:
- return int(s, 10)
- except ValueError:
- if len(s) > 1:
- if s.lower()[0:2] == '0x':
- return int(s, 16)
- elif s.lower()[0:2] == '0b':
- return int(s, 2)
- raise CDefError("invalid constant %r" % (s,))
+ if '0' <= s[0] <= '9':
+ s = s.rstrip('uUlL')
+ try:
+ if s.startswith('0'):
+ return int(s, 8)
+ else:
+ return int(s, 10)
+ except ValueError:
+ if len(s) > 1:
+ if s.lower()[0:2] == '0x':
+ return int(s, 16)
+ elif s.lower()[0:2] == '0b':
+ return int(s, 2)
+ raise CDefError("invalid constant %r" % (s,))
elif s[0] == "'" and s[-1] == "'" and (
len(s) == 3 or (len(s) == 4 and s[1] == "\\")):
return ord(s[-2])
else:
- raise CDefError("invalid constant %r" % (s,))
+ raise CDefError("invalid constant %r" % (s,))
#
if (isinstance(exprnode, pycparser.c_ast.UnaryOp) and
exprnode.op == '+'):
@@ -909,43 +909,43 @@ class Parser(object):
if partial_length_ok:
self._partial_length = True
return '...'
- raise FFIError(":%d: unsupported '[...]' here, cannot derive "
- "the actual array length in this context"
- % exprnode.coord.line)
+ raise FFIError(":%d: unsupported '[...]' here, cannot derive "
+ "the actual array length in this context"
+ % exprnode.coord.line)
#
- if isinstance(exprnode, pycparser.c_ast.BinaryOp):
- left = self._parse_constant(exprnode.left)
- right = self._parse_constant(exprnode.right)
- if exprnode.op == '+':
- return left + right
- elif exprnode.op == '-':
- return left - right
- elif exprnode.op == '*':
- return left * right
- elif exprnode.op == '/':
- return self._c_div(left, right)
- elif exprnode.op == '%':
- return left - self._c_div(left, right) * right
- elif exprnode.op == '<<':
- return left << right
- elif exprnode.op == '>>':
- return left >> right
- elif exprnode.op == '&':
- return left & right
- elif exprnode.op == '|':
- return left | right
- elif exprnode.op == '^':
- return left ^ right
- #
- raise FFIError(":%d: unsupported expression: expected a "
- "simple numeric constant" % exprnode.coord.line)
-
- def _c_div(self, a, b):
- result = a // b
- if ((a < 0) ^ (b < 0)) and (a % b) != 0:
- result += 1
- return result
-
+ if isinstance(exprnode, pycparser.c_ast.BinaryOp):
+ left = self._parse_constant(exprnode.left)
+ right = self._parse_constant(exprnode.right)
+ if exprnode.op == '+':
+ return left + right
+ elif exprnode.op == '-':
+ return left - right
+ elif exprnode.op == '*':
+ return left * right
+ elif exprnode.op == '/':
+ return self._c_div(left, right)
+ elif exprnode.op == '%':
+ return left - self._c_div(left, right) * right
+ elif exprnode.op == '<<':
+ return left << right
+ elif exprnode.op == '>>':
+ return left >> right
+ elif exprnode.op == '&':
+ return left & right
+ elif exprnode.op == '|':
+ return left | right
+ elif exprnode.op == '^':
+ return left ^ right
+ #
+ raise FFIError(":%d: unsupported expression: expected a "
+ "simple numeric constant" % exprnode.coord.line)
+
+ def _c_div(self, a, b):
+ result = a // b
+ if ((a < 0) ^ (b < 0)) and (a % b) != 0:
+ result += 1
+ return result
+
def _build_enum_type(self, explicit_name, decls):
if decls is not None:
partial = False
@@ -982,25 +982,25 @@ class Parser(object):
def _get_unknown_type(self, decl):
typenames = decl.type.type.names
- if typenames == ['__dotdotdot__']:
+ if typenames == ['__dotdotdot__']:
return model.unknown_type(decl.name)
- if typenames == ['__dotdotdotint__']:
- if self._uses_new_feature is None:
- self._uses_new_feature = "'typedef int... %s'" % decl.name
- return model.UnknownIntegerType(decl.name)
-
- if typenames == ['__dotdotdotfloat__']:
- # note: not for 'long double' so far
- if self._uses_new_feature is None:
- self._uses_new_feature = "'typedef float... %s'" % decl.name
- return model.UnknownFloatType(decl.name)
-
- raise FFIError(':%d: unsupported usage of "..." in typedef'
- % decl.coord.line)
-
- def _get_unknown_ptr_type(self, decl):
- if decl.type.type.type.names == ['__dotdotdot__']:
- return model.unknown_ptr_type(decl.name)
- raise FFIError(':%d: unsupported usage of "..." in typedef'
- % decl.coord.line)
+ if typenames == ['__dotdotdotint__']:
+ if self._uses_new_feature is None:
+ self._uses_new_feature = "'typedef int... %s'" % decl.name
+ return model.UnknownIntegerType(decl.name)
+
+ if typenames == ['__dotdotdotfloat__']:
+ # note: not for 'long double' so far
+ if self._uses_new_feature is None:
+ self._uses_new_feature = "'typedef float... %s'" % decl.name
+ return model.UnknownFloatType(decl.name)
+
+ raise FFIError(':%d: unsupported usage of "..." in typedef'
+ % decl.coord.line)
+
+ def _get_unknown_ptr_type(self, decl):
+ if decl.type.type.type.names == ['__dotdotdot__']:
+ return model.unknown_ptr_type(decl.name)
+ raise FFIError(':%d: unsupported usage of "..." in typedef'
+ % decl.coord.line)
diff --git a/contrib/python/cffi/cffi/error.py b/contrib/python/cffi/cffi/error.py
index 39eaf4d908..0a27247c32 100644
--- a/contrib/python/cffi/cffi/error.py
+++ b/contrib/python/cffi/cffi/error.py
@@ -1,31 +1,31 @@
-
-class FFIError(Exception):
- __module__ = 'cffi'
-
-class CDefError(Exception):
- __module__ = 'cffi'
- def __str__(self):
- try:
- current_decl = self.args[1]
- filename = current_decl.coord.file
- linenum = current_decl.coord.line
- prefix = '%s:%d: ' % (filename, linenum)
- except (AttributeError, TypeError, IndexError):
- prefix = ''
- return '%s%s' % (prefix, self.args[0])
-
-class VerificationError(Exception):
- """ An error raised when verification fails
- """
- __module__ = 'cffi'
-
-class VerificationMissing(Exception):
- """ An error raised when incomplete structures are passed into
- cdef, but no verification has been done
- """
- __module__ = 'cffi'
-
-class PkgConfigError(Exception):
- """ An error raised for missing modules in pkg-config
- """
- __module__ = 'cffi'
+
+class FFIError(Exception):
+ __module__ = 'cffi'
+
+class CDefError(Exception):
+ __module__ = 'cffi'
+ def __str__(self):
+ try:
+ current_decl = self.args[1]
+ filename = current_decl.coord.file
+ linenum = current_decl.coord.line
+ prefix = '%s:%d: ' % (filename, linenum)
+ except (AttributeError, TypeError, IndexError):
+ prefix = ''
+ return '%s%s' % (prefix, self.args[0])
+
+class VerificationError(Exception):
+ """ An error raised when verification fails
+ """
+ __module__ = 'cffi'
+
+class VerificationMissing(Exception):
+ """ An error raised when incomplete structures are passed into
+ cdef, but no verification has been done
+ """
+ __module__ = 'cffi'
+
+class PkgConfigError(Exception):
+ """ An error raised for missing modules in pkg-config
+ """
+ __module__ = 'cffi'
diff --git a/contrib/python/cffi/cffi/ffiplatform.py b/contrib/python/cffi/cffi/ffiplatform.py
index 621edf4431..85313460a6 100644
--- a/contrib/python/cffi/cffi/ffiplatform.py
+++ b/contrib/python/cffi/cffi/ffiplatform.py
@@ -1,12 +1,12 @@
import sys, os
-from .error import VerificationError
+from .error import VerificationError
LIST_OF_FILE_NAMES = ['sources', 'include_dirs', 'library_dirs',
'extra_objects', 'depends']
def get_extension(srcfilename, modname, sources=(), **kwds):
- _hack_at_distutils()
+ _hack_at_distutils()
from distutils.core import Extension
allsources = [srcfilename]
for src in sources:
@@ -16,7 +16,7 @@ def get_extension(srcfilename, modname, sources=(), **kwds):
def compile(tmpdir, ext, compiler_verbose=0, debug=None):
"""Compile a C extension module using distutils."""
- _hack_at_distutils()
+ _hack_at_distutils()
saved_environ = os.environ.copy()
try:
outputfilename = _build(tmpdir, ext, compiler_verbose, debug)
@@ -115,13 +115,13 @@ def flatten(x):
f = cStringIO.StringIO()
_flatten(x, f)
return f.getvalue()
-
-def _hack_at_distutils():
- # Windows-only workaround for some configurations: see
- # https://bugs.python.org/issue23246 (Python 2.7 with
- # a specific MS compiler suite download)
- if sys.platform == "win32":
- try:
- import setuptools # for side-effects, patches distutils
- except ImportError:
- pass
+
+def _hack_at_distutils():
+ # Windows-only workaround for some configurations: see
+ # https://bugs.python.org/issue23246 (Python 2.7 with
+ # a specific MS compiler suite download)
+ if sys.platform == "win32":
+ try:
+ import setuptools # for side-effects, patches distutils
+ except ImportError:
+ pass
diff --git a/contrib/python/cffi/cffi/model.py b/contrib/python/cffi/cffi/model.py
index b91684c395..ad1c176489 100644
--- a/contrib/python/cffi/cffi/model.py
+++ b/contrib/python/cffi/cffi/model.py
@@ -1,8 +1,8 @@
-import types
+import types
import weakref
from .lock import allocate_lock
-from .error import CDefError, VerificationError, VerificationMissing
+from .error import CDefError, VerificationError, VerificationMissing
# type qualifiers
Q_CONST = 0x01
@@ -95,8 +95,8 @@ void_type = VoidType()
class BasePrimitiveType(BaseType):
- def is_complex_type(self):
- return False
+ def is_complex_type(self):
+ return False
class PrimitiveType(BasePrimitiveType):
@@ -117,13 +117,13 @@ class PrimitiveType(BasePrimitiveType):
'float': 'f',
'double': 'f',
'long double': 'f',
- 'float _Complex': 'j',
- 'double _Complex': 'j',
+ 'float _Complex': 'j',
+ 'double _Complex': 'j',
'_Bool': 'i',
# the following types are not primitive in the C sense
'wchar_t': 'c',
- 'char16_t': 'c',
- 'char32_t': 'c',
+ 'char16_t': 'c',
+ 'char32_t': 'c',
'int8_t': 'i',
'uint8_t': 'i',
'int16_t': 'i',
@@ -168,8 +168,8 @@ class PrimitiveType(BasePrimitiveType):
return self.ALL_PRIMITIVE_TYPES[self.name] == 'i'
def is_float_type(self):
return self.ALL_PRIMITIVE_TYPES[self.name] == 'f'
- def is_complex_type(self):
- return self.ALL_PRIMITIVE_TYPES[self.name] == 'j'
+ def is_complex_type(self):
+ return self.ALL_PRIMITIVE_TYPES[self.name] == 'j'
def build_backend_type(self, ffi, finishlist):
return global_cache(self, ffi, 'new_primitive_type', self.name)
@@ -229,8 +229,8 @@ class RawFunctionType(BaseFunctionType):
is_raw_function = True
def build_backend_type(self, ffi, finishlist):
- raise CDefError("cannot render the type %r: it is a function "
- "type, not a pointer-to-function type" % (self,))
+ raise CDefError("cannot render the type %r: it is a function "
+ "type, not a pointer-to-function type" % (self,))
def as_function_pointer(self):
return FunctionPtrType(self.args, self.result, self.ellipsis, self.abi)
@@ -307,16 +307,16 @@ class ArrayType(BaseType):
self.c_name_with_marker = (
self.item.c_name_with_marker.replace('&', brackets))
- def length_is_unknown(self):
- return isinstance(self.length, str)
-
+ def length_is_unknown(self):
+ return isinstance(self.length, str)
+
def resolve_length(self, newlength):
return ArrayType(self.item, newlength)
def build_backend_type(self, ffi, finishlist):
- if self.length_is_unknown():
- raise CDefError("cannot render the type %r: unknown length" %
- (self,))
+ if self.length_is_unknown():
+ raise CDefError("cannot render the type %r: unknown length" %
+ (self,))
self.item.get_cached_btype(ffi, finishlist) # force the item BType
BPtrItem = PointerType(self.item).get_cached_btype(ffi, finishlist)
return global_cache(self, ffi, 'new_array_type', BPtrItem, self.length)
@@ -345,7 +345,7 @@ class StructOrUnion(StructOrUnionOrEnum):
fixedlayout = None
completed = 0
partial = False
- packed = 0
+ packed = 0
def __init__(self, name, fldnames, fldtypes, fldbitsize, fldquals=None):
self.name = name
@@ -355,20 +355,20 @@ class StructOrUnion(StructOrUnionOrEnum):
self.fldquals = fldquals
self.build_c_name_with_marker()
- def anonymous_struct_fields(self):
- if self.fldtypes is not None:
- for name, type in zip(self.fldnames, self.fldtypes):
- if name == '' and isinstance(type, StructOrUnion):
- yield type
+ def anonymous_struct_fields(self):
+ if self.fldtypes is not None:
+ for name, type in zip(self.fldnames, self.fldtypes):
+ if name == '' and isinstance(type, StructOrUnion):
+ yield type
- def enumfields(self, expand_anonymous_struct_union=True):
+ def enumfields(self, expand_anonymous_struct_union=True):
fldquals = self.fldquals
if fldquals is None:
fldquals = (0,) * len(self.fldnames)
for name, type, bitsize, quals in zip(self.fldnames, self.fldtypes,
self.fldbitsize, fldquals):
- if (name == '' and isinstance(type, StructOrUnion)
- and expand_anonymous_struct_union):
+ if (name == '' and isinstance(type, StructOrUnion)
+ and expand_anonymous_struct_union):
# nested anonymous struct/union
for result in type.enumfields():
yield result
@@ -417,14 +417,14 @@ class StructOrUnion(StructOrUnionOrEnum):
fldtypes = [tp.get_cached_btype(ffi, finishlist)
for tp in self.fldtypes]
lst = list(zip(self.fldnames, fldtypes, self.fldbitsize))
- extra_flags = ()
+ extra_flags = ()
if self.packed:
- if self.packed == 1:
- extra_flags = (8,) # SF_PACKED
- else:
- extra_flags = (0, self.packed)
+ if self.packed == 1:
+ extra_flags = (8,) # SF_PACKED
+ else:
+ extra_flags = (0, self.packed)
ffi._backend.complete_struct_or_union(BType, lst, self,
- -1, -1, *extra_flags)
+ -1, -1, *extra_flags)
#
else:
fldtypes = []
@@ -433,7 +433,7 @@ class StructOrUnion(StructOrUnionOrEnum):
fsize = fieldsize[i]
ftype = self.fldtypes[i]
#
- if isinstance(ftype, ArrayType) and ftype.length_is_unknown():
+ if isinstance(ftype, ArrayType) and ftype.length_is_unknown():
# fix the length to match the total size
BItemType = ftype.item.get_cached_btype(ffi, finishlist)
nlen, nrest = divmod(fsize, ffi.sizeof(BItemType))
@@ -468,7 +468,7 @@ class StructOrUnion(StructOrUnionOrEnum):
def check_not_partial(self):
if self.partial and self.fixedlayout is None:
- raise VerificationMissing(self._get_c_name())
+ raise VerificationMissing(self._get_c_name())
def build_backend_type(self, ffi, finishlist):
self.check_not_partial()
@@ -506,7 +506,7 @@ class EnumType(StructOrUnionOrEnum):
def check_not_partial(self):
if self.partial and not self.partial_resolved:
- raise VerificationMissing(self._get_c_name())
+ raise VerificationMissing(self._get_c_name())
def build_backend_type(self, ffi, finishlist):
self.check_not_partial()
@@ -554,8 +554,8 @@ class EnumType(StructOrUnionOrEnum):
if (smallest_value >= ((-1) << (8*size2-1)) and
largest_value < (1 << (8*size2-sign))):
return btype2
- raise CDefError("%s values don't all fit into either 'long' "
- "or 'unsigned long'" % self._get_c_name())
+ raise CDefError("%s values don't all fit into either 'long' "
+ "or 'unsigned long'" % self._get_c_name())
def unknown_type(name, structname=None):
if structname is None:
@@ -573,24 +573,24 @@ def unknown_ptr_type(name, structname=None):
global_lock = allocate_lock()
-_typecache_cffi_backend = weakref.WeakValueDictionary()
-
-def get_typecache(backend):
- # returns _typecache_cffi_backend if backend is the _cffi_backend
- # module, or type(backend).__typecache if backend is an instance of
- # CTypesBackend (or some FakeBackend class during tests)
- if isinstance(backend, types.ModuleType):
- return _typecache_cffi_backend
- with global_lock:
- if not hasattr(type(backend), '__typecache'):
- type(backend).__typecache = weakref.WeakValueDictionary()
- return type(backend).__typecache
-
+_typecache_cffi_backend = weakref.WeakValueDictionary()
+
+def get_typecache(backend):
+ # returns _typecache_cffi_backend if backend is the _cffi_backend
+ # module, or type(backend).__typecache if backend is an instance of
+ # CTypesBackend (or some FakeBackend class during tests)
+ if isinstance(backend, types.ModuleType):
+ return _typecache_cffi_backend
+ with global_lock:
+ if not hasattr(type(backend), '__typecache'):
+ type(backend).__typecache = weakref.WeakValueDictionary()
+ return type(backend).__typecache
+
def global_cache(srctype, ffi, funcname, *args, **kwds):
key = kwds.pop('key', (funcname, args))
assert not kwds
try:
- return ffi._typecache[key]
+ return ffi._typecache[key]
except KeyError:
pass
try:
@@ -600,7 +600,7 @@ def global_cache(srctype, ffi, funcname, *args, **kwds):
# note that setdefault() on WeakValueDictionary is not atomic
# and contains a rare bug (http://bugs.python.org/issue19542);
# we have to use a lock and do it ourselves
- cache = ffi._typecache
+ cache = ffi._typecache
with global_lock:
res1 = cache.get(key)
if res1 is None:
diff --git a/contrib/python/cffi/cffi/parse_c_type.h b/contrib/python/cffi/cffi/parse_c_type.h
index 176ed4679b..84e4ef8565 100644
--- a/contrib/python/cffi/cffi/parse_c_type.h
+++ b/contrib/python/cffi/cffi/parse_c_type.h
@@ -79,12 +79,12 @@ typedef void *_cffi_opcode_t;
#define _CFFI_PRIM_UINT_FAST64 45
#define _CFFI_PRIM_INTMAX 46
#define _CFFI_PRIM_UINTMAX 47
-#define _CFFI_PRIM_FLOATCOMPLEX 48
-#define _CFFI_PRIM_DOUBLECOMPLEX 49
-#define _CFFI_PRIM_CHAR16 50
-#define _CFFI_PRIM_CHAR32 51
+#define _CFFI_PRIM_FLOATCOMPLEX 48
+#define _CFFI_PRIM_DOUBLECOMPLEX 49
+#define _CFFI_PRIM_CHAR16 50
+#define _CFFI_PRIM_CHAR32 51
-#define _CFFI__NUM_PRIM 52
+#define _CFFI__NUM_PRIM 52
#define _CFFI__UNKNOWN_PRIM (-1)
#define _CFFI__UNKNOWN_FLOAT_PRIM (-2)
#define _CFFI__UNKNOWN_LONG_DOUBLE (-3)
diff --git a/contrib/python/cffi/cffi/pkgconfig.py b/contrib/python/cffi/cffi/pkgconfig.py
index ef687e5b3f..5c93f15a60 100644
--- a/contrib/python/cffi/cffi/pkgconfig.py
+++ b/contrib/python/cffi/cffi/pkgconfig.py
@@ -1,121 +1,121 @@
-# pkg-config, https://www.freedesktop.org/wiki/Software/pkg-config/ integration for cffi
-import sys, os, subprocess
-
-from .error import PkgConfigError
-
-
-def merge_flags(cfg1, cfg2):
- """Merge values from cffi config flags cfg2 to cf1
-
- Example:
- merge_flags({"libraries": ["one"]}, {"libraries": ["two"]})
- {"libraries": ["one", "two"]}
- """
- for key, value in cfg2.items():
- if key not in cfg1:
- cfg1[key] = value
- else:
- if not isinstance(cfg1[key], list):
- raise TypeError("cfg1[%r] should be a list of strings" % (key,))
- if not isinstance(value, list):
- raise TypeError("cfg2[%r] should be a list of strings" % (key,))
- cfg1[key].extend(value)
- return cfg1
-
-
-def call(libname, flag, encoding=sys.getfilesystemencoding()):
- """Calls pkg-config and returns the output if found
- """
- a = ["pkg-config", "--print-errors"]
- a.append(flag)
- a.append(libname)
- try:
- pc = subprocess.Popen(a, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- except EnvironmentError as e:
- raise PkgConfigError("cannot run pkg-config: %s" % (str(e).strip(),))
-
- bout, berr = pc.communicate()
- if pc.returncode != 0:
- try:
- berr = berr.decode(encoding)
- except Exception:
- pass
- raise PkgConfigError(berr.strip())
-
- if sys.version_info >= (3,) and not isinstance(bout, str): # Python 3.x
- try:
- bout = bout.decode(encoding)
- except UnicodeDecodeError:
- raise PkgConfigError("pkg-config %s %s returned bytes that cannot "
- "be decoded with encoding %r:\n%r" %
- (flag, libname, encoding, bout))
-
- if os.altsep != '\\' and '\\' in bout:
- raise PkgConfigError("pkg-config %s %s returned an unsupported "
- "backslash-escaped output:\n%r" %
- (flag, libname, bout))
- return bout
-
-
-def flags_from_pkgconfig(libs):
- r"""Return compiler line flags for FFI.set_source based on pkg-config output
-
- Usage
- ...
- ffibuilder.set_source("_foo", pkgconfig = ["libfoo", "libbar >= 1.8.3"])
-
- If pkg-config is installed on build machine, then arguments include_dirs,
- library_dirs, libraries, define_macros, extra_compile_args and
- extra_link_args are extended with an output of pkg-config for libfoo and
- libbar.
-
- Raises PkgConfigError in case the pkg-config call fails.
- """
-
- def get_include_dirs(string):
- return [x[2:] for x in string.split() if x.startswith("-I")]
-
- def get_library_dirs(string):
- return [x[2:] for x in string.split() if x.startswith("-L")]
-
- def get_libraries(string):
- return [x[2:] for x in string.split() if x.startswith("-l")]
-
- # convert -Dfoo=bar to list of tuples [("foo", "bar")] expected by distutils
- def get_macros(string):
- def _macro(x):
- x = x[2:] # drop "-D"
- if '=' in x:
- return tuple(x.split("=", 1)) # "-Dfoo=bar" => ("foo", "bar")
- else:
- return (x, None) # "-Dfoo" => ("foo", None)
- return [_macro(x) for x in string.split() if x.startswith("-D")]
-
- def get_other_cflags(string):
- return [x for x in string.split() if not x.startswith("-I") and
- not x.startswith("-D")]
-
- def get_other_libs(string):
- return [x for x in string.split() if not x.startswith("-L") and
- not x.startswith("-l")]
-
- # return kwargs for given libname
- def kwargs(libname):
- fse = sys.getfilesystemencoding()
- all_cflags = call(libname, "--cflags")
- all_libs = call(libname, "--libs")
- return {
- "include_dirs": get_include_dirs(all_cflags),
- "library_dirs": get_library_dirs(all_libs),
- "libraries": get_libraries(all_libs),
- "define_macros": get_macros(all_cflags),
- "extra_compile_args": get_other_cflags(all_cflags),
- "extra_link_args": get_other_libs(all_libs),
- }
-
- # merge all arguments together
- ret = {}
- for libname in libs:
- lib_flags = kwargs(libname)
- merge_flags(ret, lib_flags)
- return ret
+# pkg-config, https://www.freedesktop.org/wiki/Software/pkg-config/ integration for cffi
+import sys, os, subprocess
+
+from .error import PkgConfigError
+
+
+def merge_flags(cfg1, cfg2):
+ """Merge values from cffi config flags cfg2 to cf1
+
+ Example:
+ merge_flags({"libraries": ["one"]}, {"libraries": ["two"]})
+ {"libraries": ["one", "two"]}
+ """
+ for key, value in cfg2.items():
+ if key not in cfg1:
+ cfg1[key] = value
+ else:
+ if not isinstance(cfg1[key], list):
+ raise TypeError("cfg1[%r] should be a list of strings" % (key,))
+ if not isinstance(value, list):
+ raise TypeError("cfg2[%r] should be a list of strings" % (key,))
+ cfg1[key].extend(value)
+ return cfg1
+
+
+def call(libname, flag, encoding=sys.getfilesystemencoding()):
+ """Calls pkg-config and returns the output if found
+ """
+ a = ["pkg-config", "--print-errors"]
+ a.append(flag)
+ a.append(libname)
+ try:
+ pc = subprocess.Popen(a, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ except EnvironmentError as e:
+ raise PkgConfigError("cannot run pkg-config: %s" % (str(e).strip(),))
+
+ bout, berr = pc.communicate()
+ if pc.returncode != 0:
+ try:
+ berr = berr.decode(encoding)
+ except Exception:
+ pass
+ raise PkgConfigError(berr.strip())
+
+ if sys.version_info >= (3,) and not isinstance(bout, str): # Python 3.x
+ try:
+ bout = bout.decode(encoding)
+ except UnicodeDecodeError:
+ raise PkgConfigError("pkg-config %s %s returned bytes that cannot "
+ "be decoded with encoding %r:\n%r" %
+ (flag, libname, encoding, bout))
+
+ if os.altsep != '\\' and '\\' in bout:
+ raise PkgConfigError("pkg-config %s %s returned an unsupported "
+ "backslash-escaped output:\n%r" %
+ (flag, libname, bout))
+ return bout
+
+
+def flags_from_pkgconfig(libs):
+ r"""Return compiler line flags for FFI.set_source based on pkg-config output
+
+ Usage
+ ...
+ ffibuilder.set_source("_foo", pkgconfig = ["libfoo", "libbar >= 1.8.3"])
+
+ If pkg-config is installed on build machine, then arguments include_dirs,
+ library_dirs, libraries, define_macros, extra_compile_args and
+ extra_link_args are extended with an output of pkg-config for libfoo and
+ libbar.
+
+ Raises PkgConfigError in case the pkg-config call fails.
+ """
+
+ def get_include_dirs(string):
+ return [x[2:] for x in string.split() if x.startswith("-I")]
+
+ def get_library_dirs(string):
+ return [x[2:] for x in string.split() if x.startswith("-L")]
+
+ def get_libraries(string):
+ return [x[2:] for x in string.split() if x.startswith("-l")]
+
+ # convert -Dfoo=bar to list of tuples [("foo", "bar")] expected by distutils
+ def get_macros(string):
+ def _macro(x):
+ x = x[2:] # drop "-D"
+ if '=' in x:
+ return tuple(x.split("=", 1)) # "-Dfoo=bar" => ("foo", "bar")
+ else:
+ return (x, None) # "-Dfoo" => ("foo", None)
+ return [_macro(x) for x in string.split() if x.startswith("-D")]
+
+ def get_other_cflags(string):
+ return [x for x in string.split() if not x.startswith("-I") and
+ not x.startswith("-D")]
+
+ def get_other_libs(string):
+ return [x for x in string.split() if not x.startswith("-L") and
+ not x.startswith("-l")]
+
+ # return kwargs for given libname
+ def kwargs(libname):
+ fse = sys.getfilesystemencoding()
+ all_cflags = call(libname, "--cflags")
+ all_libs = call(libname, "--libs")
+ return {
+ "include_dirs": get_include_dirs(all_cflags),
+ "library_dirs": get_library_dirs(all_libs),
+ "libraries": get_libraries(all_libs),
+ "define_macros": get_macros(all_cflags),
+ "extra_compile_args": get_other_cflags(all_cflags),
+ "extra_link_args": get_other_libs(all_libs),
+ }
+
+ # merge all arguments together
+ ret = {}
+ for libname in libs:
+ lib_flags = kwargs(libname)
+ merge_flags(ret, lib_flags)
+ return ret
diff --git a/contrib/python/cffi/cffi/recompiler.py b/contrib/python/cffi/cffi/recompiler.py
index ae29cf9834..87203dcff8 100644
--- a/contrib/python/cffi/cffi/recompiler.py
+++ b/contrib/python/cffi/cffi/recompiler.py
@@ -1,16 +1,16 @@
import os, sys, io
from . import ffiplatform, model
-from .error import VerificationError
+from .error import VerificationError
from .cffi_opcode import *
-VERSION_BASE = 0x2601
-VERSION_EMBEDDED = 0x2701
-VERSION_CHAR16CHAR32 = 0x2801
+VERSION_BASE = 0x2601
+VERSION_EMBEDDED = 0x2701
+VERSION_CHAR16CHAR32 = 0x2801
+
+USE_LIMITED_API = (sys.platform != 'win32' or sys.version_info < (3, 0) or
+ sys.version_info >= (3, 5))
-USE_LIMITED_API = (sys.platform != 'win32' or sys.version_info < (3, 0) or
- sys.version_info >= (3, 5))
-
class GlobalExpr:
def __init__(self, name, address, type_op, size=0, check_value=0):
self.name = name
@@ -130,11 +130,11 @@ class Recompiler:
self.ffi = ffi
self.module_name = module_name
self.target_is_python = target_is_python
- self._version = VERSION_BASE
+ self._version = VERSION_BASE
+
+ def needs_version(self, ver):
+ self._version = max(self._version, ver)
- def needs_version(self, ver):
- self._version = max(self._version, ver)
-
def collect_type_table(self):
self._typesdict = {}
self._generate("collecttype")
@@ -193,17 +193,17 @@ class Recompiler:
assert isinstance(op, CffiOp)
self.cffi_types = tuple(self.cffi_types) # don't change any more
- def _enum_fields(self, tp):
- # When producing C, expand all anonymous struct/union fields.
- # That's necessary to have C code checking the offsets of the
- # individual fields contained in them. When producing Python,
- # don't do it and instead write it like it is, with the
- # corresponding fields having an empty name. Empty names are
- # recognized at runtime when we import the generated Python
- # file.
- expand_anonymous_struct_union = not self.target_is_python
- return tp.enumfields(expand_anonymous_struct_union)
-
+ def _enum_fields(self, tp):
+ # When producing C, expand all anonymous struct/union fields.
+ # That's necessary to have C code checking the offsets of the
+ # individual fields contained in them. When producing Python,
+ # don't do it and instead write it like it is, with the
+ # corresponding fields having an empty name. Empty names are
+ # recognized at runtime when we import the generated Python
+ # file.
+ expand_anonymous_struct_union = not self.target_is_python
+ return tp.enumfields(expand_anonymous_struct_union)
+
def _do_collect_type(self, tp):
if not isinstance(tp, model.BaseTypeByIdentity):
if isinstance(tp, tuple):
@@ -217,7 +217,7 @@ class Recompiler:
elif isinstance(tp, model.StructOrUnion):
if tp.fldtypes is not None and (
tp not in self.ffi._parser._included_declarations):
- for name1, tp1, _, _ in self._enum_fields(tp):
+ for name1, tp1, _, _ in self._enum_fields(tp):
self._do_collect_type(self._field_type(tp, name1, tp1))
else:
for _, x in tp._get_items():
@@ -231,7 +231,7 @@ class Recompiler:
method = getattr(self, '_generate_cpy_%s_%s' % (kind,
step_name))
except AttributeError:
- raise VerificationError(
+ raise VerificationError(
"not implemented in recompile(): %r" % name)
try:
self._current_quals = quals
@@ -288,15 +288,15 @@ class Recompiler:
def _rel_readlines(self, filename):
import pkgutil
- return pkgutil.get_data('cffi', filename).decode('utf-8').splitlines(True)
+ return pkgutil.get_data('cffi', filename).decode('utf-8').splitlines(True)
def write_c_source_to_f(self, f, preamble):
self._f = f
prnt = self._prnt
if self.ffi._embedding is not None:
prnt('#define _CFFI_USE_EMBEDDING')
- if not USE_LIMITED_API:
- prnt('#define _CFFI_NO_LIMITED_API')
+ if not USE_LIMITED_API:
+ prnt('#define _CFFI_NO_LIMITED_API')
#
# first the '#include' (actually done by inlining the file's content)
lines = self._rel_readlines('_cffi_include.h')
@@ -309,9 +309,9 @@ class Recompiler:
base_module_name = self.module_name.split('.')[-1]
if self.ffi._embedding is not None:
prnt('#define _CFFI_MODULE_NAME "%s"' % (self.module_name,))
- prnt('static const char _CFFI_PYTHON_STARTUP_CODE[] = {')
- self._print_string_literal_in_array(self.ffi._embedding)
- prnt('0 };')
+ prnt('static const char _CFFI_PYTHON_STARTUP_CODE[] = {')
+ self._print_string_literal_in_array(self.ffi._embedding)
+ prnt('0 };')
prnt('#ifdef PYPY_VERSION')
prnt('# define _CFFI_PYTHON_STARTUP_FUNC _cffi_pypyinit_%s' % (
base_module_name,))
@@ -323,10 +323,10 @@ class Recompiler:
base_module_name,))
prnt('#endif')
lines = self._rel_readlines('_embedding.h')
- i = lines.index('#include "_cffi_errors.h"\n')
- lines[i:i+1] = self._rel_readlines('_cffi_errors.h')
+ i = lines.index('#include "_cffi_errors.h"\n')
+ lines[i:i+1] = self._rel_readlines('_cffi_errors.h')
prnt(''.join(lines))
- self.needs_version(VERSION_EMBEDDED)
+ self.needs_version(VERSION_EMBEDDED)
#
# then paste the C source given by the user, verbatim.
prnt('/************************************************************/')
@@ -375,12 +375,12 @@ class Recompiler:
included_module_name, included_source = (
ffi_to_include._assigned_source[:2])
except AttributeError:
- raise VerificationError(
+ raise VerificationError(
"ffi object %r includes %r, but the latter has not "
"been prepared with set_source()" % (
self.ffi, ffi_to_include,))
if included_source is None:
- raise VerificationError(
+ raise VerificationError(
"not implemented yet: ffi.include() of a Python-based "
"ffi inside a C-based ffi")
prnt(' "%s",' % (included_module_name,))
@@ -412,10 +412,10 @@ class Recompiler:
prnt()
#
# the init function
- prnt('#ifdef __GNUC__')
- prnt('# pragma GCC visibility push(default) /* for -fvisibility= */')
- prnt('#endif')
- prnt()
+ prnt('#ifdef __GNUC__')
+ prnt('# pragma GCC visibility push(default) /* for -fvisibility= */')
+ prnt('#endif')
+ prnt()
prnt('#ifdef PYPY_VERSION')
prnt('PyMODINIT_FUNC')
prnt('_cffi_pypyinit_%s(const void *p[])' % (base_module_name,))
@@ -425,11 +425,11 @@ class Recompiler:
prnt(' _cffi_call_python_org = '
'(void(*)(struct _cffi_externpy_s *, char *))p[1];')
prnt(' }')
- prnt(' p[0] = (const void *)0x%x;' % self._version)
+ prnt(' p[0] = (const void *)0x%x;' % self._version)
prnt(' p[1] = &_cffi_type_context;')
- prnt('#if PY_MAJOR_VERSION >= 3')
- prnt(' return NULL;')
- prnt('#endif')
+ prnt('#if PY_MAJOR_VERSION >= 3')
+ prnt(' return NULL;')
+ prnt('#endif')
prnt('}')
# on Windows, distutils insists on putting init_cffi_xyz in
# 'export_symbols', so instead of fighting it, just give up and
@@ -446,22 +446,22 @@ class Recompiler:
prnt('PyMODINIT_FUNC')
prnt('PyInit_%s(void)' % (base_module_name,))
prnt('{')
- prnt(' return _cffi_init("%s", 0x%x, &_cffi_type_context);' % (
- self.module_name, self._version))
+ prnt(' return _cffi_init("%s", 0x%x, &_cffi_type_context);' % (
+ self.module_name, self._version))
prnt('}')
prnt('#else')
prnt('PyMODINIT_FUNC')
prnt('init%s(void)' % (base_module_name,))
prnt('{')
- prnt(' _cffi_init("%s", 0x%x, &_cffi_type_context);' % (
- self.module_name, self._version))
+ prnt(' _cffi_init("%s", 0x%x, &_cffi_type_context);' % (
+ self.module_name, self._version))
prnt('}')
prnt('#endif')
- prnt()
- prnt('#ifdef __GNUC__')
- prnt('# pragma GCC visibility pop')
- prnt('#endif')
- self._version = None
+ prnt()
+ prnt('#ifdef __GNUC__')
+ prnt('# pragma GCC visibility pop')
+ prnt('#endif')
+ self._version = None
def _to_py(self, x):
if isinstance(x, str):
@@ -489,19 +489,19 @@ class Recompiler:
included_module_name, included_source = (
ffi_to_include._assigned_source[:2])
except AttributeError:
- raise VerificationError(
+ raise VerificationError(
"ffi object %r includes %r, but the latter has not "
"been prepared with set_source()" % (
self.ffi, ffi_to_include,))
if included_source is not None:
- raise VerificationError(
+ raise VerificationError(
"not implemented yet: ffi.include() of a C-based "
"ffi inside a Python-based ffi")
prnt('from %s import ffi as _ffi%d' % (included_module_name, i))
prnt()
prnt("ffi = _cffi_backend.FFI('%s'," % (self.module_name,))
- prnt(" _version = 0x%x," % (self._version,))
- self._version = None
+ prnt(" _version = 0x%x," % (self._version,))
+ self._version = None
#
# the '_types' keyword argument
self.cffi_types = tuple(self.cffi_types) # don't change any more
@@ -531,7 +531,7 @@ class Recompiler:
def _convert_funcarg_to_c(self, tp, fromvar, tovar, errcode):
extraarg = ''
- if isinstance(tp, model.BasePrimitiveType) and not tp.is_complex_type():
+ if isinstance(tp, model.BasePrimitiveType) and not tp.is_complex_type():
if tp.is_integer_type() and tp.name != '_Bool':
converter = '_cffi_to_c_int'
extraarg = ', %s' % tp.name
@@ -540,11 +540,11 @@ class Recompiler:
# double' here, and _cffi_to_c_double would loose precision
converter = '(%s)_cffi_to_c_double' % (tp.get_c_name(''),)
else:
- cname = tp.get_c_name('')
- converter = '(%s)_cffi_to_c_%s' % (cname,
+ cname = tp.get_c_name('')
+ converter = '(%s)_cffi_to_c_%s' % (cname,
tp.name.replace(' ', '_'))
- if cname in ('char16_t', 'char32_t'):
- self.needs_version(VERSION_CHAR16CHAR32)
+ if cname in ('char16_t', 'char32_t'):
+ self.needs_version(VERSION_CHAR16CHAR32)
errvalue = '-1'
#
elif isinstance(tp, model.PointerType):
@@ -552,10 +552,10 @@ class Recompiler:
tovar, errcode)
return
#
- elif (isinstance(tp, model.StructOrUnionOrEnum) or
- isinstance(tp, model.BasePrimitiveType)):
- # a struct (not a struct pointer) as a function argument;
- # or, a complex (the same code works)
+ elif (isinstance(tp, model.StructOrUnionOrEnum) or
+ isinstance(tp, model.BasePrimitiveType)):
+ # a struct (not a struct pointer) as a function argument;
+ # or, a complex (the same code works)
self._prnt(' if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)'
% (tovar, self._gettypenum(tp), fromvar))
self._prnt(' %s;' % errcode)
@@ -574,38 +574,38 @@ class Recompiler:
tovar, tp.get_c_name(''), errvalue))
self._prnt(' %s;' % errcode)
- def _extra_local_variables(self, tp, localvars, freelines):
+ def _extra_local_variables(self, tp, localvars, freelines):
if isinstance(tp, model.PointerType):
localvars.add('Py_ssize_t datasize')
- localvars.add('struct _cffi_freeme_s *large_args_free = NULL')
- freelines.add('if (large_args_free != NULL)'
- ' _cffi_free_array_arguments(large_args_free);')
+ localvars.add('struct _cffi_freeme_s *large_args_free = NULL')
+ freelines.add('if (large_args_free != NULL)'
+ ' _cffi_free_array_arguments(large_args_free);')
def _convert_funcarg_to_c_ptr_or_array(self, tp, fromvar, tovar, errcode):
self._prnt(' datasize = _cffi_prepare_pointer_call_argument(')
self._prnt(' _cffi_type(%d), %s, (char **)&%s);' % (
self._gettypenum(tp), fromvar, tovar))
self._prnt(' if (datasize != 0) {')
- self._prnt(' %s = ((size_t)datasize) <= 640 ? '
- '(%s)alloca((size_t)datasize) : NULL;' % (
+ self._prnt(' %s = ((size_t)datasize) <= 640 ? '
+ '(%s)alloca((size_t)datasize) : NULL;' % (
tovar, tp.get_c_name('')))
- self._prnt(' if (_cffi_convert_array_argument(_cffi_type(%d), %s, '
- '(char **)&%s,' % (self._gettypenum(tp), fromvar, tovar))
- self._prnt(' datasize, &large_args_free) < 0)')
+ self._prnt(' if (_cffi_convert_array_argument(_cffi_type(%d), %s, '
+ '(char **)&%s,' % (self._gettypenum(tp), fromvar, tovar))
+ self._prnt(' datasize, &large_args_free) < 0)')
self._prnt(' %s;' % errcode)
self._prnt(' }')
def _convert_expr_from_c(self, tp, var, context):
if isinstance(tp, model.BasePrimitiveType):
- if tp.is_integer_type() and tp.name != '_Bool':
+ if tp.is_integer_type() and tp.name != '_Bool':
return '_cffi_from_c_int(%s, %s)' % (var, tp.name)
elif isinstance(tp, model.UnknownFloatType):
return '_cffi_from_c_double(%s)' % (var,)
- elif tp.name != 'long double' and not tp.is_complex_type():
- cname = tp.name.replace(' ', '_')
- if cname in ('char16_t', 'char32_t'):
- self.needs_version(VERSION_CHAR16CHAR32)
- return '_cffi_from_c_%s(%s)' % (cname, var)
+ elif tp.name != 'long double' and not tp.is_complex_type():
+ cname = tp.name.replace(' ', '_')
+ if cname in ('char16_t', 'char32_t'):
+ self.needs_version(VERSION_CHAR16CHAR32)
+ return '_cffi_from_c_%s(%s)' % (cname, var)
else:
return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % (
var, self._gettypenum(tp))
@@ -714,10 +714,10 @@ class Recompiler:
prnt(' %s;' % arg)
#
localvars = set()
- freelines = set()
+ freelines = set()
for type in tp.args:
- self._extra_local_variables(type, localvars, freelines)
- for decl in sorted(localvars):
+ self._extra_local_variables(type, localvars, freelines)
+ for decl in sorted(localvars):
prnt(' %s;' % (decl,))
#
if not isinstance(tp.result, model.VoidType):
@@ -725,7 +725,7 @@ class Recompiler:
context = 'result of %s' % name
result_decl = ' %s;' % tp.result.get_c_name(' result', context)
prnt(result_decl)
- prnt(' PyObject *pyresult;')
+ prnt(' PyObject *pyresult;')
else:
result_decl = None
result_code = ''
@@ -759,14 +759,14 @@ class Recompiler:
if numargs == 0:
prnt(' (void)noarg; /* unused */')
if result_code:
- prnt(' pyresult = %s;' %
+ prnt(' pyresult = %s;' %
self._convert_expr_from_c(tp.result, 'result', 'result type'))
- for freeline in freelines:
- prnt(' ' + freeline)
- prnt(' return pyresult;')
+ for freeline in freelines:
+ prnt(' ' + freeline)
+ prnt(' return pyresult;')
else:
- for freeline in freelines:
- prnt(' ' + freeline)
+ for freeline in freelines:
+ prnt(' ' + freeline)
prnt(' Py_INCREF(Py_None);')
prnt(' return Py_None;')
prnt('}')
@@ -775,26 +775,26 @@ class Recompiler:
#
# the PyPy version: need to replace struct/union arguments with
# pointers, and if the result is a struct/union, insert a first
- # arg that is a pointer to the result. We also do that for
- # complex args and return type.
- def need_indirection(type):
- return (isinstance(type, model.StructOrUnion) or
- (isinstance(type, model.PrimitiveType) and
- type.is_complex_type()))
+ # arg that is a pointer to the result. We also do that for
+ # complex args and return type.
+ def need_indirection(type):
+ return (isinstance(type, model.StructOrUnion) or
+ (isinstance(type, model.PrimitiveType) and
+ type.is_complex_type()))
difference = False
arguments = []
call_arguments = []
context = 'argument of %s' % name
for i, type in enumerate(tp.args):
indirection = ''
- if need_indirection(type):
+ if need_indirection(type):
indirection = '*'
difference = True
arg = type.get_c_name(' %sx%d' % (indirection, i), context)
arguments.append(arg)
call_arguments.append('%sx%d' % (indirection, i))
tp_result = tp.result
- if need_indirection(tp_result):
+ if need_indirection(tp_result):
context = 'result of %s' % name
arg = tp_result.get_c_name(' *result', context)
arguments.insert(0, arg)
@@ -858,10 +858,10 @@ class Recompiler:
def _struct_collecttype(self, tp):
self._do_collect_type(tp)
- if self.target_is_python:
- # also requires nested anon struct/unions in ABI mode, recursively
- for fldtype in tp.anonymous_struct_fields():
- self._struct_collecttype(fldtype)
+ if self.target_is_python:
+ # also requires nested anon struct/unions in ABI mode, recursively
+ for fldtype in tp.anonymous_struct_fields():
+ self._struct_collecttype(fldtype)
def _struct_decl(self, tp, cname, approxname):
if tp.fldtypes is None:
@@ -873,13 +873,13 @@ class Recompiler:
prnt('{')
prnt(' /* only to generate compile-time warnings or errors */')
prnt(' (void)p;')
- for fname, ftype, fbitsize, fqual in self._enum_fields(tp):
+ for fname, ftype, fbitsize, fqual in self._enum_fields(tp):
try:
if ftype.is_integer_type() or fbitsize >= 0:
# accept all integers, but complain on float or double
- if fname != '':
- prnt(" (void)((p->%s) | 0); /* check that '%s.%s' is "
- "an integer */" % (fname, cname, fname))
+ if fname != '':
+ prnt(" (void)((p->%s) | 0); /* check that '%s.%s' is "
+ "an integer */" % (fname, cname, fname))
continue
# only accept exactly the type declared, except that '[]'
# is interpreted as a '*' and so will match any array length.
@@ -891,7 +891,7 @@ class Recompiler:
prnt(' { %s = &p->%s; (void)tmp; }' % (
ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual),
fname))
- except VerificationError as e:
+ except VerificationError as e:
prnt(' /* %s */' % str(e)) # cannot verify it, ignore
prnt('}')
prnt('struct _cffi_align_%s { char x; %s y; };' % (approxname, cname))
@@ -911,17 +911,17 @@ class Recompiler:
named_ptr not in self.ffi._parser._included_declarations)):
if tp.fldtypes is None:
pass # opaque
- elif tp.partial or any(tp.anonymous_struct_fields()):
+ elif tp.partial or any(tp.anonymous_struct_fields()):
pass # field layout obtained silently from the C compiler
else:
flags.append("_CFFI_F_CHECK_FIELDS")
if tp.packed:
- if tp.packed > 1:
- raise NotImplementedError(
- "%r is declared with 'pack=%r'; only 0 or 1 are "
- "supported in API mode (try to use \"...;\", which "
- "does not require a 'pack' declaration)" %
- (tp, tp.packed))
+ if tp.packed > 1:
+ raise NotImplementedError(
+ "%r is declared with 'pack=%r'; only 0 or 1 are "
+ "supported in API mode (try to use \"...;\", which "
+ "does not require a 'pack' declaration)" %
+ (tp, tp.packed))
flags.append("_CFFI_F_PACKED")
else:
flags.append("_CFFI_F_EXTERNAL")
@@ -929,7 +929,7 @@ class Recompiler:
flags = '|'.join(flags) or '0'
c_fields = []
if reason_for_not_expanding is None:
- enumfields = list(self._enum_fields(tp))
+ enumfields = list(self._enum_fields(tp))
for fldname, fldtype, fbitsize, fqual in enumfields:
fldtype = self._field_type(tp, fldname, fldtype)
self._check_not_opaque(fldtype,
@@ -1060,7 +1060,7 @@ class Recompiler:
def _generate_cpy_const(self, is_int, name, tp=None, category='const',
check_value=None):
if (category, name) in self._seen_constants:
- raise VerificationError(
+ raise VerificationError(
"duplicate declaration of %s '%s'" % (category, name))
self._seen_constants.add((category, name))
#
@@ -1159,7 +1159,7 @@ class Recompiler:
def _generate_cpy_macro_ctx(self, tp, name):
if tp == '...':
if self.target_is_python:
- raise VerificationError(
+ raise VerificationError(
"cannot use the syntax '...' in '#define %s ...' when "
"using the ABI mode" % (name,))
check_value = None
@@ -1237,8 +1237,8 @@ class Recompiler:
size_of_result = '(int)sizeof(%s)' % (
tp.result.get_c_name('', context),)
prnt('static struct _cffi_externpy_s _cffi_externpy__%s =' % name)
- prnt(' { "%s.%s", %s, 0, 0 };' % (
- self.module_name, name, size_of_result))
+ prnt(' { "%s.%s", %s, 0, 0 };' % (
+ self.module_name, name, size_of_result))
prnt()
#
arguments = []
@@ -1293,7 +1293,7 @@ class Recompiler:
def _generate_cpy_extern_python_ctx(self, tp, name):
if self.target_is_python:
- raise VerificationError(
+ raise VerificationError(
"cannot use 'extern \"Python\"' in the ABI mode")
if tp.ellipsis:
raise NotImplementedError("a vararg function is extern \"Python\"")
@@ -1306,32 +1306,32 @@ class Recompiler:
_generate_cpy_extern_python_plus_c_ctx = \
_generate_cpy_extern_python_ctx
- def _print_string_literal_in_array(self, s):
- prnt = self._prnt
- prnt('// # NB. this is not a string because of a size limit in MSVC')
- if not isinstance(s, bytes): # unicode
- s = s.encode('utf-8') # -> bytes
- else:
- s.decode('utf-8') # got bytes, check for valid utf-8
- try:
- s.decode('ascii')
- except UnicodeDecodeError:
- s = b'# -*- encoding: utf8 -*-\n' + s
- for line in s.splitlines(True):
- comment = line
- if type('//') is bytes: # python2
- line = map(ord, line) # make a list of integers
- else: # python3
- # type(line) is bytes, which enumerates like a list of integers
- comment = ascii(comment)[1:-1]
- prnt(('// ' + comment).rstrip())
- printed_line = ''
- for c in line:
- if len(printed_line) >= 76:
- prnt(printed_line)
- printed_line = ''
- printed_line += '%d,' % (c,)
- prnt(printed_line)
+ def _print_string_literal_in_array(self, s):
+ prnt = self._prnt
+ prnt('// # NB. this is not a string because of a size limit in MSVC')
+ if not isinstance(s, bytes): # unicode
+ s = s.encode('utf-8') # -> bytes
+ else:
+ s.decode('utf-8') # got bytes, check for valid utf-8
+ try:
+ s.decode('ascii')
+ except UnicodeDecodeError:
+ s = b'# -*- encoding: utf8 -*-\n' + s
+ for line in s.splitlines(True):
+ comment = line
+ if type('//') is bytes: # python2
+ line = map(ord, line) # make a list of integers
+ else: # python3
+ # type(line) is bytes, which enumerates like a list of integers
+ comment = ascii(comment)[1:-1]
+ prnt(('// ' + comment).rstrip())
+ printed_line = ''
+ for c in line:
+ if len(printed_line) >= 76:
+ prnt(printed_line)
+ printed_line = ''
+ printed_line += '%d,' % (c,)
+ prnt(printed_line)
# ----------
# emitting the opcodes for individual types
@@ -1389,7 +1389,7 @@ class Recompiler:
if tp.length is None:
self.cffi_types[index] = CffiOp(OP_OPEN_ARRAY, item_index)
elif tp.length == '...':
- raise VerificationError(
+ raise VerificationError(
"type %s badly placed: the '...' array length can only be "
"used on global arrays or on fields of structures" % (
str(tp).replace('/*...*/', '...'),))
@@ -1552,12 +1552,12 @@ def recompile(ffi, module_name, preamble, tmpdir='.', call_c_compiler=True,
_patch_for_embedding(patchlist)
if target != '*':
_patch_for_target(patchlist, target)
- if compiler_verbose:
- if tmpdir == '.':
- msg = 'the current directory is'
- else:
- msg = 'setting the current directory to'
- print('%s %r' % (msg, os.path.abspath(tmpdir)))
+ if compiler_verbose:
+ if tmpdir == '.':
+ msg = 'the current directory is'
+ else:
+ msg = 'setting the current directory to'
+ print('%s %r' % (msg, os.path.abspath(tmpdir)))
os.chdir(tmpdir)
outputfilename = ffiplatform.compile('.', ext,
compiler_verbose, debug)
diff --git a/contrib/python/cffi/cffi/setuptools_ext.py b/contrib/python/cffi/cffi/setuptools_ext.py
index 55fdedddd9..8fe361487e 100644
--- a/contrib/python/cffi/cffi/setuptools_ext.py
+++ b/contrib/python/cffi/cffi/setuptools_ext.py
@@ -79,18 +79,18 @@ def _set_py_limited_api(Extension, kwds):
CPython itself should ignore the flag in a debugging version
(by not listing .abi3.so in the extensions it supports), but
it doesn't so far, creating troubles. That's why we check
- for "not hasattr(sys, 'gettotalrefcount')" (the 2.7 compatible equivalent
- of 'd' not in sys.abiflags). (http://bugs.python.org/issue28401)
-
- On Windows, with CPython <= 3.4, it's better not to use py_limited_api
- because virtualenv *still* doesn't copy PYTHON3.DLL on these versions.
- Recently (2020) we started shipping only >= 3.5 wheels, though. So
- we'll give it another try and set py_limited_api on Windows >= 3.5.
+ for "not hasattr(sys, 'gettotalrefcount')" (the 2.7 compatible equivalent
+ of 'd' not in sys.abiflags). (http://bugs.python.org/issue28401)
+
+ On Windows, with CPython <= 3.4, it's better not to use py_limited_api
+ because virtualenv *still* doesn't copy PYTHON3.DLL on these versions.
+ Recently (2020) we started shipping only >= 3.5 wheels, though. So
+ we'll give it another try and set py_limited_api on Windows >= 3.5.
"""
- from cffi import recompiler
-
- if ('py_limited_api' not in kwds and not hasattr(sys, 'gettotalrefcount')
- and recompiler.USE_LIMITED_API):
+ from cffi import recompiler
+
+ if ('py_limited_api' not in kwds and not hasattr(sys, 'gettotalrefcount')
+ and recompiler.USE_LIMITED_API):
import setuptools
try:
setuptools_major_version = int(setuptools.__version__.partition('.')[0])
@@ -151,8 +151,8 @@ def _add_c_module(dist, ffi, module_name, source, source_extension, kwds):
def _add_py_module(dist, ffi, module_name):
from distutils.dir_util import mkpath
- from setuptools.command.build_py import build_py
- from setuptools.command.build_ext import build_ext
+ from setuptools.command.build_py import build_py
+ from setuptools.command.build_ext import build_ext
from distutils import log
from cffi import recompiler
@@ -170,31 +170,31 @@ def _add_py_module(dist, ffi, module_name):
module_path = module_name.split('.')
module_path[-1] += '.py'
generate_mod(os.path.join(self.build_lib, *module_path))
- def get_source_files(self):
- # This is called from 'setup.py sdist' only. Exclude
- # the generate .py module in this case.
- saved_py_modules = self.py_modules
- try:
- if saved_py_modules:
- self.py_modules = [m for m in saved_py_modules
- if m != module_name]
- return base_class.get_source_files(self)
- finally:
- self.py_modules = saved_py_modules
+ def get_source_files(self):
+ # This is called from 'setup.py sdist' only. Exclude
+ # the generate .py module in this case.
+ saved_py_modules = self.py_modules
+ try:
+ if saved_py_modules:
+ self.py_modules = [m for m in saved_py_modules
+ if m != module_name]
+ return base_class.get_source_files(self)
+ finally:
+ self.py_modules = saved_py_modules
dist.cmdclass['build_py'] = build_py_make_mod
- # distutils and setuptools have no notion I could find of a
- # generated python module. If we don't add module_name to
- # dist.py_modules, then things mostly work but there are some
- # combination of options (--root and --record) that will miss
- # the module. So we add it here, which gives a few apparently
- # harmless warnings about not finding the file outside the
- # build directory.
- # Then we need to hack more in get_source_files(); see above.
- if dist.py_modules is None:
- dist.py_modules = []
- dist.py_modules.append(module_name)
-
+ # distutils and setuptools have no notion I could find of a
+ # generated python module. If we don't add module_name to
+ # dist.py_modules, then things mostly work but there are some
+ # combination of options (--root and --record) that will miss
+ # the module. So we add it here, which gives a few apparently
+ # harmless warnings about not finding the file outside the
+ # build directory.
+ # Then we need to hack more in get_source_files(); see above.
+ if dist.py_modules is None:
+ dist.py_modules = []
+ dist.py_modules.append(module_name)
+
# the following is only for "build_ext -i"
base_class_2 = dist.cmdclass.get('build_ext', build_ext)
class build_ext_make_mod(base_class_2):
diff --git a/contrib/python/cffi/cffi/vengine_cpy.py b/contrib/python/cffi/cffi/vengine_cpy.py
index c0abaa8d77..6de0df0ea4 100644
--- a/contrib/python/cffi/cffi/vengine_cpy.py
+++ b/contrib/python/cffi/cffi/vengine_cpy.py
@@ -2,8 +2,8 @@
# DEPRECATED: implementation for ffi.verify()
#
import sys, imp
-from . import model
-from .error import VerificationError
+from . import model
+from .error import VerificationError
class VCPythonEngine(object):
@@ -156,7 +156,7 @@ class VCPythonEngine(object):
self.verifier.modulefilename)
except ImportError as e:
error = "importing %r: %s" % (self.verifier.modulefilename, e)
- raise VerificationError(error)
+ raise VerificationError(error)
finally:
if hasattr(sys, "setdlopenflags"):
sys.setdlopenflags(previous_flags)
@@ -186,7 +186,7 @@ class VCPythonEngine(object):
def __dir__(self):
return FFILibrary._cffi_dir + list(self.__dict__)
library = FFILibrary()
- if module._cffi_setup(lst, VerificationError, library):
+ if module._cffi_setup(lst, VerificationError, library):
import warnings
warnings.warn("reimporting %r might overwrite older definitions"
% (self.verifier.get_module_name()))
@@ -213,7 +213,7 @@ class VCPythonEngine(object):
method = getattr(self, '_generate_cpy_%s_%s' % (kind,
step_name))
except AttributeError:
- raise VerificationError(
+ raise VerificationError(
"not implemented in verify(): %r" % name)
try:
method(tp, realname)
@@ -275,29 +275,29 @@ class VCPythonEngine(object):
tovar, tp.get_c_name(''), errvalue))
self._prnt(' %s;' % errcode)
- def _extra_local_variables(self, tp, localvars, freelines):
+ def _extra_local_variables(self, tp, localvars, freelines):
if isinstance(tp, model.PointerType):
localvars.add('Py_ssize_t datasize')
- localvars.add('struct _cffi_freeme_s *large_args_free = NULL')
- freelines.add('if (large_args_free != NULL)'
- ' _cffi_free_array_arguments(large_args_free);')
+ localvars.add('struct _cffi_freeme_s *large_args_free = NULL')
+ freelines.add('if (large_args_free != NULL)'
+ ' _cffi_free_array_arguments(large_args_free);')
def _convert_funcarg_to_c_ptr_or_array(self, tp, fromvar, tovar, errcode):
self._prnt(' datasize = _cffi_prepare_pointer_call_argument(')
self._prnt(' _cffi_type(%d), %s, (char **)&%s);' % (
self._gettypenum(tp), fromvar, tovar))
self._prnt(' if (datasize != 0) {')
- self._prnt(' %s = ((size_t)datasize) <= 640 ? '
- 'alloca((size_t)datasize) : NULL;' % (tovar,))
- self._prnt(' if (_cffi_convert_array_argument(_cffi_type(%d), %s, '
- '(char **)&%s,' % (self._gettypenum(tp), fromvar, tovar))
- self._prnt(' datasize, &large_args_free) < 0)')
+ self._prnt(' %s = ((size_t)datasize) <= 640 ? '
+ 'alloca((size_t)datasize) : NULL;' % (tovar,))
+ self._prnt(' if (_cffi_convert_array_argument(_cffi_type(%d), %s, '
+ '(char **)&%s,' % (self._gettypenum(tp), fromvar, tovar))
+ self._prnt(' datasize, &large_args_free) < 0)')
self._prnt(' %s;' % errcode)
self._prnt(' }')
def _convert_expr_from_c(self, tp, var, context):
if isinstance(tp, model.PrimitiveType):
- if tp.is_integer_type() and tp.name != '_Bool':
+ if tp.is_integer_type() and tp.name != '_Bool':
return '_cffi_from_c_int(%s, %s)' % (var, tp.name)
elif tp.name != 'long double':
return '_cffi_from_c_%s(%s)' % (tp.name.replace(' ', '_'), var)
@@ -370,17 +370,17 @@ class VCPythonEngine(object):
prnt(' %s;' % type.get_c_name(' x%d' % i, context))
#
localvars = set()
- freelines = set()
+ freelines = set()
for type in tp.args:
- self._extra_local_variables(type, localvars, freelines)
- for decl in sorted(localvars):
+ self._extra_local_variables(type, localvars, freelines)
+ for decl in sorted(localvars):
prnt(' %s;' % (decl,))
#
if not isinstance(tp.result, model.VoidType):
result_code = 'result = '
context = 'result of %s' % name
prnt(' %s;' % tp.result.get_c_name(' result', context))
- prnt(' PyObject *pyresult;')
+ prnt(' PyObject *pyresult;')
else:
result_code = ''
#
@@ -412,14 +412,14 @@ class VCPythonEngine(object):
if numargs == 0:
prnt(' (void)noarg; /* unused */')
if result_code:
- prnt(' pyresult = %s;' %
+ prnt(' pyresult = %s;' %
self._convert_expr_from_c(tp.result, 'result', 'result type'))
- for freeline in freelines:
- prnt(' ' + freeline)
- prnt(' return pyresult;')
+ for freeline in freelines:
+ prnt(' ' + freeline)
+ prnt(' return pyresult;')
else:
- for freeline in freelines:
- prnt(' ' + freeline)
+ for freeline in freelines:
+ prnt(' ' + freeline)
prnt(' Py_INCREF(Py_None);')
prnt(' return Py_None;')
prnt('}')
@@ -494,7 +494,7 @@ class VCPythonEngine(object):
prnt(' { %s = &p->%s; (void)tmp; }' % (
ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual),
fname))
- except VerificationError as e:
+ except VerificationError as e:
prnt(' /* %s */' % str(e)) # cannot verify it, ignore
prnt('}')
prnt('static PyObject *')
@@ -559,7 +559,7 @@ class VCPythonEngine(object):
# check that the layout sizes and offsets match the real ones
def check(realvalue, expectedvalue, msg):
if realvalue != expectedvalue:
- raise VerificationError(
+ raise VerificationError(
"%s (we have %d, but C compiler says %d)"
% (msg, expectedvalue, realvalue))
ffi = self.ffi
@@ -762,7 +762,7 @@ class VCPythonEngine(object):
if isinstance(tp, model.ArrayType):
tp_ptr = model.PointerType(tp.item)
self._generate_cpy_const(False, name, tp, vartp=tp_ptr,
- size_too = tp.length_is_unknown())
+ size_too = tp.length_is_unknown())
else:
tp_ptr = model.PointerType(tp)
self._generate_cpy_const(False, name, tp_ptr, category='var')
@@ -774,13 +774,13 @@ class VCPythonEngine(object):
value = getattr(library, name)
if isinstance(tp, model.ArrayType): # int a[5] is "constant" in the
# sense that "a=..." is forbidden
- if tp.length_is_unknown():
+ if tp.length_is_unknown():
assert isinstance(value, tuple)
(value, size) = value
BItemType = self.ffi._get_cached_btype(tp.item)
length, rest = divmod(size, self.ffi.sizeof(BItemType))
if rest != 0:
- raise VerificationError(
+ raise VerificationError(
"bad size: %r does not seem to be an array of %s" %
(name, tp.item))
tp = tp.resolve_length(length)
@@ -816,8 +816,8 @@ cffimod_header = r'''
#include <stddef.h>
/* this block of #ifs should be kept exactly identical between
- c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
- and cffi/_cffi_include.h */
+ c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
+ and cffi/_cffi_include.h */
#if defined(_MSC_VER)
# include <malloc.h> /* for alloca() */
# if _MSC_VER < 1600 /* MSVC < 2010 */
@@ -851,13 +851,13 @@ cffimod_header = r'''
# include <stdint.h>
# endif
# if _MSC_VER < 1800 /* MSVC < 2013 */
-# ifndef __cplusplus
- typedef unsigned char _Bool;
-# endif
+# ifndef __cplusplus
+ typedef unsigned char _Bool;
+# endif
# endif
#else
# include <stdint.h>
-# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
+# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
# include <alloca.h>
# endif
#endif
@@ -880,7 +880,7 @@ cffimod_header = r'''
#define _cffi_from_c_ulong PyLong_FromUnsignedLong
#define _cffi_from_c_longlong PyLong_FromLongLong
#define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
-#define _cffi_from_c__Bool PyBool_FromLong
+#define _cffi_from_c__Bool PyBool_FromLong
#define _cffi_to_c_double PyFloat_AsDouble
#define _cffi_to_c_float PyFloat_AsDouble
@@ -989,59 +989,59 @@ static PyObject *_cffi_setup(PyObject *self, PyObject *args)
return PyBool_FromLong(was_alive);
}
-union _cffi_union_alignment_u {
- unsigned char m_char;
- unsigned short m_short;
- unsigned int m_int;
- unsigned long m_long;
- unsigned long long m_longlong;
- float m_float;
- double m_double;
- long double m_longdouble;
-};
-
-struct _cffi_freeme_s {
- struct _cffi_freeme_s *next;
- union _cffi_union_alignment_u alignment;
-};
-
-#ifdef __GNUC__
- __attribute__((unused))
-#endif
-static int _cffi_convert_array_argument(CTypeDescrObject *ctptr, PyObject *arg,
- char **output_data, Py_ssize_t datasize,
- struct _cffi_freeme_s **freeme)
-{
- char *p;
- if (datasize < 0)
- return -1;
-
- p = *output_data;
- if (p == NULL) {
- struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc(
- offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize);
- if (fp == NULL)
- return -1;
- fp->next = *freeme;
- *freeme = fp;
- p = *output_data = (char *)&fp->alignment;
- }
- memset((void *)p, 0, (size_t)datasize);
- return _cffi_convert_array_from_object(p, ctptr, arg);
-}
-
-#ifdef __GNUC__
- __attribute__((unused))
-#endif
-static void _cffi_free_array_arguments(struct _cffi_freeme_s *freeme)
-{
- do {
- void *p = (void *)freeme;
- freeme = freeme->next;
- PyObject_Free(p);
- } while (freeme != NULL);
-}
-
+union _cffi_union_alignment_u {
+ unsigned char m_char;
+ unsigned short m_short;
+ unsigned int m_int;
+ unsigned long m_long;
+ unsigned long long m_longlong;
+ float m_float;
+ double m_double;
+ long double m_longdouble;
+};
+
+struct _cffi_freeme_s {
+ struct _cffi_freeme_s *next;
+ union _cffi_union_alignment_u alignment;
+};
+
+#ifdef __GNUC__
+ __attribute__((unused))
+#endif
+static int _cffi_convert_array_argument(CTypeDescrObject *ctptr, PyObject *arg,
+ char **output_data, Py_ssize_t datasize,
+ struct _cffi_freeme_s **freeme)
+{
+ char *p;
+ if (datasize < 0)
+ return -1;
+
+ p = *output_data;
+ if (p == NULL) {
+ struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc(
+ offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize);
+ if (fp == NULL)
+ return -1;
+ fp->next = *freeme;
+ *freeme = fp;
+ p = *output_data = (char *)&fp->alignment;
+ }
+ memset((void *)p, 0, (size_t)datasize);
+ return _cffi_convert_array_from_object(p, ctptr, arg);
+}
+
+#ifdef __GNUC__
+ __attribute__((unused))
+#endif
+static void _cffi_free_array_arguments(struct _cffi_freeme_s *freeme)
+{
+ do {
+ void *p = (void *)freeme;
+ freeme = freeme->next;
+ PyObject_Free(p);
+ } while (freeme != NULL);
+}
+
static int _cffi_init(void)
{
PyObject *module, *c_api_object = NULL;
diff --git a/contrib/python/cffi/cffi/vengine_gen.py b/contrib/python/cffi/cffi/vengine_gen.py
index c3979b73ca..26421526f6 100644
--- a/contrib/python/cffi/cffi/vengine_gen.py
+++ b/contrib/python/cffi/cffi/vengine_gen.py
@@ -4,8 +4,8 @@
import sys, os
import types
-from . import model
-from .error import VerificationError
+from . import model
+from .error import VerificationError
class VGenericEngine(object):
@@ -103,7 +103,7 @@ class VGenericEngine(object):
method = getattr(self, '_generate_gen_%s_%s' % (kind,
step_name))
except AttributeError:
- raise VerificationError(
+ raise VerificationError(
"not implemented in verify(): %r" % name)
try:
method(tp, realname)
@@ -282,7 +282,7 @@ class VGenericEngine(object):
prnt(' { %s = &p->%s; (void)tmp; }' % (
ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual),
fname))
- except VerificationError as e:
+ except VerificationError as e:
prnt(' /* %s */' % str(e)) # cannot verify it, ignore
prnt('}')
self.export_symbols.append(layoutfuncname)
@@ -345,7 +345,7 @@ class VGenericEngine(object):
# check that the layout sizes and offsets match the real ones
def check(realvalue, expectedvalue, msg):
if realvalue != expectedvalue:
- raise VerificationError(
+ raise VerificationError(
"%s (we have %d, but C compiler says %d)"
% (msg, expectedvalue, realvalue))
ffi = self.ffi
@@ -499,7 +499,7 @@ class VGenericEngine(object):
error = self.ffi.string(p)
if sys.version_info >= (3,):
error = str(error, 'utf-8')
- raise VerificationError(error)
+ raise VerificationError(error)
def _enum_funcname(self, prefix, name):
# "$enum_$1" => "___D_enum____D_1"
@@ -565,7 +565,7 @@ class VGenericEngine(object):
def _generate_gen_variable_decl(self, tp, name):
if isinstance(tp, model.ArrayType):
- if tp.length_is_unknown():
+ if tp.length_is_unknown():
prnt = self._prnt
funcname = '_cffi_sizeof_%s' % (name,)
self.export_symbols.append(funcname)
@@ -584,7 +584,7 @@ class VGenericEngine(object):
def _loaded_gen_variable(self, tp, name, module, library):
if isinstance(tp, model.ArrayType): # int a[5] is "constant" in the
# sense that "a=..." is forbidden
- if tp.length_is_unknown():
+ if tp.length_is_unknown():
funcname = '_cffi_sizeof_%s' % (name,)
BFunc = self.ffi._typeof_locked('size_t(*)(void)')[0]
function = module.load_function(BFunc, funcname)
@@ -592,7 +592,7 @@ class VGenericEngine(object):
BItemType = self.ffi._get_cached_btype(tp.item)
length, rest = divmod(size, self.ffi.sizeof(BItemType))
if rest != 0:
- raise VerificationError(
+ raise VerificationError(
"bad size: %r does not seem to be an array of %s" %
(name, tp.item))
tp = tp.resolve_length(length)
@@ -627,8 +627,8 @@ cffimod_header = r'''
#include <sys/types.h> /* XXX for ssize_t on some platforms */
/* this block of #ifs should be kept exactly identical between
- c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
- and cffi/_cffi_include.h */
+ c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
+ and cffi/_cffi_include.h */
#if defined(_MSC_VER)
# include <malloc.h> /* for alloca() */
# if _MSC_VER < 1600 /* MSVC < 2010 */
@@ -662,13 +662,13 @@ cffimod_header = r'''
# include <stdint.h>
# endif
# if _MSC_VER < 1800 /* MSVC < 2013 */
-# ifndef __cplusplus
- typedef unsigned char _Bool;
-# endif
+# ifndef __cplusplus
+ typedef unsigned char _Bool;
+# endif
# endif
#else
# include <stdint.h>
-# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
+# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
# include <alloca.h>
# endif
#endif
diff --git a/contrib/python/cffi/cffi/verifier.py b/contrib/python/cffi/cffi/verifier.py
index b06ca0fe0c..a500c7814a 100644
--- a/contrib/python/cffi/cffi/verifier.py
+++ b/contrib/python/cffi/cffi/verifier.py
@@ -4,7 +4,7 @@
import sys, os, binascii, shutil, io
from . import __version_verifier_modules__
from . import ffiplatform
-from .error import VerificationError
+from .error import VerificationError
if sys.version_info >= (3, 3):
import importlib.machinery
@@ -33,7 +33,7 @@ class Verifier(object):
ext_package=None, tag='', force_generic_engine=False,
source_extension='.c', flags=None, relative_to=None, **kwds):
if ffi._parser._uses_new_feature:
- raise VerificationError(
+ raise VerificationError(
"feature not supported with ffi.verify(), but only "
"with ffi.set_source(): %s" % (ffi._parser._uses_new_feature,))
self.ffi = ffi
@@ -50,8 +50,8 @@ class Verifier(object):
if tag:
raise TypeError("can't specify both 'modulename' and 'tag'")
else:
- key = '\x00'.join(['%d.%d' % sys.version_info[:2],
- __version_verifier_modules__,
+ key = '\x00'.join(['%d.%d' % sys.version_info[:2],
+ __version_verifier_modules__,
preamble, flattened_kwds] +
ffi._cdefsources)
if sys.version_info >= (3,):
@@ -75,7 +75,7 @@ class Verifier(object):
which can be tweaked beforehand."""
with self.ffi._lock:
if self._has_source and file is None:
- raise VerificationError(
+ raise VerificationError(
"source code already written")
self._write_source(file)
@@ -84,7 +84,7 @@ class Verifier(object):
This produces a dynamic link library in 'self.modulefilename'."""
with self.ffi._lock:
if self._has_module:
- raise VerificationError("module already compiled")
+ raise VerificationError("module already compiled")
if not self._has_source:
self._write_source()
self._compile_module()
@@ -117,7 +117,7 @@ class Verifier(object):
return basename
def get_extension(self):
- ffiplatform._hack_at_distutils() # backward compatibility hack
+ ffiplatform._hack_at_distutils() # backward compatibility hack
if not self._has_source:
with self.ffi._lock:
if not self._has_source:
@@ -302,6 +302,6 @@ def _get_so_suffixes():
return suffixes
def _ensure_dir(filename):
- dirname = os.path.dirname(filename)
- if dirname and not os.path.isdir(dirname):
- os.makedirs(dirname)
+ dirname = os.path.dirname(filename)
+ if dirname and not os.path.isdir(dirname):
+ os.makedirs(dirname)
diff --git a/contrib/python/cffi/gen/lib/ya.make b/contrib/python/cffi/gen/lib/ya.make
index 2cd5ca7201..e5891f26a4 100644
--- a/contrib/python/cffi/gen/lib/ya.make
+++ b/contrib/python/cffi/gen/lib/ya.make
@@ -16,6 +16,6 @@ PY_SRCS(
MAIN main.py
)
-NO_LINT()
-
+NO_LINT()
+
END()
diff --git a/contrib/python/cffi/gen/ya.make b/contrib/python/cffi/gen/ya.make
index f0e64d3a6b..66f8030b9b 100644
--- a/contrib/python/cffi/gen/ya.make
+++ b/contrib/python/cffi/gen/ya.make
@@ -1,4 +1,4 @@
-PY3_PROGRAM(cffigen)
+PY3_PROGRAM(cffigen)
OWNER(orivej)
diff --git a/contrib/python/cffi/ya.make b/contrib/python/cffi/ya.make
index 045a80d1c4..7bff9477f9 100644
--- a/contrib/python/cffi/ya.make
+++ b/contrib/python/cffi/ya.make
@@ -4,7 +4,7 @@ LICENSE(MIT)
OWNER(g:python-contrib)
-VERSION(1.15.0)
+VERSION(1.15.0)
PEERDIR(
contrib/restricted/libffi
@@ -34,11 +34,11 @@ PY_SRCS(
cffi/cffi_opcode.py
cffi/commontypes.py
cffi/cparser.py
- cffi/error.py
+ cffi/error.py
cffi/ffiplatform.py
cffi/lock.py
cffi/model.py
- cffi/pkgconfig.py
+ cffi/pkgconfig.py
cffi/recompiler.py
cffi/setuptools_ext.py
cffi/vengine_cpy.py
@@ -48,10 +48,10 @@ PY_SRCS(
RESOURCE_FILES(
PREFIX contrib/python/cffi/
- .dist-info/METADATA
- .dist-info/entry_points.txt
- .dist-info/top_level.txt
- cffi/_cffi_errors.h
+ .dist-info/METADATA
+ .dist-info/entry_points.txt
+ .dist-info/top_level.txt
+ cffi/_cffi_errors.h
cffi/_cffi_include.h
cffi/_embedding.h
cffi/parse_c_type.h