Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- .venv/lib/python3.11/site-packages/cffi/__init__.py +14 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/__init__.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/_imp_emulation.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/_shimmed_dist_utils.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/api.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/backend_ctypes.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/cffi_opcode.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/commontypes.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/cparser.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/error.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/ffiplatform.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/lock.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/model.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/pkgconfig.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/recompiler.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/setuptools_ext.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/vengine_cpy.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/vengine_gen.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/__pycache__/verifier.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/cffi/_cffi_include.h +389 -0
- .venv/lib/python3.11/site-packages/cffi/_imp_emulation.py +83 -0
- .venv/lib/python3.11/site-packages/cffi/_shimmed_dist_utils.py +45 -0
- .venv/lib/python3.11/site-packages/cffi/api.py +967 -0
- .venv/lib/python3.11/site-packages/cffi/cparser.py +1015 -0
- .venv/lib/python3.11/site-packages/cffi/ffiplatform.py +113 -0
- .venv/lib/python3.11/site-packages/cffi/model.py +618 -0
- .venv/lib/python3.11/site-packages/cffi/parse_c_type.h +181 -0
- .venv/lib/python3.11/site-packages/cffi/pkgconfig.py +121 -0
- .venv/lib/python3.11/site-packages/cffi/vengine_cpy.py +1084 -0
- .venv/lib/python3.11/site-packages/cffi/verifier.py +306 -0
- .venv/lib/python3.11/site-packages/diskcache/__pycache__/core.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__init__.py +72 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/asgi.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/context_managers.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/decorator.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/exposition.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/metrics_core.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/mmap_dict.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/parser.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/platform_collector.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/registry.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/__pycache__/values.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/prometheus_client/asgi.py +40 -0
- .venv/lib/python3.11/site-packages/prometheus_client/context_managers.py +82 -0
- .venv/lib/python3.11/site-packages/prometheus_client/core.py +32 -0
- .venv/lib/python3.11/site-packages/prometheus_client/decorator.py +427 -0
- .venv/lib/python3.11/site-packages/prometheus_client/exposition.py +666 -0
- .venv/lib/python3.11/site-packages/prometheus_client/gc_collector.py +45 -0
- .venv/lib/python3.11/site-packages/prometheus_client/metrics.py +776 -0
.gitattributes
CHANGED
|
@@ -204,3 +204,4 @@ tuning-competition-baseline/.venv/lib/python3.11/site-packages/torch/_inductor/_
|
|
| 204 |
.venv/lib/python3.11/site-packages/vllm/__pycache__/config.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 205 |
.venv/lib/python3.11/site-packages/vllm/__pycache__/utils.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 206 |
.venv/lib/python3.11/site-packages/grpc/_cython/cygrpc.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 204 |
.venv/lib/python3.11/site-packages/vllm/__pycache__/config.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 205 |
.venv/lib/python3.11/site-packages/vllm/__pycache__/utils.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 206 |
.venv/lib/python3.11/site-packages/grpc/_cython/cygrpc.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 207 |
+
.venv/lib/python3.11/site-packages/wrapt/_wrappers.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
.venv/lib/python3.11/site-packages/cffi/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__all__ = ['FFI', 'VerificationError', 'VerificationMissing', 'CDefError',
|
| 2 |
+
'FFIError']
|
| 3 |
+
|
| 4 |
+
from .api import FFI
|
| 5 |
+
from .error import CDefError, FFIError, VerificationError, VerificationMissing
|
| 6 |
+
from .error import PkgConfigError
|
| 7 |
+
|
| 8 |
+
__version__ = "1.17.1"
|
| 9 |
+
__version_info__ = (1, 17, 1)
|
| 10 |
+
|
| 11 |
+
# The verifier module file names are based on the CRC32 of a string that
|
| 12 |
+
# contains the following version number. It may be older than __version__
|
| 13 |
+
# if nothing is clearly incompatible.
|
| 14 |
+
__version_verifier_modules__ = "0.8.6"
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (612 Bytes). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/_imp_emulation.cpython-311.pyc
ADDED
|
Binary file (4.7 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/_shimmed_dist_utils.cpython-311.pyc
ADDED
|
Binary file (2.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/api.cpython-311.pyc
ADDED
|
Binary file (55.6 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/backend_ctypes.cpython-311.pyc
ADDED
|
Binary file (71.9 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/cffi_opcode.cpython-311.pyc
ADDED
|
Binary file (7.13 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/commontypes.cpython-311.pyc
ADDED
|
Binary file (3.29 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/cparser.cpython-311.pyc
ADDED
|
Binary file (50 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/error.cpython-311.pyc
ADDED
|
Binary file (2.11 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/ffiplatform.cpython-311.pyc
ADDED
|
Binary file (6.61 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/lock.cpython-311.pyc
ADDED
|
Binary file (622 Bytes). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/model.cpython-311.pyc
ADDED
|
Binary file (33.9 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/pkgconfig.cpython-311.pyc
ADDED
|
Binary file (8.12 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/recompiler.cpython-311.pyc
ADDED
|
Binary file (90.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/setuptools_ext.cpython-311.pyc
ADDED
|
Binary file (12.6 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/vengine_cpy.cpython-311.pyc
ADDED
|
Binary file (57.2 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/vengine_gen.cpython-311.pyc
ADDED
|
Binary file (38.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/__pycache__/verifier.cpython-311.pyc
ADDED
|
Binary file (18.1 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/cffi/_cffi_include.h
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#define _CFFI_
|
| 2 |
+
|
| 3 |
+
/* We try to define Py_LIMITED_API before including Python.h.
|
| 4 |
+
|
| 5 |
+
Mess: we can only define it if Py_DEBUG, Py_TRACE_REFS and
|
| 6 |
+
Py_REF_DEBUG are not defined. This is a best-effort approximation:
|
| 7 |
+
we can learn about Py_DEBUG from pyconfig.h, but it is unclear if
|
| 8 |
+
the same works for the other two macros. Py_DEBUG implies them,
|
| 9 |
+
but not the other way around.
|
| 10 |
+
|
| 11 |
+
The implementation is messy (issue #350): on Windows, with _MSC_VER,
|
| 12 |
+
we have to define Py_LIMITED_API even before including pyconfig.h.
|
| 13 |
+
In that case, we guess what pyconfig.h will do to the macros above,
|
| 14 |
+
and check our guess after the #include.
|
| 15 |
+
|
| 16 |
+
Note that on Windows, with CPython 3.x, you need >= 3.5 and virtualenv
|
| 17 |
+
version >= 16.0.0. With older versions of either, you don't get a
|
| 18 |
+
copy of PYTHON3.DLL in the virtualenv. We can't check the version of
|
| 19 |
+
CPython *before* we even include pyconfig.h. ffi.set_source() puts
|
| 20 |
+
a ``#define _CFFI_NO_LIMITED_API'' at the start of this file if it is
|
| 21 |
+
running on Windows < 3.5, as an attempt at fixing it, but that's
|
| 22 |
+
arguably wrong because it may not be the target version of Python.
|
| 23 |
+
Still better than nothing I guess. As another workaround, you can
|
| 24 |
+
remove the definition of Py_LIMITED_API here.
|
| 25 |
+
|
| 26 |
+
See also 'py_limited_api' in cffi/setuptools_ext.py.
|
| 27 |
+
*/
|
| 28 |
+
#if !defined(_CFFI_USE_EMBEDDING) && !defined(Py_LIMITED_API)
|
| 29 |
+
# ifdef _MSC_VER
|
| 30 |
+
# if !defined(_DEBUG) && !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) && !defined(_CFFI_NO_LIMITED_API)
|
| 31 |
+
# define Py_LIMITED_API
|
| 32 |
+
# endif
|
| 33 |
+
# include <pyconfig.h>
|
| 34 |
+
/* sanity-check: Py_LIMITED_API will cause crashes if any of these
|
| 35 |
+
are also defined. Normally, the Python file PC/pyconfig.h does not
|
| 36 |
+
cause any of these to be defined, with the exception that _DEBUG
|
| 37 |
+
causes Py_DEBUG. Double-check that. */
|
| 38 |
+
# ifdef Py_LIMITED_API
|
| 39 |
+
# if defined(Py_DEBUG)
|
| 40 |
+
# error "pyconfig.h unexpectedly defines Py_DEBUG, but Py_LIMITED_API is set"
|
| 41 |
+
# endif
|
| 42 |
+
# if defined(Py_TRACE_REFS)
|
| 43 |
+
# error "pyconfig.h unexpectedly defines Py_TRACE_REFS, but Py_LIMITED_API is set"
|
| 44 |
+
# endif
|
| 45 |
+
# if defined(Py_REF_DEBUG)
|
| 46 |
+
# error "pyconfig.h unexpectedly defines Py_REF_DEBUG, but Py_LIMITED_API is set"
|
| 47 |
+
# endif
|
| 48 |
+
# endif
|
| 49 |
+
# else
|
| 50 |
+
# include <pyconfig.h>
|
| 51 |
+
# if !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) && !defined(_CFFI_NO_LIMITED_API)
|
| 52 |
+
# define Py_LIMITED_API
|
| 53 |
+
# endif
|
| 54 |
+
# endif
|
| 55 |
+
#endif
|
| 56 |
+
|
| 57 |
+
#include <Python.h>
|
| 58 |
+
#ifdef __cplusplus
|
| 59 |
+
extern "C" {
|
| 60 |
+
#endif
|
| 61 |
+
#include <stddef.h>
|
| 62 |
+
#include "parse_c_type.h"
|
| 63 |
+
|
| 64 |
+
/* this block of #ifs should be kept exactly identical between
|
| 65 |
+
c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
|
| 66 |
+
and cffi/_cffi_include.h */
|
| 67 |
+
#if defined(_MSC_VER)
|
| 68 |
+
# include <malloc.h> /* for alloca() */
|
| 69 |
+
# if _MSC_VER < 1600 /* MSVC < 2010 */
|
| 70 |
+
typedef __int8 int8_t;
|
| 71 |
+
typedef __int16 int16_t;
|
| 72 |
+
typedef __int32 int32_t;
|
| 73 |
+
typedef __int64 int64_t;
|
| 74 |
+
typedef unsigned __int8 uint8_t;
|
| 75 |
+
typedef unsigned __int16 uint16_t;
|
| 76 |
+
typedef unsigned __int32 uint32_t;
|
| 77 |
+
typedef unsigned __int64 uint64_t;
|
| 78 |
+
typedef __int8 int_least8_t;
|
| 79 |
+
typedef __int16 int_least16_t;
|
| 80 |
+
typedef __int32 int_least32_t;
|
| 81 |
+
typedef __int64 int_least64_t;
|
| 82 |
+
typedef unsigned __int8 uint_least8_t;
|
| 83 |
+
typedef unsigned __int16 uint_least16_t;
|
| 84 |
+
typedef unsigned __int32 uint_least32_t;
|
| 85 |
+
typedef unsigned __int64 uint_least64_t;
|
| 86 |
+
typedef __int8 int_fast8_t;
|
| 87 |
+
typedef __int16 int_fast16_t;
|
| 88 |
+
typedef __int32 int_fast32_t;
|
| 89 |
+
typedef __int64 int_fast64_t;
|
| 90 |
+
typedef unsigned __int8 uint_fast8_t;
|
| 91 |
+
typedef unsigned __int16 uint_fast16_t;
|
| 92 |
+
typedef unsigned __int32 uint_fast32_t;
|
| 93 |
+
typedef unsigned __int64 uint_fast64_t;
|
| 94 |
+
typedef __int64 intmax_t;
|
| 95 |
+
typedef unsigned __int64 uintmax_t;
|
| 96 |
+
# else
|
| 97 |
+
# include <stdint.h>
|
| 98 |
+
# endif
|
| 99 |
+
# if _MSC_VER < 1800 /* MSVC < 2013 */
|
| 100 |
+
# ifndef __cplusplus
|
| 101 |
+
typedef unsigned char _Bool;
|
| 102 |
+
# endif
|
| 103 |
+
# endif
|
| 104 |
+
# define _cffi_float_complex_t _Fcomplex /* include <complex.h> for it */
|
| 105 |
+
# define _cffi_double_complex_t _Dcomplex /* include <complex.h> for it */
|
| 106 |
+
#else
|
| 107 |
+
# include <stdint.h>
|
| 108 |
+
# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
|
| 109 |
+
# include <alloca.h>
|
| 110 |
+
# endif
|
| 111 |
+
# define _cffi_float_complex_t float _Complex
|
| 112 |
+
# define _cffi_double_complex_t double _Complex
|
| 113 |
+
#endif
|
| 114 |
+
|
| 115 |
+
#ifdef __GNUC__
|
| 116 |
+
# define _CFFI_UNUSED_FN __attribute__((unused))
|
| 117 |
+
#else
|
| 118 |
+
# define _CFFI_UNUSED_FN /* nothing */
|
| 119 |
+
#endif
|
| 120 |
+
|
| 121 |
+
#ifdef __cplusplus
|
| 122 |
+
# ifndef _Bool
|
| 123 |
+
typedef bool _Bool; /* semi-hackish: C++ has no _Bool; bool is builtin */
|
| 124 |
+
# endif
|
| 125 |
+
#endif
|
| 126 |
+
|
| 127 |
+
/********** CPython-specific section **********/
|
| 128 |
+
#ifndef PYPY_VERSION
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
#if PY_MAJOR_VERSION >= 3
|
| 132 |
+
# define PyInt_FromLong PyLong_FromLong
|
| 133 |
+
#endif
|
| 134 |
+
|
| 135 |
+
#define _cffi_from_c_double PyFloat_FromDouble
|
| 136 |
+
#define _cffi_from_c_float PyFloat_FromDouble
|
| 137 |
+
#define _cffi_from_c_long PyInt_FromLong
|
| 138 |
+
#define _cffi_from_c_ulong PyLong_FromUnsignedLong
|
| 139 |
+
#define _cffi_from_c_longlong PyLong_FromLongLong
|
| 140 |
+
#define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
|
| 141 |
+
#define _cffi_from_c__Bool PyBool_FromLong
|
| 142 |
+
|
| 143 |
+
#define _cffi_to_c_double PyFloat_AsDouble
|
| 144 |
+
#define _cffi_to_c_float PyFloat_AsDouble
|
| 145 |
+
|
| 146 |
+
#define _cffi_from_c_int(x, type) \
|
| 147 |
+
(((type)-1) > 0 ? /* unsigned */ \
|
| 148 |
+
(sizeof(type) < sizeof(long) ? \
|
| 149 |
+
PyInt_FromLong((long)x) : \
|
| 150 |
+
sizeof(type) == sizeof(long) ? \
|
| 151 |
+
PyLong_FromUnsignedLong((unsigned long)x) : \
|
| 152 |
+
PyLong_FromUnsignedLongLong((unsigned long long)x)) : \
|
| 153 |
+
(sizeof(type) <= sizeof(long) ? \
|
| 154 |
+
PyInt_FromLong((long)x) : \
|
| 155 |
+
PyLong_FromLongLong((long long)x)))
|
| 156 |
+
|
| 157 |
+
#define _cffi_to_c_int(o, type) \
|
| 158 |
+
((type)( \
|
| 159 |
+
sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \
|
| 160 |
+
: (type)_cffi_to_c_i8(o)) : \
|
| 161 |
+
sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \
|
| 162 |
+
: (type)_cffi_to_c_i16(o)) : \
|
| 163 |
+
sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \
|
| 164 |
+
: (type)_cffi_to_c_i32(o)) : \
|
| 165 |
+
sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \
|
| 166 |
+
: (type)_cffi_to_c_i64(o)) : \
|
| 167 |
+
(Py_FatalError("unsupported size for type " #type), (type)0)))
|
| 168 |
+
|
| 169 |
+
#define _cffi_to_c_i8 \
|
| 170 |
+
((int(*)(PyObject *))_cffi_exports[1])
|
| 171 |
+
#define _cffi_to_c_u8 \
|
| 172 |
+
((int(*)(PyObject *))_cffi_exports[2])
|
| 173 |
+
#define _cffi_to_c_i16 \
|
| 174 |
+
((int(*)(PyObject *))_cffi_exports[3])
|
| 175 |
+
#define _cffi_to_c_u16 \
|
| 176 |
+
((int(*)(PyObject *))_cffi_exports[4])
|
| 177 |
+
#define _cffi_to_c_i32 \
|
| 178 |
+
((int(*)(PyObject *))_cffi_exports[5])
|
| 179 |
+
#define _cffi_to_c_u32 \
|
| 180 |
+
((unsigned int(*)(PyObject *))_cffi_exports[6])
|
| 181 |
+
#define _cffi_to_c_i64 \
|
| 182 |
+
((long long(*)(PyObject *))_cffi_exports[7])
|
| 183 |
+
#define _cffi_to_c_u64 \
|
| 184 |
+
((unsigned long long(*)(PyObject *))_cffi_exports[8])
|
| 185 |
+
#define _cffi_to_c_char \
|
| 186 |
+
((int(*)(PyObject *))_cffi_exports[9])
|
| 187 |
+
#define _cffi_from_c_pointer \
|
| 188 |
+
((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[10])
|
| 189 |
+
#define _cffi_to_c_pointer \
|
| 190 |
+
((char *(*)(PyObject *, struct _cffi_ctypedescr *))_cffi_exports[11])
|
| 191 |
+
#define _cffi_get_struct_layout \
|
| 192 |
+
not used any more
|
| 193 |
+
#define _cffi_restore_errno \
|
| 194 |
+
((void(*)(void))_cffi_exports[13])
|
| 195 |
+
#define _cffi_save_errno \
|
| 196 |
+
((void(*)(void))_cffi_exports[14])
|
| 197 |
+
#define _cffi_from_c_char \
|
| 198 |
+
((PyObject *(*)(char))_cffi_exports[15])
|
| 199 |
+
#define _cffi_from_c_deref \
|
| 200 |
+
((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[16])
|
| 201 |
+
#define _cffi_to_c \
|
| 202 |
+
((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[17])
|
| 203 |
+
#define _cffi_from_c_struct \
|
| 204 |
+
((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[18])
|
| 205 |
+
#define _cffi_to_c_wchar_t \
|
| 206 |
+
((_cffi_wchar_t(*)(PyObject *))_cffi_exports[19])
|
| 207 |
+
#define _cffi_from_c_wchar_t \
|
| 208 |
+
((PyObject *(*)(_cffi_wchar_t))_cffi_exports[20])
|
| 209 |
+
#define _cffi_to_c_long_double \
|
| 210 |
+
((long double(*)(PyObject *))_cffi_exports[21])
|
| 211 |
+
#define _cffi_to_c__Bool \
|
| 212 |
+
((_Bool(*)(PyObject *))_cffi_exports[22])
|
| 213 |
+
#define _cffi_prepare_pointer_call_argument \
|
| 214 |
+
((Py_ssize_t(*)(struct _cffi_ctypedescr *, \
|
| 215 |
+
PyObject *, char **))_cffi_exports[23])
|
| 216 |
+
#define _cffi_convert_array_from_object \
|
| 217 |
+
((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[24])
|
| 218 |
+
#define _CFFI_CPIDX 25
|
| 219 |
+
#define _cffi_call_python \
|
| 220 |
+
((void(*)(struct _cffi_externpy_s *, char *))_cffi_exports[_CFFI_CPIDX])
|
| 221 |
+
#define _cffi_to_c_wchar3216_t \
|
| 222 |
+
((int(*)(PyObject *))_cffi_exports[26])
|
| 223 |
+
#define _cffi_from_c_wchar3216_t \
|
| 224 |
+
((PyObject *(*)(int))_cffi_exports[27])
|
| 225 |
+
#define _CFFI_NUM_EXPORTS 28
|
| 226 |
+
|
| 227 |
+
struct _cffi_ctypedescr;
|
| 228 |
+
|
| 229 |
+
static void *_cffi_exports[_CFFI_NUM_EXPORTS];
|
| 230 |
+
|
| 231 |
+
#define _cffi_type(index) ( \
|
| 232 |
+
assert((((uintptr_t)_cffi_types[index]) & 1) == 0), \
|
| 233 |
+
(struct _cffi_ctypedescr *)_cffi_types[index])
|
| 234 |
+
|
| 235 |
+
static PyObject *_cffi_init(const char *module_name, Py_ssize_t version,
|
| 236 |
+
const struct _cffi_type_context_s *ctx)
|
| 237 |
+
{
|
| 238 |
+
PyObject *module, *o_arg, *new_module;
|
| 239 |
+
void *raw[] = {
|
| 240 |
+
(void *)module_name,
|
| 241 |
+
(void *)version,
|
| 242 |
+
(void *)_cffi_exports,
|
| 243 |
+
(void *)ctx,
|
| 244 |
+
};
|
| 245 |
+
|
| 246 |
+
module = PyImport_ImportModule("_cffi_backend");
|
| 247 |
+
if (module == NULL)
|
| 248 |
+
goto failure;
|
| 249 |
+
|
| 250 |
+
o_arg = PyLong_FromVoidPtr((void *)raw);
|
| 251 |
+
if (o_arg == NULL)
|
| 252 |
+
goto failure;
|
| 253 |
+
|
| 254 |
+
new_module = PyObject_CallMethod(
|
| 255 |
+
module, (char *)"_init_cffi_1_0_external_module", (char *)"O", o_arg);
|
| 256 |
+
|
| 257 |
+
Py_DECREF(o_arg);
|
| 258 |
+
Py_DECREF(module);
|
| 259 |
+
return new_module;
|
| 260 |
+
|
| 261 |
+
failure:
|
| 262 |
+
Py_XDECREF(module);
|
| 263 |
+
return NULL;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
#ifdef HAVE_WCHAR_H
|
| 268 |
+
typedef wchar_t _cffi_wchar_t;
|
| 269 |
+
#else
|
| 270 |
+
typedef uint16_t _cffi_wchar_t; /* same random pick as _cffi_backend.c */
|
| 271 |
+
#endif
|
| 272 |
+
|
| 273 |
+
_CFFI_UNUSED_FN static uint16_t _cffi_to_c_char16_t(PyObject *o)
|
| 274 |
+
{
|
| 275 |
+
if (sizeof(_cffi_wchar_t) == 2)
|
| 276 |
+
return (uint16_t)_cffi_to_c_wchar_t(o);
|
| 277 |
+
else
|
| 278 |
+
return (uint16_t)_cffi_to_c_wchar3216_t(o);
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
_CFFI_UNUSED_FN static PyObject *_cffi_from_c_char16_t(uint16_t x)
|
| 282 |
+
{
|
| 283 |
+
if (sizeof(_cffi_wchar_t) == 2)
|
| 284 |
+
return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
|
| 285 |
+
else
|
| 286 |
+
return _cffi_from_c_wchar3216_t((int)x);
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
_CFFI_UNUSED_FN static int _cffi_to_c_char32_t(PyObject *o)
|
| 290 |
+
{
|
| 291 |
+
if (sizeof(_cffi_wchar_t) == 4)
|
| 292 |
+
return (int)_cffi_to_c_wchar_t(o);
|
| 293 |
+
else
|
| 294 |
+
return (int)_cffi_to_c_wchar3216_t(o);
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
_CFFI_UNUSED_FN static PyObject *_cffi_from_c_char32_t(unsigned int x)
|
| 298 |
+
{
|
| 299 |
+
if (sizeof(_cffi_wchar_t) == 4)
|
| 300 |
+
return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
|
| 301 |
+
else
|
| 302 |
+
return _cffi_from_c_wchar3216_t((int)x);
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
union _cffi_union_alignment_u {
|
| 306 |
+
unsigned char m_char;
|
| 307 |
+
unsigned short m_short;
|
| 308 |
+
unsigned int m_int;
|
| 309 |
+
unsigned long m_long;
|
| 310 |
+
unsigned long long m_longlong;
|
| 311 |
+
float m_float;
|
| 312 |
+
double m_double;
|
| 313 |
+
long double m_longdouble;
|
| 314 |
+
};
|
| 315 |
+
|
| 316 |
+
struct _cffi_freeme_s {
|
| 317 |
+
struct _cffi_freeme_s *next;
|
| 318 |
+
union _cffi_union_alignment_u alignment;
|
| 319 |
+
};
|
| 320 |
+
|
| 321 |
+
_CFFI_UNUSED_FN static int
|
| 322 |
+
_cffi_convert_array_argument(struct _cffi_ctypedescr *ctptr, PyObject *arg,
|
| 323 |
+
char **output_data, Py_ssize_t datasize,
|
| 324 |
+
struct _cffi_freeme_s **freeme)
|
| 325 |
+
{
|
| 326 |
+
char *p;
|
| 327 |
+
if (datasize < 0)
|
| 328 |
+
return -1;
|
| 329 |
+
|
| 330 |
+
p = *output_data;
|
| 331 |
+
if (p == NULL) {
|
| 332 |
+
struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc(
|
| 333 |
+
offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize);
|
| 334 |
+
if (fp == NULL)
|
| 335 |
+
return -1;
|
| 336 |
+
fp->next = *freeme;
|
| 337 |
+
*freeme = fp;
|
| 338 |
+
p = *output_data = (char *)&fp->alignment;
|
| 339 |
+
}
|
| 340 |
+
memset((void *)p, 0, (size_t)datasize);
|
| 341 |
+
return _cffi_convert_array_from_object(p, ctptr, arg);
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
_CFFI_UNUSED_FN static void
|
| 345 |
+
_cffi_free_array_arguments(struct _cffi_freeme_s *freeme)
|
| 346 |
+
{
|
| 347 |
+
do {
|
| 348 |
+
void *p = (void *)freeme;
|
| 349 |
+
freeme = freeme->next;
|
| 350 |
+
PyObject_Free(p);
|
| 351 |
+
} while (freeme != NULL);
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
/********** end CPython-specific section **********/
|
| 355 |
+
#else
|
| 356 |
+
_CFFI_UNUSED_FN
|
| 357 |
+
static void (*_cffi_call_python_org)(struct _cffi_externpy_s *, char *);
|
| 358 |
+
# define _cffi_call_python _cffi_call_python_org
|
| 359 |
+
#endif
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
#define _cffi_array_len(array) (sizeof(array) / sizeof((array)[0]))
|
| 363 |
+
|
| 364 |
+
#define _cffi_prim_int(size, sign) \
|
| 365 |
+
((size) == 1 ? ((sign) ? _CFFI_PRIM_INT8 : _CFFI_PRIM_UINT8) : \
|
| 366 |
+
(size) == 2 ? ((sign) ? _CFFI_PRIM_INT16 : _CFFI_PRIM_UINT16) : \
|
| 367 |
+
(size) == 4 ? ((sign) ? _CFFI_PRIM_INT32 : _CFFI_PRIM_UINT32) : \
|
| 368 |
+
(size) == 8 ? ((sign) ? _CFFI_PRIM_INT64 : _CFFI_PRIM_UINT64) : \
|
| 369 |
+
_CFFI__UNKNOWN_PRIM)
|
| 370 |
+
|
| 371 |
+
#define _cffi_prim_float(size) \
|
| 372 |
+
((size) == sizeof(float) ? _CFFI_PRIM_FLOAT : \
|
| 373 |
+
(size) == sizeof(double) ? _CFFI_PRIM_DOUBLE : \
|
| 374 |
+
(size) == sizeof(long double) ? _CFFI__UNKNOWN_LONG_DOUBLE : \
|
| 375 |
+
_CFFI__UNKNOWN_FLOAT_PRIM)
|
| 376 |
+
|
| 377 |
+
#define _cffi_check_int(got, got_nonpos, expected) \
|
| 378 |
+
((got_nonpos) == (expected <= 0) && \
|
| 379 |
+
(got) == (unsigned long long)expected)
|
| 380 |
+
|
| 381 |
+
#ifdef MS_WIN32
|
| 382 |
+
# define _cffi_stdcall __stdcall
|
| 383 |
+
#else
|
| 384 |
+
# define _cffi_stdcall /* nothing */
|
| 385 |
+
#endif
|
| 386 |
+
|
| 387 |
+
#ifdef __cplusplus
|
| 388 |
+
}
|
| 389 |
+
#endif
|
.venv/lib/python3.11/site-packages/cffi/_imp_emulation.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
try:
|
| 3 |
+
# this works on Python < 3.12
|
| 4 |
+
from imp import *
|
| 5 |
+
|
| 6 |
+
except ImportError:
|
| 7 |
+
# this is a limited emulation for Python >= 3.12.
|
| 8 |
+
# Note that this is used only for tests or for the old ffi.verify().
|
| 9 |
+
# This is copied from the source code of Python 3.11.
|
| 10 |
+
|
| 11 |
+
from _imp import (acquire_lock, release_lock,
|
| 12 |
+
is_builtin, is_frozen)
|
| 13 |
+
|
| 14 |
+
from importlib._bootstrap import _load
|
| 15 |
+
|
| 16 |
+
from importlib import machinery
|
| 17 |
+
import os
|
| 18 |
+
import sys
|
| 19 |
+
import tokenize
|
| 20 |
+
|
| 21 |
+
SEARCH_ERROR = 0
|
| 22 |
+
PY_SOURCE = 1
|
| 23 |
+
PY_COMPILED = 2
|
| 24 |
+
C_EXTENSION = 3
|
| 25 |
+
PY_RESOURCE = 4
|
| 26 |
+
PKG_DIRECTORY = 5
|
| 27 |
+
C_BUILTIN = 6
|
| 28 |
+
PY_FROZEN = 7
|
| 29 |
+
PY_CODERESOURCE = 8
|
| 30 |
+
IMP_HOOK = 9
|
| 31 |
+
|
| 32 |
+
def get_suffixes():
|
| 33 |
+
extensions = [(s, 'rb', C_EXTENSION)
|
| 34 |
+
for s in machinery.EXTENSION_SUFFIXES]
|
| 35 |
+
source = [(s, 'r', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES]
|
| 36 |
+
bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES]
|
| 37 |
+
return extensions + source + bytecode
|
| 38 |
+
|
| 39 |
+
def find_module(name, path=None):
|
| 40 |
+
if not isinstance(name, str):
|
| 41 |
+
raise TypeError("'name' must be a str, not {}".format(type(name)))
|
| 42 |
+
elif not isinstance(path, (type(None), list)):
|
| 43 |
+
# Backwards-compatibility
|
| 44 |
+
raise RuntimeError("'path' must be None or a list, "
|
| 45 |
+
"not {}".format(type(path)))
|
| 46 |
+
|
| 47 |
+
if path is None:
|
| 48 |
+
if is_builtin(name):
|
| 49 |
+
return None, None, ('', '', C_BUILTIN)
|
| 50 |
+
elif is_frozen(name):
|
| 51 |
+
return None, None, ('', '', PY_FROZEN)
|
| 52 |
+
else:
|
| 53 |
+
path = sys.path
|
| 54 |
+
|
| 55 |
+
for entry in path:
|
| 56 |
+
package_directory = os.path.join(entry, name)
|
| 57 |
+
for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]:
|
| 58 |
+
package_file_name = '__init__' + suffix
|
| 59 |
+
file_path = os.path.join(package_directory, package_file_name)
|
| 60 |
+
if os.path.isfile(file_path):
|
| 61 |
+
return None, package_directory, ('', '', PKG_DIRECTORY)
|
| 62 |
+
for suffix, mode, type_ in get_suffixes():
|
| 63 |
+
file_name = name + suffix
|
| 64 |
+
file_path = os.path.join(entry, file_name)
|
| 65 |
+
if os.path.isfile(file_path):
|
| 66 |
+
break
|
| 67 |
+
else:
|
| 68 |
+
continue
|
| 69 |
+
break # Break out of outer loop when breaking out of inner loop.
|
| 70 |
+
else:
|
| 71 |
+
raise ImportError(name, name=name)
|
| 72 |
+
|
| 73 |
+
encoding = None
|
| 74 |
+
if 'b' not in mode:
|
| 75 |
+
with open(file_path, 'rb') as file:
|
| 76 |
+
encoding = tokenize.detect_encoding(file.readline)[0]
|
| 77 |
+
file = open(file_path, mode, encoding=encoding)
|
| 78 |
+
return file, file_path, (suffix, mode, type_)
|
| 79 |
+
|
| 80 |
+
def load_dynamic(name, path, file=None):
|
| 81 |
+
loader = machinery.ExtensionFileLoader(name, path)
|
| 82 |
+
spec = machinery.ModuleSpec(name=name, loader=loader, origin=path)
|
| 83 |
+
return _load(spec)
|
.venv/lib/python3.11/site-packages/cffi/_shimmed_dist_utils.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Temporary shim module to indirect the bits of distutils we need from setuptools/distutils while providing useful
|
| 3 |
+
error messages beyond `No module named 'distutils' on Python >= 3.12, or when setuptools' vendored distutils is broken.
|
| 4 |
+
|
| 5 |
+
This is a compromise to avoid a hard-dep on setuptools for Python >= 3.12, since many users don't need runtime compilation support from CFFI.
|
| 6 |
+
"""
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
# import setuptools first; this is the most robust way to ensure its embedded distutils is available
|
| 11 |
+
# (the .pth shim should usually work, but this is even more robust)
|
| 12 |
+
import setuptools
|
| 13 |
+
except Exception as ex:
|
| 14 |
+
if sys.version_info >= (3, 12):
|
| 15 |
+
# Python 3.12 has no built-in distutils to fall back on, so any import problem is fatal
|
| 16 |
+
raise Exception("This CFFI feature requires setuptools on Python >= 3.12. The setuptools module is missing or non-functional.") from ex
|
| 17 |
+
|
| 18 |
+
# silently ignore on older Pythons (support fallback to stdlib distutils where available)
|
| 19 |
+
else:
|
| 20 |
+
del setuptools
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# bring in just the bits of distutils we need, whether they really came from setuptools or stdlib-embedded distutils
|
| 24 |
+
from distutils import log, sysconfig
|
| 25 |
+
from distutils.ccompiler import CCompiler
|
| 26 |
+
from distutils.command.build_ext import build_ext
|
| 27 |
+
from distutils.core import Distribution, Extension
|
| 28 |
+
from distutils.dir_util import mkpath
|
| 29 |
+
from distutils.errors import DistutilsSetupError, CompileError, LinkError
|
| 30 |
+
from distutils.log import set_threshold, set_verbosity
|
| 31 |
+
|
| 32 |
+
if sys.platform == 'win32':
|
| 33 |
+
try:
|
| 34 |
+
# FUTURE: msvc9compiler module was removed in setuptools 74; consider removing, as it's only used by an ancient patch in `recompiler`
|
| 35 |
+
from distutils.msvc9compiler import MSVCCompiler
|
| 36 |
+
except ImportError:
|
| 37 |
+
MSVCCompiler = None
|
| 38 |
+
except Exception as ex:
|
| 39 |
+
if sys.version_info >= (3, 12):
|
| 40 |
+
raise Exception("This CFFI feature requires setuptools on Python >= 3.12. Please install the setuptools package.") from ex
|
| 41 |
+
|
| 42 |
+
# anything older, just let the underlying distutils import error fly
|
| 43 |
+
raise Exception("This CFFI feature requires distutils. Please install the distutils or setuptools package.") from ex
|
| 44 |
+
|
| 45 |
+
del sys
|
.venv/lib/python3.11/site-packages/cffi/api.py
ADDED
|
@@ -0,0 +1,967 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys, types
|
| 2 |
+
from .lock import allocate_lock
|
| 3 |
+
from .error import CDefError
|
| 4 |
+
from . import model
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
callable
|
| 8 |
+
except NameError:
|
| 9 |
+
# Python 3.1
|
| 10 |
+
from collections import Callable
|
| 11 |
+
callable = lambda x: isinstance(x, Callable)
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
basestring
|
| 15 |
+
except NameError:
|
| 16 |
+
# Python 3.x
|
| 17 |
+
basestring = str
|
| 18 |
+
|
| 19 |
+
_unspecified = object()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class FFI(object):
|
| 24 |
+
r'''
|
| 25 |
+
The main top-level class that you instantiate once, or once per module.
|
| 26 |
+
|
| 27 |
+
Example usage:
|
| 28 |
+
|
| 29 |
+
ffi = FFI()
|
| 30 |
+
ffi.cdef("""
|
| 31 |
+
int printf(const char *, ...);
|
| 32 |
+
""")
|
| 33 |
+
|
| 34 |
+
C = ffi.dlopen(None) # standard library
|
| 35 |
+
-or-
|
| 36 |
+
C = ffi.verify() # use a C compiler: verify the decl above is right
|
| 37 |
+
|
| 38 |
+
C.printf("hello, %s!\n", ffi.new("char[]", "world"))
|
| 39 |
+
'''
|
| 40 |
+
|
| 41 |
+
def __init__(self, backend=None):
|
| 42 |
+
"""Create an FFI instance. The 'backend' argument is used to
|
| 43 |
+
select a non-default backend, mostly for tests.
|
| 44 |
+
"""
|
| 45 |
+
if backend is None:
|
| 46 |
+
# You need PyPy (>= 2.0 beta), or a CPython (>= 2.6) with
|
| 47 |
+
# _cffi_backend.so compiled.
|
| 48 |
+
import _cffi_backend as backend
|
| 49 |
+
from . import __version__
|
| 50 |
+
if backend.__version__ != __version__:
|
| 51 |
+
# bad version! Try to be as explicit as possible.
|
| 52 |
+
if hasattr(backend, '__file__'):
|
| 53 |
+
# CPython
|
| 54 |
+
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." % (
|
| 55 |
+
__version__, __file__,
|
| 56 |
+
backend.__version__, backend.__file__))
|
| 57 |
+
else:
|
| 58 |
+
# PyPy
|
| 59 |
+
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." % (
|
| 60 |
+
__version__, __file__, backend.__version__))
|
| 61 |
+
# (If you insist you can also try to pass the option
|
| 62 |
+
# 'backend=backend_ctypes.CTypesBackend()', but don't
|
| 63 |
+
# rely on it! It's probably not going to work well.)
|
| 64 |
+
|
| 65 |
+
from . import cparser
|
| 66 |
+
self._backend = backend
|
| 67 |
+
self._lock = allocate_lock()
|
| 68 |
+
self._parser = cparser.Parser()
|
| 69 |
+
self._cached_btypes = {}
|
| 70 |
+
self._parsed_types = types.ModuleType('parsed_types').__dict__
|
| 71 |
+
self._new_types = types.ModuleType('new_types').__dict__
|
| 72 |
+
self._function_caches = []
|
| 73 |
+
self._libraries = []
|
| 74 |
+
self._cdefsources = []
|
| 75 |
+
self._included_ffis = []
|
| 76 |
+
self._windows_unicode = None
|
| 77 |
+
self._init_once_cache = {}
|
| 78 |
+
self._cdef_version = None
|
| 79 |
+
self._embedding = None
|
| 80 |
+
self._typecache = model.get_typecache(backend)
|
| 81 |
+
if hasattr(backend, 'set_ffi'):
|
| 82 |
+
backend.set_ffi(self)
|
| 83 |
+
for name in list(backend.__dict__):
|
| 84 |
+
if name.startswith('RTLD_'):
|
| 85 |
+
setattr(self, name, getattr(backend, name))
|
| 86 |
+
#
|
| 87 |
+
with self._lock:
|
| 88 |
+
self.BVoidP = self._get_cached_btype(model.voidp_type)
|
| 89 |
+
self.BCharA = self._get_cached_btype(model.char_array_type)
|
| 90 |
+
if isinstance(backend, types.ModuleType):
|
| 91 |
+
# _cffi_backend: attach these constants to the class
|
| 92 |
+
if not hasattr(FFI, 'NULL'):
|
| 93 |
+
FFI.NULL = self.cast(self.BVoidP, 0)
|
| 94 |
+
FFI.CData, FFI.CType = backend._get_types()
|
| 95 |
+
else:
|
| 96 |
+
# ctypes backend: attach these constants to the instance
|
| 97 |
+
self.NULL = self.cast(self.BVoidP, 0)
|
| 98 |
+
self.CData, self.CType = backend._get_types()
|
| 99 |
+
self.buffer = backend.buffer
|
| 100 |
+
|
| 101 |
+
def cdef(self, csource, override=False, packed=False, pack=None):
|
| 102 |
+
"""Parse the given C source. This registers all declared functions,
|
| 103 |
+
types, and global variables. The functions and global variables can
|
| 104 |
+
then be accessed via either 'ffi.dlopen()' or 'ffi.verify()'.
|
| 105 |
+
The types can be used in 'ffi.new()' and other functions.
|
| 106 |
+
If 'packed' is specified as True, all structs declared inside this
|
| 107 |
+
cdef are packed, i.e. laid out without any field alignment at all.
|
| 108 |
+
Alternatively, 'pack' can be a small integer, and requests for
|
| 109 |
+
alignment greater than that are ignored (pack=1 is equivalent to
|
| 110 |
+
packed=True).
|
| 111 |
+
"""
|
| 112 |
+
self._cdef(csource, override=override, packed=packed, pack=pack)
|
| 113 |
+
|
| 114 |
+
def embedding_api(self, csource, packed=False, pack=None):
|
| 115 |
+
self._cdef(csource, packed=packed, pack=pack, dllexport=True)
|
| 116 |
+
if self._embedding is None:
|
| 117 |
+
self._embedding = ''
|
| 118 |
+
|
| 119 |
+
def _cdef(self, csource, override=False, **options):
|
| 120 |
+
if not isinstance(csource, str): # unicode, on Python 2
|
| 121 |
+
if not isinstance(csource, basestring):
|
| 122 |
+
raise TypeError("cdef() argument must be a string")
|
| 123 |
+
csource = csource.encode('ascii')
|
| 124 |
+
with self._lock:
|
| 125 |
+
self._cdef_version = object()
|
| 126 |
+
self._parser.parse(csource, override=override, **options)
|
| 127 |
+
self._cdefsources.append(csource)
|
| 128 |
+
if override:
|
| 129 |
+
for cache in self._function_caches:
|
| 130 |
+
cache.clear()
|
| 131 |
+
finishlist = self._parser._recomplete
|
| 132 |
+
if finishlist:
|
| 133 |
+
self._parser._recomplete = []
|
| 134 |
+
for tp in finishlist:
|
| 135 |
+
tp.finish_backend_type(self, finishlist)
|
| 136 |
+
|
| 137 |
+
def dlopen(self, name, flags=0):
|
| 138 |
+
"""Load and return a dynamic library identified by 'name'.
|
| 139 |
+
The standard C library can be loaded by passing None.
|
| 140 |
+
Note that functions and types declared by 'ffi.cdef()' are not
|
| 141 |
+
linked to a particular library, just like C headers; in the
|
| 142 |
+
library we only look for the actual (untyped) symbols.
|
| 143 |
+
"""
|
| 144 |
+
if not (isinstance(name, basestring) or
|
| 145 |
+
name is None or
|
| 146 |
+
isinstance(name, self.CData)):
|
| 147 |
+
raise TypeError("dlopen(name): name must be a file name, None, "
|
| 148 |
+
"or an already-opened 'void *' handle")
|
| 149 |
+
with self._lock:
|
| 150 |
+
lib, function_cache = _make_ffi_library(self, name, flags)
|
| 151 |
+
self._function_caches.append(function_cache)
|
| 152 |
+
self._libraries.append(lib)
|
| 153 |
+
return lib
|
| 154 |
+
|
| 155 |
+
def dlclose(self, lib):
|
| 156 |
+
"""Close a library obtained with ffi.dlopen(). After this call,
|
| 157 |
+
access to functions or variables from the library will fail
|
| 158 |
+
(possibly with a segmentation fault).
|
| 159 |
+
"""
|
| 160 |
+
type(lib).__cffi_close__(lib)
|
| 161 |
+
|
| 162 |
+
def _typeof_locked(self, cdecl):
|
| 163 |
+
# call me with the lock!
|
| 164 |
+
key = cdecl
|
| 165 |
+
if key in self._parsed_types:
|
| 166 |
+
return self._parsed_types[key]
|
| 167 |
+
#
|
| 168 |
+
if not isinstance(cdecl, str): # unicode, on Python 2
|
| 169 |
+
cdecl = cdecl.encode('ascii')
|
| 170 |
+
#
|
| 171 |
+
type = self._parser.parse_type(cdecl)
|
| 172 |
+
really_a_function_type = type.is_raw_function
|
| 173 |
+
if really_a_function_type:
|
| 174 |
+
type = type.as_function_pointer()
|
| 175 |
+
btype = self._get_cached_btype(type)
|
| 176 |
+
result = btype, really_a_function_type
|
| 177 |
+
self._parsed_types[key] = result
|
| 178 |
+
return result
|
| 179 |
+
|
| 180 |
+
def _typeof(self, cdecl, consider_function_as_funcptr=False):
|
| 181 |
+
# string -> ctype object
|
| 182 |
+
try:
|
| 183 |
+
result = self._parsed_types[cdecl]
|
| 184 |
+
except KeyError:
|
| 185 |
+
with self._lock:
|
| 186 |
+
result = self._typeof_locked(cdecl)
|
| 187 |
+
#
|
| 188 |
+
btype, really_a_function_type = result
|
| 189 |
+
if really_a_function_type and not consider_function_as_funcptr:
|
| 190 |
+
raise CDefError("the type %r is a function type, not a "
|
| 191 |
+
"pointer-to-function type" % (cdecl,))
|
| 192 |
+
return btype
|
| 193 |
+
|
| 194 |
+
def typeof(self, cdecl):
|
| 195 |
+
"""Parse the C type given as a string and return the
|
| 196 |
+
corresponding <ctype> object.
|
| 197 |
+
It can also be used on 'cdata' instance to get its C type.
|
| 198 |
+
"""
|
| 199 |
+
if isinstance(cdecl, basestring):
|
| 200 |
+
return self._typeof(cdecl)
|
| 201 |
+
if isinstance(cdecl, self.CData):
|
| 202 |
+
return self._backend.typeof(cdecl)
|
| 203 |
+
if isinstance(cdecl, types.BuiltinFunctionType):
|
| 204 |
+
res = _builtin_function_type(cdecl)
|
| 205 |
+
if res is not None:
|
| 206 |
+
return res
|
| 207 |
+
if (isinstance(cdecl, types.FunctionType)
|
| 208 |
+
and hasattr(cdecl, '_cffi_base_type')):
|
| 209 |
+
with self._lock:
|
| 210 |
+
return self._get_cached_btype(cdecl._cffi_base_type)
|
| 211 |
+
raise TypeError(type(cdecl))
|
| 212 |
+
|
| 213 |
+
def sizeof(self, cdecl):
|
| 214 |
+
"""Return the size in bytes of the argument. It can be a
|
| 215 |
+
string naming a C type, or a 'cdata' instance.
|
| 216 |
+
"""
|
| 217 |
+
if isinstance(cdecl, basestring):
|
| 218 |
+
BType = self._typeof(cdecl)
|
| 219 |
+
return self._backend.sizeof(BType)
|
| 220 |
+
else:
|
| 221 |
+
return self._backend.sizeof(cdecl)
|
| 222 |
+
|
| 223 |
+
def alignof(self, cdecl):
|
| 224 |
+
"""Return the natural alignment size in bytes of the C type
|
| 225 |
+
given as a string.
|
| 226 |
+
"""
|
| 227 |
+
if isinstance(cdecl, basestring):
|
| 228 |
+
cdecl = self._typeof(cdecl)
|
| 229 |
+
return self._backend.alignof(cdecl)
|
| 230 |
+
|
| 231 |
+
def offsetof(self, cdecl, *fields_or_indexes):
|
| 232 |
+
"""Return the offset of the named field inside the given
|
| 233 |
+
structure or array, which must be given as a C type name.
|
| 234 |
+
You can give several field names in case of nested structures.
|
| 235 |
+
You can also give numeric values which correspond to array
|
| 236 |
+
items, in case of an array type.
|
| 237 |
+
"""
|
| 238 |
+
if isinstance(cdecl, basestring):
|
| 239 |
+
cdecl = self._typeof(cdecl)
|
| 240 |
+
return self._typeoffsetof(cdecl, *fields_or_indexes)[1]
|
| 241 |
+
|
| 242 |
+
def new(self, cdecl, init=None):
|
| 243 |
+
"""Allocate an instance according to the specified C type and
|
| 244 |
+
return a pointer to it. The specified C type must be either a
|
| 245 |
+
pointer or an array: ``new('X *')`` allocates an X and returns
|
| 246 |
+
a pointer to it, whereas ``new('X[n]')`` allocates an array of
|
| 247 |
+
n X'es and returns an array referencing it (which works
|
| 248 |
+
mostly like a pointer, like in C). You can also use
|
| 249 |
+
``new('X[]', n)`` to allocate an array of a non-constant
|
| 250 |
+
length n.
|
| 251 |
+
|
| 252 |
+
The memory is initialized following the rules of declaring a
|
| 253 |
+
global variable in C: by default it is zero-initialized, but
|
| 254 |
+
an explicit initializer can be given which can be used to
|
| 255 |
+
fill all or part of the memory.
|
| 256 |
+
|
| 257 |
+
When the returned <cdata> object goes out of scope, the memory
|
| 258 |
+
is freed. In other words the returned <cdata> object has
|
| 259 |
+
ownership of the value of type 'cdecl' that it points to. This
|
| 260 |
+
means that the raw data can be used as long as this object is
|
| 261 |
+
kept alive, but must not be used for a longer time. Be careful
|
| 262 |
+
about that when copying the pointer to the memory somewhere
|
| 263 |
+
else, e.g. into another structure.
|
| 264 |
+
"""
|
| 265 |
+
if isinstance(cdecl, basestring):
|
| 266 |
+
cdecl = self._typeof(cdecl)
|
| 267 |
+
return self._backend.newp(cdecl, init)
|
| 268 |
+
|
| 269 |
+
def new_allocator(self, alloc=None, free=None,
|
| 270 |
+
should_clear_after_alloc=True):
|
| 271 |
+
"""Return a new allocator, i.e. a function that behaves like ffi.new()
|
| 272 |
+
but uses the provided low-level 'alloc' and 'free' functions.
|
| 273 |
+
|
| 274 |
+
'alloc' is called with the size as argument. If it returns NULL, a
|
| 275 |
+
MemoryError is raised. 'free' is called with the result of 'alloc'
|
| 276 |
+
as argument. Both can be either Python function or directly C
|
| 277 |
+
functions. If 'free' is None, then no free function is called.
|
| 278 |
+
If both 'alloc' and 'free' are None, the default is used.
|
| 279 |
+
|
| 280 |
+
If 'should_clear_after_alloc' is set to False, then the memory
|
| 281 |
+
returned by 'alloc' is assumed to be already cleared (or you are
|
| 282 |
+
fine with garbage); otherwise CFFI will clear it.
|
| 283 |
+
"""
|
| 284 |
+
compiled_ffi = self._backend.FFI()
|
| 285 |
+
allocator = compiled_ffi.new_allocator(alloc, free,
|
| 286 |
+
should_clear_after_alloc)
|
| 287 |
+
def allocate(cdecl, init=None):
|
| 288 |
+
if isinstance(cdecl, basestring):
|
| 289 |
+
cdecl = self._typeof(cdecl)
|
| 290 |
+
return allocator(cdecl, init)
|
| 291 |
+
return allocate
|
| 292 |
+
|
| 293 |
+
def cast(self, cdecl, source):
|
| 294 |
+
"""Similar to a C cast: returns an instance of the named C
|
| 295 |
+
type initialized with the given 'source'. The source is
|
| 296 |
+
casted between integers or pointers of any type.
|
| 297 |
+
"""
|
| 298 |
+
if isinstance(cdecl, basestring):
|
| 299 |
+
cdecl = self._typeof(cdecl)
|
| 300 |
+
return self._backend.cast(cdecl, source)
|
| 301 |
+
|
| 302 |
+
def string(self, cdata, maxlen=-1):
|
| 303 |
+
"""Return a Python string (or unicode string) from the 'cdata'.
|
| 304 |
+
If 'cdata' is a pointer or array of characters or bytes, returns
|
| 305 |
+
the null-terminated string. The returned string extends until
|
| 306 |
+
the first null character, or at most 'maxlen' characters. If
|
| 307 |
+
'cdata' is an array then 'maxlen' defaults to its length.
|
| 308 |
+
|
| 309 |
+
If 'cdata' is a pointer or array of wchar_t, returns a unicode
|
| 310 |
+
string following the same rules.
|
| 311 |
+
|
| 312 |
+
If 'cdata' is a single character or byte or a wchar_t, returns
|
| 313 |
+
it as a string or unicode string.
|
| 314 |
+
|
| 315 |
+
If 'cdata' is an enum, returns the value of the enumerator as a
|
| 316 |
+
string, or 'NUMBER' if the value is out of range.
|
| 317 |
+
"""
|
| 318 |
+
return self._backend.string(cdata, maxlen)
|
| 319 |
+
|
| 320 |
+
def unpack(self, cdata, length):
|
| 321 |
+
"""Unpack an array of C data of the given length,
|
| 322 |
+
returning a Python string/unicode/list.
|
| 323 |
+
|
| 324 |
+
If 'cdata' is a pointer to 'char', returns a byte string.
|
| 325 |
+
It does not stop at the first null. This is equivalent to:
|
| 326 |
+
ffi.buffer(cdata, length)[:]
|
| 327 |
+
|
| 328 |
+
If 'cdata' is a pointer to 'wchar_t', returns a unicode string.
|
| 329 |
+
'length' is measured in wchar_t's; it is not the size in bytes.
|
| 330 |
+
|
| 331 |
+
If 'cdata' is a pointer to anything else, returns a list of
|
| 332 |
+
'length' items. This is a faster equivalent to:
|
| 333 |
+
[cdata[i] for i in range(length)]
|
| 334 |
+
"""
|
| 335 |
+
return self._backend.unpack(cdata, length)
|
| 336 |
+
|
| 337 |
+
#def buffer(self, cdata, size=-1):
|
| 338 |
+
# """Return a read-write buffer object that references the raw C data
|
| 339 |
+
# pointed to by the given 'cdata'. The 'cdata' must be a pointer or
|
| 340 |
+
# an array. Can be passed to functions expecting a buffer, or directly
|
| 341 |
+
# manipulated with:
|
| 342 |
+
#
|
| 343 |
+
# buf[:] get a copy of it in a regular string, or
|
| 344 |
+
# buf[idx] as a single character
|
| 345 |
+
# buf[:] = ...
|
| 346 |
+
# buf[idx] = ... change the content
|
| 347 |
+
# """
|
| 348 |
+
# note that 'buffer' is a type, set on this instance by __init__
|
| 349 |
+
|
| 350 |
+
def from_buffer(self, cdecl, python_buffer=_unspecified,
|
| 351 |
+
require_writable=False):
|
| 352 |
+
"""Return a cdata of the given type pointing to the data of the
|
| 353 |
+
given Python object, which must support the buffer interface.
|
| 354 |
+
Note that this is not meant to be used on the built-in types
|
| 355 |
+
str or unicode (you can build 'char[]' arrays explicitly)
|
| 356 |
+
but only on objects containing large quantities of raw data
|
| 357 |
+
in some other format, like 'array.array' or numpy arrays.
|
| 358 |
+
|
| 359 |
+
The first argument is optional and default to 'char[]'.
|
| 360 |
+
"""
|
| 361 |
+
if python_buffer is _unspecified:
|
| 362 |
+
cdecl, python_buffer = self.BCharA, cdecl
|
| 363 |
+
elif isinstance(cdecl, basestring):
|
| 364 |
+
cdecl = self._typeof(cdecl)
|
| 365 |
+
return self._backend.from_buffer(cdecl, python_buffer,
|
| 366 |
+
require_writable)
|
| 367 |
+
|
| 368 |
+
def memmove(self, dest, src, n):
|
| 369 |
+
"""ffi.memmove(dest, src, n) copies n bytes of memory from src to dest.
|
| 370 |
+
|
| 371 |
+
Like the C function memmove(), the memory areas may overlap;
|
| 372 |
+
apart from that it behaves like the C function memcpy().
|
| 373 |
+
|
| 374 |
+
'src' can be any cdata ptr or array, or any Python buffer object.
|
| 375 |
+
'dest' can be any cdata ptr or array, or a writable Python buffer
|
| 376 |
+
object. The size to copy, 'n', is always measured in bytes.
|
| 377 |
+
|
| 378 |
+
Unlike other methods, this one supports all Python buffer including
|
| 379 |
+
byte strings and bytearrays---but it still does not support
|
| 380 |
+
non-contiguous buffers.
|
| 381 |
+
"""
|
| 382 |
+
return self._backend.memmove(dest, src, n)
|
| 383 |
+
|
| 384 |
+
def callback(self, cdecl, python_callable=None, error=None, onerror=None):
|
| 385 |
+
"""Return a callback object or a decorator making such a
|
| 386 |
+
callback object. 'cdecl' must name a C function pointer type.
|
| 387 |
+
The callback invokes the specified 'python_callable' (which may
|
| 388 |
+
be provided either directly or via a decorator). Important: the
|
| 389 |
+
callback object must be manually kept alive for as long as the
|
| 390 |
+
callback may be invoked from the C level.
|
| 391 |
+
"""
|
| 392 |
+
def callback_decorator_wrap(python_callable):
|
| 393 |
+
if not callable(python_callable):
|
| 394 |
+
raise TypeError("the 'python_callable' argument "
|
| 395 |
+
"is not callable")
|
| 396 |
+
return self._backend.callback(cdecl, python_callable,
|
| 397 |
+
error, onerror)
|
| 398 |
+
if isinstance(cdecl, basestring):
|
| 399 |
+
cdecl = self._typeof(cdecl, consider_function_as_funcptr=True)
|
| 400 |
+
if python_callable is None:
|
| 401 |
+
return callback_decorator_wrap # decorator mode
|
| 402 |
+
else:
|
| 403 |
+
return callback_decorator_wrap(python_callable) # direct mode
|
| 404 |
+
|
| 405 |
+
def getctype(self, cdecl, replace_with=''):
|
| 406 |
+
"""Return a string giving the C type 'cdecl', which may be itself
|
| 407 |
+
a string or a <ctype> object. If 'replace_with' is given, it gives
|
| 408 |
+
extra text to append (or insert for more complicated C types), like
|
| 409 |
+
a variable name, or '*' to get actually the C type 'pointer-to-cdecl'.
|
| 410 |
+
"""
|
| 411 |
+
if isinstance(cdecl, basestring):
|
| 412 |
+
cdecl = self._typeof(cdecl)
|
| 413 |
+
replace_with = replace_with.strip()
|
| 414 |
+
if (replace_with.startswith('*')
|
| 415 |
+
and '&[' in self._backend.getcname(cdecl, '&')):
|
| 416 |
+
replace_with = '(%s)' % replace_with
|
| 417 |
+
elif replace_with and not replace_with[0] in '[(':
|
| 418 |
+
replace_with = ' ' + replace_with
|
| 419 |
+
return self._backend.getcname(cdecl, replace_with)
|
| 420 |
+
|
| 421 |
+
def gc(self, cdata, destructor, size=0):
|
| 422 |
+
"""Return a new cdata object that points to the same
|
| 423 |
+
data. Later, when this new cdata object is garbage-collected,
|
| 424 |
+
'destructor(old_cdata_object)' will be called.
|
| 425 |
+
|
| 426 |
+
The optional 'size' gives an estimate of the size, used to
|
| 427 |
+
trigger the garbage collection more eagerly. So far only used
|
| 428 |
+
on PyPy. It tells the GC that the returned object keeps alive
|
| 429 |
+
roughly 'size' bytes of external memory.
|
| 430 |
+
"""
|
| 431 |
+
return self._backend.gcp(cdata, destructor, size)
|
| 432 |
+
|
| 433 |
+
def _get_cached_btype(self, type):
|
| 434 |
+
assert self._lock.acquire(False) is False
|
| 435 |
+
# call me with the lock!
|
| 436 |
+
try:
|
| 437 |
+
BType = self._cached_btypes[type]
|
| 438 |
+
except KeyError:
|
| 439 |
+
finishlist = []
|
| 440 |
+
BType = type.get_cached_btype(self, finishlist)
|
| 441 |
+
for type in finishlist:
|
| 442 |
+
type.finish_backend_type(self, finishlist)
|
| 443 |
+
return BType
|
| 444 |
+
|
| 445 |
+
def verify(self, source='', tmpdir=None, **kwargs):
|
| 446 |
+
"""Verify that the current ffi signatures compile on this
|
| 447 |
+
machine, and return a dynamic library object. The dynamic
|
| 448 |
+
library can be used to call functions and access global
|
| 449 |
+
variables declared in this 'ffi'. The library is compiled
|
| 450 |
+
by the C compiler: it gives you C-level API compatibility
|
| 451 |
+
(including calling macros). This is unlike 'ffi.dlopen()',
|
| 452 |
+
which requires binary compatibility in the signatures.
|
| 453 |
+
"""
|
| 454 |
+
from .verifier import Verifier, _caller_dir_pycache
|
| 455 |
+
#
|
| 456 |
+
# If set_unicode(True) was called, insert the UNICODE and
|
| 457 |
+
# _UNICODE macro declarations
|
| 458 |
+
if self._windows_unicode:
|
| 459 |
+
self._apply_windows_unicode(kwargs)
|
| 460 |
+
#
|
| 461 |
+
# Set the tmpdir here, and not in Verifier.__init__: it picks
|
| 462 |
+
# up the caller's directory, which we want to be the caller of
|
| 463 |
+
# ffi.verify(), as opposed to the caller of Veritier().
|
| 464 |
+
tmpdir = tmpdir or _caller_dir_pycache()
|
| 465 |
+
#
|
| 466 |
+
# Make a Verifier() and use it to load the library.
|
| 467 |
+
self.verifier = Verifier(self, source, tmpdir, **kwargs)
|
| 468 |
+
lib = self.verifier.load_library()
|
| 469 |
+
#
|
| 470 |
+
# Save the loaded library for keep-alive purposes, even
|
| 471 |
+
# if the caller doesn't keep it alive itself (it should).
|
| 472 |
+
self._libraries.append(lib)
|
| 473 |
+
return lib
|
| 474 |
+
|
| 475 |
+
def _get_errno(self):
|
| 476 |
+
return self._backend.get_errno()
|
| 477 |
+
def _set_errno(self, errno):
|
| 478 |
+
self._backend.set_errno(errno)
|
| 479 |
+
errno = property(_get_errno, _set_errno, None,
|
| 480 |
+
"the value of 'errno' from/to the C calls")
|
| 481 |
+
|
| 482 |
+
def getwinerror(self, code=-1):
|
| 483 |
+
return self._backend.getwinerror(code)
|
| 484 |
+
|
| 485 |
+
def _pointer_to(self, ctype):
|
| 486 |
+
with self._lock:
|
| 487 |
+
return model.pointer_cache(self, ctype)
|
| 488 |
+
|
| 489 |
+
def addressof(self, cdata, *fields_or_indexes):
|
| 490 |
+
"""Return the address of a <cdata 'struct-or-union'>.
|
| 491 |
+
If 'fields_or_indexes' are given, returns the address of that
|
| 492 |
+
field or array item in the structure or array, recursively in
|
| 493 |
+
case of nested structures.
|
| 494 |
+
"""
|
| 495 |
+
try:
|
| 496 |
+
ctype = self._backend.typeof(cdata)
|
| 497 |
+
except TypeError:
|
| 498 |
+
if '__addressof__' in type(cdata).__dict__:
|
| 499 |
+
return type(cdata).__addressof__(cdata, *fields_or_indexes)
|
| 500 |
+
raise
|
| 501 |
+
if fields_or_indexes:
|
| 502 |
+
ctype, offset = self._typeoffsetof(ctype, *fields_or_indexes)
|
| 503 |
+
else:
|
| 504 |
+
if ctype.kind == "pointer":
|
| 505 |
+
raise TypeError("addressof(pointer)")
|
| 506 |
+
offset = 0
|
| 507 |
+
ctypeptr = self._pointer_to(ctype)
|
| 508 |
+
return self._backend.rawaddressof(ctypeptr, cdata, offset)
|
| 509 |
+
|
| 510 |
+
def _typeoffsetof(self, ctype, field_or_index, *fields_or_indexes):
|
| 511 |
+
ctype, offset = self._backend.typeoffsetof(ctype, field_or_index)
|
| 512 |
+
for field1 in fields_or_indexes:
|
| 513 |
+
ctype, offset1 = self._backend.typeoffsetof(ctype, field1, 1)
|
| 514 |
+
offset += offset1
|
| 515 |
+
return ctype, offset
|
| 516 |
+
|
| 517 |
+
def include(self, ffi_to_include):
|
| 518 |
+
"""Includes the typedefs, structs, unions and enums defined
|
| 519 |
+
in another FFI instance. Usage is similar to a #include in C,
|
| 520 |
+
where a part of the program might include types defined in
|
| 521 |
+
another part for its own usage. Note that the include()
|
| 522 |
+
method has no effect on functions, constants and global
|
| 523 |
+
variables, which must anyway be accessed directly from the
|
| 524 |
+
lib object returned by the original FFI instance.
|
| 525 |
+
"""
|
| 526 |
+
if not isinstance(ffi_to_include, FFI):
|
| 527 |
+
raise TypeError("ffi.include() expects an argument that is also of"
|
| 528 |
+
" type cffi.FFI, not %r" % (
|
| 529 |
+
type(ffi_to_include).__name__,))
|
| 530 |
+
if ffi_to_include is self:
|
| 531 |
+
raise ValueError("self.include(self)")
|
| 532 |
+
with ffi_to_include._lock:
|
| 533 |
+
with self._lock:
|
| 534 |
+
self._parser.include(ffi_to_include._parser)
|
| 535 |
+
self._cdefsources.append('[')
|
| 536 |
+
self._cdefsources.extend(ffi_to_include._cdefsources)
|
| 537 |
+
self._cdefsources.append(']')
|
| 538 |
+
self._included_ffis.append(ffi_to_include)
|
| 539 |
+
|
| 540 |
+
def new_handle(self, x):
|
| 541 |
+
return self._backend.newp_handle(self.BVoidP, x)
|
| 542 |
+
|
| 543 |
+
def from_handle(self, x):
|
| 544 |
+
return self._backend.from_handle(x)
|
| 545 |
+
|
| 546 |
+
def release(self, x):
|
| 547 |
+
self._backend.release(x)
|
| 548 |
+
|
| 549 |
+
def set_unicode(self, enabled_flag):
|
| 550 |
+
"""Windows: if 'enabled_flag' is True, enable the UNICODE and
|
| 551 |
+
_UNICODE defines in C, and declare the types like TCHAR and LPTCSTR
|
| 552 |
+
to be (pointers to) wchar_t. If 'enabled_flag' is False,
|
| 553 |
+
declare these types to be (pointers to) plain 8-bit characters.
|
| 554 |
+
This is mostly for backward compatibility; you usually want True.
|
| 555 |
+
"""
|
| 556 |
+
if self._windows_unicode is not None:
|
| 557 |
+
raise ValueError("set_unicode() can only be called once")
|
| 558 |
+
enabled_flag = bool(enabled_flag)
|
| 559 |
+
if enabled_flag:
|
| 560 |
+
self.cdef("typedef wchar_t TBYTE;"
|
| 561 |
+
"typedef wchar_t TCHAR;"
|
| 562 |
+
"typedef const wchar_t *LPCTSTR;"
|
| 563 |
+
"typedef const wchar_t *PCTSTR;"
|
| 564 |
+
"typedef wchar_t *LPTSTR;"
|
| 565 |
+
"typedef wchar_t *PTSTR;"
|
| 566 |
+
"typedef TBYTE *PTBYTE;"
|
| 567 |
+
"typedef TCHAR *PTCHAR;")
|
| 568 |
+
else:
|
| 569 |
+
self.cdef("typedef char TBYTE;"
|
| 570 |
+
"typedef char TCHAR;"
|
| 571 |
+
"typedef const char *LPCTSTR;"
|
| 572 |
+
"typedef const char *PCTSTR;"
|
| 573 |
+
"typedef char *LPTSTR;"
|
| 574 |
+
"typedef char *PTSTR;"
|
| 575 |
+
"typedef TBYTE *PTBYTE;"
|
| 576 |
+
"typedef TCHAR *PTCHAR;")
|
| 577 |
+
self._windows_unicode = enabled_flag
|
| 578 |
+
|
| 579 |
+
def _apply_windows_unicode(self, kwds):
|
| 580 |
+
defmacros = kwds.get('define_macros', ())
|
| 581 |
+
if not isinstance(defmacros, (list, tuple)):
|
| 582 |
+
raise TypeError("'define_macros' must be a list or tuple")
|
| 583 |
+
defmacros = list(defmacros) + [('UNICODE', '1'),
|
| 584 |
+
('_UNICODE', '1')]
|
| 585 |
+
kwds['define_macros'] = defmacros
|
| 586 |
+
|
| 587 |
+
def _apply_embedding_fix(self, kwds):
|
| 588 |
+
# must include an argument like "-lpython2.7" for the compiler
|
| 589 |
+
def ensure(key, value):
|
| 590 |
+
lst = kwds.setdefault(key, [])
|
| 591 |
+
if value not in lst:
|
| 592 |
+
lst.append(value)
|
| 593 |
+
#
|
| 594 |
+
if '__pypy__' in sys.builtin_module_names:
|
| 595 |
+
import os
|
| 596 |
+
if sys.platform == "win32":
|
| 597 |
+
# we need 'libpypy-c.lib'. Current distributions of
|
| 598 |
+
# pypy (>= 4.1) contain it as 'libs/python27.lib'.
|
| 599 |
+
pythonlib = "python{0[0]}{0[1]}".format(sys.version_info)
|
| 600 |
+
if hasattr(sys, 'prefix'):
|
| 601 |
+
ensure('library_dirs', os.path.join(sys.prefix, 'libs'))
|
| 602 |
+
else:
|
| 603 |
+
# we need 'libpypy-c.{so,dylib}', which should be by
|
| 604 |
+
# default located in 'sys.prefix/bin' for installed
|
| 605 |
+
# systems.
|
| 606 |
+
if sys.version_info < (3,):
|
| 607 |
+
pythonlib = "pypy-c"
|
| 608 |
+
else:
|
| 609 |
+
pythonlib = "pypy3-c"
|
| 610 |
+
if hasattr(sys, 'prefix'):
|
| 611 |
+
ensure('library_dirs', os.path.join(sys.prefix, 'bin'))
|
| 612 |
+
# On uninstalled pypy's, the libpypy-c is typically found in
|
| 613 |
+
# .../pypy/goal/.
|
| 614 |
+
if hasattr(sys, 'prefix'):
|
| 615 |
+
ensure('library_dirs', os.path.join(sys.prefix, 'pypy', 'goal'))
|
| 616 |
+
else:
|
| 617 |
+
if sys.platform == "win32":
|
| 618 |
+
template = "python%d%d"
|
| 619 |
+
if hasattr(sys, 'gettotalrefcount'):
|
| 620 |
+
template += '_d'
|
| 621 |
+
else:
|
| 622 |
+
try:
|
| 623 |
+
import sysconfig
|
| 624 |
+
except ImportError: # 2.6
|
| 625 |
+
from cffi._shimmed_dist_utils import sysconfig
|
| 626 |
+
template = "python%d.%d"
|
| 627 |
+
if sysconfig.get_config_var('DEBUG_EXT'):
|
| 628 |
+
template += sysconfig.get_config_var('DEBUG_EXT')
|
| 629 |
+
pythonlib = (template %
|
| 630 |
+
(sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
|
| 631 |
+
if hasattr(sys, 'abiflags'):
|
| 632 |
+
pythonlib += sys.abiflags
|
| 633 |
+
ensure('libraries', pythonlib)
|
| 634 |
+
if sys.platform == "win32":
|
| 635 |
+
ensure('extra_link_args', '/MANIFEST')
|
| 636 |
+
|
| 637 |
+
def set_source(self, module_name, source, source_extension='.c', **kwds):
|
| 638 |
+
import os
|
| 639 |
+
if hasattr(self, '_assigned_source'):
|
| 640 |
+
raise ValueError("set_source() cannot be called several times "
|
| 641 |
+
"per ffi object")
|
| 642 |
+
if not isinstance(module_name, basestring):
|
| 643 |
+
raise TypeError("'module_name' must be a string")
|
| 644 |
+
if os.sep in module_name or (os.altsep and os.altsep in module_name):
|
| 645 |
+
raise ValueError("'module_name' must not contain '/': use a dotted "
|
| 646 |
+
"name to make a 'package.module' location")
|
| 647 |
+
self._assigned_source = (str(module_name), source,
|
| 648 |
+
source_extension, kwds)
|
| 649 |
+
|
| 650 |
+
def set_source_pkgconfig(self, module_name, pkgconfig_libs, source,
|
| 651 |
+
source_extension='.c', **kwds):
|
| 652 |
+
from . import pkgconfig
|
| 653 |
+
if not isinstance(pkgconfig_libs, list):
|
| 654 |
+
raise TypeError("the pkgconfig_libs argument must be a list "
|
| 655 |
+
"of package names")
|
| 656 |
+
kwds2 = pkgconfig.flags_from_pkgconfig(pkgconfig_libs)
|
| 657 |
+
pkgconfig.merge_flags(kwds, kwds2)
|
| 658 |
+
self.set_source(module_name, source, source_extension, **kwds)
|
| 659 |
+
|
| 660 |
+
def distutils_extension(self, tmpdir='build', verbose=True):
|
| 661 |
+
from cffi._shimmed_dist_utils import mkpath
|
| 662 |
+
from .recompiler import recompile
|
| 663 |
+
#
|
| 664 |
+
if not hasattr(self, '_assigned_source'):
|
| 665 |
+
if hasattr(self, 'verifier'): # fallback, 'tmpdir' ignored
|
| 666 |
+
return self.verifier.get_extension()
|
| 667 |
+
raise ValueError("set_source() must be called before"
|
| 668 |
+
" distutils_extension()")
|
| 669 |
+
module_name, source, source_extension, kwds = self._assigned_source
|
| 670 |
+
if source is None:
|
| 671 |
+
raise TypeError("distutils_extension() is only for C extension "
|
| 672 |
+
"modules, not for dlopen()-style pure Python "
|
| 673 |
+
"modules")
|
| 674 |
+
mkpath(tmpdir)
|
| 675 |
+
ext, updated = recompile(self, module_name,
|
| 676 |
+
source, tmpdir=tmpdir, extradir=tmpdir,
|
| 677 |
+
source_extension=source_extension,
|
| 678 |
+
call_c_compiler=False, **kwds)
|
| 679 |
+
if verbose:
|
| 680 |
+
if updated:
|
| 681 |
+
sys.stderr.write("regenerated: %r\n" % (ext.sources[0],))
|
| 682 |
+
else:
|
| 683 |
+
sys.stderr.write("not modified: %r\n" % (ext.sources[0],))
|
| 684 |
+
return ext
|
| 685 |
+
|
| 686 |
+
def emit_c_code(self, filename):
|
| 687 |
+
from .recompiler import recompile
|
| 688 |
+
#
|
| 689 |
+
if not hasattr(self, '_assigned_source'):
|
| 690 |
+
raise ValueError("set_source() must be called before emit_c_code()")
|
| 691 |
+
module_name, source, source_extension, kwds = self._assigned_source
|
| 692 |
+
if source is None:
|
| 693 |
+
raise TypeError("emit_c_code() is only for C extension modules, "
|
| 694 |
+
"not for dlopen()-style pure Python modules")
|
| 695 |
+
recompile(self, module_name, source,
|
| 696 |
+
c_file=filename, call_c_compiler=False,
|
| 697 |
+
uses_ffiplatform=False, **kwds)
|
| 698 |
+
|
| 699 |
+
def emit_python_code(self, filename):
|
| 700 |
+
from .recompiler import recompile
|
| 701 |
+
#
|
| 702 |
+
if not hasattr(self, '_assigned_source'):
|
| 703 |
+
raise ValueError("set_source() must be called before emit_c_code()")
|
| 704 |
+
module_name, source, source_extension, kwds = self._assigned_source
|
| 705 |
+
if source is not None:
|
| 706 |
+
raise TypeError("emit_python_code() is only for dlopen()-style "
|
| 707 |
+
"pure Python modules, not for C extension modules")
|
| 708 |
+
recompile(self, module_name, source,
|
| 709 |
+
c_file=filename, call_c_compiler=False,
|
| 710 |
+
uses_ffiplatform=False, **kwds)
|
| 711 |
+
|
| 712 |
+
def compile(self, tmpdir='.', verbose=0, target=None, debug=None):
|
| 713 |
+
"""The 'target' argument gives the final file name of the
|
| 714 |
+
compiled DLL. Use '*' to force distutils' choice, suitable for
|
| 715 |
+
regular CPython C API modules. Use a file name ending in '.*'
|
| 716 |
+
to ask for the system's default extension for dynamic libraries
|
| 717 |
+
(.so/.dll/.dylib).
|
| 718 |
+
|
| 719 |
+
The default is '*' when building a non-embedded C API extension,
|
| 720 |
+
and (module_name + '.*') when building an embedded library.
|
| 721 |
+
"""
|
| 722 |
+
from .recompiler import recompile
|
| 723 |
+
#
|
| 724 |
+
if not hasattr(self, '_assigned_source'):
|
| 725 |
+
raise ValueError("set_source() must be called before compile()")
|
| 726 |
+
module_name, source, source_extension, kwds = self._assigned_source
|
| 727 |
+
return recompile(self, module_name, source, tmpdir=tmpdir,
|
| 728 |
+
target=target, source_extension=source_extension,
|
| 729 |
+
compiler_verbose=verbose, debug=debug, **kwds)
|
| 730 |
+
|
| 731 |
+
def init_once(self, func, tag):
|
| 732 |
+
# Read _init_once_cache[tag], which is either (False, lock) if
|
| 733 |
+
# we're calling the function now in some thread, or (True, result).
|
| 734 |
+
# Don't call setdefault() in most cases, to avoid allocating and
|
| 735 |
+
# immediately freeing a lock; but still use setdefaut() to avoid
|
| 736 |
+
# races.
|
| 737 |
+
try:
|
| 738 |
+
x = self._init_once_cache[tag]
|
| 739 |
+
except KeyError:
|
| 740 |
+
x = self._init_once_cache.setdefault(tag, (False, allocate_lock()))
|
| 741 |
+
# Common case: we got (True, result), so we return the result.
|
| 742 |
+
if x[0]:
|
| 743 |
+
return x[1]
|
| 744 |
+
# Else, it's a lock. Acquire it to serialize the following tests.
|
| 745 |
+
with x[1]:
|
| 746 |
+
# Read again from _init_once_cache the current status.
|
| 747 |
+
x = self._init_once_cache[tag]
|
| 748 |
+
if x[0]:
|
| 749 |
+
return x[1]
|
| 750 |
+
# Call the function and store the result back.
|
| 751 |
+
result = func()
|
| 752 |
+
self._init_once_cache[tag] = (True, result)
|
| 753 |
+
return result
|
| 754 |
+
|
| 755 |
+
def embedding_init_code(self, pysource):
|
| 756 |
+
if self._embedding:
|
| 757 |
+
raise ValueError("embedding_init_code() can only be called once")
|
| 758 |
+
# fix 'pysource' before it gets dumped into the C file:
|
| 759 |
+
# - remove empty lines at the beginning, so it starts at "line 1"
|
| 760 |
+
# - dedent, if all non-empty lines are indented
|
| 761 |
+
# - check for SyntaxErrors
|
| 762 |
+
import re
|
| 763 |
+
match = re.match(r'\s*\n', pysource)
|
| 764 |
+
if match:
|
| 765 |
+
pysource = pysource[match.end():]
|
| 766 |
+
lines = pysource.splitlines() or ['']
|
| 767 |
+
prefix = re.match(r'\s*', lines[0]).group()
|
| 768 |
+
for i in range(1, len(lines)):
|
| 769 |
+
line = lines[i]
|
| 770 |
+
if line.rstrip():
|
| 771 |
+
while not line.startswith(prefix):
|
| 772 |
+
prefix = prefix[:-1]
|
| 773 |
+
i = len(prefix)
|
| 774 |
+
lines = [line[i:]+'\n' for line in lines]
|
| 775 |
+
pysource = ''.join(lines)
|
| 776 |
+
#
|
| 777 |
+
compile(pysource, "cffi_init", "exec")
|
| 778 |
+
#
|
| 779 |
+
self._embedding = pysource
|
| 780 |
+
|
| 781 |
+
def def_extern(self, *args, **kwds):
|
| 782 |
+
raise ValueError("ffi.def_extern() is only available on API-mode FFI "
|
| 783 |
+
"objects")
|
| 784 |
+
|
| 785 |
+
def list_types(self):
|
| 786 |
+
"""Returns the user type names known to this FFI instance.
|
| 787 |
+
This returns a tuple containing three lists of names:
|
| 788 |
+
(typedef_names, names_of_structs, names_of_unions)
|
| 789 |
+
"""
|
| 790 |
+
typedefs = []
|
| 791 |
+
structs = []
|
| 792 |
+
unions = []
|
| 793 |
+
for key in self._parser._declarations:
|
| 794 |
+
if key.startswith('typedef '):
|
| 795 |
+
typedefs.append(key[8:])
|
| 796 |
+
elif key.startswith('struct '):
|
| 797 |
+
structs.append(key[7:])
|
| 798 |
+
elif key.startswith('union '):
|
| 799 |
+
unions.append(key[6:])
|
| 800 |
+
typedefs.sort()
|
| 801 |
+
structs.sort()
|
| 802 |
+
unions.sort()
|
| 803 |
+
return (typedefs, structs, unions)
|
| 804 |
+
|
| 805 |
+
|
| 806 |
+
def _load_backend_lib(backend, name, flags):
|
| 807 |
+
import os
|
| 808 |
+
if not isinstance(name, basestring):
|
| 809 |
+
if sys.platform != "win32" or name is not None:
|
| 810 |
+
return backend.load_library(name, flags)
|
| 811 |
+
name = "c" # Windows: load_library(None) fails, but this works
|
| 812 |
+
# on Python 2 (backward compatibility hack only)
|
| 813 |
+
first_error = None
|
| 814 |
+
if '.' in name or '/' in name or os.sep in name:
|
| 815 |
+
try:
|
| 816 |
+
return backend.load_library(name, flags)
|
| 817 |
+
except OSError as e:
|
| 818 |
+
first_error = e
|
| 819 |
+
import ctypes.util
|
| 820 |
+
path = ctypes.util.find_library(name)
|
| 821 |
+
if path is None:
|
| 822 |
+
if name == "c" and sys.platform == "win32" and sys.version_info >= (3,):
|
| 823 |
+
raise OSError("dlopen(None) cannot work on Windows for Python 3 "
|
| 824 |
+
"(see http://bugs.python.org/issue23606)")
|
| 825 |
+
msg = ("ctypes.util.find_library() did not manage "
|
| 826 |
+
"to locate a library called %r" % (name,))
|
| 827 |
+
if first_error is not None:
|
| 828 |
+
msg = "%s. Additionally, %s" % (first_error, msg)
|
| 829 |
+
raise OSError(msg)
|
| 830 |
+
return backend.load_library(path, flags)
|
| 831 |
+
|
| 832 |
+
def _make_ffi_library(ffi, libname, flags):
|
| 833 |
+
backend = ffi._backend
|
| 834 |
+
backendlib = _load_backend_lib(backend, libname, flags)
|
| 835 |
+
#
|
| 836 |
+
def accessor_function(name):
|
| 837 |
+
key = 'function ' + name
|
| 838 |
+
tp, _ = ffi._parser._declarations[key]
|
| 839 |
+
BType = ffi._get_cached_btype(tp)
|
| 840 |
+
value = backendlib.load_function(BType, name)
|
| 841 |
+
library.__dict__[name] = value
|
| 842 |
+
#
|
| 843 |
+
def accessor_variable(name):
|
| 844 |
+
key = 'variable ' + name
|
| 845 |
+
tp, _ = ffi._parser._declarations[key]
|
| 846 |
+
BType = ffi._get_cached_btype(tp)
|
| 847 |
+
read_variable = backendlib.read_variable
|
| 848 |
+
write_variable = backendlib.write_variable
|
| 849 |
+
setattr(FFILibrary, name, property(
|
| 850 |
+
lambda self: read_variable(BType, name),
|
| 851 |
+
lambda self, value: write_variable(BType, name, value)))
|
| 852 |
+
#
|
| 853 |
+
def addressof_var(name):
|
| 854 |
+
try:
|
| 855 |
+
return addr_variables[name]
|
| 856 |
+
except KeyError:
|
| 857 |
+
with ffi._lock:
|
| 858 |
+
if name not in addr_variables:
|
| 859 |
+
key = 'variable ' + name
|
| 860 |
+
tp, _ = ffi._parser._declarations[key]
|
| 861 |
+
BType = ffi._get_cached_btype(tp)
|
| 862 |
+
if BType.kind != 'array':
|
| 863 |
+
BType = model.pointer_cache(ffi, BType)
|
| 864 |
+
p = backendlib.load_function(BType, name)
|
| 865 |
+
addr_variables[name] = p
|
| 866 |
+
return addr_variables[name]
|
| 867 |
+
#
|
| 868 |
+
def accessor_constant(name):
|
| 869 |
+
raise NotImplementedError("non-integer constant '%s' cannot be "
|
| 870 |
+
"accessed from a dlopen() library" % (name,))
|
| 871 |
+
#
|
| 872 |
+
def accessor_int_constant(name):
|
| 873 |
+
library.__dict__[name] = ffi._parser._int_constants[name]
|
| 874 |
+
#
|
| 875 |
+
accessors = {}
|
| 876 |
+
accessors_version = [False]
|
| 877 |
+
addr_variables = {}
|
| 878 |
+
#
|
| 879 |
+
def update_accessors():
|
| 880 |
+
if accessors_version[0] is ffi._cdef_version:
|
| 881 |
+
return
|
| 882 |
+
#
|
| 883 |
+
for key, (tp, _) in ffi._parser._declarations.items():
|
| 884 |
+
if not isinstance(tp, model.EnumType):
|
| 885 |
+
tag, name = key.split(' ', 1)
|
| 886 |
+
if tag == 'function':
|
| 887 |
+
accessors[name] = accessor_function
|
| 888 |
+
elif tag == 'variable':
|
| 889 |
+
accessors[name] = accessor_variable
|
| 890 |
+
elif tag == 'constant':
|
| 891 |
+
accessors[name] = accessor_constant
|
| 892 |
+
else:
|
| 893 |
+
for i, enumname in enumerate(tp.enumerators):
|
| 894 |
+
def accessor_enum(name, tp=tp, i=i):
|
| 895 |
+
tp.check_not_partial()
|
| 896 |
+
library.__dict__[name] = tp.enumvalues[i]
|
| 897 |
+
accessors[enumname] = accessor_enum
|
| 898 |
+
for name in ffi._parser._int_constants:
|
| 899 |
+
accessors.setdefault(name, accessor_int_constant)
|
| 900 |
+
accessors_version[0] = ffi._cdef_version
|
| 901 |
+
#
|
| 902 |
+
def make_accessor(name):
|
| 903 |
+
with ffi._lock:
|
| 904 |
+
if name in library.__dict__ or name in FFILibrary.__dict__:
|
| 905 |
+
return # added by another thread while waiting for the lock
|
| 906 |
+
if name not in accessors:
|
| 907 |
+
update_accessors()
|
| 908 |
+
if name not in accessors:
|
| 909 |
+
raise AttributeError(name)
|
| 910 |
+
accessors[name](name)
|
| 911 |
+
#
|
| 912 |
+
class FFILibrary(object):
|
| 913 |
+
def __getattr__(self, name):
|
| 914 |
+
make_accessor(name)
|
| 915 |
+
return getattr(self, name)
|
| 916 |
+
def __setattr__(self, name, value):
|
| 917 |
+
try:
|
| 918 |
+
property = getattr(self.__class__, name)
|
| 919 |
+
except AttributeError:
|
| 920 |
+
make_accessor(name)
|
| 921 |
+
setattr(self, name, value)
|
| 922 |
+
else:
|
| 923 |
+
property.__set__(self, value)
|
| 924 |
+
def __dir__(self):
|
| 925 |
+
with ffi._lock:
|
| 926 |
+
update_accessors()
|
| 927 |
+
return accessors.keys()
|
| 928 |
+
def __addressof__(self, name):
|
| 929 |
+
if name in library.__dict__:
|
| 930 |
+
return library.__dict__[name]
|
| 931 |
+
if name in FFILibrary.__dict__:
|
| 932 |
+
return addressof_var(name)
|
| 933 |
+
make_accessor(name)
|
| 934 |
+
if name in library.__dict__:
|
| 935 |
+
return library.__dict__[name]
|
| 936 |
+
if name in FFILibrary.__dict__:
|
| 937 |
+
return addressof_var(name)
|
| 938 |
+
raise AttributeError("cffi library has no function or "
|
| 939 |
+
"global variable named '%s'" % (name,))
|
| 940 |
+
def __cffi_close__(self):
|
| 941 |
+
backendlib.close_lib()
|
| 942 |
+
self.__dict__.clear()
|
| 943 |
+
#
|
| 944 |
+
if isinstance(libname, basestring):
|
| 945 |
+
try:
|
| 946 |
+
if not isinstance(libname, str): # unicode, on Python 2
|
| 947 |
+
libname = libname.encode('utf-8')
|
| 948 |
+
FFILibrary.__name__ = 'FFILibrary_%s' % libname
|
| 949 |
+
except UnicodeError:
|
| 950 |
+
pass
|
| 951 |
+
library = FFILibrary()
|
| 952 |
+
return library, library.__dict__
|
| 953 |
+
|
| 954 |
+
def _builtin_function_type(func):
|
| 955 |
+
# a hack to make at least ffi.typeof(builtin_function) work,
|
| 956 |
+
# if the builtin function was obtained by 'vengine_cpy'.
|
| 957 |
+
import sys
|
| 958 |
+
try:
|
| 959 |
+
module = sys.modules[func.__module__]
|
| 960 |
+
ffi = module._cffi_original_ffi
|
| 961 |
+
types_of_builtin_funcs = module._cffi_types_of_builtin_funcs
|
| 962 |
+
tp = types_of_builtin_funcs[func]
|
| 963 |
+
except (KeyError, AttributeError, TypeError):
|
| 964 |
+
return None
|
| 965 |
+
else:
|
| 966 |
+
with ffi._lock:
|
| 967 |
+
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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from . import model
|
| 2 |
+
from .commontypes import COMMON_TYPES, resolve_common_type
|
| 3 |
+
from .error import FFIError, CDefError
|
| 4 |
+
try:
|
| 5 |
+
from . import _pycparser as pycparser
|
| 6 |
+
except ImportError:
|
| 7 |
+
import pycparser
|
| 8 |
+
import weakref, re, sys
|
| 9 |
+
|
| 10 |
+
try:
|
| 11 |
+
if sys.version_info < (3,):
|
| 12 |
+
import thread as _thread
|
| 13 |
+
else:
|
| 14 |
+
import _thread
|
| 15 |
+
lock = _thread.allocate_lock()
|
| 16 |
+
except ImportError:
|
| 17 |
+
lock = None
|
| 18 |
+
|
| 19 |
+
def _workaround_for_static_import_finders():
|
| 20 |
+
# Issue #392: packaging tools like cx_Freeze can not find these
|
| 21 |
+
# because pycparser uses exec dynamic import. This is an obscure
|
| 22 |
+
# workaround. This function is never called.
|
| 23 |
+
import pycparser.yacctab
|
| 24 |
+
import pycparser.lextab
|
| 25 |
+
|
| 26 |
+
CDEF_SOURCE_STRING = "<cdef source string>"
|
| 27 |
+
_r_comment = re.compile(r"/\*.*?\*/|//([^\n\\]|\\.)*?$",
|
| 28 |
+
re.DOTALL | re.MULTILINE)
|
| 29 |
+
_r_define = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)"
|
| 30 |
+
r"\b((?:[^\n\\]|\\.)*?)$",
|
| 31 |
+
re.DOTALL | re.MULTILINE)
|
| 32 |
+
_r_line_directive = re.compile(r"^[ \t]*#[ \t]*(?:line|\d+)\b.*$", re.MULTILINE)
|
| 33 |
+
_r_partial_enum = re.compile(r"=\s*\.\.\.\s*[,}]|\.\.\.\s*\}")
|
| 34 |
+
_r_enum_dotdotdot = re.compile(r"__dotdotdot\d+__$")
|
| 35 |
+
_r_partial_array = re.compile(r"\[\s*\.\.\.\s*\]")
|
| 36 |
+
_r_words = re.compile(r"\w+|\S")
|
| 37 |
+
_parser_cache = None
|
| 38 |
+
_r_int_literal = re.compile(r"-?0?x?[0-9a-f]+[lu]*$", re.IGNORECASE)
|
| 39 |
+
_r_stdcall1 = re.compile(r"\b(__stdcall|WINAPI)\b")
|
| 40 |
+
_r_stdcall2 = re.compile(r"[(]\s*(__stdcall|WINAPI)\b")
|
| 41 |
+
_r_cdecl = re.compile(r"\b__cdecl\b")
|
| 42 |
+
_r_extern_python = re.compile(r'\bextern\s*"'
|
| 43 |
+
r'(Python|Python\s*\+\s*C|C\s*\+\s*Python)"\s*.')
|
| 44 |
+
_r_star_const_space = re.compile( # matches "* const "
|
| 45 |
+
r"[*]\s*((const|volatile|restrict)\b\s*)+")
|
| 46 |
+
_r_int_dotdotdot = re.compile(r"(\b(int|long|short|signed|unsigned|char)\s*)+"
|
| 47 |
+
r"\.\.\.")
|
| 48 |
+
_r_float_dotdotdot = re.compile(r"\b(double|float)\s*\.\.\.")
|
| 49 |
+
|
| 50 |
+
def _get_parser():
|
| 51 |
+
global _parser_cache
|
| 52 |
+
if _parser_cache is None:
|
| 53 |
+
_parser_cache = pycparser.CParser()
|
| 54 |
+
return _parser_cache
|
| 55 |
+
|
| 56 |
+
def _workaround_for_old_pycparser(csource):
|
| 57 |
+
# Workaround for a pycparser issue (fixed between pycparser 2.10 and
|
| 58 |
+
# 2.14): "char*const***" gives us a wrong syntax tree, the same as
|
| 59 |
+
# for "char***(*const)". This means we can't tell the difference
|
| 60 |
+
# afterwards. But "char(*const(***))" gives us the right syntax
|
| 61 |
+
# tree. The issue only occurs if there are several stars in
|
| 62 |
+
# sequence with no parenthesis inbetween, just possibly qualifiers.
|
| 63 |
+
# Attempt to fix it by adding some parentheses in the source: each
|
| 64 |
+
# time we see "* const" or "* const *", we add an opening
|
| 65 |
+
# parenthesis before each star---the hard part is figuring out where
|
| 66 |
+
# to close them.
|
| 67 |
+
parts = []
|
| 68 |
+
while True:
|
| 69 |
+
match = _r_star_const_space.search(csource)
|
| 70 |
+
if not match:
|
| 71 |
+
break
|
| 72 |
+
#print repr(''.join(parts)+csource), '=>',
|
| 73 |
+
parts.append(csource[:match.start()])
|
| 74 |
+
parts.append('('); closing = ')'
|
| 75 |
+
parts.append(match.group()) # e.g. "* const "
|
| 76 |
+
endpos = match.end()
|
| 77 |
+
if csource.startswith('*', endpos):
|
| 78 |
+
parts.append('('); closing += ')'
|
| 79 |
+
level = 0
|
| 80 |
+
i = endpos
|
| 81 |
+
while i < len(csource):
|
| 82 |
+
c = csource[i]
|
| 83 |
+
if c == '(':
|
| 84 |
+
level += 1
|
| 85 |
+
elif c == ')':
|
| 86 |
+
if level == 0:
|
| 87 |
+
break
|
| 88 |
+
level -= 1
|
| 89 |
+
elif c in ',;=':
|
| 90 |
+
if level == 0:
|
| 91 |
+
break
|
| 92 |
+
i += 1
|
| 93 |
+
csource = csource[endpos:i] + closing + csource[i:]
|
| 94 |
+
#print repr(''.join(parts)+csource)
|
| 95 |
+
parts.append(csource)
|
| 96 |
+
return ''.join(parts)
|
| 97 |
+
|
| 98 |
+
def _preprocess_extern_python(csource):
|
| 99 |
+
# input: `extern "Python" int foo(int);` or
|
| 100 |
+
# `extern "Python" { int foo(int); }`
|
| 101 |
+
# output:
|
| 102 |
+
# void __cffi_extern_python_start;
|
| 103 |
+
# int foo(int);
|
| 104 |
+
# void __cffi_extern_python_stop;
|
| 105 |
+
#
|
| 106 |
+
# input: `extern "Python+C" int foo(int);`
|
| 107 |
+
# output:
|
| 108 |
+
# void __cffi_extern_python_plus_c_start;
|
| 109 |
+
# int foo(int);
|
| 110 |
+
# void __cffi_extern_python_stop;
|
| 111 |
+
parts = []
|
| 112 |
+
while True:
|
| 113 |
+
match = _r_extern_python.search(csource)
|
| 114 |
+
if not match:
|
| 115 |
+
break
|
| 116 |
+
endpos = match.end() - 1
|
| 117 |
+
#print
|
| 118 |
+
#print ''.join(parts)+csource
|
| 119 |
+
#print '=>'
|
| 120 |
+
parts.append(csource[:match.start()])
|
| 121 |
+
if 'C' in match.group(1):
|
| 122 |
+
parts.append('void __cffi_extern_python_plus_c_start; ')
|
| 123 |
+
else:
|
| 124 |
+
parts.append('void __cffi_extern_python_start; ')
|
| 125 |
+
if csource[endpos] == '{':
|
| 126 |
+
# grouping variant
|
| 127 |
+
closing = csource.find('}', endpos)
|
| 128 |
+
if closing < 0:
|
| 129 |
+
raise CDefError("'extern \"Python\" {': no '}' found")
|
| 130 |
+
if csource.find('{', endpos + 1, closing) >= 0:
|
| 131 |
+
raise NotImplementedError("cannot use { } inside a block "
|
| 132 |
+
"'extern \"Python\" { ... }'")
|
| 133 |
+
parts.append(csource[endpos+1:closing])
|
| 134 |
+
csource = csource[closing+1:]
|
| 135 |
+
else:
|
| 136 |
+
# non-grouping variant
|
| 137 |
+
semicolon = csource.find(';', endpos)
|
| 138 |
+
if semicolon < 0:
|
| 139 |
+
raise CDefError("'extern \"Python\": no ';' found")
|
| 140 |
+
parts.append(csource[endpos:semicolon+1])
|
| 141 |
+
csource = csource[semicolon+1:]
|
| 142 |
+
parts.append(' void __cffi_extern_python_stop;')
|
| 143 |
+
#print ''.join(parts)+csource
|
| 144 |
+
#print
|
| 145 |
+
parts.append(csource)
|
| 146 |
+
return ''.join(parts)
|
| 147 |
+
|
| 148 |
+
def _warn_for_string_literal(csource):
|
| 149 |
+
if '"' not in csource:
|
| 150 |
+
return
|
| 151 |
+
for line in csource.splitlines():
|
| 152 |
+
if '"' in line and not line.lstrip().startswith('#'):
|
| 153 |
+
import warnings
|
| 154 |
+
warnings.warn("String literal found in cdef() or type source. "
|
| 155 |
+
"String literals are ignored here, but you should "
|
| 156 |
+
"remove them anyway because some character sequences "
|
| 157 |
+
"confuse pre-parsing.")
|
| 158 |
+
break
|
| 159 |
+
|
| 160 |
+
def _warn_for_non_extern_non_static_global_variable(decl):
|
| 161 |
+
if not decl.storage:
|
| 162 |
+
import warnings
|
| 163 |
+
warnings.warn("Global variable '%s' in cdef(): for consistency "
|
| 164 |
+
"with C it should have a storage class specifier "
|
| 165 |
+
"(usually 'extern')" % (decl.name,))
|
| 166 |
+
|
| 167 |
+
def _remove_line_directives(csource):
|
| 168 |
+
# _r_line_directive matches whole lines, without the final \n, if they
|
| 169 |
+
# start with '#line' with some spacing allowed, or '#NUMBER'. This
|
| 170 |
+
# function stores them away and replaces them with exactly the string
|
| 171 |
+
# '#line@N', where N is the index in the list 'line_directives'.
|
| 172 |
+
line_directives = []
|
| 173 |
+
def replace(m):
|
| 174 |
+
i = len(line_directives)
|
| 175 |
+
line_directives.append(m.group())
|
| 176 |
+
return '#line@%d' % i
|
| 177 |
+
csource = _r_line_directive.sub(replace, csource)
|
| 178 |
+
return csource, line_directives
|
| 179 |
+
|
| 180 |
+
def _put_back_line_directives(csource, line_directives):
|
| 181 |
+
def replace(m):
|
| 182 |
+
s = m.group()
|
| 183 |
+
if not s.startswith('#line@'):
|
| 184 |
+
raise AssertionError("unexpected #line directive "
|
| 185 |
+
"(should have been processed and removed")
|
| 186 |
+
return line_directives[int(s[6:])]
|
| 187 |
+
return _r_line_directive.sub(replace, csource)
|
| 188 |
+
|
| 189 |
+
def _preprocess(csource):
|
| 190 |
+
# First, remove the lines of the form '#line N "filename"' because
|
| 191 |
+
# the "filename" part could confuse the rest
|
| 192 |
+
csource, line_directives = _remove_line_directives(csource)
|
| 193 |
+
# Remove comments. NOTE: this only work because the cdef() section
|
| 194 |
+
# should not contain any string literals (except in line directives)!
|
| 195 |
+
def replace_keeping_newlines(m):
|
| 196 |
+
return ' ' + m.group().count('\n') * '\n'
|
| 197 |
+
csource = _r_comment.sub(replace_keeping_newlines, csource)
|
| 198 |
+
# Remove the "#define FOO x" lines
|
| 199 |
+
macros = {}
|
| 200 |
+
for match in _r_define.finditer(csource):
|
| 201 |
+
macroname, macrovalue = match.groups()
|
| 202 |
+
macrovalue = macrovalue.replace('\\\n', '').strip()
|
| 203 |
+
macros[macroname] = macrovalue
|
| 204 |
+
csource = _r_define.sub('', csource)
|
| 205 |
+
#
|
| 206 |
+
if pycparser.__version__ < '2.14':
|
| 207 |
+
csource = _workaround_for_old_pycparser(csource)
|
| 208 |
+
#
|
| 209 |
+
# BIG HACK: replace WINAPI or __stdcall with "volatile const".
|
| 210 |
+
# It doesn't make sense for the return type of a function to be
|
| 211 |
+
# "volatile volatile const", so we abuse it to detect __stdcall...
|
| 212 |
+
# Hack number 2 is that "int(volatile *fptr)();" is not valid C
|
| 213 |
+
# syntax, so we place the "volatile" before the opening parenthesis.
|
| 214 |
+
csource = _r_stdcall2.sub(' volatile volatile const(', csource)
|
| 215 |
+
csource = _r_stdcall1.sub(' volatile volatile const ', csource)
|
| 216 |
+
csource = _r_cdecl.sub(' ', csource)
|
| 217 |
+
#
|
| 218 |
+
# Replace `extern "Python"` with start/end markers
|
| 219 |
+
csource = _preprocess_extern_python(csource)
|
| 220 |
+
#
|
| 221 |
+
# Now there should not be any string literal left; warn if we get one
|
| 222 |
+
_warn_for_string_literal(csource)
|
| 223 |
+
#
|
| 224 |
+
# Replace "[...]" with "[__dotdotdotarray__]"
|
| 225 |
+
csource = _r_partial_array.sub('[__dotdotdotarray__]', csource)
|
| 226 |
+
#
|
| 227 |
+
# Replace "...}" with "__dotdotdotNUM__}". This construction should
|
| 228 |
+
# occur only at the end of enums; at the end of structs we have "...;}"
|
| 229 |
+
# and at the end of vararg functions "...);". Also replace "=...[,}]"
|
| 230 |
+
# with ",__dotdotdotNUM__[,}]": this occurs in the enums too, when
|
| 231 |
+
# giving an unknown value.
|
| 232 |
+
matches = list(_r_partial_enum.finditer(csource))
|
| 233 |
+
for number, match in enumerate(reversed(matches)):
|
| 234 |
+
p = match.start()
|
| 235 |
+
if csource[p] == '=':
|
| 236 |
+
p2 = csource.find('...', p, match.end())
|
| 237 |
+
assert p2 > p
|
| 238 |
+
csource = '%s,__dotdotdot%d__ %s' % (csource[:p], number,
|
| 239 |
+
csource[p2+3:])
|
| 240 |
+
else:
|
| 241 |
+
assert csource[p:p+3] == '...'
|
| 242 |
+
csource = '%s __dotdotdot%d__ %s' % (csource[:p], number,
|
| 243 |
+
csource[p+3:])
|
| 244 |
+
# Replace "int ..." or "unsigned long int..." with "__dotdotdotint__"
|
| 245 |
+
csource = _r_int_dotdotdot.sub(' __dotdotdotint__ ', csource)
|
| 246 |
+
# Replace "float ..." or "double..." with "__dotdotdotfloat__"
|
| 247 |
+
csource = _r_float_dotdotdot.sub(' __dotdotdotfloat__ ', csource)
|
| 248 |
+
# Replace all remaining "..." with the same name, "__dotdotdot__",
|
| 249 |
+
# which is declared with a typedef for the purpose of C parsing.
|
| 250 |
+
csource = csource.replace('...', ' __dotdotdot__ ')
|
| 251 |
+
# Finally, put back the line directives
|
| 252 |
+
csource = _put_back_line_directives(csource, line_directives)
|
| 253 |
+
return csource, macros
|
| 254 |
+
|
| 255 |
+
def _common_type_names(csource):
|
| 256 |
+
# Look in the source for what looks like usages of types from the
|
| 257 |
+
# list of common types. A "usage" is approximated here as the
|
| 258 |
+
# appearance of the word, minus a "definition" of the type, which
|
| 259 |
+
# is the last word in a "typedef" statement. Approximative only
|
| 260 |
+
# but should be fine for all the common types.
|
| 261 |
+
look_for_words = set(COMMON_TYPES)
|
| 262 |
+
look_for_words.add(';')
|
| 263 |
+
look_for_words.add(',')
|
| 264 |
+
look_for_words.add('(')
|
| 265 |
+
look_for_words.add(')')
|
| 266 |
+
look_for_words.add('typedef')
|
| 267 |
+
words_used = set()
|
| 268 |
+
is_typedef = False
|
| 269 |
+
paren = 0
|
| 270 |
+
previous_word = ''
|
| 271 |
+
for word in _r_words.findall(csource):
|
| 272 |
+
if word in look_for_words:
|
| 273 |
+
if word == ';':
|
| 274 |
+
if is_typedef:
|
| 275 |
+
words_used.discard(previous_word)
|
| 276 |
+
look_for_words.discard(previous_word)
|
| 277 |
+
is_typedef = False
|
| 278 |
+
elif word == 'typedef':
|
| 279 |
+
is_typedef = True
|
| 280 |
+
paren = 0
|
| 281 |
+
elif word == '(':
|
| 282 |
+
paren += 1
|
| 283 |
+
elif word == ')':
|
| 284 |
+
paren -= 1
|
| 285 |
+
elif word == ',':
|
| 286 |
+
if is_typedef and paren == 0:
|
| 287 |
+
words_used.discard(previous_word)
|
| 288 |
+
look_for_words.discard(previous_word)
|
| 289 |
+
else: # word in COMMON_TYPES
|
| 290 |
+
words_used.add(word)
|
| 291 |
+
previous_word = word
|
| 292 |
+
return words_used
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
class Parser(object):
|
| 296 |
+
|
| 297 |
+
def __init__(self):
|
| 298 |
+
self._declarations = {}
|
| 299 |
+
self._included_declarations = set()
|
| 300 |
+
self._anonymous_counter = 0
|
| 301 |
+
self._structnode2type = weakref.WeakKeyDictionary()
|
| 302 |
+
self._options = {}
|
| 303 |
+
self._int_constants = {}
|
| 304 |
+
self._recomplete = []
|
| 305 |
+
self._uses_new_feature = None
|
| 306 |
+
|
| 307 |
+
def _parse(self, csource):
|
| 308 |
+
csource, macros = _preprocess(csource)
|
| 309 |
+
# XXX: for more efficiency we would need to poke into the
|
| 310 |
+
# internals of CParser... the following registers the
|
| 311 |
+
# typedefs, because their presence or absence influences the
|
| 312 |
+
# parsing itself (but what they are typedef'ed to plays no role)
|
| 313 |
+
ctn = _common_type_names(csource)
|
| 314 |
+
typenames = []
|
| 315 |
+
for name in sorted(self._declarations):
|
| 316 |
+
if name.startswith('typedef '):
|
| 317 |
+
name = name[8:]
|
| 318 |
+
typenames.append(name)
|
| 319 |
+
ctn.discard(name)
|
| 320 |
+
typenames += sorted(ctn)
|
| 321 |
+
#
|
| 322 |
+
csourcelines = []
|
| 323 |
+
csourcelines.append('# 1 "<cdef automatic initialization code>"')
|
| 324 |
+
for typename in typenames:
|
| 325 |
+
csourcelines.append('typedef int %s;' % typename)
|
| 326 |
+
csourcelines.append('typedef int __dotdotdotint__, __dotdotdotfloat__,'
|
| 327 |
+
' __dotdotdot__;')
|
| 328 |
+
# this forces pycparser to consider the following in the file
|
| 329 |
+
# called <cdef source string> from line 1
|
| 330 |
+
csourcelines.append('# 1 "%s"' % (CDEF_SOURCE_STRING,))
|
| 331 |
+
csourcelines.append(csource)
|
| 332 |
+
csourcelines.append('') # see test_missing_newline_bug
|
| 333 |
+
fullcsource = '\n'.join(csourcelines)
|
| 334 |
+
if lock is not None:
|
| 335 |
+
lock.acquire() # pycparser is not thread-safe...
|
| 336 |
+
try:
|
| 337 |
+
ast = _get_parser().parse(fullcsource)
|
| 338 |
+
except pycparser.c_parser.ParseError as e:
|
| 339 |
+
self.convert_pycparser_error(e, csource)
|
| 340 |
+
finally:
|
| 341 |
+
if lock is not None:
|
| 342 |
+
lock.release()
|
| 343 |
+
# csource will be used to find buggy source text
|
| 344 |
+
return ast, macros, csource
|
| 345 |
+
|
| 346 |
+
def _convert_pycparser_error(self, e, csource):
|
| 347 |
+
# xxx look for "<cdef source string>:NUM:" at the start of str(e)
|
| 348 |
+
# and interpret that as a line number. This will not work if
|
| 349 |
+
# the user gives explicit ``# NUM "FILE"`` directives.
|
| 350 |
+
line = None
|
| 351 |
+
msg = str(e)
|
| 352 |
+
match = re.match(r"%s:(\d+):" % (CDEF_SOURCE_STRING,), msg)
|
| 353 |
+
if match:
|
| 354 |
+
linenum = int(match.group(1), 10)
|
| 355 |
+
csourcelines = csource.splitlines()
|
| 356 |
+
if 1 <= linenum <= len(csourcelines):
|
| 357 |
+
line = csourcelines[linenum-1]
|
| 358 |
+
return line
|
| 359 |
+
|
| 360 |
+
def convert_pycparser_error(self, e, csource):
|
| 361 |
+
line = self._convert_pycparser_error(e, csource)
|
| 362 |
+
|
| 363 |
+
msg = str(e)
|
| 364 |
+
if line:
|
| 365 |
+
msg = 'cannot parse "%s"\n%s' % (line.strip(), msg)
|
| 366 |
+
else:
|
| 367 |
+
msg = 'parse error\n%s' % (msg,)
|
| 368 |
+
raise CDefError(msg)
|
| 369 |
+
|
| 370 |
+
def parse(self, csource, override=False, packed=False, pack=None,
|
| 371 |
+
dllexport=False):
|
| 372 |
+
if packed:
|
| 373 |
+
if packed != True:
|
| 374 |
+
raise ValueError("'packed' should be False or True; use "
|
| 375 |
+
"'pack' to give another value")
|
| 376 |
+
if pack:
|
| 377 |
+
raise ValueError("cannot give both 'pack' and 'packed'")
|
| 378 |
+
pack = 1
|
| 379 |
+
elif pack:
|
| 380 |
+
if pack & (pack - 1):
|
| 381 |
+
raise ValueError("'pack' must be a power of two, not %r" %
|
| 382 |
+
(pack,))
|
| 383 |
+
else:
|
| 384 |
+
pack = 0
|
| 385 |
+
prev_options = self._options
|
| 386 |
+
try:
|
| 387 |
+
self._options = {'override': override,
|
| 388 |
+
'packed': pack,
|
| 389 |
+
'dllexport': dllexport}
|
| 390 |
+
self._internal_parse(csource)
|
| 391 |
+
finally:
|
| 392 |
+
self._options = prev_options
|
| 393 |
+
|
| 394 |
+
def _internal_parse(self, csource):
|
| 395 |
+
ast, macros, csource = self._parse(csource)
|
| 396 |
+
# add the macros
|
| 397 |
+
self._process_macros(macros)
|
| 398 |
+
# find the first "__dotdotdot__" and use that as a separator
|
| 399 |
+
# between the repeated typedefs and the real csource
|
| 400 |
+
iterator = iter(ast.ext)
|
| 401 |
+
for decl in iterator:
|
| 402 |
+
if decl.name == '__dotdotdot__':
|
| 403 |
+
break
|
| 404 |
+
else:
|
| 405 |
+
assert 0
|
| 406 |
+
current_decl = None
|
| 407 |
+
#
|
| 408 |
+
try:
|
| 409 |
+
self._inside_extern_python = '__cffi_extern_python_stop'
|
| 410 |
+
for decl in iterator:
|
| 411 |
+
current_decl = decl
|
| 412 |
+
if isinstance(decl, pycparser.c_ast.Decl):
|
| 413 |
+
self._parse_decl(decl)
|
| 414 |
+
elif isinstance(decl, pycparser.c_ast.Typedef):
|
| 415 |
+
if not decl.name:
|
| 416 |
+
raise CDefError("typedef does not declare any name",
|
| 417 |
+
decl)
|
| 418 |
+
quals = 0
|
| 419 |
+
if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType) and
|
| 420 |
+
decl.type.type.names[-1].startswith('__dotdotdot')):
|
| 421 |
+
realtype = self._get_unknown_type(decl)
|
| 422 |
+
elif (isinstance(decl.type, pycparser.c_ast.PtrDecl) and
|
| 423 |
+
isinstance(decl.type.type, pycparser.c_ast.TypeDecl) and
|
| 424 |
+
isinstance(decl.type.type.type,
|
| 425 |
+
pycparser.c_ast.IdentifierType) and
|
| 426 |
+
decl.type.type.type.names[-1].startswith('__dotdotdot')):
|
| 427 |
+
realtype = self._get_unknown_ptr_type(decl)
|
| 428 |
+
else:
|
| 429 |
+
realtype, quals = self._get_type_and_quals(
|
| 430 |
+
decl.type, name=decl.name, partial_length_ok=True,
|
| 431 |
+
typedef_example="*(%s *)0" % (decl.name,))
|
| 432 |
+
self._declare('typedef ' + decl.name, realtype, quals=quals)
|
| 433 |
+
elif decl.__class__.__name__ == 'Pragma':
|
| 434 |
+
# skip pragma, only in pycparser 2.15
|
| 435 |
+
import warnings
|
| 436 |
+
warnings.warn(
|
| 437 |
+
"#pragma in cdef() are entirely ignored. "
|
| 438 |
+
"They should be removed for now, otherwise your "
|
| 439 |
+
"code might behave differently in a future version "
|
| 440 |
+
"of CFFI if #pragma support gets added. Note that "
|
| 441 |
+
"'#pragma pack' needs to be replaced with the "
|
| 442 |
+
"'packed' keyword argument to cdef().")
|
| 443 |
+
else:
|
| 444 |
+
raise CDefError("unexpected <%s>: this construct is valid "
|
| 445 |
+
"C but not valid in cdef()" %
|
| 446 |
+
decl.__class__.__name__, decl)
|
| 447 |
+
except CDefError as e:
|
| 448 |
+
if len(e.args) == 1:
|
| 449 |
+
e.args = e.args + (current_decl,)
|
| 450 |
+
raise
|
| 451 |
+
except FFIError as e:
|
| 452 |
+
msg = self._convert_pycparser_error(e, csource)
|
| 453 |
+
if msg:
|
| 454 |
+
e.args = (e.args[0] + "\n *** Err: %s" % msg,)
|
| 455 |
+
raise
|
| 456 |
+
|
| 457 |
+
def _add_constants(self, key, val):
|
| 458 |
+
if key in self._int_constants:
|
| 459 |
+
if self._int_constants[key] == val:
|
| 460 |
+
return # ignore identical double declarations
|
| 461 |
+
raise FFIError(
|
| 462 |
+
"multiple declarations of constant: %s" % (key,))
|
| 463 |
+
self._int_constants[key] = val
|
| 464 |
+
|
| 465 |
+
def _add_integer_constant(self, name, int_str):
|
| 466 |
+
int_str = int_str.lower().rstrip("ul")
|
| 467 |
+
neg = int_str.startswith('-')
|
| 468 |
+
if neg:
|
| 469 |
+
int_str = int_str[1:]
|
| 470 |
+
# "010" is not valid oct in py3
|
| 471 |
+
if (int_str.startswith("0") and int_str != '0'
|
| 472 |
+
and not int_str.startswith("0x")):
|
| 473 |
+
int_str = "0o" + int_str[1:]
|
| 474 |
+
pyvalue = int(int_str, 0)
|
| 475 |
+
if neg:
|
| 476 |
+
pyvalue = -pyvalue
|
| 477 |
+
self._add_constants(name, pyvalue)
|
| 478 |
+
self._declare('macro ' + name, pyvalue)
|
| 479 |
+
|
| 480 |
+
def _process_macros(self, macros):
|
| 481 |
+
for key, value in macros.items():
|
| 482 |
+
value = value.strip()
|
| 483 |
+
if _r_int_literal.match(value):
|
| 484 |
+
self._add_integer_constant(key, value)
|
| 485 |
+
elif value == '...':
|
| 486 |
+
self._declare('macro ' + key, value)
|
| 487 |
+
else:
|
| 488 |
+
raise CDefError(
|
| 489 |
+
'only supports one of the following syntax:\n'
|
| 490 |
+
' #define %s ... (literally dot-dot-dot)\n'
|
| 491 |
+
' #define %s NUMBER (with NUMBER an integer'
|
| 492 |
+
' constant, decimal/hex/octal)\n'
|
| 493 |
+
'got:\n'
|
| 494 |
+
' #define %s %s'
|
| 495 |
+
% (key, key, key, value))
|
| 496 |
+
|
| 497 |
+
def _declare_function(self, tp, quals, decl):
|
| 498 |
+
tp = self._get_type_pointer(tp, quals)
|
| 499 |
+
if self._options.get('dllexport'):
|
| 500 |
+
tag = 'dllexport_python '
|
| 501 |
+
elif self._inside_extern_python == '__cffi_extern_python_start':
|
| 502 |
+
tag = 'extern_python '
|
| 503 |
+
elif self._inside_extern_python == '__cffi_extern_python_plus_c_start':
|
| 504 |
+
tag = 'extern_python_plus_c '
|
| 505 |
+
else:
|
| 506 |
+
tag = 'function '
|
| 507 |
+
self._declare(tag + decl.name, tp)
|
| 508 |
+
|
| 509 |
+
def _parse_decl(self, decl):
|
| 510 |
+
node = decl.type
|
| 511 |
+
if isinstance(node, pycparser.c_ast.FuncDecl):
|
| 512 |
+
tp, quals = self._get_type_and_quals(node, name=decl.name)
|
| 513 |
+
assert isinstance(tp, model.RawFunctionType)
|
| 514 |
+
self._declare_function(tp, quals, decl)
|
| 515 |
+
else:
|
| 516 |
+
if isinstance(node, pycparser.c_ast.Struct):
|
| 517 |
+
self._get_struct_union_enum_type('struct', node)
|
| 518 |
+
elif isinstance(node, pycparser.c_ast.Union):
|
| 519 |
+
self._get_struct_union_enum_type('union', node)
|
| 520 |
+
elif isinstance(node, pycparser.c_ast.Enum):
|
| 521 |
+
self._get_struct_union_enum_type('enum', node)
|
| 522 |
+
elif not decl.name:
|
| 523 |
+
raise CDefError("construct does not declare any variable",
|
| 524 |
+
decl)
|
| 525 |
+
#
|
| 526 |
+
if decl.name:
|
| 527 |
+
tp, quals = self._get_type_and_quals(node,
|
| 528 |
+
partial_length_ok=True)
|
| 529 |
+
if tp.is_raw_function:
|
| 530 |
+
self._declare_function(tp, quals, decl)
|
| 531 |
+
elif (tp.is_integer_type() and
|
| 532 |
+
hasattr(decl, 'init') and
|
| 533 |
+
hasattr(decl.init, 'value') and
|
| 534 |
+
_r_int_literal.match(decl.init.value)):
|
| 535 |
+
self._add_integer_constant(decl.name, decl.init.value)
|
| 536 |
+
elif (tp.is_integer_type() and
|
| 537 |
+
isinstance(decl.init, pycparser.c_ast.UnaryOp) and
|
| 538 |
+
decl.init.op == '-' and
|
| 539 |
+
hasattr(decl.init.expr, 'value') and
|
| 540 |
+
_r_int_literal.match(decl.init.expr.value)):
|
| 541 |
+
self._add_integer_constant(decl.name,
|
| 542 |
+
'-' + decl.init.expr.value)
|
| 543 |
+
elif (tp is model.void_type and
|
| 544 |
+
decl.name.startswith('__cffi_extern_python_')):
|
| 545 |
+
# hack: `extern "Python"` in the C source is replaced
|
| 546 |
+
# with "void __cffi_extern_python_start;" and
|
| 547 |
+
# "void __cffi_extern_python_stop;"
|
| 548 |
+
self._inside_extern_python = decl.name
|
| 549 |
+
else:
|
| 550 |
+
if self._inside_extern_python !='__cffi_extern_python_stop':
|
| 551 |
+
raise CDefError(
|
| 552 |
+
"cannot declare constants or "
|
| 553 |
+
"variables with 'extern \"Python\"'")
|
| 554 |
+
if (quals & model.Q_CONST) and not tp.is_array_type:
|
| 555 |
+
self._declare('constant ' + decl.name, tp, quals=quals)
|
| 556 |
+
else:
|
| 557 |
+
_warn_for_non_extern_non_static_global_variable(decl)
|
| 558 |
+
self._declare('variable ' + decl.name, tp, quals=quals)
|
| 559 |
+
|
| 560 |
+
def parse_type(self, cdecl):
|
| 561 |
+
return self.parse_type_and_quals(cdecl)[0]
|
| 562 |
+
|
| 563 |
+
def parse_type_and_quals(self, cdecl):
|
| 564 |
+
ast, macros = self._parse('void __dummy(\n%s\n);' % cdecl)[:2]
|
| 565 |
+
assert not macros
|
| 566 |
+
exprnode = ast.ext[-1].type.args.params[0]
|
| 567 |
+
if isinstance(exprnode, pycparser.c_ast.ID):
|
| 568 |
+
raise CDefError("unknown identifier '%s'" % (exprnode.name,))
|
| 569 |
+
return self._get_type_and_quals(exprnode.type)
|
| 570 |
+
|
| 571 |
+
def _declare(self, name, obj, included=False, quals=0):
|
| 572 |
+
if name in self._declarations:
|
| 573 |
+
prevobj, prevquals = self._declarations[name]
|
| 574 |
+
if prevobj is obj and prevquals == quals:
|
| 575 |
+
return
|
| 576 |
+
if not self._options.get('override'):
|
| 577 |
+
raise FFIError(
|
| 578 |
+
"multiple declarations of %s (for interactive usage, "
|
| 579 |
+
"try cdef(xx, override=True))" % (name,))
|
| 580 |
+
assert '__dotdotdot__' not in name.split()
|
| 581 |
+
self._declarations[name] = (obj, quals)
|
| 582 |
+
if included:
|
| 583 |
+
self._included_declarations.add(obj)
|
| 584 |
+
|
| 585 |
+
def _extract_quals(self, type):
|
| 586 |
+
quals = 0
|
| 587 |
+
if isinstance(type, (pycparser.c_ast.TypeDecl,
|
| 588 |
+
pycparser.c_ast.PtrDecl)):
|
| 589 |
+
if 'const' in type.quals:
|
| 590 |
+
quals |= model.Q_CONST
|
| 591 |
+
if 'volatile' in type.quals:
|
| 592 |
+
quals |= model.Q_VOLATILE
|
| 593 |
+
if 'restrict' in type.quals:
|
| 594 |
+
quals |= model.Q_RESTRICT
|
| 595 |
+
return quals
|
| 596 |
+
|
| 597 |
+
def _get_type_pointer(self, type, quals, declname=None):
|
| 598 |
+
if isinstance(type, model.RawFunctionType):
|
| 599 |
+
return type.as_function_pointer()
|
| 600 |
+
if (isinstance(type, model.StructOrUnionOrEnum) and
|
| 601 |
+
type.name.startswith('$') and type.name[1:].isdigit() and
|
| 602 |
+
type.forcename is None and declname is not None):
|
| 603 |
+
return model.NamedPointerType(type, declname, quals)
|
| 604 |
+
return model.PointerType(type, quals)
|
| 605 |
+
|
| 606 |
+
def _get_type_and_quals(self, typenode, name=None, partial_length_ok=False,
|
| 607 |
+
typedef_example=None):
|
| 608 |
+
# first, dereference typedefs, if we have it already parsed, we're good
|
| 609 |
+
if (isinstance(typenode, pycparser.c_ast.TypeDecl) and
|
| 610 |
+
isinstance(typenode.type, pycparser.c_ast.IdentifierType) and
|
| 611 |
+
len(typenode.type.names) == 1 and
|
| 612 |
+
('typedef ' + typenode.type.names[0]) in self._declarations):
|
| 613 |
+
tp, quals = self._declarations['typedef ' + typenode.type.names[0]]
|
| 614 |
+
quals |= self._extract_quals(typenode)
|
| 615 |
+
return tp, quals
|
| 616 |
+
#
|
| 617 |
+
if isinstance(typenode, pycparser.c_ast.ArrayDecl):
|
| 618 |
+
# array type
|
| 619 |
+
if typenode.dim is None:
|
| 620 |
+
length = None
|
| 621 |
+
else:
|
| 622 |
+
length = self._parse_constant(
|
| 623 |
+
typenode.dim, partial_length_ok=partial_length_ok)
|
| 624 |
+
# a hack: in 'typedef int foo_t[...][...];', don't use '...' as
|
| 625 |
+
# the length but use directly the C expression that would be
|
| 626 |
+
# generated by recompiler.py. This lets the typedef be used in
|
| 627 |
+
# many more places within recompiler.py
|
| 628 |
+
if typedef_example is not None:
|
| 629 |
+
if length == '...':
|
| 630 |
+
length = '_cffi_array_len(%s)' % (typedef_example,)
|
| 631 |
+
typedef_example = "*" + typedef_example
|
| 632 |
+
#
|
| 633 |
+
tp, quals = self._get_type_and_quals(typenode.type,
|
| 634 |
+
partial_length_ok=partial_length_ok,
|
| 635 |
+
typedef_example=typedef_example)
|
| 636 |
+
return model.ArrayType(tp, length), quals
|
| 637 |
+
#
|
| 638 |
+
if isinstance(typenode, pycparser.c_ast.PtrDecl):
|
| 639 |
+
# pointer type
|
| 640 |
+
itemtype, itemquals = self._get_type_and_quals(typenode.type)
|
| 641 |
+
tp = self._get_type_pointer(itemtype, itemquals, declname=name)
|
| 642 |
+
quals = self._extract_quals(typenode)
|
| 643 |
+
return tp, quals
|
| 644 |
+
#
|
| 645 |
+
if isinstance(typenode, pycparser.c_ast.TypeDecl):
|
| 646 |
+
quals = self._extract_quals(typenode)
|
| 647 |
+
type = typenode.type
|
| 648 |
+
if isinstance(type, pycparser.c_ast.IdentifierType):
|
| 649 |
+
# assume a primitive type. get it from .names, but reduce
|
| 650 |
+
# synonyms to a single chosen combination
|
| 651 |
+
names = list(type.names)
|
| 652 |
+
if names != ['signed', 'char']: # keep this unmodified
|
| 653 |
+
prefixes = {}
|
| 654 |
+
while names:
|
| 655 |
+
name = names[0]
|
| 656 |
+
if name in ('short', 'long', 'signed', 'unsigned'):
|
| 657 |
+
prefixes[name] = prefixes.get(name, 0) + 1
|
| 658 |
+
del names[0]
|
| 659 |
+
else:
|
| 660 |
+
break
|
| 661 |
+
# ignore the 'signed' prefix below, and reorder the others
|
| 662 |
+
newnames = []
|
| 663 |
+
for prefix in ('unsigned', 'short', 'long'):
|
| 664 |
+
for i in range(prefixes.get(prefix, 0)):
|
| 665 |
+
newnames.append(prefix)
|
| 666 |
+
if not names:
|
| 667 |
+
names = ['int'] # implicitly
|
| 668 |
+
if names == ['int']: # but kill it if 'short' or 'long'
|
| 669 |
+
if 'short' in prefixes or 'long' in prefixes:
|
| 670 |
+
names = []
|
| 671 |
+
names = newnames + names
|
| 672 |
+
ident = ' '.join(names)
|
| 673 |
+
if ident == 'void':
|
| 674 |
+
return model.void_type, quals
|
| 675 |
+
if ident == '__dotdotdot__':
|
| 676 |
+
raise FFIError(':%d: bad usage of "..."' %
|
| 677 |
+
typenode.coord.line)
|
| 678 |
+
tp0, quals0 = resolve_common_type(self, ident)
|
| 679 |
+
return tp0, (quals | quals0)
|
| 680 |
+
#
|
| 681 |
+
if isinstance(type, pycparser.c_ast.Struct):
|
| 682 |
+
# 'struct foobar'
|
| 683 |
+
tp = self._get_struct_union_enum_type('struct', type, name)
|
| 684 |
+
return tp, quals
|
| 685 |
+
#
|
| 686 |
+
if isinstance(type, pycparser.c_ast.Union):
|
| 687 |
+
# 'union foobar'
|
| 688 |
+
tp = self._get_struct_union_enum_type('union', type, name)
|
| 689 |
+
return tp, quals
|
| 690 |
+
#
|
| 691 |
+
if isinstance(type, pycparser.c_ast.Enum):
|
| 692 |
+
# 'enum foobar'
|
| 693 |
+
tp = self._get_struct_union_enum_type('enum', type, name)
|
| 694 |
+
return tp, quals
|
| 695 |
+
#
|
| 696 |
+
if isinstance(typenode, pycparser.c_ast.FuncDecl):
|
| 697 |
+
# a function type
|
| 698 |
+
return self._parse_function_type(typenode, name), 0
|
| 699 |
+
#
|
| 700 |
+
# nested anonymous structs or unions end up here
|
| 701 |
+
if isinstance(typenode, pycparser.c_ast.Struct):
|
| 702 |
+
return self._get_struct_union_enum_type('struct', typenode, name,
|
| 703 |
+
nested=True), 0
|
| 704 |
+
if isinstance(typenode, pycparser.c_ast.Union):
|
| 705 |
+
return self._get_struct_union_enum_type('union', typenode, name,
|
| 706 |
+
nested=True), 0
|
| 707 |
+
#
|
| 708 |
+
raise FFIError(":%d: bad or unsupported type declaration" %
|
| 709 |
+
typenode.coord.line)
|
| 710 |
+
|
| 711 |
+
def _parse_function_type(self, typenode, funcname=None):
|
| 712 |
+
params = list(getattr(typenode.args, 'params', []))
|
| 713 |
+
for i, arg in enumerate(params):
|
| 714 |
+
if not hasattr(arg, 'type'):
|
| 715 |
+
raise CDefError("%s arg %d: unknown type '%s'"
|
| 716 |
+
" (if you meant to use the old C syntax of giving"
|
| 717 |
+
" untyped arguments, it is not supported)"
|
| 718 |
+
% (funcname or 'in expression', i + 1,
|
| 719 |
+
getattr(arg, 'name', '?')))
|
| 720 |
+
ellipsis = (
|
| 721 |
+
len(params) > 0 and
|
| 722 |
+
isinstance(params[-1].type, pycparser.c_ast.TypeDecl) and
|
| 723 |
+
isinstance(params[-1].type.type,
|
| 724 |
+
pycparser.c_ast.IdentifierType) and
|
| 725 |
+
params[-1].type.type.names == ['__dotdotdot__'])
|
| 726 |
+
if ellipsis:
|
| 727 |
+
params.pop()
|
| 728 |
+
if not params:
|
| 729 |
+
raise CDefError(
|
| 730 |
+
"%s: a function with only '(...)' as argument"
|
| 731 |
+
" is not correct C" % (funcname or 'in expression'))
|
| 732 |
+
args = [self._as_func_arg(*self._get_type_and_quals(argdeclnode.type))
|
| 733 |
+
for argdeclnode in params]
|
| 734 |
+
if not ellipsis and args == [model.void_type]:
|
| 735 |
+
args = []
|
| 736 |
+
result, quals = self._get_type_and_quals(typenode.type)
|
| 737 |
+
# the 'quals' on the result type are ignored. HACK: we absure them
|
| 738 |
+
# to detect __stdcall functions: we textually replace "__stdcall"
|
| 739 |
+
# with "volatile volatile const" above.
|
| 740 |
+
abi = None
|
| 741 |
+
if hasattr(typenode.type, 'quals'): # else, probable syntax error anyway
|
| 742 |
+
if typenode.type.quals[-3:] == ['volatile', 'volatile', 'const']:
|
| 743 |
+
abi = '__stdcall'
|
| 744 |
+
return model.RawFunctionType(tuple(args), result, ellipsis, abi)
|
| 745 |
+
|
| 746 |
+
def _as_func_arg(self, type, quals):
|
| 747 |
+
if isinstance(type, model.ArrayType):
|
| 748 |
+
return model.PointerType(type.item, quals)
|
| 749 |
+
elif isinstance(type, model.RawFunctionType):
|
| 750 |
+
return type.as_function_pointer()
|
| 751 |
+
else:
|
| 752 |
+
return type
|
| 753 |
+
|
| 754 |
+
def _get_struct_union_enum_type(self, kind, type, name=None, nested=False):
|
| 755 |
+
# First, a level of caching on the exact 'type' node of the AST.
|
| 756 |
+
# This is obscure, but needed because pycparser "unrolls" declarations
|
| 757 |
+
# such as "typedef struct { } foo_t, *foo_p" and we end up with
|
| 758 |
+
# an AST that is not a tree, but a DAG, with the "type" node of the
|
| 759 |
+
# two branches foo_t and foo_p of the trees being the same node.
|
| 760 |
+
# It's a bit silly but detecting "DAG-ness" in the AST tree seems
|
| 761 |
+
# to be the only way to distinguish this case from two independent
|
| 762 |
+
# structs. See test_struct_with_two_usages.
|
| 763 |
+
try:
|
| 764 |
+
return self._structnode2type[type]
|
| 765 |
+
except KeyError:
|
| 766 |
+
pass
|
| 767 |
+
#
|
| 768 |
+
# Note that this must handle parsing "struct foo" any number of
|
| 769 |
+
# times and always return the same StructType object. Additionally,
|
| 770 |
+
# one of these times (not necessarily the first), the fields of
|
| 771 |
+
# the struct can be specified with "struct foo { ...fields... }".
|
| 772 |
+
# If no name is given, then we have to create a new anonymous struct
|
| 773 |
+
# with no caching; in this case, the fields are either specified
|
| 774 |
+
# right now or never.
|
| 775 |
+
#
|
| 776 |
+
force_name = name
|
| 777 |
+
name = type.name
|
| 778 |
+
#
|
| 779 |
+
# get the type or create it if needed
|
| 780 |
+
if name is None:
|
| 781 |
+
# 'force_name' is used to guess a more readable name for
|
| 782 |
+
# anonymous structs, for the common case "typedef struct { } foo".
|
| 783 |
+
if force_name is not None:
|
| 784 |
+
explicit_name = '$%s' % force_name
|
| 785 |
+
else:
|
| 786 |
+
self._anonymous_counter += 1
|
| 787 |
+
explicit_name = '$%d' % self._anonymous_counter
|
| 788 |
+
tp = None
|
| 789 |
+
else:
|
| 790 |
+
explicit_name = name
|
| 791 |
+
key = '%s %s' % (kind, name)
|
| 792 |
+
tp, _ = self._declarations.get(key, (None, None))
|
| 793 |
+
#
|
| 794 |
+
if tp is None:
|
| 795 |
+
if kind == 'struct':
|
| 796 |
+
tp = model.StructType(explicit_name, None, None, None)
|
| 797 |
+
elif kind == 'union':
|
| 798 |
+
tp = model.UnionType(explicit_name, None, None, None)
|
| 799 |
+
elif kind == 'enum':
|
| 800 |
+
if explicit_name == '__dotdotdot__':
|
| 801 |
+
raise CDefError("Enums cannot be declared with ...")
|
| 802 |
+
tp = self._build_enum_type(explicit_name, type.values)
|
| 803 |
+
else:
|
| 804 |
+
raise AssertionError("kind = %r" % (kind,))
|
| 805 |
+
if name is not None:
|
| 806 |
+
self._declare(key, tp)
|
| 807 |
+
else:
|
| 808 |
+
if kind == 'enum' and type.values is not None:
|
| 809 |
+
raise NotImplementedError(
|
| 810 |
+
"enum %s: the '{}' declaration should appear on the first "
|
| 811 |
+
"time the enum is mentioned, not later" % explicit_name)
|
| 812 |
+
if not tp.forcename:
|
| 813 |
+
tp.force_the_name(force_name)
|
| 814 |
+
if tp.forcename and '$' in tp.name:
|
| 815 |
+
self._declare('anonymous %s' % tp.forcename, tp)
|
| 816 |
+
#
|
| 817 |
+
self._structnode2type[type] = tp
|
| 818 |
+
#
|
| 819 |
+
# enums: done here
|
| 820 |
+
if kind == 'enum':
|
| 821 |
+
return tp
|
| 822 |
+
#
|
| 823 |
+
# is there a 'type.decls'? If yes, then this is the place in the
|
| 824 |
+
# C sources that declare the fields. If no, then just return the
|
| 825 |
+
# existing type, possibly still incomplete.
|
| 826 |
+
if type.decls is None:
|
| 827 |
+
return tp
|
| 828 |
+
#
|
| 829 |
+
if tp.fldnames is not None:
|
| 830 |
+
raise CDefError("duplicate declaration of struct %s" % name)
|
| 831 |
+
fldnames = []
|
| 832 |
+
fldtypes = []
|
| 833 |
+
fldbitsize = []
|
| 834 |
+
fldquals = []
|
| 835 |
+
for decl in type.decls:
|
| 836 |
+
if (isinstance(decl.type, pycparser.c_ast.IdentifierType) and
|
| 837 |
+
''.join(decl.type.names) == '__dotdotdot__'):
|
| 838 |
+
# XXX pycparser is inconsistent: 'names' should be a list
|
| 839 |
+
# of strings, but is sometimes just one string. Use
|
| 840 |
+
# str.join() as a way to cope with both.
|
| 841 |
+
self._make_partial(tp, nested)
|
| 842 |
+
continue
|
| 843 |
+
if decl.bitsize is None:
|
| 844 |
+
bitsize = -1
|
| 845 |
+
else:
|
| 846 |
+
bitsize = self._parse_constant(decl.bitsize)
|
| 847 |
+
self._partial_length = False
|
| 848 |
+
type, fqual = self._get_type_and_quals(decl.type,
|
| 849 |
+
partial_length_ok=True)
|
| 850 |
+
if self._partial_length:
|
| 851 |
+
self._make_partial(tp, nested)
|
| 852 |
+
if isinstance(type, model.StructType) and type.partial:
|
| 853 |
+
self._make_partial(tp, nested)
|
| 854 |
+
fldnames.append(decl.name or '')
|
| 855 |
+
fldtypes.append(type)
|
| 856 |
+
fldbitsize.append(bitsize)
|
| 857 |
+
fldquals.append(fqual)
|
| 858 |
+
tp.fldnames = tuple(fldnames)
|
| 859 |
+
tp.fldtypes = tuple(fldtypes)
|
| 860 |
+
tp.fldbitsize = tuple(fldbitsize)
|
| 861 |
+
tp.fldquals = tuple(fldquals)
|
| 862 |
+
if fldbitsize != [-1] * len(fldbitsize):
|
| 863 |
+
if isinstance(tp, model.StructType) and tp.partial:
|
| 864 |
+
raise NotImplementedError("%s: using both bitfields and '...;'"
|
| 865 |
+
% (tp,))
|
| 866 |
+
tp.packed = self._options.get('packed')
|
| 867 |
+
if tp.completed: # must be re-completed: it is not opaque any more
|
| 868 |
+
tp.completed = 0
|
| 869 |
+
self._recomplete.append(tp)
|
| 870 |
+
return tp
|
| 871 |
+
|
| 872 |
+
def _make_partial(self, tp, nested):
|
| 873 |
+
if not isinstance(tp, model.StructOrUnion):
|
| 874 |
+
raise CDefError("%s cannot be partial" % (tp,))
|
| 875 |
+
if not tp.has_c_name() and not nested:
|
| 876 |
+
raise NotImplementedError("%s is partial but has no C name" %(tp,))
|
| 877 |
+
tp.partial = True
|
| 878 |
+
|
| 879 |
+
def _parse_constant(self, exprnode, partial_length_ok=False):
|
| 880 |
+
# for now, limited to expressions that are an immediate number
|
| 881 |
+
# or positive/negative number
|
| 882 |
+
if isinstance(exprnode, pycparser.c_ast.Constant):
|
| 883 |
+
s = exprnode.value
|
| 884 |
+
if '0' <= s[0] <= '9':
|
| 885 |
+
s = s.rstrip('uUlL')
|
| 886 |
+
try:
|
| 887 |
+
if s.startswith('0'):
|
| 888 |
+
return int(s, 8)
|
| 889 |
+
else:
|
| 890 |
+
return int(s, 10)
|
| 891 |
+
except ValueError:
|
| 892 |
+
if len(s) > 1:
|
| 893 |
+
if s.lower()[0:2] == '0x':
|
| 894 |
+
return int(s, 16)
|
| 895 |
+
elif s.lower()[0:2] == '0b':
|
| 896 |
+
return int(s, 2)
|
| 897 |
+
raise CDefError("invalid constant %r" % (s,))
|
| 898 |
+
elif s[0] == "'" and s[-1] == "'" and (
|
| 899 |
+
len(s) == 3 or (len(s) == 4 and s[1] == "\\")):
|
| 900 |
+
return ord(s[-2])
|
| 901 |
+
else:
|
| 902 |
+
raise CDefError("invalid constant %r" % (s,))
|
| 903 |
+
#
|
| 904 |
+
if (isinstance(exprnode, pycparser.c_ast.UnaryOp) and
|
| 905 |
+
exprnode.op == '+'):
|
| 906 |
+
return self._parse_constant(exprnode.expr)
|
| 907 |
+
#
|
| 908 |
+
if (isinstance(exprnode, pycparser.c_ast.UnaryOp) and
|
| 909 |
+
exprnode.op == '-'):
|
| 910 |
+
return -self._parse_constant(exprnode.expr)
|
| 911 |
+
# load previously defined int constant
|
| 912 |
+
if (isinstance(exprnode, pycparser.c_ast.ID) and
|
| 913 |
+
exprnode.name in self._int_constants):
|
| 914 |
+
return self._int_constants[exprnode.name]
|
| 915 |
+
#
|
| 916 |
+
if (isinstance(exprnode, pycparser.c_ast.ID) and
|
| 917 |
+
exprnode.name == '__dotdotdotarray__'):
|
| 918 |
+
if partial_length_ok:
|
| 919 |
+
self._partial_length = True
|
| 920 |
+
return '...'
|
| 921 |
+
raise FFIError(":%d: unsupported '[...]' here, cannot derive "
|
| 922 |
+
"the actual array length in this context"
|
| 923 |
+
% exprnode.coord.line)
|
| 924 |
+
#
|
| 925 |
+
if isinstance(exprnode, pycparser.c_ast.BinaryOp):
|
| 926 |
+
left = self._parse_constant(exprnode.left)
|
| 927 |
+
right = self._parse_constant(exprnode.right)
|
| 928 |
+
if exprnode.op == '+':
|
| 929 |
+
return left + right
|
| 930 |
+
elif exprnode.op == '-':
|
| 931 |
+
return left - right
|
| 932 |
+
elif exprnode.op == '*':
|
| 933 |
+
return left * right
|
| 934 |
+
elif exprnode.op == '/':
|
| 935 |
+
return self._c_div(left, right)
|
| 936 |
+
elif exprnode.op == '%':
|
| 937 |
+
return left - self._c_div(left, right) * right
|
| 938 |
+
elif exprnode.op == '<<':
|
| 939 |
+
return left << right
|
| 940 |
+
elif exprnode.op == '>>':
|
| 941 |
+
return left >> right
|
| 942 |
+
elif exprnode.op == '&':
|
| 943 |
+
return left & right
|
| 944 |
+
elif exprnode.op == '|':
|
| 945 |
+
return left | right
|
| 946 |
+
elif exprnode.op == '^':
|
| 947 |
+
return left ^ right
|
| 948 |
+
#
|
| 949 |
+
raise FFIError(":%d: unsupported expression: expected a "
|
| 950 |
+
"simple numeric constant" % exprnode.coord.line)
|
| 951 |
+
|
| 952 |
+
def _c_div(self, a, b):
|
| 953 |
+
result = a // b
|
| 954 |
+
if ((a < 0) ^ (b < 0)) and (a % b) != 0:
|
| 955 |
+
result += 1
|
| 956 |
+
return result
|
| 957 |
+
|
| 958 |
+
def _build_enum_type(self, explicit_name, decls):
|
| 959 |
+
if decls is not None:
|
| 960 |
+
partial = False
|
| 961 |
+
enumerators = []
|
| 962 |
+
enumvalues = []
|
| 963 |
+
nextenumvalue = 0
|
| 964 |
+
for enum in decls.enumerators:
|
| 965 |
+
if _r_enum_dotdotdot.match(enum.name):
|
| 966 |
+
partial = True
|
| 967 |
+
continue
|
| 968 |
+
if enum.value is not None:
|
| 969 |
+
nextenumvalue = self._parse_constant(enum.value)
|
| 970 |
+
enumerators.append(enum.name)
|
| 971 |
+
enumvalues.append(nextenumvalue)
|
| 972 |
+
self._add_constants(enum.name, nextenumvalue)
|
| 973 |
+
nextenumvalue += 1
|
| 974 |
+
enumerators = tuple(enumerators)
|
| 975 |
+
enumvalues = tuple(enumvalues)
|
| 976 |
+
tp = model.EnumType(explicit_name, enumerators, enumvalues)
|
| 977 |
+
tp.partial = partial
|
| 978 |
+
else: # opaque enum
|
| 979 |
+
tp = model.EnumType(explicit_name, (), ())
|
| 980 |
+
return tp
|
| 981 |
+
|
| 982 |
+
def include(self, other):
|
| 983 |
+
for name, (tp, quals) in other._declarations.items():
|
| 984 |
+
if name.startswith('anonymous $enum_$'):
|
| 985 |
+
continue # fix for test_anonymous_enum_include
|
| 986 |
+
kind = name.split(' ', 1)[0]
|
| 987 |
+
if kind in ('struct', 'union', 'enum', 'anonymous', 'typedef'):
|
| 988 |
+
self._declare(name, tp, included=True, quals=quals)
|
| 989 |
+
for k, v in other._int_constants.items():
|
| 990 |
+
self._add_constants(k, v)
|
| 991 |
+
|
| 992 |
+
def _get_unknown_type(self, decl):
|
| 993 |
+
typenames = decl.type.type.names
|
| 994 |
+
if typenames == ['__dotdotdot__']:
|
| 995 |
+
return model.unknown_type(decl.name)
|
| 996 |
+
|
| 997 |
+
if typenames == ['__dotdotdotint__']:
|
| 998 |
+
if self._uses_new_feature is None:
|
| 999 |
+
self._uses_new_feature = "'typedef int... %s'" % decl.name
|
| 1000 |
+
return model.UnknownIntegerType(decl.name)
|
| 1001 |
+
|
| 1002 |
+
if typenames == ['__dotdotdotfloat__']:
|
| 1003 |
+
# note: not for 'long double' so far
|
| 1004 |
+
if self._uses_new_feature is None:
|
| 1005 |
+
self._uses_new_feature = "'typedef float... %s'" % decl.name
|
| 1006 |
+
return model.UnknownFloatType(decl.name)
|
| 1007 |
+
|
| 1008 |
+
raise FFIError(':%d: unsupported usage of "..." in typedef'
|
| 1009 |
+
% decl.coord.line)
|
| 1010 |
+
|
| 1011 |
+
def _get_unknown_ptr_type(self, decl):
|
| 1012 |
+
if decl.type.type.type.names == ['__dotdotdot__']:
|
| 1013 |
+
return model.unknown_ptr_type(decl.name)
|
| 1014 |
+
raise FFIError(':%d: unsupported usage of "..." in typedef'
|
| 1015 |
+
% decl.coord.line)
|
.venv/lib/python3.11/site-packages/cffi/ffiplatform.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys, os
|
| 2 |
+
from .error import VerificationError
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
LIST_OF_FILE_NAMES = ['sources', 'include_dirs', 'library_dirs',
|
| 6 |
+
'extra_objects', 'depends']
|
| 7 |
+
|
| 8 |
+
def get_extension(srcfilename, modname, sources=(), **kwds):
|
| 9 |
+
from cffi._shimmed_dist_utils import Extension
|
| 10 |
+
allsources = [srcfilename]
|
| 11 |
+
for src in sources:
|
| 12 |
+
allsources.append(os.path.normpath(src))
|
| 13 |
+
return Extension(name=modname, sources=allsources, **kwds)
|
| 14 |
+
|
| 15 |
+
def compile(tmpdir, ext, compiler_verbose=0, debug=None):
|
| 16 |
+
"""Compile a C extension module using distutils."""
|
| 17 |
+
|
| 18 |
+
saved_environ = os.environ.copy()
|
| 19 |
+
try:
|
| 20 |
+
outputfilename = _build(tmpdir, ext, compiler_verbose, debug)
|
| 21 |
+
outputfilename = os.path.abspath(outputfilename)
|
| 22 |
+
finally:
|
| 23 |
+
# workaround for a distutils bugs where some env vars can
|
| 24 |
+
# become longer and longer every time it is used
|
| 25 |
+
for key, value in saved_environ.items():
|
| 26 |
+
if os.environ.get(key) != value:
|
| 27 |
+
os.environ[key] = value
|
| 28 |
+
return outputfilename
|
| 29 |
+
|
| 30 |
+
def _build(tmpdir, ext, compiler_verbose=0, debug=None):
|
| 31 |
+
# XXX compact but horrible :-(
|
| 32 |
+
from cffi._shimmed_dist_utils import Distribution, CompileError, LinkError, set_threshold, set_verbosity
|
| 33 |
+
|
| 34 |
+
dist = Distribution({'ext_modules': [ext]})
|
| 35 |
+
dist.parse_config_files()
|
| 36 |
+
options = dist.get_option_dict('build_ext')
|
| 37 |
+
if debug is None:
|
| 38 |
+
debug = sys.flags.debug
|
| 39 |
+
options['debug'] = ('ffiplatform', debug)
|
| 40 |
+
options['force'] = ('ffiplatform', True)
|
| 41 |
+
options['build_lib'] = ('ffiplatform', tmpdir)
|
| 42 |
+
options['build_temp'] = ('ffiplatform', tmpdir)
|
| 43 |
+
#
|
| 44 |
+
try:
|
| 45 |
+
old_level = set_threshold(0) or 0
|
| 46 |
+
try:
|
| 47 |
+
set_verbosity(compiler_verbose)
|
| 48 |
+
dist.run_command('build_ext')
|
| 49 |
+
cmd_obj = dist.get_command_obj('build_ext')
|
| 50 |
+
[soname] = cmd_obj.get_outputs()
|
| 51 |
+
finally:
|
| 52 |
+
set_threshold(old_level)
|
| 53 |
+
except (CompileError, LinkError) as e:
|
| 54 |
+
raise VerificationError('%s: %s' % (e.__class__.__name__, e))
|
| 55 |
+
#
|
| 56 |
+
return soname
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
from os.path import samefile
|
| 60 |
+
except ImportError:
|
| 61 |
+
def samefile(f1, f2):
|
| 62 |
+
return os.path.abspath(f1) == os.path.abspath(f2)
|
| 63 |
+
|
| 64 |
+
def maybe_relative_path(path):
|
| 65 |
+
if not os.path.isabs(path):
|
| 66 |
+
return path # already relative
|
| 67 |
+
dir = path
|
| 68 |
+
names = []
|
| 69 |
+
while True:
|
| 70 |
+
prevdir = dir
|
| 71 |
+
dir, name = os.path.split(prevdir)
|
| 72 |
+
if dir == prevdir or not dir:
|
| 73 |
+
return path # failed to make it relative
|
| 74 |
+
names.append(name)
|
| 75 |
+
try:
|
| 76 |
+
if samefile(dir, os.curdir):
|
| 77 |
+
names.reverse()
|
| 78 |
+
return os.path.join(*names)
|
| 79 |
+
except OSError:
|
| 80 |
+
pass
|
| 81 |
+
|
| 82 |
+
# ____________________________________________________________
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
int_or_long = (int, long)
|
| 86 |
+
import cStringIO
|
| 87 |
+
except NameError:
|
| 88 |
+
int_or_long = int # Python 3
|
| 89 |
+
import io as cStringIO
|
| 90 |
+
|
| 91 |
+
def _flatten(x, f):
|
| 92 |
+
if isinstance(x, str):
|
| 93 |
+
f.write('%ds%s' % (len(x), x))
|
| 94 |
+
elif isinstance(x, dict):
|
| 95 |
+
keys = sorted(x.keys())
|
| 96 |
+
f.write('%dd' % len(keys))
|
| 97 |
+
for key in keys:
|
| 98 |
+
_flatten(key, f)
|
| 99 |
+
_flatten(x[key], f)
|
| 100 |
+
elif isinstance(x, (list, tuple)):
|
| 101 |
+
f.write('%dl' % len(x))
|
| 102 |
+
for value in x:
|
| 103 |
+
_flatten(value, f)
|
| 104 |
+
elif isinstance(x, int_or_long):
|
| 105 |
+
f.write('%di' % (x,))
|
| 106 |
+
else:
|
| 107 |
+
raise TypeError(
|
| 108 |
+
"the keywords to verify() contains unsupported object %r" % (x,))
|
| 109 |
+
|
| 110 |
+
def flatten(x):
|
| 111 |
+
f = cStringIO.StringIO()
|
| 112 |
+
_flatten(x, f)
|
| 113 |
+
return f.getvalue()
|
.venv/lib/python3.11/site-packages/cffi/model.py
ADDED
|
@@ -0,0 +1,618 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 4 |
+
from .lock import allocate_lock
|
| 5 |
+
from .error import CDefError, VerificationError, VerificationMissing
|
| 6 |
+
|
| 7 |
+
# type qualifiers
|
| 8 |
+
Q_CONST = 0x01
|
| 9 |
+
Q_RESTRICT = 0x02
|
| 10 |
+
Q_VOLATILE = 0x04
|
| 11 |
+
|
| 12 |
+
def qualify(quals, replace_with):
|
| 13 |
+
if quals & Q_CONST:
|
| 14 |
+
replace_with = ' const ' + replace_with.lstrip()
|
| 15 |
+
if quals & Q_VOLATILE:
|
| 16 |
+
replace_with = ' volatile ' + replace_with.lstrip()
|
| 17 |
+
if quals & Q_RESTRICT:
|
| 18 |
+
# It seems that __restrict is supported by gcc and msvc.
|
| 19 |
+
# If you hit some different compiler, add a #define in
|
| 20 |
+
# _cffi_include.h for it (and in its copies, documented there)
|
| 21 |
+
replace_with = ' __restrict ' + replace_with.lstrip()
|
| 22 |
+
return replace_with
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class BaseTypeByIdentity(object):
|
| 26 |
+
is_array_type = False
|
| 27 |
+
is_raw_function = False
|
| 28 |
+
|
| 29 |
+
def get_c_name(self, replace_with='', context='a C file', quals=0):
|
| 30 |
+
result = self.c_name_with_marker
|
| 31 |
+
assert result.count('&') == 1
|
| 32 |
+
# some logic duplication with ffi.getctype()... :-(
|
| 33 |
+
replace_with = replace_with.strip()
|
| 34 |
+
if replace_with:
|
| 35 |
+
if replace_with.startswith('*') and '&[' in result:
|
| 36 |
+
replace_with = '(%s)' % replace_with
|
| 37 |
+
elif not replace_with[0] in '[(':
|
| 38 |
+
replace_with = ' ' + replace_with
|
| 39 |
+
replace_with = qualify(quals, replace_with)
|
| 40 |
+
result = result.replace('&', replace_with)
|
| 41 |
+
if '$' in result:
|
| 42 |
+
raise VerificationError(
|
| 43 |
+
"cannot generate '%s' in %s: unknown type name"
|
| 44 |
+
% (self._get_c_name(), context))
|
| 45 |
+
return result
|
| 46 |
+
|
| 47 |
+
def _get_c_name(self):
|
| 48 |
+
return self.c_name_with_marker.replace('&', '')
|
| 49 |
+
|
| 50 |
+
def has_c_name(self):
|
| 51 |
+
return '$' not in self._get_c_name()
|
| 52 |
+
|
| 53 |
+
def is_integer_type(self):
|
| 54 |
+
return False
|
| 55 |
+
|
| 56 |
+
def get_cached_btype(self, ffi, finishlist, can_delay=False):
|
| 57 |
+
try:
|
| 58 |
+
BType = ffi._cached_btypes[self]
|
| 59 |
+
except KeyError:
|
| 60 |
+
BType = self.build_backend_type(ffi, finishlist)
|
| 61 |
+
BType2 = ffi._cached_btypes.setdefault(self, BType)
|
| 62 |
+
assert BType2 is BType
|
| 63 |
+
return BType
|
| 64 |
+
|
| 65 |
+
def __repr__(self):
|
| 66 |
+
return '<%s>' % (self._get_c_name(),)
|
| 67 |
+
|
| 68 |
+
def _get_items(self):
|
| 69 |
+
return [(name, getattr(self, name)) for name in self._attrs_]
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class BaseType(BaseTypeByIdentity):
|
| 73 |
+
|
| 74 |
+
def __eq__(self, other):
|
| 75 |
+
return (self.__class__ == other.__class__ and
|
| 76 |
+
self._get_items() == other._get_items())
|
| 77 |
+
|
| 78 |
+
def __ne__(self, other):
|
| 79 |
+
return not self == other
|
| 80 |
+
|
| 81 |
+
def __hash__(self):
|
| 82 |
+
return hash((self.__class__, tuple(self._get_items())))
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class VoidType(BaseType):
|
| 86 |
+
_attrs_ = ()
|
| 87 |
+
|
| 88 |
+
def __init__(self):
|
| 89 |
+
self.c_name_with_marker = 'void&'
|
| 90 |
+
|
| 91 |
+
def build_backend_type(self, ffi, finishlist):
|
| 92 |
+
return global_cache(self, ffi, 'new_void_type')
|
| 93 |
+
|
| 94 |
+
void_type = VoidType()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class BasePrimitiveType(BaseType):
|
| 98 |
+
def is_complex_type(self):
|
| 99 |
+
return False
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class PrimitiveType(BasePrimitiveType):
|
| 103 |
+
_attrs_ = ('name',)
|
| 104 |
+
|
| 105 |
+
ALL_PRIMITIVE_TYPES = {
|
| 106 |
+
'char': 'c',
|
| 107 |
+
'short': 'i',
|
| 108 |
+
'int': 'i',
|
| 109 |
+
'long': 'i',
|
| 110 |
+
'long long': 'i',
|
| 111 |
+
'signed char': 'i',
|
| 112 |
+
'unsigned char': 'i',
|
| 113 |
+
'unsigned short': 'i',
|
| 114 |
+
'unsigned int': 'i',
|
| 115 |
+
'unsigned long': 'i',
|
| 116 |
+
'unsigned long long': 'i',
|
| 117 |
+
'float': 'f',
|
| 118 |
+
'double': 'f',
|
| 119 |
+
'long double': 'f',
|
| 120 |
+
'_cffi_float_complex_t': 'j',
|
| 121 |
+
'_cffi_double_complex_t': 'j',
|
| 122 |
+
'_Bool': 'i',
|
| 123 |
+
# the following types are not primitive in the C sense
|
| 124 |
+
'wchar_t': 'c',
|
| 125 |
+
'char16_t': 'c',
|
| 126 |
+
'char32_t': 'c',
|
| 127 |
+
'int8_t': 'i',
|
| 128 |
+
'uint8_t': 'i',
|
| 129 |
+
'int16_t': 'i',
|
| 130 |
+
'uint16_t': 'i',
|
| 131 |
+
'int32_t': 'i',
|
| 132 |
+
'uint32_t': 'i',
|
| 133 |
+
'int64_t': 'i',
|
| 134 |
+
'uint64_t': 'i',
|
| 135 |
+
'int_least8_t': 'i',
|
| 136 |
+
'uint_least8_t': 'i',
|
| 137 |
+
'int_least16_t': 'i',
|
| 138 |
+
'uint_least16_t': 'i',
|
| 139 |
+
'int_least32_t': 'i',
|
| 140 |
+
'uint_least32_t': 'i',
|
| 141 |
+
'int_least64_t': 'i',
|
| 142 |
+
'uint_least64_t': 'i',
|
| 143 |
+
'int_fast8_t': 'i',
|
| 144 |
+
'uint_fast8_t': 'i',
|
| 145 |
+
'int_fast16_t': 'i',
|
| 146 |
+
'uint_fast16_t': 'i',
|
| 147 |
+
'int_fast32_t': 'i',
|
| 148 |
+
'uint_fast32_t': 'i',
|
| 149 |
+
'int_fast64_t': 'i',
|
| 150 |
+
'uint_fast64_t': 'i',
|
| 151 |
+
'intptr_t': 'i',
|
| 152 |
+
'uintptr_t': 'i',
|
| 153 |
+
'intmax_t': 'i',
|
| 154 |
+
'uintmax_t': 'i',
|
| 155 |
+
'ptrdiff_t': 'i',
|
| 156 |
+
'size_t': 'i',
|
| 157 |
+
'ssize_t': 'i',
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
def __init__(self, name):
|
| 161 |
+
assert name in self.ALL_PRIMITIVE_TYPES
|
| 162 |
+
self.name = name
|
| 163 |
+
self.c_name_with_marker = name + '&'
|
| 164 |
+
|
| 165 |
+
def is_char_type(self):
|
| 166 |
+
return self.ALL_PRIMITIVE_TYPES[self.name] == 'c'
|
| 167 |
+
def is_integer_type(self):
|
| 168 |
+
return self.ALL_PRIMITIVE_TYPES[self.name] == 'i'
|
| 169 |
+
def is_float_type(self):
|
| 170 |
+
return self.ALL_PRIMITIVE_TYPES[self.name] == 'f'
|
| 171 |
+
def is_complex_type(self):
|
| 172 |
+
return self.ALL_PRIMITIVE_TYPES[self.name] == 'j'
|
| 173 |
+
|
| 174 |
+
def build_backend_type(self, ffi, finishlist):
|
| 175 |
+
return global_cache(self, ffi, 'new_primitive_type', self.name)
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
class UnknownIntegerType(BasePrimitiveType):
|
| 179 |
+
_attrs_ = ('name',)
|
| 180 |
+
|
| 181 |
+
def __init__(self, name):
|
| 182 |
+
self.name = name
|
| 183 |
+
self.c_name_with_marker = name + '&'
|
| 184 |
+
|
| 185 |
+
def is_integer_type(self):
|
| 186 |
+
return True
|
| 187 |
+
|
| 188 |
+
def build_backend_type(self, ffi, finishlist):
|
| 189 |
+
raise NotImplementedError("integer type '%s' can only be used after "
|
| 190 |
+
"compilation" % self.name)
|
| 191 |
+
|
| 192 |
+
class UnknownFloatType(BasePrimitiveType):
|
| 193 |
+
_attrs_ = ('name', )
|
| 194 |
+
|
| 195 |
+
def __init__(self, name):
|
| 196 |
+
self.name = name
|
| 197 |
+
self.c_name_with_marker = name + '&'
|
| 198 |
+
|
| 199 |
+
def build_backend_type(self, ffi, finishlist):
|
| 200 |
+
raise NotImplementedError("float type '%s' can only be used after "
|
| 201 |
+
"compilation" % self.name)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
class BaseFunctionType(BaseType):
|
| 205 |
+
_attrs_ = ('args', 'result', 'ellipsis', 'abi')
|
| 206 |
+
|
| 207 |
+
def __init__(self, args, result, ellipsis, abi=None):
|
| 208 |
+
self.args = args
|
| 209 |
+
self.result = result
|
| 210 |
+
self.ellipsis = ellipsis
|
| 211 |
+
self.abi = abi
|
| 212 |
+
#
|
| 213 |
+
reprargs = [arg._get_c_name() for arg in self.args]
|
| 214 |
+
if self.ellipsis:
|
| 215 |
+
reprargs.append('...')
|
| 216 |
+
reprargs = reprargs or ['void']
|
| 217 |
+
replace_with = self._base_pattern % (', '.join(reprargs),)
|
| 218 |
+
if abi is not None:
|
| 219 |
+
replace_with = replace_with[:1] + abi + ' ' + replace_with[1:]
|
| 220 |
+
self.c_name_with_marker = (
|
| 221 |
+
self.result.c_name_with_marker.replace('&', replace_with))
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
class RawFunctionType(BaseFunctionType):
|
| 225 |
+
# Corresponds to a C type like 'int(int)', which is the C type of
|
| 226 |
+
# a function, but not a pointer-to-function. The backend has no
|
| 227 |
+
# notion of such a type; it's used temporarily by parsing.
|
| 228 |
+
_base_pattern = '(&)(%s)'
|
| 229 |
+
is_raw_function = True
|
| 230 |
+
|
| 231 |
+
def build_backend_type(self, ffi, finishlist):
|
| 232 |
+
raise CDefError("cannot render the type %r: it is a function "
|
| 233 |
+
"type, not a pointer-to-function type" % (self,))
|
| 234 |
+
|
| 235 |
+
def as_function_pointer(self):
|
| 236 |
+
return FunctionPtrType(self.args, self.result, self.ellipsis, self.abi)
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
class FunctionPtrType(BaseFunctionType):
|
| 240 |
+
_base_pattern = '(*&)(%s)'
|
| 241 |
+
|
| 242 |
+
def build_backend_type(self, ffi, finishlist):
|
| 243 |
+
result = self.result.get_cached_btype(ffi, finishlist)
|
| 244 |
+
args = []
|
| 245 |
+
for tp in self.args:
|
| 246 |
+
args.append(tp.get_cached_btype(ffi, finishlist))
|
| 247 |
+
abi_args = ()
|
| 248 |
+
if self.abi == "__stdcall":
|
| 249 |
+
if not self.ellipsis: # __stdcall ignored for variadic funcs
|
| 250 |
+
try:
|
| 251 |
+
abi_args = (ffi._backend.FFI_STDCALL,)
|
| 252 |
+
except AttributeError:
|
| 253 |
+
pass
|
| 254 |
+
return global_cache(self, ffi, 'new_function_type',
|
| 255 |
+
tuple(args), result, self.ellipsis, *abi_args)
|
| 256 |
+
|
| 257 |
+
def as_raw_function(self):
|
| 258 |
+
return RawFunctionType(self.args, self.result, self.ellipsis, self.abi)
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
class PointerType(BaseType):
|
| 262 |
+
_attrs_ = ('totype', 'quals')
|
| 263 |
+
|
| 264 |
+
def __init__(self, totype, quals=0):
|
| 265 |
+
self.totype = totype
|
| 266 |
+
self.quals = quals
|
| 267 |
+
extra = " *&"
|
| 268 |
+
if totype.is_array_type:
|
| 269 |
+
extra = "(%s)" % (extra.lstrip(),)
|
| 270 |
+
extra = qualify(quals, extra)
|
| 271 |
+
self.c_name_with_marker = totype.c_name_with_marker.replace('&', extra)
|
| 272 |
+
|
| 273 |
+
def build_backend_type(self, ffi, finishlist):
|
| 274 |
+
BItem = self.totype.get_cached_btype(ffi, finishlist, can_delay=True)
|
| 275 |
+
return global_cache(self, ffi, 'new_pointer_type', BItem)
|
| 276 |
+
|
| 277 |
+
voidp_type = PointerType(void_type)
|
| 278 |
+
|
| 279 |
+
def ConstPointerType(totype):
|
| 280 |
+
return PointerType(totype, Q_CONST)
|
| 281 |
+
|
| 282 |
+
const_voidp_type = ConstPointerType(void_type)
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
class NamedPointerType(PointerType):
|
| 286 |
+
_attrs_ = ('totype', 'name')
|
| 287 |
+
|
| 288 |
+
def __init__(self, totype, name, quals=0):
|
| 289 |
+
PointerType.__init__(self, totype, quals)
|
| 290 |
+
self.name = name
|
| 291 |
+
self.c_name_with_marker = name + '&'
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
class ArrayType(BaseType):
|
| 295 |
+
_attrs_ = ('item', 'length')
|
| 296 |
+
is_array_type = True
|
| 297 |
+
|
| 298 |
+
def __init__(self, item, length):
|
| 299 |
+
self.item = item
|
| 300 |
+
self.length = length
|
| 301 |
+
#
|
| 302 |
+
if length is None:
|
| 303 |
+
brackets = '&[]'
|
| 304 |
+
elif length == '...':
|
| 305 |
+
brackets = '&[/*...*/]'
|
| 306 |
+
else:
|
| 307 |
+
brackets = '&[%s]' % length
|
| 308 |
+
self.c_name_with_marker = (
|
| 309 |
+
self.item.c_name_with_marker.replace('&', brackets))
|
| 310 |
+
|
| 311 |
+
def length_is_unknown(self):
|
| 312 |
+
return isinstance(self.length, str)
|
| 313 |
+
|
| 314 |
+
def resolve_length(self, newlength):
|
| 315 |
+
return ArrayType(self.item, newlength)
|
| 316 |
+
|
| 317 |
+
def build_backend_type(self, ffi, finishlist):
|
| 318 |
+
if self.length_is_unknown():
|
| 319 |
+
raise CDefError("cannot render the type %r: unknown length" %
|
| 320 |
+
(self,))
|
| 321 |
+
self.item.get_cached_btype(ffi, finishlist) # force the item BType
|
| 322 |
+
BPtrItem = PointerType(self.item).get_cached_btype(ffi, finishlist)
|
| 323 |
+
return global_cache(self, ffi, 'new_array_type', BPtrItem, self.length)
|
| 324 |
+
|
| 325 |
+
char_array_type = ArrayType(PrimitiveType('char'), None)
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
class StructOrUnionOrEnum(BaseTypeByIdentity):
|
| 329 |
+
_attrs_ = ('name',)
|
| 330 |
+
forcename = None
|
| 331 |
+
|
| 332 |
+
def build_c_name_with_marker(self):
|
| 333 |
+
name = self.forcename or '%s %s' % (self.kind, self.name)
|
| 334 |
+
self.c_name_with_marker = name + '&'
|
| 335 |
+
|
| 336 |
+
def force_the_name(self, forcename):
|
| 337 |
+
self.forcename = forcename
|
| 338 |
+
self.build_c_name_with_marker()
|
| 339 |
+
|
| 340 |
+
def get_official_name(self):
|
| 341 |
+
assert self.c_name_with_marker.endswith('&')
|
| 342 |
+
return self.c_name_with_marker[:-1]
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
class StructOrUnion(StructOrUnionOrEnum):
|
| 346 |
+
fixedlayout = None
|
| 347 |
+
completed = 0
|
| 348 |
+
partial = False
|
| 349 |
+
packed = 0
|
| 350 |
+
|
| 351 |
+
def __init__(self, name, fldnames, fldtypes, fldbitsize, fldquals=None):
|
| 352 |
+
self.name = name
|
| 353 |
+
self.fldnames = fldnames
|
| 354 |
+
self.fldtypes = fldtypes
|
| 355 |
+
self.fldbitsize = fldbitsize
|
| 356 |
+
self.fldquals = fldquals
|
| 357 |
+
self.build_c_name_with_marker()
|
| 358 |
+
|
| 359 |
+
def anonymous_struct_fields(self):
|
| 360 |
+
if self.fldtypes is not None:
|
| 361 |
+
for name, type in zip(self.fldnames, self.fldtypes):
|
| 362 |
+
if name == '' and isinstance(type, StructOrUnion):
|
| 363 |
+
yield type
|
| 364 |
+
|
| 365 |
+
def enumfields(self, expand_anonymous_struct_union=True):
|
| 366 |
+
fldquals = self.fldquals
|
| 367 |
+
if fldquals is None:
|
| 368 |
+
fldquals = (0,) * len(self.fldnames)
|
| 369 |
+
for name, type, bitsize, quals in zip(self.fldnames, self.fldtypes,
|
| 370 |
+
self.fldbitsize, fldquals):
|
| 371 |
+
if (name == '' and isinstance(type, StructOrUnion)
|
| 372 |
+
and expand_anonymous_struct_union):
|
| 373 |
+
# nested anonymous struct/union
|
| 374 |
+
for result in type.enumfields():
|
| 375 |
+
yield result
|
| 376 |
+
else:
|
| 377 |
+
yield (name, type, bitsize, quals)
|
| 378 |
+
|
| 379 |
+
def force_flatten(self):
|
| 380 |
+
# force the struct or union to have a declaration that lists
|
| 381 |
+
# directly all fields returned by enumfields(), flattening
|
| 382 |
+
# nested anonymous structs/unions.
|
| 383 |
+
names = []
|
| 384 |
+
types = []
|
| 385 |
+
bitsizes = []
|
| 386 |
+
fldquals = []
|
| 387 |
+
for name, type, bitsize, quals in self.enumfields():
|
| 388 |
+
names.append(name)
|
| 389 |
+
types.append(type)
|
| 390 |
+
bitsizes.append(bitsize)
|
| 391 |
+
fldquals.append(quals)
|
| 392 |
+
self.fldnames = tuple(names)
|
| 393 |
+
self.fldtypes = tuple(types)
|
| 394 |
+
self.fldbitsize = tuple(bitsizes)
|
| 395 |
+
self.fldquals = tuple(fldquals)
|
| 396 |
+
|
| 397 |
+
def get_cached_btype(self, ffi, finishlist, can_delay=False):
|
| 398 |
+
BType = StructOrUnionOrEnum.get_cached_btype(self, ffi, finishlist,
|
| 399 |
+
can_delay)
|
| 400 |
+
if not can_delay:
|
| 401 |
+
self.finish_backend_type(ffi, finishlist)
|
| 402 |
+
return BType
|
| 403 |
+
|
| 404 |
+
def finish_backend_type(self, ffi, finishlist):
|
| 405 |
+
if self.completed:
|
| 406 |
+
if self.completed != 2:
|
| 407 |
+
raise NotImplementedError("recursive structure declaration "
|
| 408 |
+
"for '%s'" % (self.name,))
|
| 409 |
+
return
|
| 410 |
+
BType = ffi._cached_btypes[self]
|
| 411 |
+
#
|
| 412 |
+
self.completed = 1
|
| 413 |
+
#
|
| 414 |
+
if self.fldtypes is None:
|
| 415 |
+
pass # not completing it: it's an opaque struct
|
| 416 |
+
#
|
| 417 |
+
elif self.fixedlayout is None:
|
| 418 |
+
fldtypes = [tp.get_cached_btype(ffi, finishlist)
|
| 419 |
+
for tp in self.fldtypes]
|
| 420 |
+
lst = list(zip(self.fldnames, fldtypes, self.fldbitsize))
|
| 421 |
+
extra_flags = ()
|
| 422 |
+
if self.packed:
|
| 423 |
+
if self.packed == 1:
|
| 424 |
+
extra_flags = (8,) # SF_PACKED
|
| 425 |
+
else:
|
| 426 |
+
extra_flags = (0, self.packed)
|
| 427 |
+
ffi._backend.complete_struct_or_union(BType, lst, self,
|
| 428 |
+
-1, -1, *extra_flags)
|
| 429 |
+
#
|
| 430 |
+
else:
|
| 431 |
+
fldtypes = []
|
| 432 |
+
fieldofs, fieldsize, totalsize, totalalignment = self.fixedlayout
|
| 433 |
+
for i in range(len(self.fldnames)):
|
| 434 |
+
fsize = fieldsize[i]
|
| 435 |
+
ftype = self.fldtypes[i]
|
| 436 |
+
#
|
| 437 |
+
if isinstance(ftype, ArrayType) and ftype.length_is_unknown():
|
| 438 |
+
# fix the length to match the total size
|
| 439 |
+
BItemType = ftype.item.get_cached_btype(ffi, finishlist)
|
| 440 |
+
nlen, nrest = divmod(fsize, ffi.sizeof(BItemType))
|
| 441 |
+
if nrest != 0:
|
| 442 |
+
self._verification_error(
|
| 443 |
+
"field '%s.%s' has a bogus size?" % (
|
| 444 |
+
self.name, self.fldnames[i] or '{}'))
|
| 445 |
+
ftype = ftype.resolve_length(nlen)
|
| 446 |
+
self.fldtypes = (self.fldtypes[:i] + (ftype,) +
|
| 447 |
+
self.fldtypes[i+1:])
|
| 448 |
+
#
|
| 449 |
+
BFieldType = ftype.get_cached_btype(ffi, finishlist)
|
| 450 |
+
if isinstance(ftype, ArrayType) and ftype.length is None:
|
| 451 |
+
assert fsize == 0
|
| 452 |
+
else:
|
| 453 |
+
bitemsize = ffi.sizeof(BFieldType)
|
| 454 |
+
if bitemsize != fsize:
|
| 455 |
+
self._verification_error(
|
| 456 |
+
"field '%s.%s' is declared as %d bytes, but is "
|
| 457 |
+
"really %d bytes" % (self.name,
|
| 458 |
+
self.fldnames[i] or '{}',
|
| 459 |
+
bitemsize, fsize))
|
| 460 |
+
fldtypes.append(BFieldType)
|
| 461 |
+
#
|
| 462 |
+
lst = list(zip(self.fldnames, fldtypes, self.fldbitsize, fieldofs))
|
| 463 |
+
ffi._backend.complete_struct_or_union(BType, lst, self,
|
| 464 |
+
totalsize, totalalignment)
|
| 465 |
+
self.completed = 2
|
| 466 |
+
|
| 467 |
+
def _verification_error(self, msg):
|
| 468 |
+
raise VerificationError(msg)
|
| 469 |
+
|
| 470 |
+
def check_not_partial(self):
|
| 471 |
+
if self.partial and self.fixedlayout is None:
|
| 472 |
+
raise VerificationMissing(self._get_c_name())
|
| 473 |
+
|
| 474 |
+
def build_backend_type(self, ffi, finishlist):
|
| 475 |
+
self.check_not_partial()
|
| 476 |
+
finishlist.append(self)
|
| 477 |
+
#
|
| 478 |
+
return global_cache(self, ffi, 'new_%s_type' % self.kind,
|
| 479 |
+
self.get_official_name(), key=self)
|
| 480 |
+
|
| 481 |
+
|
| 482 |
+
class StructType(StructOrUnion):
|
| 483 |
+
kind = 'struct'
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
class UnionType(StructOrUnion):
|
| 487 |
+
kind = 'union'
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
class EnumType(StructOrUnionOrEnum):
|
| 491 |
+
kind = 'enum'
|
| 492 |
+
partial = False
|
| 493 |
+
partial_resolved = False
|
| 494 |
+
|
| 495 |
+
def __init__(self, name, enumerators, enumvalues, baseinttype=None):
|
| 496 |
+
self.name = name
|
| 497 |
+
self.enumerators = enumerators
|
| 498 |
+
self.enumvalues = enumvalues
|
| 499 |
+
self.baseinttype = baseinttype
|
| 500 |
+
self.build_c_name_with_marker()
|
| 501 |
+
|
| 502 |
+
def force_the_name(self, forcename):
|
| 503 |
+
StructOrUnionOrEnum.force_the_name(self, forcename)
|
| 504 |
+
if self.forcename is None:
|
| 505 |
+
name = self.get_official_name()
|
| 506 |
+
self.forcename = '$' + name.replace(' ', '_')
|
| 507 |
+
|
| 508 |
+
def check_not_partial(self):
|
| 509 |
+
if self.partial and not self.partial_resolved:
|
| 510 |
+
raise VerificationMissing(self._get_c_name())
|
| 511 |
+
|
| 512 |
+
def build_backend_type(self, ffi, finishlist):
|
| 513 |
+
self.check_not_partial()
|
| 514 |
+
base_btype = self.build_baseinttype(ffi, finishlist)
|
| 515 |
+
return global_cache(self, ffi, 'new_enum_type',
|
| 516 |
+
self.get_official_name(),
|
| 517 |
+
self.enumerators, self.enumvalues,
|
| 518 |
+
base_btype, key=self)
|
| 519 |
+
|
| 520 |
+
def build_baseinttype(self, ffi, finishlist):
|
| 521 |
+
if self.baseinttype is not None:
|
| 522 |
+
return self.baseinttype.get_cached_btype(ffi, finishlist)
|
| 523 |
+
#
|
| 524 |
+
if self.enumvalues:
|
| 525 |
+
smallest_value = min(self.enumvalues)
|
| 526 |
+
largest_value = max(self.enumvalues)
|
| 527 |
+
else:
|
| 528 |
+
import warnings
|
| 529 |
+
try:
|
| 530 |
+
# XXX! The goal is to ensure that the warnings.warn()
|
| 531 |
+
# will not suppress the warning. We want to get it
|
| 532 |
+
# several times if we reach this point several times.
|
| 533 |
+
__warningregistry__.clear()
|
| 534 |
+
except NameError:
|
| 535 |
+
pass
|
| 536 |
+
warnings.warn("%r has no values explicitly defined; "
|
| 537 |
+
"guessing that it is equivalent to 'unsigned int'"
|
| 538 |
+
% self._get_c_name())
|
| 539 |
+
smallest_value = largest_value = 0
|
| 540 |
+
if smallest_value < 0: # needs a signed type
|
| 541 |
+
sign = 1
|
| 542 |
+
candidate1 = PrimitiveType("int")
|
| 543 |
+
candidate2 = PrimitiveType("long")
|
| 544 |
+
else:
|
| 545 |
+
sign = 0
|
| 546 |
+
candidate1 = PrimitiveType("unsigned int")
|
| 547 |
+
candidate2 = PrimitiveType("unsigned long")
|
| 548 |
+
btype1 = candidate1.get_cached_btype(ffi, finishlist)
|
| 549 |
+
btype2 = candidate2.get_cached_btype(ffi, finishlist)
|
| 550 |
+
size1 = ffi.sizeof(btype1)
|
| 551 |
+
size2 = ffi.sizeof(btype2)
|
| 552 |
+
if (smallest_value >= ((-1) << (8*size1-1)) and
|
| 553 |
+
largest_value < (1 << (8*size1-sign))):
|
| 554 |
+
return btype1
|
| 555 |
+
if (smallest_value >= ((-1) << (8*size2-1)) and
|
| 556 |
+
largest_value < (1 << (8*size2-sign))):
|
| 557 |
+
return btype2
|
| 558 |
+
raise CDefError("%s values don't all fit into either 'long' "
|
| 559 |
+
"or 'unsigned long'" % self._get_c_name())
|
| 560 |
+
|
| 561 |
+
def unknown_type(name, structname=None):
|
| 562 |
+
if structname is None:
|
| 563 |
+
structname = '$%s' % name
|
| 564 |
+
tp = StructType(structname, None, None, None)
|
| 565 |
+
tp.force_the_name(name)
|
| 566 |
+
tp.origin = "unknown_type"
|
| 567 |
+
return tp
|
| 568 |
+
|
| 569 |
+
def unknown_ptr_type(name, structname=None):
|
| 570 |
+
if structname is None:
|
| 571 |
+
structname = '$$%s' % name
|
| 572 |
+
tp = StructType(structname, None, None, None)
|
| 573 |
+
return NamedPointerType(tp, name)
|
| 574 |
+
|
| 575 |
+
|
| 576 |
+
global_lock = allocate_lock()
|
| 577 |
+
_typecache_cffi_backend = weakref.WeakValueDictionary()
|
| 578 |
+
|
| 579 |
+
def get_typecache(backend):
|
| 580 |
+
# returns _typecache_cffi_backend if backend is the _cffi_backend
|
| 581 |
+
# module, or type(backend).__typecache if backend is an instance of
|
| 582 |
+
# CTypesBackend (or some FakeBackend class during tests)
|
| 583 |
+
if isinstance(backend, types.ModuleType):
|
| 584 |
+
return _typecache_cffi_backend
|
| 585 |
+
with global_lock:
|
| 586 |
+
if not hasattr(type(backend), '__typecache'):
|
| 587 |
+
type(backend).__typecache = weakref.WeakValueDictionary()
|
| 588 |
+
return type(backend).__typecache
|
| 589 |
+
|
| 590 |
+
def global_cache(srctype, ffi, funcname, *args, **kwds):
|
| 591 |
+
key = kwds.pop('key', (funcname, args))
|
| 592 |
+
assert not kwds
|
| 593 |
+
try:
|
| 594 |
+
return ffi._typecache[key]
|
| 595 |
+
except KeyError:
|
| 596 |
+
pass
|
| 597 |
+
try:
|
| 598 |
+
res = getattr(ffi._backend, funcname)(*args)
|
| 599 |
+
except NotImplementedError as e:
|
| 600 |
+
raise NotImplementedError("%s: %r: %s" % (funcname, srctype, e))
|
| 601 |
+
# note that setdefault() on WeakValueDictionary is not atomic
|
| 602 |
+
# and contains a rare bug (http://bugs.python.org/issue19542);
|
| 603 |
+
# we have to use a lock and do it ourselves
|
| 604 |
+
cache = ffi._typecache
|
| 605 |
+
with global_lock:
|
| 606 |
+
res1 = cache.get(key)
|
| 607 |
+
if res1 is None:
|
| 608 |
+
cache[key] = res
|
| 609 |
+
return res
|
| 610 |
+
else:
|
| 611 |
+
return res1
|
| 612 |
+
|
| 613 |
+
def pointer_cache(ffi, BType):
|
| 614 |
+
return global_cache('?', ffi, 'new_pointer_type', BType)
|
| 615 |
+
|
| 616 |
+
def attach_exception_info(e, name):
|
| 617 |
+
if e.args and type(e.args[0]) is str:
|
| 618 |
+
e.args = ('%s: %s' % (name, e.args[0]),) + e.args[1:]
|
.venv/lib/python3.11/site-packages/cffi/parse_c_type.h
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/* This part is from file 'cffi/parse_c_type.h'. It is copied at the
|
| 3 |
+
beginning of C sources generated by CFFI's ffi.set_source(). */
|
| 4 |
+
|
| 5 |
+
typedef void *_cffi_opcode_t;
|
| 6 |
+
|
| 7 |
+
#define _CFFI_OP(opcode, arg) (_cffi_opcode_t)(opcode | (((uintptr_t)(arg)) << 8))
|
| 8 |
+
#define _CFFI_GETOP(cffi_opcode) ((unsigned char)(uintptr_t)cffi_opcode)
|
| 9 |
+
#define _CFFI_GETARG(cffi_opcode) (((intptr_t)cffi_opcode) >> 8)
|
| 10 |
+
|
| 11 |
+
#define _CFFI_OP_PRIMITIVE 1
|
| 12 |
+
#define _CFFI_OP_POINTER 3
|
| 13 |
+
#define _CFFI_OP_ARRAY 5
|
| 14 |
+
#define _CFFI_OP_OPEN_ARRAY 7
|
| 15 |
+
#define _CFFI_OP_STRUCT_UNION 9
|
| 16 |
+
#define _CFFI_OP_ENUM 11
|
| 17 |
+
#define _CFFI_OP_FUNCTION 13
|
| 18 |
+
#define _CFFI_OP_FUNCTION_END 15
|
| 19 |
+
#define _CFFI_OP_NOOP 17
|
| 20 |
+
#define _CFFI_OP_BITFIELD 19
|
| 21 |
+
#define _CFFI_OP_TYPENAME 21
|
| 22 |
+
#define _CFFI_OP_CPYTHON_BLTN_V 23 // varargs
|
| 23 |
+
#define _CFFI_OP_CPYTHON_BLTN_N 25 // noargs
|
| 24 |
+
#define _CFFI_OP_CPYTHON_BLTN_O 27 // O (i.e. a single arg)
|
| 25 |
+
#define _CFFI_OP_CONSTANT 29
|
| 26 |
+
#define _CFFI_OP_CONSTANT_INT 31
|
| 27 |
+
#define _CFFI_OP_GLOBAL_VAR 33
|
| 28 |
+
#define _CFFI_OP_DLOPEN_FUNC 35
|
| 29 |
+
#define _CFFI_OP_DLOPEN_CONST 37
|
| 30 |
+
#define _CFFI_OP_GLOBAL_VAR_F 39
|
| 31 |
+
#define _CFFI_OP_EXTERN_PYTHON 41
|
| 32 |
+
|
| 33 |
+
#define _CFFI_PRIM_VOID 0
|
| 34 |
+
#define _CFFI_PRIM_BOOL 1
|
| 35 |
+
#define _CFFI_PRIM_CHAR 2
|
| 36 |
+
#define _CFFI_PRIM_SCHAR 3
|
| 37 |
+
#define _CFFI_PRIM_UCHAR 4
|
| 38 |
+
#define _CFFI_PRIM_SHORT 5
|
| 39 |
+
#define _CFFI_PRIM_USHORT 6
|
| 40 |
+
#define _CFFI_PRIM_INT 7
|
| 41 |
+
#define _CFFI_PRIM_UINT 8
|
| 42 |
+
#define _CFFI_PRIM_LONG 9
|
| 43 |
+
#define _CFFI_PRIM_ULONG 10
|
| 44 |
+
#define _CFFI_PRIM_LONGLONG 11
|
| 45 |
+
#define _CFFI_PRIM_ULONGLONG 12
|
| 46 |
+
#define _CFFI_PRIM_FLOAT 13
|
| 47 |
+
#define _CFFI_PRIM_DOUBLE 14
|
| 48 |
+
#define _CFFI_PRIM_LONGDOUBLE 15
|
| 49 |
+
|
| 50 |
+
#define _CFFI_PRIM_WCHAR 16
|
| 51 |
+
#define _CFFI_PRIM_INT8 17
|
| 52 |
+
#define _CFFI_PRIM_UINT8 18
|
| 53 |
+
#define _CFFI_PRIM_INT16 19
|
| 54 |
+
#define _CFFI_PRIM_UINT16 20
|
| 55 |
+
#define _CFFI_PRIM_INT32 21
|
| 56 |
+
#define _CFFI_PRIM_UINT32 22
|
| 57 |
+
#define _CFFI_PRIM_INT64 23
|
| 58 |
+
#define _CFFI_PRIM_UINT64 24
|
| 59 |
+
#define _CFFI_PRIM_INTPTR 25
|
| 60 |
+
#define _CFFI_PRIM_UINTPTR 26
|
| 61 |
+
#define _CFFI_PRIM_PTRDIFF 27
|
| 62 |
+
#define _CFFI_PRIM_SIZE 28
|
| 63 |
+
#define _CFFI_PRIM_SSIZE 29
|
| 64 |
+
#define _CFFI_PRIM_INT_LEAST8 30
|
| 65 |
+
#define _CFFI_PRIM_UINT_LEAST8 31
|
| 66 |
+
#define _CFFI_PRIM_INT_LEAST16 32
|
| 67 |
+
#define _CFFI_PRIM_UINT_LEAST16 33
|
| 68 |
+
#define _CFFI_PRIM_INT_LEAST32 34
|
| 69 |
+
#define _CFFI_PRIM_UINT_LEAST32 35
|
| 70 |
+
#define _CFFI_PRIM_INT_LEAST64 36
|
| 71 |
+
#define _CFFI_PRIM_UINT_LEAST64 37
|
| 72 |
+
#define _CFFI_PRIM_INT_FAST8 38
|
| 73 |
+
#define _CFFI_PRIM_UINT_FAST8 39
|
| 74 |
+
#define _CFFI_PRIM_INT_FAST16 40
|
| 75 |
+
#define _CFFI_PRIM_UINT_FAST16 41
|
| 76 |
+
#define _CFFI_PRIM_INT_FAST32 42
|
| 77 |
+
#define _CFFI_PRIM_UINT_FAST32 43
|
| 78 |
+
#define _CFFI_PRIM_INT_FAST64 44
|
| 79 |
+
#define _CFFI_PRIM_UINT_FAST64 45
|
| 80 |
+
#define _CFFI_PRIM_INTMAX 46
|
| 81 |
+
#define _CFFI_PRIM_UINTMAX 47
|
| 82 |
+
#define _CFFI_PRIM_FLOATCOMPLEX 48
|
| 83 |
+
#define _CFFI_PRIM_DOUBLECOMPLEX 49
|
| 84 |
+
#define _CFFI_PRIM_CHAR16 50
|
| 85 |
+
#define _CFFI_PRIM_CHAR32 51
|
| 86 |
+
|
| 87 |
+
#define _CFFI__NUM_PRIM 52
|
| 88 |
+
#define _CFFI__UNKNOWN_PRIM (-1)
|
| 89 |
+
#define _CFFI__UNKNOWN_FLOAT_PRIM (-2)
|
| 90 |
+
#define _CFFI__UNKNOWN_LONG_DOUBLE (-3)
|
| 91 |
+
|
| 92 |
+
#define _CFFI__IO_FILE_STRUCT (-1)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
struct _cffi_global_s {
|
| 96 |
+
const char *name;
|
| 97 |
+
void *address;
|
| 98 |
+
_cffi_opcode_t type_op;
|
| 99 |
+
void *size_or_direct_fn; // OP_GLOBAL_VAR: size, or 0 if unknown
|
| 100 |
+
// OP_CPYTHON_BLTN_*: addr of direct function
|
| 101 |
+
};
|
| 102 |
+
|
| 103 |
+
struct _cffi_getconst_s {
|
| 104 |
+
unsigned long long value;
|
| 105 |
+
const struct _cffi_type_context_s *ctx;
|
| 106 |
+
int gindex;
|
| 107 |
+
};
|
| 108 |
+
|
| 109 |
+
struct _cffi_struct_union_s {
|
| 110 |
+
const char *name;
|
| 111 |
+
int type_index; // -> _cffi_types, on a OP_STRUCT_UNION
|
| 112 |
+
int flags; // _CFFI_F_* flags below
|
| 113 |
+
size_t size;
|
| 114 |
+
int alignment;
|
| 115 |
+
int first_field_index; // -> _cffi_fields array
|
| 116 |
+
int num_fields;
|
| 117 |
+
};
|
| 118 |
+
#define _CFFI_F_UNION 0x01 // is a union, not a struct
|
| 119 |
+
#define _CFFI_F_CHECK_FIELDS 0x02 // complain if fields are not in the
|
| 120 |
+
// "standard layout" or if some are missing
|
| 121 |
+
#define _CFFI_F_PACKED 0x04 // for CHECK_FIELDS, assume a packed struct
|
| 122 |
+
#define _CFFI_F_EXTERNAL 0x08 // in some other ffi.include()
|
| 123 |
+
#define _CFFI_F_OPAQUE 0x10 // opaque
|
| 124 |
+
|
| 125 |
+
struct _cffi_field_s {
|
| 126 |
+
const char *name;
|
| 127 |
+
size_t field_offset;
|
| 128 |
+
size_t field_size;
|
| 129 |
+
_cffi_opcode_t field_type_op;
|
| 130 |
+
};
|
| 131 |
+
|
| 132 |
+
struct _cffi_enum_s {
|
| 133 |
+
const char *name;
|
| 134 |
+
int type_index; // -> _cffi_types, on a OP_ENUM
|
| 135 |
+
int type_prim; // _CFFI_PRIM_xxx
|
| 136 |
+
const char *enumerators; // comma-delimited string
|
| 137 |
+
};
|
| 138 |
+
|
| 139 |
+
struct _cffi_typename_s {
|
| 140 |
+
const char *name;
|
| 141 |
+
int type_index; /* if opaque, points to a possibly artificial
|
| 142 |
+
OP_STRUCT which is itself opaque */
|
| 143 |
+
};
|
| 144 |
+
|
| 145 |
+
struct _cffi_type_context_s {
|
| 146 |
+
_cffi_opcode_t *types;
|
| 147 |
+
const struct _cffi_global_s *globals;
|
| 148 |
+
const struct _cffi_field_s *fields;
|
| 149 |
+
const struct _cffi_struct_union_s *struct_unions;
|
| 150 |
+
const struct _cffi_enum_s *enums;
|
| 151 |
+
const struct _cffi_typename_s *typenames;
|
| 152 |
+
int num_globals;
|
| 153 |
+
int num_struct_unions;
|
| 154 |
+
int num_enums;
|
| 155 |
+
int num_typenames;
|
| 156 |
+
const char *const *includes;
|
| 157 |
+
int num_types;
|
| 158 |
+
int flags; /* future extension */
|
| 159 |
+
};
|
| 160 |
+
|
| 161 |
+
struct _cffi_parse_info_s {
|
| 162 |
+
const struct _cffi_type_context_s *ctx;
|
| 163 |
+
_cffi_opcode_t *output;
|
| 164 |
+
unsigned int output_size;
|
| 165 |
+
size_t error_location;
|
| 166 |
+
const char *error_message;
|
| 167 |
+
};
|
| 168 |
+
|
| 169 |
+
struct _cffi_externpy_s {
|
| 170 |
+
const char *name;
|
| 171 |
+
size_t size_of_result;
|
| 172 |
+
void *reserved1, *reserved2;
|
| 173 |
+
};
|
| 174 |
+
|
| 175 |
+
#ifdef _CFFI_INTERNAL
|
| 176 |
+
static int parse_c_type(struct _cffi_parse_info_s *info, const char *input);
|
| 177 |
+
static int search_in_globals(const struct _cffi_type_context_s *ctx,
|
| 178 |
+
const char *search, size_t search_len);
|
| 179 |
+
static int search_in_struct_unions(const struct _cffi_type_context_s *ctx,
|
| 180 |
+
const char *search, size_t search_len);
|
| 181 |
+
#endif
|
.venv/lib/python3.11/site-packages/cffi/pkgconfig.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pkg-config, https://www.freedesktop.org/wiki/Software/pkg-config/ integration for cffi
|
| 2 |
+
import sys, os, subprocess
|
| 3 |
+
|
| 4 |
+
from .error import PkgConfigError
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def merge_flags(cfg1, cfg2):
|
| 8 |
+
"""Merge values from cffi config flags cfg2 to cf1
|
| 9 |
+
|
| 10 |
+
Example:
|
| 11 |
+
merge_flags({"libraries": ["one"]}, {"libraries": ["two"]})
|
| 12 |
+
{"libraries": ["one", "two"]}
|
| 13 |
+
"""
|
| 14 |
+
for key, value in cfg2.items():
|
| 15 |
+
if key not in cfg1:
|
| 16 |
+
cfg1[key] = value
|
| 17 |
+
else:
|
| 18 |
+
if not isinstance(cfg1[key], list):
|
| 19 |
+
raise TypeError("cfg1[%r] should be a list of strings" % (key,))
|
| 20 |
+
if not isinstance(value, list):
|
| 21 |
+
raise TypeError("cfg2[%r] should be a list of strings" % (key,))
|
| 22 |
+
cfg1[key].extend(value)
|
| 23 |
+
return cfg1
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def call(libname, flag, encoding=sys.getfilesystemencoding()):
|
| 27 |
+
"""Calls pkg-config and returns the output if found
|
| 28 |
+
"""
|
| 29 |
+
a = ["pkg-config", "--print-errors"]
|
| 30 |
+
a.append(flag)
|
| 31 |
+
a.append(libname)
|
| 32 |
+
try:
|
| 33 |
+
pc = subprocess.Popen(a, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 34 |
+
except EnvironmentError as e:
|
| 35 |
+
raise PkgConfigError("cannot run pkg-config: %s" % (str(e).strip(),))
|
| 36 |
+
|
| 37 |
+
bout, berr = pc.communicate()
|
| 38 |
+
if pc.returncode != 0:
|
| 39 |
+
try:
|
| 40 |
+
berr = berr.decode(encoding)
|
| 41 |
+
except Exception:
|
| 42 |
+
pass
|
| 43 |
+
raise PkgConfigError(berr.strip())
|
| 44 |
+
|
| 45 |
+
if sys.version_info >= (3,) and not isinstance(bout, str): # Python 3.x
|
| 46 |
+
try:
|
| 47 |
+
bout = bout.decode(encoding)
|
| 48 |
+
except UnicodeDecodeError:
|
| 49 |
+
raise PkgConfigError("pkg-config %s %s returned bytes that cannot "
|
| 50 |
+
"be decoded with encoding %r:\n%r" %
|
| 51 |
+
(flag, libname, encoding, bout))
|
| 52 |
+
|
| 53 |
+
if os.altsep != '\\' and '\\' in bout:
|
| 54 |
+
raise PkgConfigError("pkg-config %s %s returned an unsupported "
|
| 55 |
+
"backslash-escaped output:\n%r" %
|
| 56 |
+
(flag, libname, bout))
|
| 57 |
+
return bout
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def flags_from_pkgconfig(libs):
|
| 61 |
+
r"""Return compiler line flags for FFI.set_source based on pkg-config output
|
| 62 |
+
|
| 63 |
+
Usage
|
| 64 |
+
...
|
| 65 |
+
ffibuilder.set_source("_foo", pkgconfig = ["libfoo", "libbar >= 1.8.3"])
|
| 66 |
+
|
| 67 |
+
If pkg-config is installed on build machine, then arguments include_dirs,
|
| 68 |
+
library_dirs, libraries, define_macros, extra_compile_args and
|
| 69 |
+
extra_link_args are extended with an output of pkg-config for libfoo and
|
| 70 |
+
libbar.
|
| 71 |
+
|
| 72 |
+
Raises PkgConfigError in case the pkg-config call fails.
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
def get_include_dirs(string):
|
| 76 |
+
return [x[2:] for x in string.split() if x.startswith("-I")]
|
| 77 |
+
|
| 78 |
+
def get_library_dirs(string):
|
| 79 |
+
return [x[2:] for x in string.split() if x.startswith("-L")]
|
| 80 |
+
|
| 81 |
+
def get_libraries(string):
|
| 82 |
+
return [x[2:] for x in string.split() if x.startswith("-l")]
|
| 83 |
+
|
| 84 |
+
# convert -Dfoo=bar to list of tuples [("foo", "bar")] expected by distutils
|
| 85 |
+
def get_macros(string):
|
| 86 |
+
def _macro(x):
|
| 87 |
+
x = x[2:] # drop "-D"
|
| 88 |
+
if '=' in x:
|
| 89 |
+
return tuple(x.split("=", 1)) # "-Dfoo=bar" => ("foo", "bar")
|
| 90 |
+
else:
|
| 91 |
+
return (x, None) # "-Dfoo" => ("foo", None)
|
| 92 |
+
return [_macro(x) for x in string.split() if x.startswith("-D")]
|
| 93 |
+
|
| 94 |
+
def get_other_cflags(string):
|
| 95 |
+
return [x for x in string.split() if not x.startswith("-I") and
|
| 96 |
+
not x.startswith("-D")]
|
| 97 |
+
|
| 98 |
+
def get_other_libs(string):
|
| 99 |
+
return [x for x in string.split() if not x.startswith("-L") and
|
| 100 |
+
not x.startswith("-l")]
|
| 101 |
+
|
| 102 |
+
# return kwargs for given libname
|
| 103 |
+
def kwargs(libname):
|
| 104 |
+
fse = sys.getfilesystemencoding()
|
| 105 |
+
all_cflags = call(libname, "--cflags")
|
| 106 |
+
all_libs = call(libname, "--libs")
|
| 107 |
+
return {
|
| 108 |
+
"include_dirs": get_include_dirs(all_cflags),
|
| 109 |
+
"library_dirs": get_library_dirs(all_libs),
|
| 110 |
+
"libraries": get_libraries(all_libs),
|
| 111 |
+
"define_macros": get_macros(all_cflags),
|
| 112 |
+
"extra_compile_args": get_other_cflags(all_cflags),
|
| 113 |
+
"extra_link_args": get_other_libs(all_libs),
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
# merge all arguments together
|
| 117 |
+
ret = {}
|
| 118 |
+
for libname in libs:
|
| 119 |
+
lib_flags = kwargs(libname)
|
| 120 |
+
merge_flags(ret, lib_flags)
|
| 121 |
+
return ret
|
.venv/lib/python3.11/site-packages/cffi/vengine_cpy.py
ADDED
|
@@ -0,0 +1,1084 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# DEPRECATED: implementation for ffi.verify()
|
| 3 |
+
#
|
| 4 |
+
import sys
|
| 5 |
+
from . import model
|
| 6 |
+
from .error import VerificationError
|
| 7 |
+
from . import _imp_emulation as imp
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class VCPythonEngine(object):
|
| 11 |
+
_class_key = 'x'
|
| 12 |
+
_gen_python_module = True
|
| 13 |
+
|
| 14 |
+
def __init__(self, verifier):
|
| 15 |
+
self.verifier = verifier
|
| 16 |
+
self.ffi = verifier.ffi
|
| 17 |
+
self._struct_pending_verification = {}
|
| 18 |
+
self._types_of_builtin_functions = {}
|
| 19 |
+
|
| 20 |
+
def patch_extension_kwds(self, kwds):
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
def find_module(self, module_name, path, so_suffixes):
|
| 24 |
+
try:
|
| 25 |
+
f, filename, descr = imp.find_module(module_name, path)
|
| 26 |
+
except ImportError:
|
| 27 |
+
return None
|
| 28 |
+
if f is not None:
|
| 29 |
+
f.close()
|
| 30 |
+
# Note that after a setuptools installation, there are both .py
|
| 31 |
+
# and .so files with the same basename. The code here relies on
|
| 32 |
+
# imp.find_module() locating the .so in priority.
|
| 33 |
+
if descr[0] not in so_suffixes:
|
| 34 |
+
return None
|
| 35 |
+
return filename
|
| 36 |
+
|
| 37 |
+
def collect_types(self):
|
| 38 |
+
self._typesdict = {}
|
| 39 |
+
self._generate("collecttype")
|
| 40 |
+
|
| 41 |
+
def _prnt(self, what=''):
|
| 42 |
+
self._f.write(what + '\n')
|
| 43 |
+
|
| 44 |
+
def _gettypenum(self, type):
|
| 45 |
+
# a KeyError here is a bug. please report it! :-)
|
| 46 |
+
return self._typesdict[type]
|
| 47 |
+
|
| 48 |
+
def _do_collect_type(self, tp):
|
| 49 |
+
if ((not isinstance(tp, model.PrimitiveType)
|
| 50 |
+
or tp.name == 'long double')
|
| 51 |
+
and tp not in self._typesdict):
|
| 52 |
+
num = len(self._typesdict)
|
| 53 |
+
self._typesdict[tp] = num
|
| 54 |
+
|
| 55 |
+
def write_source_to_f(self):
|
| 56 |
+
self.collect_types()
|
| 57 |
+
#
|
| 58 |
+
# The new module will have a _cffi_setup() function that receives
|
| 59 |
+
# objects from the ffi world, and that calls some setup code in
|
| 60 |
+
# the module. This setup code is split in several independent
|
| 61 |
+
# functions, e.g. one per constant. The functions are "chained"
|
| 62 |
+
# by ending in a tail call to each other.
|
| 63 |
+
#
|
| 64 |
+
# This is further split in two chained lists, depending on if we
|
| 65 |
+
# can do it at import-time or if we must wait for _cffi_setup() to
|
| 66 |
+
# provide us with the <ctype> objects. This is needed because we
|
| 67 |
+
# need the values of the enum constants in order to build the
|
| 68 |
+
# <ctype 'enum'> that we may have to pass to _cffi_setup().
|
| 69 |
+
#
|
| 70 |
+
# The following two 'chained_list_constants' items contains
|
| 71 |
+
# the head of these two chained lists, as a string that gives the
|
| 72 |
+
# call to do, if any.
|
| 73 |
+
self._chained_list_constants = ['((void)lib,0)', '((void)lib,0)']
|
| 74 |
+
#
|
| 75 |
+
prnt = self._prnt
|
| 76 |
+
# first paste some standard set of lines that are mostly '#define'
|
| 77 |
+
prnt(cffimod_header)
|
| 78 |
+
prnt()
|
| 79 |
+
# then paste the C source given by the user, verbatim.
|
| 80 |
+
prnt(self.verifier.preamble)
|
| 81 |
+
prnt()
|
| 82 |
+
#
|
| 83 |
+
# call generate_cpy_xxx_decl(), for every xxx found from
|
| 84 |
+
# ffi._parser._declarations. This generates all the functions.
|
| 85 |
+
self._generate("decl")
|
| 86 |
+
#
|
| 87 |
+
# implement the function _cffi_setup_custom() as calling the
|
| 88 |
+
# head of the chained list.
|
| 89 |
+
self._generate_setup_custom()
|
| 90 |
+
prnt()
|
| 91 |
+
#
|
| 92 |
+
# produce the method table, including the entries for the
|
| 93 |
+
# generated Python->C function wrappers, which are done
|
| 94 |
+
# by generate_cpy_function_method().
|
| 95 |
+
prnt('static PyMethodDef _cffi_methods[] = {')
|
| 96 |
+
self._generate("method")
|
| 97 |
+
prnt(' {"_cffi_setup", _cffi_setup, METH_VARARGS, NULL},')
|
| 98 |
+
prnt(' {NULL, NULL, 0, NULL} /* Sentinel */')
|
| 99 |
+
prnt('};')
|
| 100 |
+
prnt()
|
| 101 |
+
#
|
| 102 |
+
# standard init.
|
| 103 |
+
modname = self.verifier.get_module_name()
|
| 104 |
+
constants = self._chained_list_constants[False]
|
| 105 |
+
prnt('#if PY_MAJOR_VERSION >= 3')
|
| 106 |
+
prnt()
|
| 107 |
+
prnt('static struct PyModuleDef _cffi_module_def = {')
|
| 108 |
+
prnt(' PyModuleDef_HEAD_INIT,')
|
| 109 |
+
prnt(' "%s",' % modname)
|
| 110 |
+
prnt(' NULL,')
|
| 111 |
+
prnt(' -1,')
|
| 112 |
+
prnt(' _cffi_methods,')
|
| 113 |
+
prnt(' NULL, NULL, NULL, NULL')
|
| 114 |
+
prnt('};')
|
| 115 |
+
prnt()
|
| 116 |
+
prnt('PyMODINIT_FUNC')
|
| 117 |
+
prnt('PyInit_%s(void)' % modname)
|
| 118 |
+
prnt('{')
|
| 119 |
+
prnt(' PyObject *lib;')
|
| 120 |
+
prnt(' lib = PyModule_Create(&_cffi_module_def);')
|
| 121 |
+
prnt(' if (lib == NULL)')
|
| 122 |
+
prnt(' return NULL;')
|
| 123 |
+
prnt(' if (%s < 0 || _cffi_init() < 0) {' % (constants,))
|
| 124 |
+
prnt(' Py_DECREF(lib);')
|
| 125 |
+
prnt(' return NULL;')
|
| 126 |
+
prnt(' }')
|
| 127 |
+
prnt(' return lib;')
|
| 128 |
+
prnt('}')
|
| 129 |
+
prnt()
|
| 130 |
+
prnt('#else')
|
| 131 |
+
prnt()
|
| 132 |
+
prnt('PyMODINIT_FUNC')
|
| 133 |
+
prnt('init%s(void)' % modname)
|
| 134 |
+
prnt('{')
|
| 135 |
+
prnt(' PyObject *lib;')
|
| 136 |
+
prnt(' lib = Py_InitModule("%s", _cffi_methods);' % modname)
|
| 137 |
+
prnt(' if (lib == NULL)')
|
| 138 |
+
prnt(' return;')
|
| 139 |
+
prnt(' if (%s < 0 || _cffi_init() < 0)' % (constants,))
|
| 140 |
+
prnt(' return;')
|
| 141 |
+
prnt(' return;')
|
| 142 |
+
prnt('}')
|
| 143 |
+
prnt()
|
| 144 |
+
prnt('#endif')
|
| 145 |
+
|
| 146 |
+
def load_library(self, flags=None):
|
| 147 |
+
# XXX review all usages of 'self' here!
|
| 148 |
+
# import it as a new extension module
|
| 149 |
+
imp.acquire_lock()
|
| 150 |
+
try:
|
| 151 |
+
if hasattr(sys, "getdlopenflags"):
|
| 152 |
+
previous_flags = sys.getdlopenflags()
|
| 153 |
+
try:
|
| 154 |
+
if hasattr(sys, "setdlopenflags") and flags is not None:
|
| 155 |
+
sys.setdlopenflags(flags)
|
| 156 |
+
module = imp.load_dynamic(self.verifier.get_module_name(),
|
| 157 |
+
self.verifier.modulefilename)
|
| 158 |
+
except ImportError as e:
|
| 159 |
+
error = "importing %r: %s" % (self.verifier.modulefilename, e)
|
| 160 |
+
raise VerificationError(error)
|
| 161 |
+
finally:
|
| 162 |
+
if hasattr(sys, "setdlopenflags"):
|
| 163 |
+
sys.setdlopenflags(previous_flags)
|
| 164 |
+
finally:
|
| 165 |
+
imp.release_lock()
|
| 166 |
+
#
|
| 167 |
+
# call loading_cpy_struct() to get the struct layout inferred by
|
| 168 |
+
# the C compiler
|
| 169 |
+
self._load(module, 'loading')
|
| 170 |
+
#
|
| 171 |
+
# the C code will need the <ctype> objects. Collect them in
|
| 172 |
+
# order in a list.
|
| 173 |
+
revmapping = dict([(value, key)
|
| 174 |
+
for (key, value) in self._typesdict.items()])
|
| 175 |
+
lst = [revmapping[i] for i in range(len(revmapping))]
|
| 176 |
+
lst = list(map(self.ffi._get_cached_btype, lst))
|
| 177 |
+
#
|
| 178 |
+
# build the FFILibrary class and instance and call _cffi_setup().
|
| 179 |
+
# this will set up some fields like '_cffi_types', and only then
|
| 180 |
+
# it will invoke the chained list of functions that will really
|
| 181 |
+
# build (notably) the constant objects, as <cdata> if they are
|
| 182 |
+
# pointers, and store them as attributes on the 'library' object.
|
| 183 |
+
class FFILibrary(object):
|
| 184 |
+
_cffi_python_module = module
|
| 185 |
+
_cffi_ffi = self.ffi
|
| 186 |
+
_cffi_dir = []
|
| 187 |
+
def __dir__(self):
|
| 188 |
+
return FFILibrary._cffi_dir + list(self.__dict__)
|
| 189 |
+
library = FFILibrary()
|
| 190 |
+
if module._cffi_setup(lst, VerificationError, library):
|
| 191 |
+
import warnings
|
| 192 |
+
warnings.warn("reimporting %r might overwrite older definitions"
|
| 193 |
+
% (self.verifier.get_module_name()))
|
| 194 |
+
#
|
| 195 |
+
# finally, call the loaded_cpy_xxx() functions. This will perform
|
| 196 |
+
# the final adjustments, like copying the Python->C wrapper
|
| 197 |
+
# functions from the module to the 'library' object, and setting
|
| 198 |
+
# up the FFILibrary class with properties for the global C variables.
|
| 199 |
+
self._load(module, 'loaded', library=library)
|
| 200 |
+
module._cffi_original_ffi = self.ffi
|
| 201 |
+
module._cffi_types_of_builtin_funcs = self._types_of_builtin_functions
|
| 202 |
+
return library
|
| 203 |
+
|
| 204 |
+
def _get_declarations(self):
|
| 205 |
+
lst = [(key, tp) for (key, (tp, qual)) in
|
| 206 |
+
self.ffi._parser._declarations.items()]
|
| 207 |
+
lst.sort()
|
| 208 |
+
return lst
|
| 209 |
+
|
| 210 |
+
def _generate(self, step_name):
|
| 211 |
+
for name, tp in self._get_declarations():
|
| 212 |
+
kind, realname = name.split(' ', 1)
|
| 213 |
+
try:
|
| 214 |
+
method = getattr(self, '_generate_cpy_%s_%s' % (kind,
|
| 215 |
+
step_name))
|
| 216 |
+
except AttributeError:
|
| 217 |
+
raise VerificationError(
|
| 218 |
+
"not implemented in verify(): %r" % name)
|
| 219 |
+
try:
|
| 220 |
+
method(tp, realname)
|
| 221 |
+
except Exception as e:
|
| 222 |
+
model.attach_exception_info(e, name)
|
| 223 |
+
raise
|
| 224 |
+
|
| 225 |
+
def _load(self, module, step_name, **kwds):
|
| 226 |
+
for name, tp in self._get_declarations():
|
| 227 |
+
kind, realname = name.split(' ', 1)
|
| 228 |
+
method = getattr(self, '_%s_cpy_%s' % (step_name, kind))
|
| 229 |
+
try:
|
| 230 |
+
method(tp, realname, module, **kwds)
|
| 231 |
+
except Exception as e:
|
| 232 |
+
model.attach_exception_info(e, name)
|
| 233 |
+
raise
|
| 234 |
+
|
| 235 |
+
def _generate_nothing(self, tp, name):
|
| 236 |
+
pass
|
| 237 |
+
|
| 238 |
+
def _loaded_noop(self, tp, name, module, **kwds):
|
| 239 |
+
pass
|
| 240 |
+
|
| 241 |
+
# ----------
|
| 242 |
+
|
| 243 |
+
def _convert_funcarg_to_c(self, tp, fromvar, tovar, errcode):
|
| 244 |
+
extraarg = ''
|
| 245 |
+
if isinstance(tp, model.PrimitiveType):
|
| 246 |
+
if tp.is_integer_type() and tp.name != '_Bool':
|
| 247 |
+
converter = '_cffi_to_c_int'
|
| 248 |
+
extraarg = ', %s' % tp.name
|
| 249 |
+
elif tp.is_complex_type():
|
| 250 |
+
raise VerificationError(
|
| 251 |
+
"not implemented in verify(): complex types")
|
| 252 |
+
else:
|
| 253 |
+
converter = '(%s)_cffi_to_c_%s' % (tp.get_c_name(''),
|
| 254 |
+
tp.name.replace(' ', '_'))
|
| 255 |
+
errvalue = '-1'
|
| 256 |
+
#
|
| 257 |
+
elif isinstance(tp, model.PointerType):
|
| 258 |
+
self._convert_funcarg_to_c_ptr_or_array(tp, fromvar,
|
| 259 |
+
tovar, errcode)
|
| 260 |
+
return
|
| 261 |
+
#
|
| 262 |
+
elif isinstance(tp, (model.StructOrUnion, model.EnumType)):
|
| 263 |
+
# a struct (not a struct pointer) as a function argument
|
| 264 |
+
self._prnt(' if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)'
|
| 265 |
+
% (tovar, self._gettypenum(tp), fromvar))
|
| 266 |
+
self._prnt(' %s;' % errcode)
|
| 267 |
+
return
|
| 268 |
+
#
|
| 269 |
+
elif isinstance(tp, model.FunctionPtrType):
|
| 270 |
+
converter = '(%s)_cffi_to_c_pointer' % tp.get_c_name('')
|
| 271 |
+
extraarg = ', _cffi_type(%d)' % self._gettypenum(tp)
|
| 272 |
+
errvalue = 'NULL'
|
| 273 |
+
#
|
| 274 |
+
else:
|
| 275 |
+
raise NotImplementedError(tp)
|
| 276 |
+
#
|
| 277 |
+
self._prnt(' %s = %s(%s%s);' % (tovar, converter, fromvar, extraarg))
|
| 278 |
+
self._prnt(' if (%s == (%s)%s && PyErr_Occurred())' % (
|
| 279 |
+
tovar, tp.get_c_name(''), errvalue))
|
| 280 |
+
self._prnt(' %s;' % errcode)
|
| 281 |
+
|
| 282 |
+
def _extra_local_variables(self, tp, localvars, freelines):
|
| 283 |
+
if isinstance(tp, model.PointerType):
|
| 284 |
+
localvars.add('Py_ssize_t datasize')
|
| 285 |
+
localvars.add('struct _cffi_freeme_s *large_args_free = NULL')
|
| 286 |
+
freelines.add('if (large_args_free != NULL)'
|
| 287 |
+
' _cffi_free_array_arguments(large_args_free);')
|
| 288 |
+
|
| 289 |
+
def _convert_funcarg_to_c_ptr_or_array(self, tp, fromvar, tovar, errcode):
|
| 290 |
+
self._prnt(' datasize = _cffi_prepare_pointer_call_argument(')
|
| 291 |
+
self._prnt(' _cffi_type(%d), %s, (char **)&%s);' % (
|
| 292 |
+
self._gettypenum(tp), fromvar, tovar))
|
| 293 |
+
self._prnt(' if (datasize != 0) {')
|
| 294 |
+
self._prnt(' %s = ((size_t)datasize) <= 640 ? '
|
| 295 |
+
'alloca((size_t)datasize) : NULL;' % (tovar,))
|
| 296 |
+
self._prnt(' if (_cffi_convert_array_argument(_cffi_type(%d), %s, '
|
| 297 |
+
'(char **)&%s,' % (self._gettypenum(tp), fromvar, tovar))
|
| 298 |
+
self._prnt(' datasize, &large_args_free) < 0)')
|
| 299 |
+
self._prnt(' %s;' % errcode)
|
| 300 |
+
self._prnt(' }')
|
| 301 |
+
|
| 302 |
+
def _convert_expr_from_c(self, tp, var, context):
|
| 303 |
+
if isinstance(tp, model.PrimitiveType):
|
| 304 |
+
if tp.is_integer_type() and tp.name != '_Bool':
|
| 305 |
+
return '_cffi_from_c_int(%s, %s)' % (var, tp.name)
|
| 306 |
+
elif tp.name != 'long double':
|
| 307 |
+
return '_cffi_from_c_%s(%s)' % (tp.name.replace(' ', '_'), var)
|
| 308 |
+
else:
|
| 309 |
+
return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % (
|
| 310 |
+
var, self._gettypenum(tp))
|
| 311 |
+
elif isinstance(tp, (model.PointerType, model.FunctionPtrType)):
|
| 312 |
+
return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % (
|
| 313 |
+
var, self._gettypenum(tp))
|
| 314 |
+
elif isinstance(tp, model.ArrayType):
|
| 315 |
+
return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % (
|
| 316 |
+
var, self._gettypenum(model.PointerType(tp.item)))
|
| 317 |
+
elif isinstance(tp, model.StructOrUnion):
|
| 318 |
+
if tp.fldnames is None:
|
| 319 |
+
raise TypeError("'%s' is used as %s, but is opaque" % (
|
| 320 |
+
tp._get_c_name(), context))
|
| 321 |
+
return '_cffi_from_c_struct((char *)&%s, _cffi_type(%d))' % (
|
| 322 |
+
var, self._gettypenum(tp))
|
| 323 |
+
elif isinstance(tp, model.EnumType):
|
| 324 |
+
return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % (
|
| 325 |
+
var, self._gettypenum(tp))
|
| 326 |
+
else:
|
| 327 |
+
raise NotImplementedError(tp)
|
| 328 |
+
|
| 329 |
+
# ----------
|
| 330 |
+
# typedefs: generates no code so far
|
| 331 |
+
|
| 332 |
+
_generate_cpy_typedef_collecttype = _generate_nothing
|
| 333 |
+
_generate_cpy_typedef_decl = _generate_nothing
|
| 334 |
+
_generate_cpy_typedef_method = _generate_nothing
|
| 335 |
+
_loading_cpy_typedef = _loaded_noop
|
| 336 |
+
_loaded_cpy_typedef = _loaded_noop
|
| 337 |
+
|
| 338 |
+
# ----------
|
| 339 |
+
# function declarations
|
| 340 |
+
|
| 341 |
+
def _generate_cpy_function_collecttype(self, tp, name):
|
| 342 |
+
assert isinstance(tp, model.FunctionPtrType)
|
| 343 |
+
if tp.ellipsis:
|
| 344 |
+
self._do_collect_type(tp)
|
| 345 |
+
else:
|
| 346 |
+
# don't call _do_collect_type(tp) in this common case,
|
| 347 |
+
# otherwise test_autofilled_struct_as_argument fails
|
| 348 |
+
for type in tp.args:
|
| 349 |
+
self._do_collect_type(type)
|
| 350 |
+
self._do_collect_type(tp.result)
|
| 351 |
+
|
| 352 |
+
def _generate_cpy_function_decl(self, tp, name):
|
| 353 |
+
assert isinstance(tp, model.FunctionPtrType)
|
| 354 |
+
if tp.ellipsis:
|
| 355 |
+
# cannot support vararg functions better than this: check for its
|
| 356 |
+
# exact type (including the fixed arguments), and build it as a
|
| 357 |
+
# constant function pointer (no CPython wrapper)
|
| 358 |
+
self._generate_cpy_const(False, name, tp)
|
| 359 |
+
return
|
| 360 |
+
prnt = self._prnt
|
| 361 |
+
numargs = len(tp.args)
|
| 362 |
+
if numargs == 0:
|
| 363 |
+
argname = 'noarg'
|
| 364 |
+
elif numargs == 1:
|
| 365 |
+
argname = 'arg0'
|
| 366 |
+
else:
|
| 367 |
+
argname = 'args'
|
| 368 |
+
prnt('static PyObject *')
|
| 369 |
+
prnt('_cffi_f_%s(PyObject *self, PyObject *%s)' % (name, argname))
|
| 370 |
+
prnt('{')
|
| 371 |
+
#
|
| 372 |
+
context = 'argument of %s' % name
|
| 373 |
+
for i, type in enumerate(tp.args):
|
| 374 |
+
prnt(' %s;' % type.get_c_name(' x%d' % i, context))
|
| 375 |
+
#
|
| 376 |
+
localvars = set()
|
| 377 |
+
freelines = set()
|
| 378 |
+
for type in tp.args:
|
| 379 |
+
self._extra_local_variables(type, localvars, freelines)
|
| 380 |
+
for decl in sorted(localvars):
|
| 381 |
+
prnt(' %s;' % (decl,))
|
| 382 |
+
#
|
| 383 |
+
if not isinstance(tp.result, model.VoidType):
|
| 384 |
+
result_code = 'result = '
|
| 385 |
+
context = 'result of %s' % name
|
| 386 |
+
prnt(' %s;' % tp.result.get_c_name(' result', context))
|
| 387 |
+
prnt(' PyObject *pyresult;')
|
| 388 |
+
else:
|
| 389 |
+
result_code = ''
|
| 390 |
+
#
|
| 391 |
+
if len(tp.args) > 1:
|
| 392 |
+
rng = range(len(tp.args))
|
| 393 |
+
for i in rng:
|
| 394 |
+
prnt(' PyObject *arg%d;' % i)
|
| 395 |
+
prnt()
|
| 396 |
+
prnt(' if (!PyArg_ParseTuple(args, "%s:%s", %s))' % (
|
| 397 |
+
'O' * numargs, name, ', '.join(['&arg%d' % i for i in rng])))
|
| 398 |
+
prnt(' return NULL;')
|
| 399 |
+
prnt()
|
| 400 |
+
#
|
| 401 |
+
for i, type in enumerate(tp.args):
|
| 402 |
+
self._convert_funcarg_to_c(type, 'arg%d' % i, 'x%d' % i,
|
| 403 |
+
'return NULL')
|
| 404 |
+
prnt()
|
| 405 |
+
#
|
| 406 |
+
prnt(' Py_BEGIN_ALLOW_THREADS')
|
| 407 |
+
prnt(' _cffi_restore_errno();')
|
| 408 |
+
prnt(' { %s%s(%s); }' % (
|
| 409 |
+
result_code, name,
|
| 410 |
+
', '.join(['x%d' % i for i in range(len(tp.args))])))
|
| 411 |
+
prnt(' _cffi_save_errno();')
|
| 412 |
+
prnt(' Py_END_ALLOW_THREADS')
|
| 413 |
+
prnt()
|
| 414 |
+
#
|
| 415 |
+
prnt(' (void)self; /* unused */')
|
| 416 |
+
if numargs == 0:
|
| 417 |
+
prnt(' (void)noarg; /* unused */')
|
| 418 |
+
if result_code:
|
| 419 |
+
prnt(' pyresult = %s;' %
|
| 420 |
+
self._convert_expr_from_c(tp.result, 'result', 'result type'))
|
| 421 |
+
for freeline in freelines:
|
| 422 |
+
prnt(' ' + freeline)
|
| 423 |
+
prnt(' return pyresult;')
|
| 424 |
+
else:
|
| 425 |
+
for freeline in freelines:
|
| 426 |
+
prnt(' ' + freeline)
|
| 427 |
+
prnt(' Py_INCREF(Py_None);')
|
| 428 |
+
prnt(' return Py_None;')
|
| 429 |
+
prnt('}')
|
| 430 |
+
prnt()
|
| 431 |
+
|
| 432 |
+
def _generate_cpy_function_method(self, tp, name):
|
| 433 |
+
if tp.ellipsis:
|
| 434 |
+
return
|
| 435 |
+
numargs = len(tp.args)
|
| 436 |
+
if numargs == 0:
|
| 437 |
+
meth = 'METH_NOARGS'
|
| 438 |
+
elif numargs == 1:
|
| 439 |
+
meth = 'METH_O'
|
| 440 |
+
else:
|
| 441 |
+
meth = 'METH_VARARGS'
|
| 442 |
+
self._prnt(' {"%s", _cffi_f_%s, %s, NULL},' % (name, name, meth))
|
| 443 |
+
|
| 444 |
+
_loading_cpy_function = _loaded_noop
|
| 445 |
+
|
| 446 |
+
def _loaded_cpy_function(self, tp, name, module, library):
|
| 447 |
+
if tp.ellipsis:
|
| 448 |
+
return
|
| 449 |
+
func = getattr(module, name)
|
| 450 |
+
setattr(library, name, func)
|
| 451 |
+
self._types_of_builtin_functions[func] = tp
|
| 452 |
+
|
| 453 |
+
# ----------
|
| 454 |
+
# named structs
|
| 455 |
+
|
| 456 |
+
_generate_cpy_struct_collecttype = _generate_nothing
|
| 457 |
+
def _generate_cpy_struct_decl(self, tp, name):
|
| 458 |
+
assert name == tp.name
|
| 459 |
+
self._generate_struct_or_union_decl(tp, 'struct', name)
|
| 460 |
+
def _generate_cpy_struct_method(self, tp, name):
|
| 461 |
+
self._generate_struct_or_union_method(tp, 'struct', name)
|
| 462 |
+
def _loading_cpy_struct(self, tp, name, module):
|
| 463 |
+
self._loading_struct_or_union(tp, 'struct', name, module)
|
| 464 |
+
def _loaded_cpy_struct(self, tp, name, module, **kwds):
|
| 465 |
+
self._loaded_struct_or_union(tp)
|
| 466 |
+
|
| 467 |
+
_generate_cpy_union_collecttype = _generate_nothing
|
| 468 |
+
def _generate_cpy_union_decl(self, tp, name):
|
| 469 |
+
assert name == tp.name
|
| 470 |
+
self._generate_struct_or_union_decl(tp, 'union', name)
|
| 471 |
+
def _generate_cpy_union_method(self, tp, name):
|
| 472 |
+
self._generate_struct_or_union_method(tp, 'union', name)
|
| 473 |
+
def _loading_cpy_union(self, tp, name, module):
|
| 474 |
+
self._loading_struct_or_union(tp, 'union', name, module)
|
| 475 |
+
def _loaded_cpy_union(self, tp, name, module, **kwds):
|
| 476 |
+
self._loaded_struct_or_union(tp)
|
| 477 |
+
|
| 478 |
+
def _generate_struct_or_union_decl(self, tp, prefix, name):
|
| 479 |
+
if tp.fldnames is None:
|
| 480 |
+
return # nothing to do with opaque structs
|
| 481 |
+
checkfuncname = '_cffi_check_%s_%s' % (prefix, name)
|
| 482 |
+
layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name)
|
| 483 |
+
cname = ('%s %s' % (prefix, name)).strip()
|
| 484 |
+
#
|
| 485 |
+
prnt = self._prnt
|
| 486 |
+
prnt('static void %s(%s *p)' % (checkfuncname, cname))
|
| 487 |
+
prnt('{')
|
| 488 |
+
prnt(' /* only to generate compile-time warnings or errors */')
|
| 489 |
+
prnt(' (void)p;')
|
| 490 |
+
for fname, ftype, fbitsize, fqual in tp.enumfields():
|
| 491 |
+
if (isinstance(ftype, model.PrimitiveType)
|
| 492 |
+
and ftype.is_integer_type()) or fbitsize >= 0:
|
| 493 |
+
# accept all integers, but complain on float or double
|
| 494 |
+
prnt(' (void)((p->%s) << 1);' % fname)
|
| 495 |
+
else:
|
| 496 |
+
# only accept exactly the type declared.
|
| 497 |
+
try:
|
| 498 |
+
prnt(' { %s = &p->%s; (void)tmp; }' % (
|
| 499 |
+
ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual),
|
| 500 |
+
fname))
|
| 501 |
+
except VerificationError as e:
|
| 502 |
+
prnt(' /* %s */' % str(e)) # cannot verify it, ignore
|
| 503 |
+
prnt('}')
|
| 504 |
+
prnt('static PyObject *')
|
| 505 |
+
prnt('%s(PyObject *self, PyObject *noarg)' % (layoutfuncname,))
|
| 506 |
+
prnt('{')
|
| 507 |
+
prnt(' struct _cffi_aligncheck { char x; %s y; };' % cname)
|
| 508 |
+
prnt(' static Py_ssize_t nums[] = {')
|
| 509 |
+
prnt(' sizeof(%s),' % cname)
|
| 510 |
+
prnt(' offsetof(struct _cffi_aligncheck, y),')
|
| 511 |
+
for fname, ftype, fbitsize, fqual in tp.enumfields():
|
| 512 |
+
if fbitsize >= 0:
|
| 513 |
+
continue # xxx ignore fbitsize for now
|
| 514 |
+
prnt(' offsetof(%s, %s),' % (cname, fname))
|
| 515 |
+
if isinstance(ftype, model.ArrayType) and ftype.length is None:
|
| 516 |
+
prnt(' 0, /* %s */' % ftype._get_c_name())
|
| 517 |
+
else:
|
| 518 |
+
prnt(' sizeof(((%s *)0)->%s),' % (cname, fname))
|
| 519 |
+
prnt(' -1')
|
| 520 |
+
prnt(' };')
|
| 521 |
+
prnt(' (void)self; /* unused */')
|
| 522 |
+
prnt(' (void)noarg; /* unused */')
|
| 523 |
+
prnt(' return _cffi_get_struct_layout(nums);')
|
| 524 |
+
prnt(' /* the next line is not executed, but compiled */')
|
| 525 |
+
prnt(' %s(0);' % (checkfuncname,))
|
| 526 |
+
prnt('}')
|
| 527 |
+
prnt()
|
| 528 |
+
|
| 529 |
+
def _generate_struct_or_union_method(self, tp, prefix, name):
|
| 530 |
+
if tp.fldnames is None:
|
| 531 |
+
return # nothing to do with opaque structs
|
| 532 |
+
layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name)
|
| 533 |
+
self._prnt(' {"%s", %s, METH_NOARGS, NULL},' % (layoutfuncname,
|
| 534 |
+
layoutfuncname))
|
| 535 |
+
|
| 536 |
+
def _loading_struct_or_union(self, tp, prefix, name, module):
|
| 537 |
+
if tp.fldnames is None:
|
| 538 |
+
return # nothing to do with opaque structs
|
| 539 |
+
layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name)
|
| 540 |
+
#
|
| 541 |
+
function = getattr(module, layoutfuncname)
|
| 542 |
+
layout = function()
|
| 543 |
+
if isinstance(tp, model.StructOrUnion) and tp.partial:
|
| 544 |
+
# use the function()'s sizes and offsets to guide the
|
| 545 |
+
# layout of the struct
|
| 546 |
+
totalsize = layout[0]
|
| 547 |
+
totalalignment = layout[1]
|
| 548 |
+
fieldofs = layout[2::2]
|
| 549 |
+
fieldsize = layout[3::2]
|
| 550 |
+
tp.force_flatten()
|
| 551 |
+
assert len(fieldofs) == len(fieldsize) == len(tp.fldnames)
|
| 552 |
+
tp.fixedlayout = fieldofs, fieldsize, totalsize, totalalignment
|
| 553 |
+
else:
|
| 554 |
+
cname = ('%s %s' % (prefix, name)).strip()
|
| 555 |
+
self._struct_pending_verification[tp] = layout, cname
|
| 556 |
+
|
| 557 |
+
def _loaded_struct_or_union(self, tp):
|
| 558 |
+
if tp.fldnames is None:
|
| 559 |
+
return # nothing to do with opaque structs
|
| 560 |
+
self.ffi._get_cached_btype(tp) # force 'fixedlayout' to be considered
|
| 561 |
+
|
| 562 |
+
if tp in self._struct_pending_verification:
|
| 563 |
+
# check that the layout sizes and offsets match the real ones
|
| 564 |
+
def check(realvalue, expectedvalue, msg):
|
| 565 |
+
if realvalue != expectedvalue:
|
| 566 |
+
raise VerificationError(
|
| 567 |
+
"%s (we have %d, but C compiler says %d)"
|
| 568 |
+
% (msg, expectedvalue, realvalue))
|
| 569 |
+
ffi = self.ffi
|
| 570 |
+
BStruct = ffi._get_cached_btype(tp)
|
| 571 |
+
layout, cname = self._struct_pending_verification.pop(tp)
|
| 572 |
+
check(layout[0], ffi.sizeof(BStruct), "wrong total size")
|
| 573 |
+
check(layout[1], ffi.alignof(BStruct), "wrong total alignment")
|
| 574 |
+
i = 2
|
| 575 |
+
for fname, ftype, fbitsize, fqual in tp.enumfields():
|
| 576 |
+
if fbitsize >= 0:
|
| 577 |
+
continue # xxx ignore fbitsize for now
|
| 578 |
+
check(layout[i], ffi.offsetof(BStruct, fname),
|
| 579 |
+
"wrong offset for field %r" % (fname,))
|
| 580 |
+
if layout[i+1] != 0:
|
| 581 |
+
BField = ffi._get_cached_btype(ftype)
|
| 582 |
+
check(layout[i+1], ffi.sizeof(BField),
|
| 583 |
+
"wrong size for field %r" % (fname,))
|
| 584 |
+
i += 2
|
| 585 |
+
assert i == len(layout)
|
| 586 |
+
|
| 587 |
+
# ----------
|
| 588 |
+
# 'anonymous' declarations. These are produced for anonymous structs
|
| 589 |
+
# or unions; the 'name' is obtained by a typedef.
|
| 590 |
+
|
| 591 |
+
_generate_cpy_anonymous_collecttype = _generate_nothing
|
| 592 |
+
|
| 593 |
+
def _generate_cpy_anonymous_decl(self, tp, name):
|
| 594 |
+
if isinstance(tp, model.EnumType):
|
| 595 |
+
self._generate_cpy_enum_decl(tp, name, '')
|
| 596 |
+
else:
|
| 597 |
+
self._generate_struct_or_union_decl(tp, '', name)
|
| 598 |
+
|
| 599 |
+
def _generate_cpy_anonymous_method(self, tp, name):
|
| 600 |
+
if not isinstance(tp, model.EnumType):
|
| 601 |
+
self._generate_struct_or_union_method(tp, '', name)
|
| 602 |
+
|
| 603 |
+
def _loading_cpy_anonymous(self, tp, name, module):
|
| 604 |
+
if isinstance(tp, model.EnumType):
|
| 605 |
+
self._loading_cpy_enum(tp, name, module)
|
| 606 |
+
else:
|
| 607 |
+
self._loading_struct_or_union(tp, '', name, module)
|
| 608 |
+
|
| 609 |
+
def _loaded_cpy_anonymous(self, tp, name, module, **kwds):
|
| 610 |
+
if isinstance(tp, model.EnumType):
|
| 611 |
+
self._loaded_cpy_enum(tp, name, module, **kwds)
|
| 612 |
+
else:
|
| 613 |
+
self._loaded_struct_or_union(tp)
|
| 614 |
+
|
| 615 |
+
# ----------
|
| 616 |
+
# constants, likely declared with '#define'
|
| 617 |
+
|
| 618 |
+
def _generate_cpy_const(self, is_int, name, tp=None, category='const',
|
| 619 |
+
vartp=None, delayed=True, size_too=False,
|
| 620 |
+
check_value=None):
|
| 621 |
+
prnt = self._prnt
|
| 622 |
+
funcname = '_cffi_%s_%s' % (category, name)
|
| 623 |
+
prnt('static int %s(PyObject *lib)' % funcname)
|
| 624 |
+
prnt('{')
|
| 625 |
+
prnt(' PyObject *o;')
|
| 626 |
+
prnt(' int res;')
|
| 627 |
+
if not is_int:
|
| 628 |
+
prnt(' %s;' % (vartp or tp).get_c_name(' i', name))
|
| 629 |
+
else:
|
| 630 |
+
assert category == 'const'
|
| 631 |
+
#
|
| 632 |
+
if check_value is not None:
|
| 633 |
+
self._check_int_constant_value(name, check_value)
|
| 634 |
+
#
|
| 635 |
+
if not is_int:
|
| 636 |
+
if category == 'var':
|
| 637 |
+
realexpr = '&' + name
|
| 638 |
+
else:
|
| 639 |
+
realexpr = name
|
| 640 |
+
prnt(' i = (%s);' % (realexpr,))
|
| 641 |
+
prnt(' o = %s;' % (self._convert_expr_from_c(tp, 'i',
|
| 642 |
+
'variable type'),))
|
| 643 |
+
assert delayed
|
| 644 |
+
else:
|
| 645 |
+
prnt(' o = _cffi_from_c_int_const(%s);' % name)
|
| 646 |
+
prnt(' if (o == NULL)')
|
| 647 |
+
prnt(' return -1;')
|
| 648 |
+
if size_too:
|
| 649 |
+
prnt(' {')
|
| 650 |
+
prnt(' PyObject *o1 = o;')
|
| 651 |
+
prnt(' o = Py_BuildValue("On", o1, (Py_ssize_t)sizeof(%s));'
|
| 652 |
+
% (name,))
|
| 653 |
+
prnt(' Py_DECREF(o1);')
|
| 654 |
+
prnt(' if (o == NULL)')
|
| 655 |
+
prnt(' return -1;')
|
| 656 |
+
prnt(' }')
|
| 657 |
+
prnt(' res = PyObject_SetAttrString(lib, "%s", o);' % name)
|
| 658 |
+
prnt(' Py_DECREF(o);')
|
| 659 |
+
prnt(' if (res < 0)')
|
| 660 |
+
prnt(' return -1;')
|
| 661 |
+
prnt(' return %s;' % self._chained_list_constants[delayed])
|
| 662 |
+
self._chained_list_constants[delayed] = funcname + '(lib)'
|
| 663 |
+
prnt('}')
|
| 664 |
+
prnt()
|
| 665 |
+
|
| 666 |
+
def _generate_cpy_constant_collecttype(self, tp, name):
|
| 667 |
+
is_int = isinstance(tp, model.PrimitiveType) and tp.is_integer_type()
|
| 668 |
+
if not is_int:
|
| 669 |
+
self._do_collect_type(tp)
|
| 670 |
+
|
| 671 |
+
def _generate_cpy_constant_decl(self, tp, name):
|
| 672 |
+
is_int = isinstance(tp, model.PrimitiveType) and tp.is_integer_type()
|
| 673 |
+
self._generate_cpy_const(is_int, name, tp)
|
| 674 |
+
|
| 675 |
+
_generate_cpy_constant_method = _generate_nothing
|
| 676 |
+
_loading_cpy_constant = _loaded_noop
|
| 677 |
+
_loaded_cpy_constant = _loaded_noop
|
| 678 |
+
|
| 679 |
+
# ----------
|
| 680 |
+
# enums
|
| 681 |
+
|
| 682 |
+
def _check_int_constant_value(self, name, value, err_prefix=''):
|
| 683 |
+
prnt = self._prnt
|
| 684 |
+
if value <= 0:
|
| 685 |
+
prnt(' if ((%s) > 0 || (long)(%s) != %dL) {' % (
|
| 686 |
+
name, name, value))
|
| 687 |
+
else:
|
| 688 |
+
prnt(' if ((%s) <= 0 || (unsigned long)(%s) != %dUL) {' % (
|
| 689 |
+
name, name, value))
|
| 690 |
+
prnt(' char buf[64];')
|
| 691 |
+
prnt(' if ((%s) <= 0)' % name)
|
| 692 |
+
prnt(' snprintf(buf, 63, "%%ld", (long)(%s));' % name)
|
| 693 |
+
prnt(' else')
|
| 694 |
+
prnt(' snprintf(buf, 63, "%%lu", (unsigned long)(%s));' %
|
| 695 |
+
name)
|
| 696 |
+
prnt(' PyErr_Format(_cffi_VerificationError,')
|
| 697 |
+
prnt(' "%s%s has the real value %s, not %s",')
|
| 698 |
+
prnt(' "%s", "%s", buf, "%d");' % (
|
| 699 |
+
err_prefix, name, value))
|
| 700 |
+
prnt(' return -1;')
|
| 701 |
+
prnt(' }')
|
| 702 |
+
|
| 703 |
+
def _enum_funcname(self, prefix, name):
|
| 704 |
+
# "$enum_$1" => "___D_enum____D_1"
|
| 705 |
+
name = name.replace('$', '___D_')
|
| 706 |
+
return '_cffi_e_%s_%s' % (prefix, name)
|
| 707 |
+
|
| 708 |
+
def _generate_cpy_enum_decl(self, tp, name, prefix='enum'):
|
| 709 |
+
if tp.partial:
|
| 710 |
+
for enumerator in tp.enumerators:
|
| 711 |
+
self._generate_cpy_const(True, enumerator, delayed=False)
|
| 712 |
+
return
|
| 713 |
+
#
|
| 714 |
+
funcname = self._enum_funcname(prefix, name)
|
| 715 |
+
prnt = self._prnt
|
| 716 |
+
prnt('static int %s(PyObject *lib)' % funcname)
|
| 717 |
+
prnt('{')
|
| 718 |
+
for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues):
|
| 719 |
+
self._check_int_constant_value(enumerator, enumvalue,
|
| 720 |
+
"enum %s: " % name)
|
| 721 |
+
prnt(' return %s;' % self._chained_list_constants[True])
|
| 722 |
+
self._chained_list_constants[True] = funcname + '(lib)'
|
| 723 |
+
prnt('}')
|
| 724 |
+
prnt()
|
| 725 |
+
|
| 726 |
+
_generate_cpy_enum_collecttype = _generate_nothing
|
| 727 |
+
_generate_cpy_enum_method = _generate_nothing
|
| 728 |
+
|
| 729 |
+
def _loading_cpy_enum(self, tp, name, module):
|
| 730 |
+
if tp.partial:
|
| 731 |
+
enumvalues = [getattr(module, enumerator)
|
| 732 |
+
for enumerator in tp.enumerators]
|
| 733 |
+
tp.enumvalues = tuple(enumvalues)
|
| 734 |
+
tp.partial_resolved = True
|
| 735 |
+
|
| 736 |
+
def _loaded_cpy_enum(self, tp, name, module, library):
|
| 737 |
+
for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues):
|
| 738 |
+
setattr(library, enumerator, enumvalue)
|
| 739 |
+
|
| 740 |
+
# ----------
|
| 741 |
+
# macros: for now only for integers
|
| 742 |
+
|
| 743 |
+
def _generate_cpy_macro_decl(self, tp, name):
|
| 744 |
+
if tp == '...':
|
| 745 |
+
check_value = None
|
| 746 |
+
else:
|
| 747 |
+
check_value = tp # an integer
|
| 748 |
+
self._generate_cpy_const(True, name, check_value=check_value)
|
| 749 |
+
|
| 750 |
+
_generate_cpy_macro_collecttype = _generate_nothing
|
| 751 |
+
_generate_cpy_macro_method = _generate_nothing
|
| 752 |
+
_loading_cpy_macro = _loaded_noop
|
| 753 |
+
_loaded_cpy_macro = _loaded_noop
|
| 754 |
+
|
| 755 |
+
# ----------
|
| 756 |
+
# global variables
|
| 757 |
+
|
| 758 |
+
def _generate_cpy_variable_collecttype(self, tp, name):
|
| 759 |
+
if isinstance(tp, model.ArrayType):
|
| 760 |
+
tp_ptr = model.PointerType(tp.item)
|
| 761 |
+
else:
|
| 762 |
+
tp_ptr = model.PointerType(tp)
|
| 763 |
+
self._do_collect_type(tp_ptr)
|
| 764 |
+
|
| 765 |
+
def _generate_cpy_variable_decl(self, tp, name):
|
| 766 |
+
if isinstance(tp, model.ArrayType):
|
| 767 |
+
tp_ptr = model.PointerType(tp.item)
|
| 768 |
+
self._generate_cpy_const(False, name, tp, vartp=tp_ptr,
|
| 769 |
+
size_too = tp.length_is_unknown())
|
| 770 |
+
else:
|
| 771 |
+
tp_ptr = model.PointerType(tp)
|
| 772 |
+
self._generate_cpy_const(False, name, tp_ptr, category='var')
|
| 773 |
+
|
| 774 |
+
_generate_cpy_variable_method = _generate_nothing
|
| 775 |
+
_loading_cpy_variable = _loaded_noop
|
| 776 |
+
|
| 777 |
+
def _loaded_cpy_variable(self, tp, name, module, library):
|
| 778 |
+
value = getattr(library, name)
|
| 779 |
+
if isinstance(tp, model.ArrayType): # int a[5] is "constant" in the
|
| 780 |
+
# sense that "a=..." is forbidden
|
| 781 |
+
if tp.length_is_unknown():
|
| 782 |
+
assert isinstance(value, tuple)
|
| 783 |
+
(value, size) = value
|
| 784 |
+
BItemType = self.ffi._get_cached_btype(tp.item)
|
| 785 |
+
length, rest = divmod(size, self.ffi.sizeof(BItemType))
|
| 786 |
+
if rest != 0:
|
| 787 |
+
raise VerificationError(
|
| 788 |
+
"bad size: %r does not seem to be an array of %s" %
|
| 789 |
+
(name, tp.item))
|
| 790 |
+
tp = tp.resolve_length(length)
|
| 791 |
+
# 'value' is a <cdata 'type *'> which we have to replace with
|
| 792 |
+
# a <cdata 'type[N]'> if the N is actually known
|
| 793 |
+
if tp.length is not None:
|
| 794 |
+
BArray = self.ffi._get_cached_btype(tp)
|
| 795 |
+
value = self.ffi.cast(BArray, value)
|
| 796 |
+
setattr(library, name, value)
|
| 797 |
+
return
|
| 798 |
+
# remove ptr=<cdata 'int *'> from the library instance, and replace
|
| 799 |
+
# it by a property on the class, which reads/writes into ptr[0].
|
| 800 |
+
ptr = value
|
| 801 |
+
delattr(library, name)
|
| 802 |
+
def getter(library):
|
| 803 |
+
return ptr[0]
|
| 804 |
+
def setter(library, value):
|
| 805 |
+
ptr[0] = value
|
| 806 |
+
setattr(type(library), name, property(getter, setter))
|
| 807 |
+
type(library)._cffi_dir.append(name)
|
| 808 |
+
|
| 809 |
+
# ----------
|
| 810 |
+
|
| 811 |
+
def _generate_setup_custom(self):
|
| 812 |
+
prnt = self._prnt
|
| 813 |
+
prnt('static int _cffi_setup_custom(PyObject *lib)')
|
| 814 |
+
prnt('{')
|
| 815 |
+
prnt(' return %s;' % self._chained_list_constants[True])
|
| 816 |
+
prnt('}')
|
| 817 |
+
|
| 818 |
+
cffimod_header = r'''
|
| 819 |
+
#include <Python.h>
|
| 820 |
+
#include <stddef.h>
|
| 821 |
+
|
| 822 |
+
/* this block of #ifs should be kept exactly identical between
|
| 823 |
+
c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
|
| 824 |
+
and cffi/_cffi_include.h */
|
| 825 |
+
#if defined(_MSC_VER)
|
| 826 |
+
# include <malloc.h> /* for alloca() */
|
| 827 |
+
# if _MSC_VER < 1600 /* MSVC < 2010 */
|
| 828 |
+
typedef __int8 int8_t;
|
| 829 |
+
typedef __int16 int16_t;
|
| 830 |
+
typedef __int32 int32_t;
|
| 831 |
+
typedef __int64 int64_t;
|
| 832 |
+
typedef unsigned __int8 uint8_t;
|
| 833 |
+
typedef unsigned __int16 uint16_t;
|
| 834 |
+
typedef unsigned __int32 uint32_t;
|
| 835 |
+
typedef unsigned __int64 uint64_t;
|
| 836 |
+
typedef __int8 int_least8_t;
|
| 837 |
+
typedef __int16 int_least16_t;
|
| 838 |
+
typedef __int32 int_least32_t;
|
| 839 |
+
typedef __int64 int_least64_t;
|
| 840 |
+
typedef unsigned __int8 uint_least8_t;
|
| 841 |
+
typedef unsigned __int16 uint_least16_t;
|
| 842 |
+
typedef unsigned __int32 uint_least32_t;
|
| 843 |
+
typedef unsigned __int64 uint_least64_t;
|
| 844 |
+
typedef __int8 int_fast8_t;
|
| 845 |
+
typedef __int16 int_fast16_t;
|
| 846 |
+
typedef __int32 int_fast32_t;
|
| 847 |
+
typedef __int64 int_fast64_t;
|
| 848 |
+
typedef unsigned __int8 uint_fast8_t;
|
| 849 |
+
typedef unsigned __int16 uint_fast16_t;
|
| 850 |
+
typedef unsigned __int32 uint_fast32_t;
|
| 851 |
+
typedef unsigned __int64 uint_fast64_t;
|
| 852 |
+
typedef __int64 intmax_t;
|
| 853 |
+
typedef unsigned __int64 uintmax_t;
|
| 854 |
+
# else
|
| 855 |
+
# include <stdint.h>
|
| 856 |
+
# endif
|
| 857 |
+
# if _MSC_VER < 1800 /* MSVC < 2013 */
|
| 858 |
+
# ifndef __cplusplus
|
| 859 |
+
typedef unsigned char _Bool;
|
| 860 |
+
# endif
|
| 861 |
+
# endif
|
| 862 |
+
# define _cffi_float_complex_t _Fcomplex /* include <complex.h> for it */
|
| 863 |
+
# define _cffi_double_complex_t _Dcomplex /* include <complex.h> for it */
|
| 864 |
+
#else
|
| 865 |
+
# include <stdint.h>
|
| 866 |
+
# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
|
| 867 |
+
# include <alloca.h>
|
| 868 |
+
# endif
|
| 869 |
+
# define _cffi_float_complex_t float _Complex
|
| 870 |
+
# define _cffi_double_complex_t double _Complex
|
| 871 |
+
#endif
|
| 872 |
+
|
| 873 |
+
#if PY_MAJOR_VERSION < 3
|
| 874 |
+
# undef PyCapsule_CheckExact
|
| 875 |
+
# undef PyCapsule_GetPointer
|
| 876 |
+
# define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule))
|
| 877 |
+
# define PyCapsule_GetPointer(capsule, name) \
|
| 878 |
+
(PyCObject_AsVoidPtr(capsule))
|
| 879 |
+
#endif
|
| 880 |
+
|
| 881 |
+
#if PY_MAJOR_VERSION >= 3
|
| 882 |
+
# define PyInt_FromLong PyLong_FromLong
|
| 883 |
+
#endif
|
| 884 |
+
|
| 885 |
+
#define _cffi_from_c_double PyFloat_FromDouble
|
| 886 |
+
#define _cffi_from_c_float PyFloat_FromDouble
|
| 887 |
+
#define _cffi_from_c_long PyInt_FromLong
|
| 888 |
+
#define _cffi_from_c_ulong PyLong_FromUnsignedLong
|
| 889 |
+
#define _cffi_from_c_longlong PyLong_FromLongLong
|
| 890 |
+
#define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
|
| 891 |
+
#define _cffi_from_c__Bool PyBool_FromLong
|
| 892 |
+
|
| 893 |
+
#define _cffi_to_c_double PyFloat_AsDouble
|
| 894 |
+
#define _cffi_to_c_float PyFloat_AsDouble
|
| 895 |
+
|
| 896 |
+
#define _cffi_from_c_int_const(x) \
|
| 897 |
+
(((x) > 0) ? \
|
| 898 |
+
((unsigned long long)(x) <= (unsigned long long)LONG_MAX) ? \
|
| 899 |
+
PyInt_FromLong((long)(x)) : \
|
| 900 |
+
PyLong_FromUnsignedLongLong((unsigned long long)(x)) : \
|
| 901 |
+
((long long)(x) >= (long long)LONG_MIN) ? \
|
| 902 |
+
PyInt_FromLong((long)(x)) : \
|
| 903 |
+
PyLong_FromLongLong((long long)(x)))
|
| 904 |
+
|
| 905 |
+
#define _cffi_from_c_int(x, type) \
|
| 906 |
+
(((type)-1) > 0 ? /* unsigned */ \
|
| 907 |
+
(sizeof(type) < sizeof(long) ? \
|
| 908 |
+
PyInt_FromLong((long)x) : \
|
| 909 |
+
sizeof(type) == sizeof(long) ? \
|
| 910 |
+
PyLong_FromUnsignedLong((unsigned long)x) : \
|
| 911 |
+
PyLong_FromUnsignedLongLong((unsigned long long)x)) : \
|
| 912 |
+
(sizeof(type) <= sizeof(long) ? \
|
| 913 |
+
PyInt_FromLong((long)x) : \
|
| 914 |
+
PyLong_FromLongLong((long long)x)))
|
| 915 |
+
|
| 916 |
+
#define _cffi_to_c_int(o, type) \
|
| 917 |
+
((type)( \
|
| 918 |
+
sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \
|
| 919 |
+
: (type)_cffi_to_c_i8(o)) : \
|
| 920 |
+
sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \
|
| 921 |
+
: (type)_cffi_to_c_i16(o)) : \
|
| 922 |
+
sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \
|
| 923 |
+
: (type)_cffi_to_c_i32(o)) : \
|
| 924 |
+
sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \
|
| 925 |
+
: (type)_cffi_to_c_i64(o)) : \
|
| 926 |
+
(Py_FatalError("unsupported size for type " #type), (type)0)))
|
| 927 |
+
|
| 928 |
+
#define _cffi_to_c_i8 \
|
| 929 |
+
((int(*)(PyObject *))_cffi_exports[1])
|
| 930 |
+
#define _cffi_to_c_u8 \
|
| 931 |
+
((int(*)(PyObject *))_cffi_exports[2])
|
| 932 |
+
#define _cffi_to_c_i16 \
|
| 933 |
+
((int(*)(PyObject *))_cffi_exports[3])
|
| 934 |
+
#define _cffi_to_c_u16 \
|
| 935 |
+
((int(*)(PyObject *))_cffi_exports[4])
|
| 936 |
+
#define _cffi_to_c_i32 \
|
| 937 |
+
((int(*)(PyObject *))_cffi_exports[5])
|
| 938 |
+
#define _cffi_to_c_u32 \
|
| 939 |
+
((unsigned int(*)(PyObject *))_cffi_exports[6])
|
| 940 |
+
#define _cffi_to_c_i64 \
|
| 941 |
+
((long long(*)(PyObject *))_cffi_exports[7])
|
| 942 |
+
#define _cffi_to_c_u64 \
|
| 943 |
+
((unsigned long long(*)(PyObject *))_cffi_exports[8])
|
| 944 |
+
#define _cffi_to_c_char \
|
| 945 |
+
((int(*)(PyObject *))_cffi_exports[9])
|
| 946 |
+
#define _cffi_from_c_pointer \
|
| 947 |
+
((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[10])
|
| 948 |
+
#define _cffi_to_c_pointer \
|
| 949 |
+
((char *(*)(PyObject *, CTypeDescrObject *))_cffi_exports[11])
|
| 950 |
+
#define _cffi_get_struct_layout \
|
| 951 |
+
((PyObject *(*)(Py_ssize_t[]))_cffi_exports[12])
|
| 952 |
+
#define _cffi_restore_errno \
|
| 953 |
+
((void(*)(void))_cffi_exports[13])
|
| 954 |
+
#define _cffi_save_errno \
|
| 955 |
+
((void(*)(void))_cffi_exports[14])
|
| 956 |
+
#define _cffi_from_c_char \
|
| 957 |
+
((PyObject *(*)(char))_cffi_exports[15])
|
| 958 |
+
#define _cffi_from_c_deref \
|
| 959 |
+
((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[16])
|
| 960 |
+
#define _cffi_to_c \
|
| 961 |
+
((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[17])
|
| 962 |
+
#define _cffi_from_c_struct \
|
| 963 |
+
((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[18])
|
| 964 |
+
#define _cffi_to_c_wchar_t \
|
| 965 |
+
((wchar_t(*)(PyObject *))_cffi_exports[19])
|
| 966 |
+
#define _cffi_from_c_wchar_t \
|
| 967 |
+
((PyObject *(*)(wchar_t))_cffi_exports[20])
|
| 968 |
+
#define _cffi_to_c_long_double \
|
| 969 |
+
((long double(*)(PyObject *))_cffi_exports[21])
|
| 970 |
+
#define _cffi_to_c__Bool \
|
| 971 |
+
((_Bool(*)(PyObject *))_cffi_exports[22])
|
| 972 |
+
#define _cffi_prepare_pointer_call_argument \
|
| 973 |
+
((Py_ssize_t(*)(CTypeDescrObject *, PyObject *, char **))_cffi_exports[23])
|
| 974 |
+
#define _cffi_convert_array_from_object \
|
| 975 |
+
((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[24])
|
| 976 |
+
#define _CFFI_NUM_EXPORTS 25
|
| 977 |
+
|
| 978 |
+
typedef struct _ctypedescr CTypeDescrObject;
|
| 979 |
+
|
| 980 |
+
static void *_cffi_exports[_CFFI_NUM_EXPORTS];
|
| 981 |
+
static PyObject *_cffi_types, *_cffi_VerificationError;
|
| 982 |
+
|
| 983 |
+
static int _cffi_setup_custom(PyObject *lib); /* forward */
|
| 984 |
+
|
| 985 |
+
static PyObject *_cffi_setup(PyObject *self, PyObject *args)
|
| 986 |
+
{
|
| 987 |
+
PyObject *library;
|
| 988 |
+
int was_alive = (_cffi_types != NULL);
|
| 989 |
+
(void)self; /* unused */
|
| 990 |
+
if (!PyArg_ParseTuple(args, "OOO", &_cffi_types, &_cffi_VerificationError,
|
| 991 |
+
&library))
|
| 992 |
+
return NULL;
|
| 993 |
+
Py_INCREF(_cffi_types);
|
| 994 |
+
Py_INCREF(_cffi_VerificationError);
|
| 995 |
+
if (_cffi_setup_custom(library) < 0)
|
| 996 |
+
return NULL;
|
| 997 |
+
return PyBool_FromLong(was_alive);
|
| 998 |
+
}
|
| 999 |
+
|
| 1000 |
+
union _cffi_union_alignment_u {
|
| 1001 |
+
unsigned char m_char;
|
| 1002 |
+
unsigned short m_short;
|
| 1003 |
+
unsigned int m_int;
|
| 1004 |
+
unsigned long m_long;
|
| 1005 |
+
unsigned long long m_longlong;
|
| 1006 |
+
float m_float;
|
| 1007 |
+
double m_double;
|
| 1008 |
+
long double m_longdouble;
|
| 1009 |
+
};
|
| 1010 |
+
|
| 1011 |
+
struct _cffi_freeme_s {
|
| 1012 |
+
struct _cffi_freeme_s *next;
|
| 1013 |
+
union _cffi_union_alignment_u alignment;
|
| 1014 |
+
};
|
| 1015 |
+
|
| 1016 |
+
#ifdef __GNUC__
|
| 1017 |
+
__attribute__((unused))
|
| 1018 |
+
#endif
|
| 1019 |
+
static int _cffi_convert_array_argument(CTypeDescrObject *ctptr, PyObject *arg,
|
| 1020 |
+
char **output_data, Py_ssize_t datasize,
|
| 1021 |
+
struct _cffi_freeme_s **freeme)
|
| 1022 |
+
{
|
| 1023 |
+
char *p;
|
| 1024 |
+
if (datasize < 0)
|
| 1025 |
+
return -1;
|
| 1026 |
+
|
| 1027 |
+
p = *output_data;
|
| 1028 |
+
if (p == NULL) {
|
| 1029 |
+
struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc(
|
| 1030 |
+
offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize);
|
| 1031 |
+
if (fp == NULL)
|
| 1032 |
+
return -1;
|
| 1033 |
+
fp->next = *freeme;
|
| 1034 |
+
*freeme = fp;
|
| 1035 |
+
p = *output_data = (char *)&fp->alignment;
|
| 1036 |
+
}
|
| 1037 |
+
memset((void *)p, 0, (size_t)datasize);
|
| 1038 |
+
return _cffi_convert_array_from_object(p, ctptr, arg);
|
| 1039 |
+
}
|
| 1040 |
+
|
| 1041 |
+
#ifdef __GNUC__
|
| 1042 |
+
__attribute__((unused))
|
| 1043 |
+
#endif
|
| 1044 |
+
static void _cffi_free_array_arguments(struct _cffi_freeme_s *freeme)
|
| 1045 |
+
{
|
| 1046 |
+
do {
|
| 1047 |
+
void *p = (void *)freeme;
|
| 1048 |
+
freeme = freeme->next;
|
| 1049 |
+
PyObject_Free(p);
|
| 1050 |
+
} while (freeme != NULL);
|
| 1051 |
+
}
|
| 1052 |
+
|
| 1053 |
+
static int _cffi_init(void)
|
| 1054 |
+
{
|
| 1055 |
+
PyObject *module, *c_api_object = NULL;
|
| 1056 |
+
|
| 1057 |
+
module = PyImport_ImportModule("_cffi_backend");
|
| 1058 |
+
if (module == NULL)
|
| 1059 |
+
goto failure;
|
| 1060 |
+
|
| 1061 |
+
c_api_object = PyObject_GetAttrString(module, "_C_API");
|
| 1062 |
+
if (c_api_object == NULL)
|
| 1063 |
+
goto failure;
|
| 1064 |
+
if (!PyCapsule_CheckExact(c_api_object)) {
|
| 1065 |
+
PyErr_SetNone(PyExc_ImportError);
|
| 1066 |
+
goto failure;
|
| 1067 |
+
}
|
| 1068 |
+
memcpy(_cffi_exports, PyCapsule_GetPointer(c_api_object, "cffi"),
|
| 1069 |
+
_CFFI_NUM_EXPORTS * sizeof(void *));
|
| 1070 |
+
|
| 1071 |
+
Py_DECREF(module);
|
| 1072 |
+
Py_DECREF(c_api_object);
|
| 1073 |
+
return 0;
|
| 1074 |
+
|
| 1075 |
+
failure:
|
| 1076 |
+
Py_XDECREF(module);
|
| 1077 |
+
Py_XDECREF(c_api_object);
|
| 1078 |
+
return -1;
|
| 1079 |
+
}
|
| 1080 |
+
|
| 1081 |
+
#define _cffi_type(num) ((CTypeDescrObject *)PyList_GET_ITEM(_cffi_types, num))
|
| 1082 |
+
|
| 1083 |
+
/**********/
|
| 1084 |
+
'''
|
.venv/lib/python3.11/site-packages/cffi/verifier.py
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# DEPRECATED: implementation for ffi.verify()
|
| 3 |
+
#
|
| 4 |
+
import sys, os, binascii, shutil, io
|
| 5 |
+
from . import __version_verifier_modules__
|
| 6 |
+
from . import ffiplatform
|
| 7 |
+
from .error import VerificationError
|
| 8 |
+
|
| 9 |
+
if sys.version_info >= (3, 3):
|
| 10 |
+
import importlib.machinery
|
| 11 |
+
def _extension_suffixes():
|
| 12 |
+
return importlib.machinery.EXTENSION_SUFFIXES[:]
|
| 13 |
+
else:
|
| 14 |
+
import imp
|
| 15 |
+
def _extension_suffixes():
|
| 16 |
+
return [suffix for suffix, _, type in imp.get_suffixes()
|
| 17 |
+
if type == imp.C_EXTENSION]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
if sys.version_info >= (3,):
|
| 21 |
+
NativeIO = io.StringIO
|
| 22 |
+
else:
|
| 23 |
+
class NativeIO(io.BytesIO):
|
| 24 |
+
def write(self, s):
|
| 25 |
+
if isinstance(s, unicode):
|
| 26 |
+
s = s.encode('ascii')
|
| 27 |
+
super(NativeIO, self).write(s)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class Verifier(object):
|
| 31 |
+
|
| 32 |
+
def __init__(self, ffi, preamble, tmpdir=None, modulename=None,
|
| 33 |
+
ext_package=None, tag='', force_generic_engine=False,
|
| 34 |
+
source_extension='.c', flags=None, relative_to=None, **kwds):
|
| 35 |
+
if ffi._parser._uses_new_feature:
|
| 36 |
+
raise VerificationError(
|
| 37 |
+
"feature not supported with ffi.verify(), but only "
|
| 38 |
+
"with ffi.set_source(): %s" % (ffi._parser._uses_new_feature,))
|
| 39 |
+
self.ffi = ffi
|
| 40 |
+
self.preamble = preamble
|
| 41 |
+
if not modulename:
|
| 42 |
+
flattened_kwds = ffiplatform.flatten(kwds)
|
| 43 |
+
vengine_class = _locate_engine_class(ffi, force_generic_engine)
|
| 44 |
+
self._vengine = vengine_class(self)
|
| 45 |
+
self._vengine.patch_extension_kwds(kwds)
|
| 46 |
+
self.flags = flags
|
| 47 |
+
self.kwds = self.make_relative_to(kwds, relative_to)
|
| 48 |
+
#
|
| 49 |
+
if modulename:
|
| 50 |
+
if tag:
|
| 51 |
+
raise TypeError("can't specify both 'modulename' and 'tag'")
|
| 52 |
+
else:
|
| 53 |
+
key = '\x00'.join(['%d.%d' % sys.version_info[:2],
|
| 54 |
+
__version_verifier_modules__,
|
| 55 |
+
preamble, flattened_kwds] +
|
| 56 |
+
ffi._cdefsources)
|
| 57 |
+
if sys.version_info >= (3,):
|
| 58 |
+
key = key.encode('utf-8')
|
| 59 |
+
k1 = hex(binascii.crc32(key[0::2]) & 0xffffffff)
|
| 60 |
+
k1 = k1.lstrip('0x').rstrip('L')
|
| 61 |
+
k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff)
|
| 62 |
+
k2 = k2.lstrip('0').rstrip('L')
|
| 63 |
+
modulename = '_cffi_%s_%s%s%s' % (tag, self._vengine._class_key,
|
| 64 |
+
k1, k2)
|
| 65 |
+
suffix = _get_so_suffixes()[0]
|
| 66 |
+
self.tmpdir = tmpdir or _caller_dir_pycache()
|
| 67 |
+
self.sourcefilename = os.path.join(self.tmpdir, modulename + source_extension)
|
| 68 |
+
self.modulefilename = os.path.join(self.tmpdir, modulename + suffix)
|
| 69 |
+
self.ext_package = ext_package
|
| 70 |
+
self._has_source = False
|
| 71 |
+
self._has_module = False
|
| 72 |
+
|
| 73 |
+
def write_source(self, file=None):
|
| 74 |
+
"""Write the C source code. It is produced in 'self.sourcefilename',
|
| 75 |
+
which can be tweaked beforehand."""
|
| 76 |
+
with self.ffi._lock:
|
| 77 |
+
if self._has_source and file is None:
|
| 78 |
+
raise VerificationError(
|
| 79 |
+
"source code already written")
|
| 80 |
+
self._write_source(file)
|
| 81 |
+
|
| 82 |
+
def compile_module(self):
|
| 83 |
+
"""Write the C source code (if not done already) and compile it.
|
| 84 |
+
This produces a dynamic link library in 'self.modulefilename'."""
|
| 85 |
+
with self.ffi._lock:
|
| 86 |
+
if self._has_module:
|
| 87 |
+
raise VerificationError("module already compiled")
|
| 88 |
+
if not self._has_source:
|
| 89 |
+
self._write_source()
|
| 90 |
+
self._compile_module()
|
| 91 |
+
|
| 92 |
+
def load_library(self):
|
| 93 |
+
"""Get a C module from this Verifier instance.
|
| 94 |
+
Returns an instance of a FFILibrary class that behaves like the
|
| 95 |
+
objects returned by ffi.dlopen(), but that delegates all
|
| 96 |
+
operations to the C module. If necessary, the C code is written
|
| 97 |
+
and compiled first.
|
| 98 |
+
"""
|
| 99 |
+
with self.ffi._lock:
|
| 100 |
+
if not self._has_module:
|
| 101 |
+
self._locate_module()
|
| 102 |
+
if not self._has_module:
|
| 103 |
+
if not self._has_source:
|
| 104 |
+
self._write_source()
|
| 105 |
+
self._compile_module()
|
| 106 |
+
return self._load_library()
|
| 107 |
+
|
| 108 |
+
def get_module_name(self):
|
| 109 |
+
basename = os.path.basename(self.modulefilename)
|
| 110 |
+
# kill both the .so extension and the other .'s, as introduced
|
| 111 |
+
# by Python 3: 'basename.cpython-33m.so'
|
| 112 |
+
basename = basename.split('.', 1)[0]
|
| 113 |
+
# and the _d added in Python 2 debug builds --- but try to be
|
| 114 |
+
# conservative and not kill a legitimate _d
|
| 115 |
+
if basename.endswith('_d') and hasattr(sys, 'gettotalrefcount'):
|
| 116 |
+
basename = basename[:-2]
|
| 117 |
+
return basename
|
| 118 |
+
|
| 119 |
+
def get_extension(self):
|
| 120 |
+
if not self._has_source:
|
| 121 |
+
with self.ffi._lock:
|
| 122 |
+
if not self._has_source:
|
| 123 |
+
self._write_source()
|
| 124 |
+
sourcename = ffiplatform.maybe_relative_path(self.sourcefilename)
|
| 125 |
+
modname = self.get_module_name()
|
| 126 |
+
return ffiplatform.get_extension(sourcename, modname, **self.kwds)
|
| 127 |
+
|
| 128 |
+
def generates_python_module(self):
|
| 129 |
+
return self._vengine._gen_python_module
|
| 130 |
+
|
| 131 |
+
def make_relative_to(self, kwds, relative_to):
|
| 132 |
+
if relative_to and os.path.dirname(relative_to):
|
| 133 |
+
dirname = os.path.dirname(relative_to)
|
| 134 |
+
kwds = kwds.copy()
|
| 135 |
+
for key in ffiplatform.LIST_OF_FILE_NAMES:
|
| 136 |
+
if key in kwds:
|
| 137 |
+
lst = kwds[key]
|
| 138 |
+
if not isinstance(lst, (list, tuple)):
|
| 139 |
+
raise TypeError("keyword '%s' should be a list or tuple"
|
| 140 |
+
% (key,))
|
| 141 |
+
lst = [os.path.join(dirname, fn) for fn in lst]
|
| 142 |
+
kwds[key] = lst
|
| 143 |
+
return kwds
|
| 144 |
+
|
| 145 |
+
# ----------
|
| 146 |
+
|
| 147 |
+
def _locate_module(self):
|
| 148 |
+
if not os.path.isfile(self.modulefilename):
|
| 149 |
+
if self.ext_package:
|
| 150 |
+
try:
|
| 151 |
+
pkg = __import__(self.ext_package, None, None, ['__doc__'])
|
| 152 |
+
except ImportError:
|
| 153 |
+
return # cannot import the package itself, give up
|
| 154 |
+
# (e.g. it might be called differently before installation)
|
| 155 |
+
path = pkg.__path__
|
| 156 |
+
else:
|
| 157 |
+
path = None
|
| 158 |
+
filename = self._vengine.find_module(self.get_module_name(), path,
|
| 159 |
+
_get_so_suffixes())
|
| 160 |
+
if filename is None:
|
| 161 |
+
return
|
| 162 |
+
self.modulefilename = filename
|
| 163 |
+
self._vengine.collect_types()
|
| 164 |
+
self._has_module = True
|
| 165 |
+
|
| 166 |
+
def _write_source_to(self, file):
|
| 167 |
+
self._vengine._f = file
|
| 168 |
+
try:
|
| 169 |
+
self._vengine.write_source_to_f()
|
| 170 |
+
finally:
|
| 171 |
+
del self._vengine._f
|
| 172 |
+
|
| 173 |
+
def _write_source(self, file=None):
|
| 174 |
+
if file is not None:
|
| 175 |
+
self._write_source_to(file)
|
| 176 |
+
else:
|
| 177 |
+
# Write our source file to an in memory file.
|
| 178 |
+
f = NativeIO()
|
| 179 |
+
self._write_source_to(f)
|
| 180 |
+
source_data = f.getvalue()
|
| 181 |
+
|
| 182 |
+
# Determine if this matches the current file
|
| 183 |
+
if os.path.exists(self.sourcefilename):
|
| 184 |
+
with open(self.sourcefilename, "r") as fp:
|
| 185 |
+
needs_written = not (fp.read() == source_data)
|
| 186 |
+
else:
|
| 187 |
+
needs_written = True
|
| 188 |
+
|
| 189 |
+
# Actually write the file out if it doesn't match
|
| 190 |
+
if needs_written:
|
| 191 |
+
_ensure_dir(self.sourcefilename)
|
| 192 |
+
with open(self.sourcefilename, "w") as fp:
|
| 193 |
+
fp.write(source_data)
|
| 194 |
+
|
| 195 |
+
# Set this flag
|
| 196 |
+
self._has_source = True
|
| 197 |
+
|
| 198 |
+
def _compile_module(self):
|
| 199 |
+
# compile this C source
|
| 200 |
+
tmpdir = os.path.dirname(self.sourcefilename)
|
| 201 |
+
outputfilename = ffiplatform.compile(tmpdir, self.get_extension())
|
| 202 |
+
try:
|
| 203 |
+
same = ffiplatform.samefile(outputfilename, self.modulefilename)
|
| 204 |
+
except OSError:
|
| 205 |
+
same = False
|
| 206 |
+
if not same:
|
| 207 |
+
_ensure_dir(self.modulefilename)
|
| 208 |
+
shutil.move(outputfilename, self.modulefilename)
|
| 209 |
+
self._has_module = True
|
| 210 |
+
|
| 211 |
+
def _load_library(self):
|
| 212 |
+
assert self._has_module
|
| 213 |
+
if self.flags is not None:
|
| 214 |
+
return self._vengine.load_library(self.flags)
|
| 215 |
+
else:
|
| 216 |
+
return self._vengine.load_library()
|
| 217 |
+
|
| 218 |
+
# ____________________________________________________________
|
| 219 |
+
|
| 220 |
+
_FORCE_GENERIC_ENGINE = False # for tests
|
| 221 |
+
|
| 222 |
+
def _locate_engine_class(ffi, force_generic_engine):
|
| 223 |
+
if _FORCE_GENERIC_ENGINE:
|
| 224 |
+
force_generic_engine = True
|
| 225 |
+
if not force_generic_engine:
|
| 226 |
+
if '__pypy__' in sys.builtin_module_names:
|
| 227 |
+
force_generic_engine = True
|
| 228 |
+
else:
|
| 229 |
+
try:
|
| 230 |
+
import _cffi_backend
|
| 231 |
+
except ImportError:
|
| 232 |
+
_cffi_backend = '?'
|
| 233 |
+
if ffi._backend is not _cffi_backend:
|
| 234 |
+
force_generic_engine = True
|
| 235 |
+
if force_generic_engine:
|
| 236 |
+
from . import vengine_gen
|
| 237 |
+
return vengine_gen.VGenericEngine
|
| 238 |
+
else:
|
| 239 |
+
from . import vengine_cpy
|
| 240 |
+
return vengine_cpy.VCPythonEngine
|
| 241 |
+
|
| 242 |
+
# ____________________________________________________________
|
| 243 |
+
|
| 244 |
+
_TMPDIR = None
|
| 245 |
+
|
| 246 |
+
def _caller_dir_pycache():
|
| 247 |
+
if _TMPDIR:
|
| 248 |
+
return _TMPDIR
|
| 249 |
+
result = os.environ.get('CFFI_TMPDIR')
|
| 250 |
+
if result:
|
| 251 |
+
return result
|
| 252 |
+
filename = sys._getframe(2).f_code.co_filename
|
| 253 |
+
return os.path.abspath(os.path.join(os.path.dirname(filename),
|
| 254 |
+
'__pycache__'))
|
| 255 |
+
|
| 256 |
+
def set_tmpdir(dirname):
|
| 257 |
+
"""Set the temporary directory to use instead of __pycache__."""
|
| 258 |
+
global _TMPDIR
|
| 259 |
+
_TMPDIR = dirname
|
| 260 |
+
|
| 261 |
+
def cleanup_tmpdir(tmpdir=None, keep_so=False):
|
| 262 |
+
"""Clean up the temporary directory by removing all files in it
|
| 263 |
+
called `_cffi_*.{c,so}` as well as the `build` subdirectory."""
|
| 264 |
+
tmpdir = tmpdir or _caller_dir_pycache()
|
| 265 |
+
try:
|
| 266 |
+
filelist = os.listdir(tmpdir)
|
| 267 |
+
except OSError:
|
| 268 |
+
return
|
| 269 |
+
if keep_so:
|
| 270 |
+
suffix = '.c' # only remove .c files
|
| 271 |
+
else:
|
| 272 |
+
suffix = _get_so_suffixes()[0].lower()
|
| 273 |
+
for fn in filelist:
|
| 274 |
+
if fn.lower().startswith('_cffi_') and (
|
| 275 |
+
fn.lower().endswith(suffix) or fn.lower().endswith('.c')):
|
| 276 |
+
try:
|
| 277 |
+
os.unlink(os.path.join(tmpdir, fn))
|
| 278 |
+
except OSError:
|
| 279 |
+
pass
|
| 280 |
+
clean_dir = [os.path.join(tmpdir, 'build')]
|
| 281 |
+
for dir in clean_dir:
|
| 282 |
+
try:
|
| 283 |
+
for fn in os.listdir(dir):
|
| 284 |
+
fn = os.path.join(dir, fn)
|
| 285 |
+
if os.path.isdir(fn):
|
| 286 |
+
clean_dir.append(fn)
|
| 287 |
+
else:
|
| 288 |
+
os.unlink(fn)
|
| 289 |
+
except OSError:
|
| 290 |
+
pass
|
| 291 |
+
|
| 292 |
+
def _get_so_suffixes():
|
| 293 |
+
suffixes = _extension_suffixes()
|
| 294 |
+
if not suffixes:
|
| 295 |
+
# bah, no C_EXTENSION available. Occurs on pypy without cpyext
|
| 296 |
+
if sys.platform == 'win32':
|
| 297 |
+
suffixes = [".pyd"]
|
| 298 |
+
else:
|
| 299 |
+
suffixes = [".so"]
|
| 300 |
+
|
| 301 |
+
return suffixes
|
| 302 |
+
|
| 303 |
+
def _ensure_dir(filename):
|
| 304 |
+
dirname = os.path.dirname(filename)
|
| 305 |
+
if dirname and not os.path.isdir(dirname):
|
| 306 |
+
os.makedirs(dirname)
|
.venv/lib/python3.11/site-packages/diskcache/__pycache__/core.cpython-311.pyc
ADDED
|
Binary file (95.4 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__init__.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
from . import (
|
| 4 |
+
exposition, gc_collector, metrics, metrics_core, platform_collector,
|
| 5 |
+
process_collector, registry,
|
| 6 |
+
)
|
| 7 |
+
from .exposition import (
|
| 8 |
+
CONTENT_TYPE_LATEST, delete_from_gateway, generate_latest,
|
| 9 |
+
instance_ip_grouping_key, make_asgi_app, make_wsgi_app, MetricsHandler,
|
| 10 |
+
push_to_gateway, pushadd_to_gateway, start_http_server, start_wsgi_server,
|
| 11 |
+
write_to_textfile,
|
| 12 |
+
)
|
| 13 |
+
from .gc_collector import GC_COLLECTOR, GCCollector
|
| 14 |
+
from .metrics import (
|
| 15 |
+
Counter, disable_created_metrics, enable_created_metrics, Enum, Gauge,
|
| 16 |
+
Histogram, Info, Summary,
|
| 17 |
+
)
|
| 18 |
+
from .metrics_core import Metric
|
| 19 |
+
from .platform_collector import PLATFORM_COLLECTOR, PlatformCollector
|
| 20 |
+
from .process_collector import PROCESS_COLLECTOR, ProcessCollector
|
| 21 |
+
from .registry import CollectorRegistry, REGISTRY
|
| 22 |
+
|
| 23 |
+
__all__ = (
|
| 24 |
+
'CollectorRegistry',
|
| 25 |
+
'REGISTRY',
|
| 26 |
+
'Metric',
|
| 27 |
+
'Counter',
|
| 28 |
+
'Gauge',
|
| 29 |
+
'Summary',
|
| 30 |
+
'Histogram',
|
| 31 |
+
'Info',
|
| 32 |
+
'Enum',
|
| 33 |
+
'enable_created_metrics',
|
| 34 |
+
'disable_created_metrics',
|
| 35 |
+
'CONTENT_TYPE_LATEST',
|
| 36 |
+
'generate_latest',
|
| 37 |
+
'MetricsHandler',
|
| 38 |
+
'make_wsgi_app',
|
| 39 |
+
'make_asgi_app',
|
| 40 |
+
'start_http_server',
|
| 41 |
+
'start_wsgi_server',
|
| 42 |
+
'write_to_textfile',
|
| 43 |
+
'push_to_gateway',
|
| 44 |
+
'pushadd_to_gateway',
|
| 45 |
+
'delete_from_gateway',
|
| 46 |
+
'instance_ip_grouping_key',
|
| 47 |
+
'ProcessCollector',
|
| 48 |
+
'PROCESS_COLLECTOR',
|
| 49 |
+
'PlatformCollector',
|
| 50 |
+
'PLATFORM_COLLECTOR',
|
| 51 |
+
'GCCollector',
|
| 52 |
+
'GC_COLLECTOR',
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == '__main__':
|
| 56 |
+
c = Counter('cc', 'A counter')
|
| 57 |
+
c.inc()
|
| 58 |
+
|
| 59 |
+
g = Gauge('gg', 'A gauge')
|
| 60 |
+
g.set(17)
|
| 61 |
+
|
| 62 |
+
s = Summary('ss', 'A summary', ['a', 'b'])
|
| 63 |
+
s.labels('c', 'd').observe(17)
|
| 64 |
+
|
| 65 |
+
h = Histogram('hh', 'A histogram')
|
| 66 |
+
h.observe(.6)
|
| 67 |
+
|
| 68 |
+
start_http_server(8000)
|
| 69 |
+
import time
|
| 70 |
+
|
| 71 |
+
while True:
|
| 72 |
+
time.sleep(1)
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/asgi.cpython-311.pyc
ADDED
|
Binary file (3.53 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/context_managers.cpython-311.pyc
ADDED
|
Binary file (6.03 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/decorator.cpython-311.pyc
ADDED
|
Binary file (20.9 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/exposition.cpython-311.pyc
ADDED
|
Binary file (34.4 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/metrics_core.cpython-311.pyc
ADDED
|
Binary file (22.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/mmap_dict.cpython-311.pyc
ADDED
|
Binary file (8.88 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/parser.cpython-311.pyc
ADDED
|
Binary file (9.45 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/platform_collector.cpython-311.pyc
ADDED
|
Binary file (3.62 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/registry.cpython-311.pyc
ADDED
|
Binary file (10.4 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/__pycache__/values.cpython-311.pyc
ADDED
|
Binary file (9.14 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/prometheus_client/asgi.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Callable
|
| 2 |
+
from urllib.parse import parse_qs
|
| 3 |
+
|
| 4 |
+
from .exposition import _bake_output
|
| 5 |
+
from .registry import CollectorRegistry, REGISTRY
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def make_asgi_app(registry: CollectorRegistry = REGISTRY, disable_compression: bool = False) -> Callable:
|
| 9 |
+
"""Create a ASGI app which serves the metrics from a registry."""
|
| 10 |
+
|
| 11 |
+
async def prometheus_app(scope, receive, send):
|
| 12 |
+
assert scope.get("type") == "http"
|
| 13 |
+
# Prepare parameters
|
| 14 |
+
params = parse_qs(scope.get('query_string', b''))
|
| 15 |
+
accept_header = ",".join([
|
| 16 |
+
value.decode("utf8") for (name, value) in scope.get('headers')
|
| 17 |
+
if name.decode("utf8").lower() == 'accept'
|
| 18 |
+
])
|
| 19 |
+
accept_encoding_header = ",".join([
|
| 20 |
+
value.decode("utf8") for (name, value) in scope.get('headers')
|
| 21 |
+
if name.decode("utf8").lower() == 'accept-encoding'
|
| 22 |
+
])
|
| 23 |
+
# Bake output
|
| 24 |
+
status, headers, output = _bake_output(registry, accept_header, accept_encoding_header, params, disable_compression)
|
| 25 |
+
formatted_headers = []
|
| 26 |
+
for header in headers:
|
| 27 |
+
formatted_headers.append(tuple(x.encode('utf8') for x in header))
|
| 28 |
+
# Return output
|
| 29 |
+
payload = await receive()
|
| 30 |
+
if payload.get("type") == "http.request":
|
| 31 |
+
await send(
|
| 32 |
+
{
|
| 33 |
+
"type": "http.response.start",
|
| 34 |
+
"status": int(status.split(' ')[0]),
|
| 35 |
+
"headers": formatted_headers,
|
| 36 |
+
}
|
| 37 |
+
)
|
| 38 |
+
await send({"type": "http.response.body", "body": output})
|
| 39 |
+
|
| 40 |
+
return prometheus_app
|
.venv/lib/python3.11/site-packages/prometheus_client/context_managers.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from timeit import default_timer
|
| 2 |
+
from types import TracebackType
|
| 3 |
+
from typing import (
|
| 4 |
+
Any, Callable, Literal, Optional, Tuple, Type, TYPE_CHECKING, TypeVar,
|
| 5 |
+
Union,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
from .decorator import decorate
|
| 9 |
+
|
| 10 |
+
if TYPE_CHECKING:
|
| 11 |
+
from . import Counter
|
| 12 |
+
F = TypeVar("F", bound=Callable[..., Any])
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class ExceptionCounter:
|
| 16 |
+
def __init__(self, counter: "Counter", exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]]) -> None:
|
| 17 |
+
self._counter = counter
|
| 18 |
+
self._exception = exception
|
| 19 |
+
|
| 20 |
+
def __enter__(self) -> None:
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
def __exit__(self, typ: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> Literal[False]:
|
| 24 |
+
if isinstance(value, self._exception):
|
| 25 |
+
self._counter.inc()
|
| 26 |
+
return False
|
| 27 |
+
|
| 28 |
+
def __call__(self, f: "F") -> "F":
|
| 29 |
+
def wrapped(func, *args, **kwargs):
|
| 30 |
+
with self:
|
| 31 |
+
return func(*args, **kwargs)
|
| 32 |
+
|
| 33 |
+
return decorate(f, wrapped)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class InprogressTracker:
|
| 37 |
+
def __init__(self, gauge):
|
| 38 |
+
self._gauge = gauge
|
| 39 |
+
|
| 40 |
+
def __enter__(self):
|
| 41 |
+
self._gauge.inc()
|
| 42 |
+
|
| 43 |
+
def __exit__(self, typ, value, traceback):
|
| 44 |
+
self._gauge.dec()
|
| 45 |
+
|
| 46 |
+
def __call__(self, f: "F") -> "F":
|
| 47 |
+
def wrapped(func, *args, **kwargs):
|
| 48 |
+
with self:
|
| 49 |
+
return func(*args, **kwargs)
|
| 50 |
+
|
| 51 |
+
return decorate(f, wrapped)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class Timer:
|
| 55 |
+
def __init__(self, metric, callback_name):
|
| 56 |
+
self._metric = metric
|
| 57 |
+
self._callback_name = callback_name
|
| 58 |
+
|
| 59 |
+
def _new_timer(self):
|
| 60 |
+
return self.__class__(self._metric, self._callback_name)
|
| 61 |
+
|
| 62 |
+
def __enter__(self):
|
| 63 |
+
self._start = default_timer()
|
| 64 |
+
return self
|
| 65 |
+
|
| 66 |
+
def __exit__(self, typ, value, traceback):
|
| 67 |
+
# Time can go backwards.
|
| 68 |
+
duration = max(default_timer() - self._start, 0)
|
| 69 |
+
callback = getattr(self._metric, self._callback_name)
|
| 70 |
+
callback(duration)
|
| 71 |
+
|
| 72 |
+
def labels(self, *args, **kw):
|
| 73 |
+
self._metric = self._metric.labels(*args, **kw)
|
| 74 |
+
|
| 75 |
+
def __call__(self, f: "F") -> "F":
|
| 76 |
+
def wrapped(func, *args, **kwargs):
|
| 77 |
+
# Obtaining new instance of timer every time
|
| 78 |
+
# ensures thread safety and reentrancy.
|
| 79 |
+
with self._new_timer():
|
| 80 |
+
return func(*args, **kwargs)
|
| 81 |
+
|
| 82 |
+
return decorate(f, wrapped)
|
.venv/lib/python3.11/site-packages/prometheus_client/core.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .metrics import Counter, Enum, Gauge, Histogram, Info, Summary
|
| 2 |
+
from .metrics_core import (
|
| 3 |
+
CounterMetricFamily, GaugeHistogramMetricFamily, GaugeMetricFamily,
|
| 4 |
+
HistogramMetricFamily, InfoMetricFamily, Metric, StateSetMetricFamily,
|
| 5 |
+
SummaryMetricFamily, UnknownMetricFamily, UntypedMetricFamily,
|
| 6 |
+
)
|
| 7 |
+
from .registry import CollectorRegistry, REGISTRY
|
| 8 |
+
from .samples import Exemplar, Sample, Timestamp
|
| 9 |
+
|
| 10 |
+
__all__ = (
|
| 11 |
+
'CollectorRegistry',
|
| 12 |
+
'Counter',
|
| 13 |
+
'CounterMetricFamily',
|
| 14 |
+
'Enum',
|
| 15 |
+
'Exemplar',
|
| 16 |
+
'Gauge',
|
| 17 |
+
'GaugeHistogramMetricFamily',
|
| 18 |
+
'GaugeMetricFamily',
|
| 19 |
+
'Histogram',
|
| 20 |
+
'HistogramMetricFamily',
|
| 21 |
+
'Info',
|
| 22 |
+
'InfoMetricFamily',
|
| 23 |
+
'Metric',
|
| 24 |
+
'REGISTRY',
|
| 25 |
+
'Sample',
|
| 26 |
+
'StateSetM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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ######################### LICENSE ############################ #
|
| 2 |
+
|
| 3 |
+
# Copyright (c) 2005-2016, Michele Simionato
|
| 4 |
+
# All rights reserved.
|
| 5 |
+
|
| 6 |
+
# Redistribution and use in source and binary forms, with or without
|
| 7 |
+
# modification, are permitted provided that the following conditions are
|
| 8 |
+
# met:
|
| 9 |
+
|
| 10 |
+
# Redistributions of source code must retain the above copyright
|
| 11 |
+
# notice, this list of conditions and the following disclaimer.
|
| 12 |
+
# Redistributions in bytecode form must reproduce the above copyright
|
| 13 |
+
# notice, this list of conditions and the following disclaimer in
|
| 14 |
+
# the documentation and/or other materials provided with the
|
| 15 |
+
# distribution.
|
| 16 |
+
|
| 17 |
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 18 |
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 19 |
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 20 |
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 21 |
+
# HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 22 |
+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
| 23 |
+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
| 24 |
+
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
| 25 |
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
| 26 |
+
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
| 27 |
+
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
| 28 |
+
# DAMAGE.
|
| 29 |
+
|
| 30 |
+
"""
|
| 31 |
+
Decorator module, see http://pypi.python.org/pypi/decorator
|
| 32 |
+
for the documentation.
|
| 33 |
+
"""
|
| 34 |
+
from __future__ import print_function
|
| 35 |
+
|
| 36 |
+
import collections
|
| 37 |
+
import inspect
|
| 38 |
+
import itertools
|
| 39 |
+
import operator
|
| 40 |
+
import re
|
| 41 |
+
import sys
|
| 42 |
+
|
| 43 |
+
__version__ = '4.0.10'
|
| 44 |
+
|
| 45 |
+
if sys.version_info >= (3,):
|
| 46 |
+
from inspect import getfullargspec
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def get_init(cls):
|
| 50 |
+
return cls.__init__
|
| 51 |
+
else:
|
| 52 |
+
class getfullargspec(object):
|
| 53 |
+
"A quick and dirty replacement for getfullargspec for Python 2.X"
|
| 54 |
+
|
| 55 |
+
def __init__(self, f):
|
| 56 |
+
self.args, self.varargs, self.varkw, self.defaults = \
|
| 57 |
+
inspect.getargspec(f)
|
| 58 |
+
self.kwonlyargs = []
|
| 59 |
+
self.kwonlydefaults = None
|
| 60 |
+
|
| 61 |
+
def __iter__(self):
|
| 62 |
+
yield self.args
|
| 63 |
+
yield self.varargs
|
| 64 |
+
yield self.varkw
|
| 65 |
+
yield self.defaults
|
| 66 |
+
|
| 67 |
+
getargspec = inspect.getargspec
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def get_init(cls):
|
| 71 |
+
return cls.__init__.__func__
|
| 72 |
+
|
| 73 |
+
# getargspec has been deprecated in Python 3.5
|
| 74 |
+
ArgSpec = collections.namedtuple(
|
| 75 |
+
'ArgSpec', 'args varargs varkw defaults')
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def getargspec(f):
|
| 79 |
+
"""A replacement for inspect.getargspec"""
|
| 80 |
+
spec = getfullargspec(f)
|
| 81 |
+
return ArgSpec(spec.args, spec.varargs, spec.varkw, spec.defaults)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
DEF = re.compile(r'\s*def\s*([_\w][_\w\d]*)\s*\(')
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# basic functionality
|
| 88 |
+
class FunctionMaker(object):
|
| 89 |
+
"""
|
| 90 |
+
An object with the ability to create functions with a given signature.
|
| 91 |
+
It has attributes name, doc, module, signature, defaults, dict and
|
| 92 |
+
methods update and make.
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
# Atomic get-and-increment provided by the GIL
|
| 96 |
+
_compile_count = itertools.count()
|
| 97 |
+
|
| 98 |
+
def __init__(self, func=None, name=None, signature=None,
|
| 99 |
+
defaults=None, doc=None, module=None, funcdict=None):
|
| 100 |
+
self.shortsignature = signature
|
| 101 |
+
if func:
|
| 102 |
+
# func can be a class or a callable, but not an instance method
|
| 103 |
+
self.name = func.__name__
|
| 104 |
+
if self.name == '<lambda>': # small hack for lambda functions
|
| 105 |
+
self.name = '_lambda_'
|
| 106 |
+
self.doc = func.__doc__
|
| 107 |
+
self.module = func.__module__
|
| 108 |
+
if inspect.isfunction(func):
|
| 109 |
+
argspec = getfullargspec(func)
|
| 110 |
+
self.annotations = getattr(func, '__annotations__', {})
|
| 111 |
+
for a in ('args', 'varargs', 'varkw', 'defaults', 'kwonlyargs',
|
| 112 |
+
'kwonlydefaults'):
|
| 113 |
+
setattr(self, a, getattr(argspec, a))
|
| 114 |
+
for i, arg in enumerate(self.args):
|
| 115 |
+
setattr(self, 'arg%d' % i, arg)
|
| 116 |
+
if sys.version_info < (3,): # easy way
|
| 117 |
+
self.shortsignature = self.signature = (
|
| 118 |
+
inspect.formatargspec(
|
| 119 |
+
formatvalue=lambda val: "", *argspec)[1:-1])
|
| 120 |
+
else: # Python 3 way
|
| 121 |
+
allargs = list(self.args)
|
| 122 |
+
allshortargs = list(self.args)
|
| 123 |
+
if self.varargs:
|
| 124 |
+
allargs.append('*' + self.varargs)
|
| 125 |
+
allshortargs.append('*' + self.varargs)
|
| 126 |
+
elif self.kwonlyargs:
|
| 127 |
+
allargs.append('*') # single star syntax
|
| 128 |
+
for a in self.kwonlyargs:
|
| 129 |
+
allargs.append('%s=None' % a)
|
| 130 |
+
allshortargs.append('%s=%s' % (a, a))
|
| 131 |
+
if self.varkw:
|
| 132 |
+
allargs.append('**' + self.varkw)
|
| 133 |
+
allshortargs.append('**' + self.varkw)
|
| 134 |
+
self.signature = ', '.join(allargs)
|
| 135 |
+
self.shortsignature = ', '.join(allshortargs)
|
| 136 |
+
self.dict = func.__dict__.copy()
|
| 137 |
+
# func=None happens when decorating a caller
|
| 138 |
+
if name:
|
| 139 |
+
self.name = name
|
| 140 |
+
if signature is not None:
|
| 141 |
+
self.signature = signature
|
| 142 |
+
if defaults:
|
| 143 |
+
self.defaults = defaults
|
| 144 |
+
if doc:
|
| 145 |
+
self.doc = doc
|
| 146 |
+
if module:
|
| 147 |
+
self.module = module
|
| 148 |
+
if funcdict:
|
| 149 |
+
self.dict = funcdict
|
| 150 |
+
# check existence required attributes
|
| 151 |
+
assert hasattr(self, 'name')
|
| 152 |
+
if not hasattr(self, 'signature'):
|
| 153 |
+
raise TypeError('You are decorating a non function: %s' % func)
|
| 154 |
+
|
| 155 |
+
def update(self, func, **kw):
|
| 156 |
+
"Update the signature of func with the data in self"
|
| 157 |
+
func.__name__ = self.name
|
| 158 |
+
func.__doc__ = getattr(self, 'doc', None)
|
| 159 |
+
func.__dict__ = getattr(self, 'dict', {})
|
| 160 |
+
func.__defaults__ = getattr(self, 'defaults', ())
|
| 161 |
+
func.__kwdefaults__ = getattr(self, 'kwonlydefaults', None)
|
| 162 |
+
func.__annotations__ = getattr(self, 'annotations', None)
|
| 163 |
+
try:
|
| 164 |
+
frame = sys._getframe(3)
|
| 165 |
+
except AttributeError: # for IronPython and similar implementations
|
| 166 |
+
callermodule = '?'
|
| 167 |
+
else:
|
| 168 |
+
callermodule = frame.f_globals.get('__name__', '?')
|
| 169 |
+
func.__module__ = getattr(self, 'module', callermodule)
|
| 170 |
+
func.__dict__.update(kw)
|
| 171 |
+
|
| 172 |
+
def make(self, src_templ, evaldict=None, addsource=False, **attrs):
|
| 173 |
+
"Make a new function from a given template and update the signature"
|
| 174 |
+
src = src_templ % vars(self) # expand name and signature
|
| 175 |
+
evaldict = evaldict or {}
|
| 176 |
+
mo = DEF.match(src)
|
| 177 |
+
if mo is None:
|
| 178 |
+
raise SyntaxError('not a valid function template\n%s' % src)
|
| 179 |
+
name = mo.group(1) # extract the function name
|
| 180 |
+
names = set([name] + [arg.strip(' *') for arg in
|
| 181 |
+
self.shortsignature.split(',')])
|
| 182 |
+
for n in names:
|
| 183 |
+
if n in ('_func_', '_call_'):
|
| 184 |
+
raise NameError('%s is overridden in\n%s' % (n, src))
|
| 185 |
+
|
| 186 |
+
if not src.endswith('\n'): # add a newline for old Pythons
|
| 187 |
+
src += '\n'
|
| 188 |
+
|
| 189 |
+
# Ensure each generated function has a unique filename for profilers
|
| 190 |
+
# (such as cProfile) that depend on the tuple of (<filename>,
|
| 191 |
+
# <definition line>, <function name>) being unique.
|
| 192 |
+
filename = '<decorator-gen-%d>' % (next(self._compile_count),)
|
| 193 |
+
try:
|
| 194 |
+
code = compile(src, filename, 'single')
|
| 195 |
+
exec(code, evaldict)
|
| 196 |
+
except:
|
| 197 |
+
print('Error in generated code:', file=sys.stderr)
|
| 198 |
+
print(src, file=sys.stderr)
|
| 199 |
+
raise
|
| 200 |
+
func = evaldict[name]
|
| 201 |
+
if addsource:
|
| 202 |
+
attrs['__source__'] = src
|
| 203 |
+
self.update(func, **attrs)
|
| 204 |
+
return func
|
| 205 |
+
|
| 206 |
+
@classmethod
|
| 207 |
+
def create(cls, obj, body, evaldict, defaults=None,
|
| 208 |
+
doc=None, module=None, addsource=True, **attrs):
|
| 209 |
+
"""
|
| 210 |
+
Create a function from the strings name, signature and body.
|
| 211 |
+
evaldict is the evaluation dictionary. If addsource is true an
|
| 212 |
+
attribute __source__ is added to the result. The attributes attrs
|
| 213 |
+
are added, if any.
|
| 214 |
+
"""
|
| 215 |
+
if isinstance(obj, str): # "name(signature)"
|
| 216 |
+
name, rest = obj.strip().split('(', 1)
|
| 217 |
+
signature = rest[:-1] # strip a right parens
|
| 218 |
+
func = None
|
| 219 |
+
else: # a function
|
| 220 |
+
name = None
|
| 221 |
+
signature = None
|
| 222 |
+
func = obj
|
| 223 |
+
self = cls(func, name, signature, defaults, doc, module)
|
| 224 |
+
ibody = '\n'.join(' ' + line for line in body.splitlines())
|
| 225 |
+
return self.make('def %(name)s(%(signature)s):\n' + ibody,
|
| 226 |
+
evaldict, addsource, **attrs)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def decorate(func, caller):
|
| 230 |
+
"""
|
| 231 |
+
decorate(func, caller) decorates a function using a caller.
|
| 232 |
+
"""
|
| 233 |
+
evaldict = dict(_call_=caller, _func_=func)
|
| 234 |
+
fun = FunctionMaker.create(
|
| 235 |
+
func, "return _call_(_func_, %(shortsignature)s)",
|
| 236 |
+
evaldict, __wrapped__=func)
|
| 237 |
+
if hasattr(func, '__qualname__'):
|
| 238 |
+
fun.__qualname__ = func.__qualname__
|
| 239 |
+
return fun
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
def decorator(caller, _func=None):
|
| 243 |
+
"""decorator(caller) converts a caller function into a decorator"""
|
| 244 |
+
if _func is not None: # return a decorated function
|
| 245 |
+
# this is obsolete behavior; you should use decorate instead
|
| 246 |
+
return decorate(_func, caller)
|
| 247 |
+
# else return a decorator function
|
| 248 |
+
if inspect.isclass(caller):
|
| 249 |
+
name = caller.__name__.lower()
|
| 250 |
+
doc = 'decorator(%s) converts functions/generators into ' \
|
| 251 |
+
'factories of %s objects' % (caller.__name__, caller.__name__)
|
| 252 |
+
elif inspect.isfunction(caller):
|
| 253 |
+
if caller.__name__ == '<lambda>':
|
| 254 |
+
name = '_lambda_'
|
| 255 |
+
else:
|
| 256 |
+
name = caller.__name__
|
| 257 |
+
doc = caller.__doc__
|
| 258 |
+
else: # assume caller is an object with a __call__ method
|
| 259 |
+
name = caller.__class__.__name__.lower()
|
| 260 |
+
doc = caller.__call__.__doc__
|
| 261 |
+
evaldict = dict(_call_=caller, _decorate_=decorate)
|
| 262 |
+
return FunctionMaker.create(
|
| 263 |
+
'%s(func)' % name, 'return _decorate_(func, _call_)',
|
| 264 |
+
evaldict, doc=doc, module=caller.__module__,
|
| 265 |
+
__wrapped__=caller)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
# ####################### contextmanager ####################### #
|
| 269 |
+
|
| 270 |
+
try: # Python >= 3.2
|
| 271 |
+
from contextlib import _GeneratorContextManager
|
| 272 |
+
except ImportError: # Python >= 2.5
|
| 273 |
+
from contextlib import GeneratorContextManager as _GeneratorContextManager
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
class ContextManager(_GeneratorContextManager):
|
| 277 |
+
def __call__(self, func):
|
| 278 |
+
"""Context manager decorator"""
|
| 279 |
+
return FunctionMaker.create(
|
| 280 |
+
func, "with _self_: return _func_(%(shortsignature)s)",
|
| 281 |
+
dict(_self_=self, _func_=func), __wrapped__=func)
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
init = getfullargspec(_GeneratorContextManager.__init__)
|
| 285 |
+
n_args = len(init.args)
|
| 286 |
+
if n_args == 2 and not init.varargs: # (self, genobj) Python 2.7
|
| 287 |
+
def __init__(self, g, *a, **k):
|
| 288 |
+
return _GeneratorContextManager.__init__(self, g(*a, **k))
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
ContextManager.__init__ = __init__
|
| 292 |
+
elif n_args == 2 and init.varargs: # (self, gen, *a, **k) Python 3.4
|
| 293 |
+
pass
|
| 294 |
+
elif n_args == 4: # (self, gen, args, kwds) Python 3.5
|
| 295 |
+
def __init__(self, g, *a, **k):
|
| 296 |
+
return _GeneratorContextManager.__init__(self, g, a, k)
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
ContextManager.__init__ = __init__
|
| 300 |
+
|
| 301 |
+
contextmanager = decorator(ContextManager)
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
# ############################ dispatch_on ############################ #
|
| 305 |
+
|
| 306 |
+
def append(a, vancestors):
|
| 307 |
+
"""
|
| 308 |
+
Append ``a`` to the list of the virtual ancestors, unless it is already
|
| 309 |
+
included.
|
| 310 |
+
"""
|
| 311 |
+
add = True
|
| 312 |
+
for j, va in enumerate(vancestors):
|
| 313 |
+
if issubclass(va, a):
|
| 314 |
+
add = False
|
| 315 |
+
break
|
| 316 |
+
if issubclass(a, va):
|
| 317 |
+
vancestors[j] = a
|
| 318 |
+
add = False
|
| 319 |
+
if add:
|
| 320 |
+
vancestors.append(a)
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
# inspired from simplegeneric by P.J. Eby and functools.singledispatch
|
| 324 |
+
def dispatch_on(*dispatch_args):
|
| 325 |
+
"""
|
| 326 |
+
Factory of decorators turning a function into a generic function
|
| 327 |
+
dispatching on the given arguments.
|
| 328 |
+
"""
|
| 329 |
+
assert dispatch_args, 'No dispatch args passed'
|
| 330 |
+
dispatch_str = '(%s,)' % ', '.join(dispatch_args)
|
| 331 |
+
|
| 332 |
+
def check(arguments, wrong=operator.ne, msg=''):
|
| 333 |
+
"""Make sure one passes the expected number of arguments"""
|
| 334 |
+
if wrong(len(arguments), len(dispatch_args)):
|
| 335 |
+
raise TypeError('Expected %d arguments, got %d%s' %
|
| 336 |
+
(len(dispatch_args), len(arguments), msg))
|
| 337 |
+
|
| 338 |
+
def gen_func_dec(func):
|
| 339 |
+
"""Decorator turning a function into a generic function"""
|
| 340 |
+
|
| 341 |
+
# first check the dispatch arguments
|
| 342 |
+
argset = set(getfullargspec(func).args)
|
| 343 |
+
if not set(dispatch_args) <= argset:
|
| 344 |
+
raise NameError('Unknown dispatch arguments %s' % dispatch_str)
|
| 345 |
+
|
| 346 |
+
typemap = {}
|
| 347 |
+
|
| 348 |
+
def vancestors(*types):
|
| 349 |
+
"""
|
| 350 |
+
Get a list of sets of virtual ancestors for the given types
|
| 351 |
+
"""
|
| 352 |
+
check(types)
|
| 353 |
+
ras = [[] for _ in range(len(dispatch_args))]
|
| 354 |
+
for types_ in typemap:
|
| 355 |
+
for t, type_, ra in zip(types, types_, ras):
|
| 356 |
+
if issubclass(t, type_) and type_ not in t.__mro__:
|
| 357 |
+
append(type_, ra)
|
| 358 |
+
return [set(ra) for ra in ras]
|
| 359 |
+
|
| 360 |
+
def ancestors(*types):
|
| 361 |
+
"""
|
| 362 |
+
Get a list of virtual MROs, one for each type
|
| 363 |
+
"""
|
| 364 |
+
check(types)
|
| 365 |
+
lists = []
|
| 366 |
+
for t, vas in zip(types, vancestors(*types)):
|
| 367 |
+
n_vas = len(vas)
|
| 368 |
+
if n_vas > 1:
|
| 369 |
+
raise RuntimeError(
|
| 370 |
+
'Ambiguous dispatch for %s: %s' % (t, vas))
|
| 371 |
+
elif n_vas == 1:
|
| 372 |
+
va, = vas
|
| 373 |
+
mro = type('t', (t, va), {}).__mro__[1:]
|
| 374 |
+
else:
|
| 375 |
+
mro = t.__mro__
|
| 376 |
+
lists.append(mro[:-1]) # discard t and object
|
| 377 |
+
return lists
|
| 378 |
+
|
| 379 |
+
def register(*types):
|
| 380 |
+
"""
|
| 381 |
+
Decorator to register an implementation for the given types
|
| 382 |
+
"""
|
| 383 |
+
check(types)
|
| 384 |
+
|
| 385 |
+
def dec(f):
|
| 386 |
+
check(getfullargspec(f).args, operator.lt, ' in ' + f.__name__)
|
| 387 |
+
typemap[types] = f
|
| 388 |
+
return f
|
| 389 |
+
|
| 390 |
+
return dec
|
| 391 |
+
|
| 392 |
+
def dispatch_info(*types):
|
| 393 |
+
"""
|
| 394 |
+
An utility to introspect the dispatch algorithm
|
| 395 |
+
"""
|
| 396 |
+
check(types)
|
| 397 |
+
lst = []
|
| 398 |
+
for anc in itertools.product(*ancestors(*types)):
|
| 399 |
+
lst.append(tuple(a.__name__ for a in anc))
|
| 400 |
+
return lst
|
| 401 |
+
|
| 402 |
+
def _dispatch(dispatch_args, *args, **kw):
|
| 403 |
+
types = tuple(type(arg) for arg in dispatch_args)
|
| 404 |
+
try: # fast path
|
| 405 |
+
f = typemap[types]
|
| 406 |
+
except KeyError:
|
| 407 |
+
pass
|
| 408 |
+
else:
|
| 409 |
+
return f(*args, **kw)
|
| 410 |
+
combinations = itertools.product(*ancestors(*types))
|
| 411 |
+
next(combinations) # the first one has been already tried
|
| 412 |
+
for types_ in combinations:
|
| 413 |
+
f = typemap.get(types_)
|
| 414 |
+
if f is not None:
|
| 415 |
+
return f(*args, **kw)
|
| 416 |
+
|
| 417 |
+
# else call the default implementation
|
| 418 |
+
return func(*args, **kw)
|
| 419 |
+
|
| 420 |
+
return FunctionMaker.create(
|
| 421 |
+
func, 'return _f_(%s, %%(shortsignature)s)' % dispatch_str,
|
| 422 |
+
dict(_f_=_dispatch), register=register, default=func,
|
| 423 |
+
typemap=typemap, vancestors=vancestors, ancestors=ancestors,
|
| 424 |
+
dispatch_info=dispatch_info, __wrapped__=func)
|
| 425 |
+
|
| 426 |
+
gen_func_dec.__name__ = 'dispatch_on' + dispatch_str
|
| 427 |
+
return gen_func_dec
|
.venv/lib/python3.11/site-packages/prometheus_client/exposition.py
ADDED
|
@@ -0,0 +1,666 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
from contextlib import closing
|
| 3 |
+
import gzip
|
| 4 |
+
from http.server import BaseHTTPRequestHandler
|
| 5 |
+
import os
|
| 6 |
+
import socket
|
| 7 |
+
from socketserver import ThreadingMixIn
|
| 8 |
+
import ssl
|
| 9 |
+
import sys
|
| 10 |
+
import threading
|
| 11 |
+
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
|
| 12 |
+
from urllib.error import HTTPError
|
| 13 |
+
from urllib.parse import parse_qs, quote_plus, urlparse
|
| 14 |
+
from urllib.request import (
|
| 15 |
+
BaseHandler, build_opener, HTTPHandler, HTTPRedirectHandler, HTTPSHandler,
|
| 16 |
+
Request,
|
| 17 |
+
)
|
| 18 |
+
from wsgiref.simple_server import make_server, WSGIRequestHandler, WSGIServer
|
| 19 |
+
|
| 20 |
+
from .openmetrics import exposition as openmetrics
|
| 21 |
+
from .registry import CollectorRegistry, REGISTRY
|
| 22 |
+
from .utils import floatToGoString
|
| 23 |
+
|
| 24 |
+
__all__ = (
|
| 25 |
+
'CONTENT_TYPE_LATEST',
|
| 26 |
+
'delete_from_gateway',
|
| 27 |
+
'generate_latest',
|
| 28 |
+
'instance_ip_grouping_key',
|
| 29 |
+
'make_asgi_app',
|
| 30 |
+
'make_wsgi_app',
|
| 31 |
+
'MetricsHandler',
|
| 32 |
+
'push_to_gateway',
|
| 33 |
+
'pushadd_to_gateway',
|
| 34 |
+
'start_http_server',
|
| 35 |
+
'start_wsgi_server',
|
| 36 |
+
'write_to_textfile',
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
CONTENT_TYPE_LATEST = 'text/plain; version=0.0.4; charset=utf-8'
|
| 40 |
+
"""Content type of the latest text format"""
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class _PrometheusRedirectHandler(HTTPRedirectHandler):
|
| 44 |
+
"""
|
| 45 |
+
Allow additional methods (e.g. PUT) and data forwarding in redirects.
|
| 46 |
+
|
| 47 |
+
Use of this class constitute a user's explicit agreement to the
|
| 48 |
+
redirect responses the Prometheus client will receive when using it.
|
| 49 |
+
You should only use this class if you control or otherwise trust the
|
| 50 |
+
redirect behavior involved and are certain it is safe to full transfer
|
| 51 |
+
the original request (method and data) to the redirected URL. For
|
| 52 |
+
example, if you know there is a cosmetic URL redirect in front of a
|
| 53 |
+
local deployment of a Prometheus server, and all redirects are safe,
|
| 54 |
+
this is the class to use to handle redirects in that case.
|
| 55 |
+
|
| 56 |
+
The standard HTTPRedirectHandler does not forward request data nor
|
| 57 |
+
does it allow redirected PUT requests (which Prometheus uses for some
|
| 58 |
+
operations, for example `push_to_gateway`) because these cannot
|
| 59 |
+
generically guarantee no violations of HTTP RFC 2616 requirements for
|
| 60 |
+
the user to explicitly confirm redirects that could have unexpected
|
| 61 |
+
side effects (such as rendering a PUT request non-idempotent or
|
| 62 |
+
creating multiple resources not named in the original request).
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
| 66 |
+
"""
|
| 67 |
+
Apply redirect logic to a request.
|
| 68 |
+
|
| 69 |
+
See parent HTTPRedirectHandler.redirect_request for parameter info.
|
| 70 |
+
|
| 71 |
+
If the redirect is disallowed, this raises the corresponding HTTP error.
|
| 72 |
+
If the redirect can't be determined, return None to allow other handlers
|
| 73 |
+
to try. If the redirect is allowed, return the new request.
|
| 74 |
+
|
| 75 |
+
This method specialized for the case when (a) the user knows that the
|
| 76 |
+
redirect will not cause unacceptable side effects for any request method,
|
| 77 |
+
and (b) the user knows that any request data should be passed through to
|
| 78 |
+
the redirect. If either condition is not met, this should not be used.
|
| 79 |
+
"""
|
| 80 |
+
# note that requests being provided by a handler will use get_method to
|
| 81 |
+
# indicate the method, by monkeypatching this, instead of setting the
|
| 82 |
+
# Request object's method attribute.
|
| 83 |
+
m = getattr(req, "method", req.get_method())
|
| 84 |
+
if not (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
|
| 85 |
+
or code in (301, 302, 303) and m in ("POST", "PUT")):
|
| 86 |
+
raise HTTPError(req.full_url, code, msg, headers, fp)
|
| 87 |
+
new_request = Request(
|
| 88 |
+
newurl.replace(' ', '%20'), # space escaping in new url if needed.
|
| 89 |
+
headers=req.headers,
|
| 90 |
+
origin_req_host=req.origin_req_host,
|
| 91 |
+
unverifiable=True,
|
| 92 |
+
data=req.data,
|
| 93 |
+
)
|
| 94 |
+
new_request.method = m
|
| 95 |
+
return new_request
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def _bake_output(registry, accept_header, accept_encoding_header, params, disable_compression):
|
| 99 |
+
"""Bake output for metrics output."""
|
| 100 |
+
# Choose the correct plain text format of the output.
|
| 101 |
+
encoder, content_type = choose_encoder(accept_header)
|
| 102 |
+
if 'name[]' in params:
|
| 103 |
+
registry = registry.restricted_registry(params['name[]'])
|
| 104 |
+
output = encoder(registry)
|
| 105 |
+
headers = [('Content-Type', content_type)]
|
| 106 |
+
# If gzip encoding required, gzip the output.
|
| 107 |
+
if not disable_compression and gzip_accepted(accept_encoding_header):
|
| 108 |
+
output = gzip.compress(output)
|
| 109 |
+
headers.append(('Content-Encoding', 'gzip'))
|
| 110 |
+
return '200 OK', headers, output
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def make_wsgi_app(registry: CollectorRegistry = REGISTRY, disable_compression: bool = False) -> Callable:
|
| 114 |
+
"""Create a WSGI app which serves the metrics from a registry."""
|
| 115 |
+
|
| 116 |
+
def prometheus_app(environ, start_response):
|
| 117 |
+
# Prepare parameters
|
| 118 |
+
accept_header = environ.get('HTTP_ACCEPT')
|
| 119 |
+
accept_encoding_header = environ.get('HTTP_ACCEPT_ENCODING')
|
| 120 |
+
params = parse_qs(environ.get('QUERY_STRING', ''))
|
| 121 |
+
method = environ['REQUEST_METHOD']
|
| 122 |
+
|
| 123 |
+
if method == 'OPTIONS':
|
| 124 |
+
status = '200 OK'
|
| 125 |
+
headers = [('Allow', 'OPTIONS,GET')]
|
| 126 |
+
output = b''
|
| 127 |
+
elif method != 'GET':
|
| 128 |
+
status = '405 Method Not Allowed'
|
| 129 |
+
headers = [('Allow', 'OPTIONS,GET')]
|
| 130 |
+
output = '# HTTP {}: {}; use OPTIONS or GET\n'.format(status, method).encode()
|
| 131 |
+
elif environ['PATH_INFO'] == '/favicon.ico':
|
| 132 |
+
# Serve empty response for browsers
|
| 133 |
+
status = '200 OK'
|
| 134 |
+
headers = [('', '')]
|
| 135 |
+
output = b''
|
| 136 |
+
else:
|
| 137 |
+
# Note: For backwards compatibility, the URI path for GET is not
|
| 138 |
+
# constrained to the documented /metrics, but any path is allowed.
|
| 139 |
+
# Bake output
|
| 140 |
+
status, headers, output = _bake_output(registry, accept_header, accept_encoding_header, params, disable_compression)
|
| 141 |
+
# Return output
|
| 142 |
+
start_response(status, headers)
|
| 143 |
+
return [output]
|
| 144 |
+
|
| 145 |
+
return prometheus_app
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
class _SilentHandler(WSGIRequestHandler):
|
| 149 |
+
"""WSGI handler that does not log requests."""
|
| 150 |
+
|
| 151 |
+
def log_message(self, format, *args):
|
| 152 |
+
"""Log nothing."""
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
class ThreadingWSGIServer(ThreadingMixIn, WSGIServer):
|
| 156 |
+
"""Thread per request HTTP server."""
|
| 157 |
+
# Make worker threads "fire and forget". Beginning with Python 3.7 this
|
| 158 |
+
# prevents a memory leak because ``ThreadingMixIn`` starts to gather all
|
| 159 |
+
# non-daemon threads in a list in order to join on them at server close.
|
| 160 |
+
daemon_threads = True
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def _get_best_family(address, port):
|
| 164 |
+
"""Automatically select address family depending on address"""
|
| 165 |
+
# HTTPServer defaults to AF_INET, which will not start properly if
|
| 166 |
+
# binding an ipv6 address is requested.
|
| 167 |
+
# This function is based on what upstream python did for http.server
|
| 168 |
+
# in https://github.com/python/cpython/pull/11767
|
| 169 |
+
infos = socket.getaddrinfo(address, port, type=socket.SOCK_STREAM, flags=socket.AI_PASSIVE)
|
| 170 |
+
family, _, _, _, sockaddr = next(iter(infos))
|
| 171 |
+
return family, sockaddr[0]
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def _get_ssl_ctx(
|
| 175 |
+
certfile: str,
|
| 176 |
+
keyfile: str,
|
| 177 |
+
protocol: int,
|
| 178 |
+
cafile: Optional[str] = None,
|
| 179 |
+
capath: Optional[str] = None,
|
| 180 |
+
client_auth_required: bool = False,
|
| 181 |
+
) -> ssl.SSLContext:
|
| 182 |
+
"""Load context supports SSL."""
|
| 183 |
+
ssl_cxt = ssl.SSLContext(protocol=protocol)
|
| 184 |
+
|
| 185 |
+
if cafile is not None or capath is not None:
|
| 186 |
+
try:
|
| 187 |
+
ssl_cxt.load_verify_locations(cafile, capath)
|
| 188 |
+
except IOError as exc:
|
| 189 |
+
exc_type = type(exc)
|
| 190 |
+
msg = str(exc)
|
| 191 |
+
raise exc_type(f"Cannot load CA certificate chain from file "
|
| 192 |
+
f"{cafile!r} or directory {capath!r}: {msg}")
|
| 193 |
+
else:
|
| 194 |
+
try:
|
| 195 |
+
ssl_cxt.load_default_certs(purpose=ssl.Purpose.CLIENT_AUTH)
|
| 196 |
+
except IOError as exc:
|
| 197 |
+
exc_type = type(exc)
|
| 198 |
+
msg = str(exc)
|
| 199 |
+
raise exc_type(f"Cannot load default CA certificate chain: {msg}")
|
| 200 |
+
|
| 201 |
+
if client_auth_required:
|
| 202 |
+
ssl_cxt.verify_mode = ssl.CERT_REQUIRED
|
| 203 |
+
|
| 204 |
+
try:
|
| 205 |
+
ssl_cxt.load_cert_chain(certfile=certfile, keyfile=keyfile)
|
| 206 |
+
except IOError as exc:
|
| 207 |
+
exc_type = type(exc)
|
| 208 |
+
msg = str(exc)
|
| 209 |
+
raise exc_type(f"Cannot load server certificate file {certfile!r} or "
|
| 210 |
+
f"its private key file {keyfile!r}: {msg}")
|
| 211 |
+
|
| 212 |
+
return ssl_cxt
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def start_wsgi_server(
|
| 216 |
+
port: int,
|
| 217 |
+
addr: str = '0.0.0.0',
|
| 218 |
+
registry: CollectorRegistry = REGISTRY,
|
| 219 |
+
certfile: Optional[str] = None,
|
| 220 |
+
keyfile: Optional[str] = None,
|
| 221 |
+
client_cafile: Optional[str] = None,
|
| 222 |
+
client_capath: Optional[str] = None,
|
| 223 |
+
protocol: int = ssl.PROTOCOL_TLS_SERVER,
|
| 224 |
+
client_auth_required: bool = False,
|
| 225 |
+
) -> Tuple[WSGIServer, threading.Thread]:
|
| 226 |
+
"""Starts a WSGI server for prometheus metrics as a daemon thread."""
|
| 227 |
+
|
| 228 |
+
class TmpServer(ThreadingWSGIServer):
|
| 229 |
+
"""Copy of ThreadingWSGIServer to update address_family locally"""
|
| 230 |
+
|
| 231 |
+
TmpServer.address_family, addr = _get_best_family(addr, port)
|
| 232 |
+
app = make_wsgi_app(registry)
|
| 233 |
+
httpd = make_server(addr, port, app, TmpServer, handler_class=_SilentHandler)
|
| 234 |
+
if certfile and keyfile:
|
| 235 |
+
context = _get_ssl_ctx(certfile, keyfile, protocol, client_cafile, client_capath, client_auth_required)
|
| 236 |
+
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
|
| 237 |
+
t = threading.Thread(target=httpd.serve_forever)
|
| 238 |
+
t.daemon = True
|
| 239 |
+
t.start()
|
| 240 |
+
|
| 241 |
+
return httpd, t
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
start_http_server = start_wsgi_server
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def generate_latest(registry: CollectorRegistry = REGISTRY) -> bytes:
|
| 248 |
+
"""Returns the metrics from the registry in latest text format as a string."""
|
| 249 |
+
|
| 250 |
+
def sample_line(line):
|
| 251 |
+
if line.labels:
|
| 252 |
+
labelstr = '{{{0}}}'.format(','.join(
|
| 253 |
+
['{}="{}"'.format(
|
| 254 |
+
k, v.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"'))
|
| 255 |
+
for k, v in sorted(line.labels.items())]))
|
| 256 |
+
else:
|
| 257 |
+
labelstr = ''
|
| 258 |
+
timestamp = ''
|
| 259 |
+
if line.timestamp is not None:
|
| 260 |
+
# Convert to milliseconds.
|
| 261 |
+
timestamp = f' {int(float(line.timestamp) * 1000):d}'
|
| 262 |
+
return f'{line.name}{labelstr} {floatToGoString(line.value)}{timestamp}\n'
|
| 263 |
+
|
| 264 |
+
output = []
|
| 265 |
+
for metric in registry.collect():
|
| 266 |
+
try:
|
| 267 |
+
mname = metric.name
|
| 268 |
+
mtype = metric.type
|
| 269 |
+
# Munging from OpenMetrics into Prometheus format.
|
| 270 |
+
if mtype == 'counter':
|
| 271 |
+
mname = mname + '_total'
|
| 272 |
+
elif mtype == 'info':
|
| 273 |
+
mname = mname + '_info'
|
| 274 |
+
mtype = 'gauge'
|
| 275 |
+
elif mtype == 'stateset':
|
| 276 |
+
mtype = 'gauge'
|
| 277 |
+
elif mtype == 'gaugehistogram':
|
| 278 |
+
# A gauge histogram is really a gauge,
|
| 279 |
+
# but this captures the structure better.
|
| 280 |
+
mtype = 'histogram'
|
| 281 |
+
elif mtype == 'unknown':
|
| 282 |
+
mtype = 'untyped'
|
| 283 |
+
|
| 284 |
+
output.append('# HELP {} {}\n'.format(
|
| 285 |
+
mname, metric.documentation.replace('\\', r'\\').replace('\n', r'\n')))
|
| 286 |
+
output.append(f'# TYPE {mname} {mtype}\n')
|
| 287 |
+
|
| 288 |
+
om_samples: Dict[str, List[str]] = {}
|
| 289 |
+
for s in metric.samples:
|
| 290 |
+
for suffix in ['_created', '_gsum', '_gcount']:
|
| 291 |
+
if s.name == metric.name + suffix:
|
| 292 |
+
# OpenMetrics specific sample, put in a gauge at the end.
|
| 293 |
+
om_samples.setdefault(suffix, []).append(sample_line(s))
|
| 294 |
+
break
|
| 295 |
+
else:
|
| 296 |
+
output.append(sample_line(s))
|
| 297 |
+
except Exception as exception:
|
| 298 |
+
exception.args = (exception.args or ('',)) + (metric,)
|
| 299 |
+
raise
|
| 300 |
+
|
| 301 |
+
for suffix, lines in sorted(om_samples.items()):
|
| 302 |
+
output.append('# HELP {}{} {}\n'.format(metric.name, suffix,
|
| 303 |
+
metric.documentation.replace('\\', r'\\').replace('\n', r'\n')))
|
| 304 |
+
output.append(f'# TYPE {metric.name}{suffix} gauge\n')
|
| 305 |
+
output.extend(lines)
|
| 306 |
+
return ''.join(output).encode('utf-8')
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
def choose_encoder(accept_header: str) -> Tuple[Callable[[CollectorRegistry], bytes], str]:
|
| 310 |
+
accept_header = accept_header or ''
|
| 311 |
+
for accepted in accept_header.split(','):
|
| 312 |
+
if accepted.split(';')[0].strip() == 'application/openmetrics-text':
|
| 313 |
+
return (openmetrics.generate_latest,
|
| 314 |
+
openmetrics.CONTENT_TYPE_LATEST)
|
| 315 |
+
return generate_latest, CONTENT_TYPE_LATEST
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
def gzip_accepted(accept_encoding_header: str) -> bool:
|
| 319 |
+
accept_encoding_header = accept_encoding_header or ''
|
| 320 |
+
for accepted in accept_encoding_header.split(','):
|
| 321 |
+
if accepted.split(';')[0].strip().lower() == 'gzip':
|
| 322 |
+
return True
|
| 323 |
+
return False
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
class MetricsHandler(BaseHTTPRequestHandler):
|
| 327 |
+
"""HTTP handler that gives metrics from ``REGISTRY``."""
|
| 328 |
+
registry: CollectorRegistry = REGISTRY
|
| 329 |
+
|
| 330 |
+
def do_GET(self) -> None:
|
| 331 |
+
# Prepare parameters
|
| 332 |
+
registry = self.registry
|
| 333 |
+
accept_header = self.headers.get('Accept')
|
| 334 |
+
accept_encoding_header = self.headers.get('Accept-Encoding')
|
| 335 |
+
params = parse_qs(urlparse(self.path).query)
|
| 336 |
+
# Bake output
|
| 337 |
+
status, headers, output = _bake_output(registry, accept_header, accept_encoding_header, params, False)
|
| 338 |
+
# Return output
|
| 339 |
+
self.send_response(int(status.split(' ')[0]))
|
| 340 |
+
for header in headers:
|
| 341 |
+
self.send_header(*header)
|
| 342 |
+
self.end_headers()
|
| 343 |
+
self.wfile.write(output)
|
| 344 |
+
|
| 345 |
+
def log_message(self, format: str, *args: Any) -> None:
|
| 346 |
+
"""Log nothing."""
|
| 347 |
+
|
| 348 |
+
@classmethod
|
| 349 |
+
def factory(cls, registry: CollectorRegistry) -> type:
|
| 350 |
+
"""Returns a dynamic MetricsHandler class tied
|
| 351 |
+
to the passed registry.
|
| 352 |
+
"""
|
| 353 |
+
# This implementation relies on MetricsHandler.registry
|
| 354 |
+
# (defined above and defaulted to REGISTRY).
|
| 355 |
+
|
| 356 |
+
# As we have unicode_literals, we need to create a str()
|
| 357 |
+
# object for type().
|
| 358 |
+
cls_name = str(cls.__name__)
|
| 359 |
+
MyMetricsHandler = type(cls_name, (cls, object),
|
| 360 |
+
{"registry": registry})
|
| 361 |
+
return MyMetricsHandler
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def write_to_textfile(path: str, registry: CollectorRegistry) -> None:
|
| 365 |
+
"""Write metrics to the given path.
|
| 366 |
+
|
| 367 |
+
This is intended for use with the Node exporter textfile collector.
|
| 368 |
+
The path must end in .prom for the textfile collector to process it."""
|
| 369 |
+
tmppath = f'{path}.{os.getpid()}.{threading.current_thread().ident}'
|
| 370 |
+
with open(tmppath, 'wb') as f:
|
| 371 |
+
f.write(generate_latest(registry))
|
| 372 |
+
|
| 373 |
+
# rename(2) is atomic but fails on Windows if the destination file exists
|
| 374 |
+
if os.name == 'nt':
|
| 375 |
+
os.replace(tmppath, path)
|
| 376 |
+
else:
|
| 377 |
+
os.rename(tmppath, path)
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
def _make_handler(
|
| 381 |
+
url: str,
|
| 382 |
+
method: str,
|
| 383 |
+
timeout: Optional[float],
|
| 384 |
+
headers: Sequence[Tuple[str, str]],
|
| 385 |
+
data: bytes,
|
| 386 |
+
base_handler: Union[BaseHandler, type],
|
| 387 |
+
) -> Callable[[], None]:
|
| 388 |
+
def handle() -> None:
|
| 389 |
+
request = Request(url, data=data)
|
| 390 |
+
request.get_method = lambda: method # type: ignore
|
| 391 |
+
for k, v in headers:
|
| 392 |
+
request.add_header(k, v)
|
| 393 |
+
resp = build_opener(base_handler).open(request, timeout=timeout)
|
| 394 |
+
if resp.code >= 400:
|
| 395 |
+
raise OSError(f"error talking to pushgateway: {resp.code} {resp.msg}")
|
| 396 |
+
|
| 397 |
+
return handle
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
def default_handler(
|
| 401 |
+
url: str,
|
| 402 |
+
method: str,
|
| 403 |
+
timeout: Optional[float],
|
| 404 |
+
headers: List[Tuple[str, str]],
|
| 405 |
+
data: bytes,
|
| 406 |
+
) -> Callable[[], None]:
|
| 407 |
+
"""Default handler that implements HTTP/HTTPS connections.
|
| 408 |
+
|
| 409 |
+
Used by the push_to_gateway functions. Can be re-used by other handlers."""
|
| 410 |
+
|
| 411 |
+
return _make_handler(url, method, timeout, headers, data, HTTPHandler)
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
def passthrough_redirect_handler(
|
| 415 |
+
url: str,
|
| 416 |
+
method: str,
|
| 417 |
+
timeout: Optional[float],
|
| 418 |
+
headers: List[Tuple[str, str]],
|
| 419 |
+
data: bytes,
|
| 420 |
+
) -> Callable[[], None]:
|
| 421 |
+
"""
|
| 422 |
+
Handler that automatically trusts redirect responses for all HTTP methods.
|
| 423 |
+
|
| 424 |
+
Augments standard HTTPRedirectHandler capability by permitting PUT requests,
|
| 425 |
+
preserving the method upon redirect, and passing through all headers and
|
| 426 |
+
data from the original request. Only use this handler if you control or
|
| 427 |
+
trust the source of redirect responses you encounter when making requests
|
| 428 |
+
via the Prometheus client. This handler will simply repeat the identical
|
| 429 |
+
request, including same method and data, to the new redirect URL."""
|
| 430 |
+
|
| 431 |
+
return _make_handler(url, method, timeout, headers, data, _PrometheusRedirectHandler)
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
def basic_auth_handler(
|
| 435 |
+
url: str,
|
| 436 |
+
method: str,
|
| 437 |
+
timeout: Optional[float],
|
| 438 |
+
headers: List[Tuple[str, str]],
|
| 439 |
+
data: bytes,
|
| 440 |
+
username: Optional[str] = None,
|
| 441 |
+
password: Optional[str] = None,
|
| 442 |
+
) -> Callable[[], None]:
|
| 443 |
+
"""Handler that implements HTTP/HTTPS connections with Basic Auth.
|
| 444 |
+
|
| 445 |
+
Sets auth headers using supplied 'username' and 'password', if set.
|
| 446 |
+
Used by the push_to_gateway functions. Can be re-used by other handlers."""
|
| 447 |
+
|
| 448 |
+
def handle():
|
| 449 |
+
"""Handler that implements HTTP Basic Auth.
|
| 450 |
+
"""
|
| 451 |
+
if username is not None and password is not None:
|
| 452 |
+
auth_value = f'{username}:{password}'.encode()
|
| 453 |
+
auth_token = base64.b64encode(auth_value)
|
| 454 |
+
auth_header = b'Basic ' + auth_token
|
| 455 |
+
headers.append(('Authorization', auth_header))
|
| 456 |
+
default_handler(url, method, timeout, headers, data)()
|
| 457 |
+
|
| 458 |
+
return handle
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
def tls_auth_handler(
|
| 462 |
+
url: str,
|
| 463 |
+
method: str,
|
| 464 |
+
timeout: Optional[float],
|
| 465 |
+
headers: List[Tuple[str, str]],
|
| 466 |
+
data: bytes,
|
| 467 |
+
certfile: str,
|
| 468 |
+
keyfile: str,
|
| 469 |
+
cafile: Optional[str] = None,
|
| 470 |
+
protocol: int = ssl.PROTOCOL_TLS_CLIENT,
|
| 471 |
+
insecure_skip_verify: bool = False,
|
| 472 |
+
) -> Callable[[], None]:
|
| 473 |
+
"""Handler that implements an HTTPS connection with TLS Auth.
|
| 474 |
+
|
| 475 |
+
The default protocol (ssl.PROTOCOL_TLS_CLIENT) will also enable
|
| 476 |
+
ssl.CERT_REQUIRED and SSLContext.check_hostname by default. This can be
|
| 477 |
+
disabled by setting insecure_skip_verify to True.
|
| 478 |
+
|
| 479 |
+
Both this handler and the TLS feature on pushgateay are experimental."""
|
| 480 |
+
context = ssl.SSLContext(protocol=protocol)
|
| 481 |
+
if cafile is not None:
|
| 482 |
+
context.load_verify_locations(cafile)
|
| 483 |
+
else:
|
| 484 |
+
context.load_default_certs()
|
| 485 |
+
|
| 486 |
+
if insecure_skip_verify:
|
| 487 |
+
context.check_hostname = False
|
| 488 |
+
context.verify_mode = ssl.CERT_NONE
|
| 489 |
+
|
| 490 |
+
context.load_cert_chain(certfile=certfile, keyfile=keyfile)
|
| 491 |
+
handler = HTTPSHandler(context=context)
|
| 492 |
+
return _make_handler(url, method, timeout, headers, data, handler)
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
def push_to_gateway(
|
| 496 |
+
gateway: str,
|
| 497 |
+
job: str,
|
| 498 |
+
registry: CollectorRegistry,
|
| 499 |
+
grouping_key: Optional[Dict[str, Any]] = None,
|
| 500 |
+
timeout: Optional[float] = 30,
|
| 501 |
+
handler: Callable = default_handler,
|
| 502 |
+
) -> None:
|
| 503 |
+
"""Push metrics to the given pushgateway.
|
| 504 |
+
|
| 505 |
+
`gateway` the url for your push gateway. Either of the form
|
| 506 |
+
'http://pushgateway.local', or 'pushgateway.local'.
|
| 507 |
+
Scheme defaults to 'http' if none is provided
|
| 508 |
+
`job` is the job label to be attached to all pushed metrics
|
| 509 |
+
`registry` is an instance of CollectorRegistry
|
| 510 |
+
`grouping_key` please see the pushgateway documentation for details.
|
| 511 |
+
Defaults to None
|
| 512 |
+
`timeout` is how long push will attempt to connect before giving up.
|
| 513 |
+
Defaults to 30s, can be set to None for no timeout.
|
| 514 |
+
`handler` is an optional function which can be provided to perform
|
| 515 |
+
requests to the 'gateway'.
|
| 516 |
+
Defaults to None, in which case an http or https request
|
| 517 |
+
will be carried out by a default handler.
|
| 518 |
+
If not None, the argument must be a function which accepts
|
| 519 |
+
the following arguments:
|
| 520 |
+
url, method, timeout, headers, and content
|
| 521 |
+
May be used to implement additional functionality not
|
| 522 |
+
supported by the built-in default handler (such as SSL
|
| 523 |
+
client certicates, and HTTP authentication mechanisms).
|
| 524 |
+
'url' is the URL for the request, the 'gateway' argument
|
| 525 |
+
described earlier will form the basis of this URL.
|
| 526 |
+
'method' is the HTTP method which should be used when
|
| 527 |
+
carrying out the request.
|
| 528 |
+
'timeout' requests not successfully completed after this
|
| 529 |
+
many seconds should be aborted. If timeout is None, then
|
| 530 |
+
the handler should not set a timeout.
|
| 531 |
+
'headers' is a list of ("header-name","header-value") tuples
|
| 532 |
+
which must be passed to the pushgateway in the form of HTTP
|
| 533 |
+
request headers.
|
| 534 |
+
The function should raise an exception (e.g. IOError) on
|
| 535 |
+
failure.
|
| 536 |
+
'content' is the data which should be used to form the HTTP
|
| 537 |
+
Message Body.
|
| 538 |
+
|
| 539 |
+
This overwrites all metrics with the same job and grouping_key.
|
| 540 |
+
This uses the PUT HTTP method."""
|
| 541 |
+
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler)
|
| 542 |
+
|
| 543 |
+
|
| 544 |
+
def pushadd_to_gateway(
|
| 545 |
+
gateway: str,
|
| 546 |
+
job: str,
|
| 547 |
+
registry: Optional[CollectorRegistry],
|
| 548 |
+
grouping_key: Optional[Dict[str, Any]] = None,
|
| 549 |
+
timeout: Optional[float] = 30,
|
| 550 |
+
handler: Callable = default_handler,
|
| 551 |
+
) -> None:
|
| 552 |
+
"""PushAdd metrics to the given pushgateway.
|
| 553 |
+
|
| 554 |
+
`gateway` the url for your push gateway. Either of the form
|
| 555 |
+
'http://pushgateway.local', or 'pushgateway.local'.
|
| 556 |
+
Scheme defaults to 'http' if none is provided
|
| 557 |
+
`job` is the job label to be attached to all pushed metrics
|
| 558 |
+
`registry` is an instance of CollectorRegistry
|
| 559 |
+
`grouping_key` please see the pushgateway documentation for details.
|
| 560 |
+
Defaults to None
|
| 561 |
+
`timeout` is how long push will attempt to connect before giving up.
|
| 562 |
+
Defaults to 30s, can be set to None for no timeout.
|
| 563 |
+
`handler` is an optional function which can be provided to perform
|
| 564 |
+
requests to the 'gateway'.
|
| 565 |
+
Defaults to None, in which case an http or https request
|
| 566 |
+
will be carried out by a default handler.
|
| 567 |
+
See the 'prometheus_client.push_to_gateway' documentation
|
| 568 |
+
for implementation requirements.
|
| 569 |
+
|
| 570 |
+
This replaces metrics with the same name, job and grouping_key.
|
| 571 |
+
This uses the POST HTTP method."""
|
| 572 |
+
_use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler)
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
def delete_from_gateway(
|
| 576 |
+
gateway: str,
|
| 577 |
+
job: str,
|
| 578 |
+
grouping_key: Optional[Dict[str, Any]] = None,
|
| 579 |
+
timeout: Optional[float] = 30,
|
| 580 |
+
handler: Callable = default_handler,
|
| 581 |
+
) -> None:
|
| 582 |
+
"""Delete metrics from the given pushgateway.
|
| 583 |
+
|
| 584 |
+
`gateway` the url for your push gateway. Either of the form
|
| 585 |
+
'http://pushgateway.local', or 'pushgateway.local'.
|
| 586 |
+
Scheme defaults to 'http' if none is provided
|
| 587 |
+
`job` is the job label to be attached to all pushed metrics
|
| 588 |
+
`grouping_key` please see the pushgateway documentation for details.
|
| 589 |
+
Defaults to None
|
| 590 |
+
`timeout` is how long delete will attempt to connect before giving up.
|
| 591 |
+
Defaults to 30s, can be set to None for no timeout.
|
| 592 |
+
`handler` is an optional function which can be provided to perform
|
| 593 |
+
requests to the 'gateway'.
|
| 594 |
+
Defaults to None, in which case an http or https request
|
| 595 |
+
will be carried out by a default handler.
|
| 596 |
+
See the 'prometheus_client.push_to_gateway' documentation
|
| 597 |
+
for implementation requirements.
|
| 598 |
+
|
| 599 |
+
This deletes metrics with the given job and grouping_key.
|
| 600 |
+
This uses the DELETE HTTP method."""
|
| 601 |
+
_use_gateway('DELETE', gateway, job, None, grouping_key, timeout, handler)
|
| 602 |
+
|
| 603 |
+
|
| 604 |
+
def _use_gateway(
|
| 605 |
+
method: str,
|
| 606 |
+
gateway: str,
|
| 607 |
+
job: str,
|
| 608 |
+
registry: Optional[CollectorRegistry],
|
| 609 |
+
grouping_key: Optional[Dict[str, Any]],
|
| 610 |
+
timeout: Optional[float],
|
| 611 |
+
handler: Callable,
|
| 612 |
+
) -> None:
|
| 613 |
+
gateway_url = urlparse(gateway)
|
| 614 |
+
# See https://bugs.python.org/issue27657 for details on urlparse in py>=3.7.6.
|
| 615 |
+
if not gateway_url.scheme or gateway_url.scheme not in ['http', 'https']:
|
| 616 |
+
gateway = f'http://{gateway}'
|
| 617 |
+
|
| 618 |
+
gateway = gateway.rstrip('/')
|
| 619 |
+
url = '{}/metrics/{}/{}'.format(gateway, *_escape_grouping_key("job", job))
|
| 620 |
+
|
| 621 |
+
data = b''
|
| 622 |
+
if method != 'DELETE':
|
| 623 |
+
if registry is None:
|
| 624 |
+
registry = REGISTRY
|
| 625 |
+
data = generate_latest(registry)
|
| 626 |
+
|
| 627 |
+
if grouping_key is None:
|
| 628 |
+
grouping_key = {}
|
| 629 |
+
url += ''.join(
|
| 630 |
+
'/{}/{}'.format(*_escape_grouping_key(str(k), str(v)))
|
| 631 |
+
for k, v in sorted(grouping_key.items()))
|
| 632 |
+
|
| 633 |
+
handler(
|
| 634 |
+
url=url, method=method, timeout=timeout,
|
| 635 |
+
headers=[('Content-Type', CONTENT_TYPE_LATEST)], data=data,
|
| 636 |
+
)()
|
| 637 |
+
|
| 638 |
+
|
| 639 |
+
def _escape_grouping_key(k, v):
|
| 640 |
+
if v == "":
|
| 641 |
+
# Per https://github.com/prometheus/pushgateway/pull/346.
|
| 642 |
+
return k + "@base64", "="
|
| 643 |
+
elif '/' in v:
|
| 644 |
+
# Added in Pushgateway 0.9.0.
|
| 645 |
+
return k + "@base64", base64.urlsafe_b64encode(v.encode("utf-8")).decode("utf-8")
|
| 646 |
+
else:
|
| 647 |
+
return k, quote_plus(v)
|
| 648 |
+
|
| 649 |
+
|
| 650 |
+
def instance_ip_grouping_key() -> Dict[str, Any]:
|
| 651 |
+
"""Grouping key with instance set to the IP Address of this host."""
|
| 652 |
+
with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as s:
|
| 653 |
+
if sys.platform == 'darwin':
|
| 654 |
+
# This check is done this way only on MacOS devices
|
| 655 |
+
# it is done this way because the localhost method does
|
| 656 |
+
# not work.
|
| 657 |
+
# This method was adapted from this StackOverflow answer:
|
| 658 |
+
# https://stackoverflow.com/a/28950776
|
| 659 |
+
s.connect(('10.255.255.255', 1))
|
| 660 |
+
else:
|
| 661 |
+
s.connect(('localhost', 0))
|
| 662 |
+
|
| 663 |
+
return {'instance': s.getsockname()[0]}
|
| 664 |
+
|
| 665 |
+
|
| 666 |
+
from .asgi import make_asgi_app # noqa
|
.venv/lib/python3.11/site-packages/prometheus_client/gc_collector.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gc
|
| 2 |
+
import platform
|
| 3 |
+
from typing import Iterable
|
| 4 |
+
|
| 5 |
+
from .metrics_core import CounterMetricFamily, Metric
|
| 6 |
+
from .registry import Collector, CollectorRegistry, REGISTRY
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class GCCollector(Collector):
|
| 10 |
+
"""Collector for Garbage collection statistics."""
|
| 11 |
+
|
| 12 |
+
def __init__(self, registry: CollectorRegistry = REGISTRY):
|
| 13 |
+
if not hasattr(gc, 'get_stats') or platform.python_implementation() != 'CPython':
|
| 14 |
+
return
|
| 15 |
+
registry.register(self)
|
| 16 |
+
|
| 17 |
+
def collect(self) -> Iterable[Metric]:
|
| 18 |
+
collected = CounterMetricFamily(
|
| 19 |
+
'python_gc_objects_collected',
|
| 20 |
+
'Objects collected during gc',
|
| 21 |
+
labels=['generation'],
|
| 22 |
+
)
|
| 23 |
+
uncollectable = CounterMetricFamily(
|
| 24 |
+
'python_gc_objects_uncollectable',
|
| 25 |
+
'Uncollectable objects found during GC',
|
| 26 |
+
labels=['generation'],
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
collections = CounterMetricFamily(
|
| 30 |
+
'python_gc_collections',
|
| 31 |
+
'Number of times this generation was collected',
|
| 32 |
+
labels=['generation'],
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
for gen, stat in enumerate(gc.get_stats()):
|
| 36 |
+
generation = str(gen)
|
| 37 |
+
collected.add_metric([generation], value=stat['collected'])
|
| 38 |
+
uncollectable.add_metric([generation], value=stat['uncollectable'])
|
| 39 |
+
collections.add_metric([generation], value=stat['collections'])
|
| 40 |
+
|
| 41 |
+
return [collected, uncollectable, collections]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
GC_COLLECTOR = GCCollector()
|
| 45 |
+
"""Default GCCollector in default Registry REGISTRY."""
|
.venv/lib/python3.11/site-packages/prometheus_client/metrics.py
ADDED
|
@@ -0,0 +1,776 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from threading import Lock
|
| 3 |
+
import time
|
| 4 |
+
import types
|
| 5 |
+
from typing import (
|
| 6 |
+
Any, Callable, Dict, Iterable, List, Literal, Optional, Sequence, Tuple,
|
| 7 |
+
Type, TypeVar, Union,
|
| 8 |
+
)
|
| 9 |
+
import warnings
|
| 10 |
+
|
| 11 |
+
from . import values # retain this import style for testability
|
| 12 |
+
from .context_managers import ExceptionCounter, InprogressTracker, Timer
|
| 13 |
+
from .metrics_core import (
|
| 14 |
+
Metric, METRIC_LABEL_NAME_RE, METRIC_NAME_RE,
|
| 15 |
+
RESERVED_METRIC_LABEL_NAME_RE,
|
| 16 |
+
)
|
| 17 |
+
from .registry import Collector, CollectorRegistry, REGISTRY
|
| 18 |
+
from .samples import Exemplar, Sample
|
| 19 |
+
from .utils import floatToGoString, INF
|
| 20 |
+
|
| 21 |
+
T = TypeVar('T', bound='MetricWrapperBase')
|
| 22 |
+
F = TypeVar("F", bound=Callable[..., Any])
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _build_full_name(metric_type, name, namespace, subsystem, unit):
|
| 26 |
+
full_name = ''
|
| 27 |
+
if namespace:
|
| 28 |
+
full_name += namespace + '_'
|
| 29 |
+
if subsystem:
|
| 30 |
+
full_name += subsystem + '_'
|
| 31 |
+
full_name += name
|
| 32 |
+
if metric_type == 'counter' and full_name.endswith('_total'):
|
| 33 |
+
full_name = full_name[:-6] # Munge to OpenMetrics.
|
| 34 |
+
if unit and not full_name.endswith("_" + unit):
|
| 35 |
+
full_name += "_" + unit
|
| 36 |
+
if unit and metric_type in ('info', 'stateset'):
|
| 37 |
+
raise ValueError('Metric name is of a type that cannot have a unit: ' + full_name)
|
| 38 |
+
return full_name
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _validate_labelname(l):
|
| 42 |
+
if not METRIC_LABEL_NAME_RE.match(l):
|
| 43 |
+
raise ValueError('Invalid label metric name: ' + l)
|
| 44 |
+
if RESERVED_METRIC_LABEL_NAME_RE.match(l):
|
| 45 |
+
raise ValueError('Reserved label metric name: ' + l)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _validate_labelnames(cls, labelnames):
|
| 49 |
+
labelnames = tuple(labelnames)
|
| 50 |
+
for l in labelnames:
|
| 51 |
+
_validate_labelname(l)
|
| 52 |
+
if l in cls._reserved_labelnames:
|
| 53 |
+
raise ValueError('Reserved label metric name: ' + l)
|
| 54 |
+
return labelnames
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _validate_exemplar(exemplar):
|
| 58 |
+
runes = 0
|
| 59 |
+
for k, v in exemplar.items():
|
| 60 |
+
_validate_labelname(k)
|
| 61 |
+
runes += len(k)
|
| 62 |
+
runes += len(v)
|
| 63 |
+
if runes > 128:
|
| 64 |
+
raise ValueError('Exemplar labels have %d UTF-8 characters, exceeding the limit of 128')
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def _get_use_created() -> bool:
|
| 68 |
+
return os.environ.get("PROMETHEUS_DISABLE_CREATED_SERIES", 'False').lower() not in ('true', '1', 't')
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
_use_created = _get_use_created()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def disable_created_metrics():
|
| 75 |
+
"""Disable exporting _created metrics on counters, histograms, and summaries."""
|
| 76 |
+
global _use_created
|
| 77 |
+
_use_created = False
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def enable_created_metrics():
|
| 81 |
+
"""Enable exporting _created metrics on counters, histograms, and summaries."""
|
| 82 |
+
global _use_created
|
| 83 |
+
_use_created = True
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
class MetricWrapperBase(Collector):
|
| 87 |
+
_type: Optional[str] = None
|
| 88 |
+
_reserved_labelnames: Sequence[str] = ()
|
| 89 |
+
|
| 90 |
+
def _is_observable(self):
|
| 91 |
+
# Whether this metric is observable, i.e.
|
| 92 |
+
# * a metric without label names and values, or
|
| 93 |
+
# * the child of a labelled metric.
|
| 94 |
+
return not self._labelnames or (self._labelnames and self._labelvalues)
|
| 95 |
+
|
| 96 |
+
def _raise_if_not_observable(self):
|
| 97 |
+
# Functions that mutate the state of the metric, for example incrementing
|
| 98 |
+
# a counter, will fail if the metric is not observable, because only if a
|
| 99 |
+
# metric is observable will the value be initialized.
|
| 100 |
+
if not self._is_observable():
|
| 101 |
+
raise ValueError('%s metric is missing label values' % str(self._type))
|
| 102 |
+
|
| 103 |
+
def _is_parent(self):
|
| 104 |
+
return self._labelnames and not self._labelvalues
|
| 105 |
+
|
| 106 |
+
def _get_metric(self):
|
| 107 |
+
return Metric(self._name, self._documentation, self._type, self._unit)
|
| 108 |
+
|
| 109 |
+
def describe(self) -> Iterable[Metric]:
|
| 110 |
+
return [self._get_metric()]
|
| 111 |
+
|
| 112 |
+
def collect(self) -> Iterable[Metric]:
|
| 113 |
+
metric = self._get_metric()
|
| 114 |
+
for suffix, labels, value, timestamp, exemplar in self._samples():
|
| 115 |
+
metric.add_sample(self._name + suffix, labels, value, timestamp, exemplar)
|
| 116 |
+
return [metric]
|
| 117 |
+
|
| 118 |
+
def __str__(self) -> str:
|
| 119 |
+
return f"{self._type}:{self._name}"
|
| 120 |
+
|
| 121 |
+
def __repr__(self) -> str:
|
| 122 |
+
metric_type = type(self)
|
| 123 |
+
return f"{metric_type.__module__}.{metric_type.__name__}({self._name})"
|
| 124 |
+
|
| 125 |
+
def __init__(self: T,
|
| 126 |
+
name: str,
|
| 127 |
+
documentation: str,
|
| 128 |
+
labelnames: Iterable[str] = (),
|
| 129 |
+
namespace: str = '',
|
| 130 |
+
subsystem: str = '',
|
| 131 |
+
unit: str = '',
|
| 132 |
+
registry: Optional[CollectorRegistry] = REGISTRY,
|
| 133 |
+
_labelvalues: Optional[Sequence[str]] = None,
|
| 134 |
+
) -> None:
|
| 135 |
+
self._name = _build_full_name(self._type, name, namespace, subsystem, unit)
|
| 136 |
+
self._labelnames = _validate_labelnames(self, labelnames)
|
| 137 |
+
self._labelvalues = tuple(_labelvalues or ())
|
| 138 |
+
self._kwargs: Dict[str, Any] = {}
|
| 139 |
+
self._documentation = documentation
|
| 140 |
+
self._unit = unit
|
| 141 |
+
|
| 142 |
+
if not METRIC_NAME_RE.match(self._name):
|
| 143 |
+
raise ValueError('Invalid metric name: ' + self._name)
|
| 144 |
+
|
| 145 |
+
if self._is_parent():
|
| 146 |
+
# Prepare the fields needed for child metrics.
|
| 147 |
+
self._lock = Lock()
|
| 148 |
+
self._metrics: Dict[Sequence[str], T] = {}
|
| 149 |
+
|
| 150 |
+
if self._is_observable():
|
| 151 |
+
self._metric_init()
|
| 152 |
+
|
| 153 |
+
if not self._labelvalues:
|
| 154 |
+
# Register the multi-wrapper parent metric, or if a label-less metric, the whole shebang.
|
| 155 |
+
if registry:
|
| 156 |
+
registry.register(self)
|
| 157 |
+
|
| 158 |
+
def labels(self: T, *labelvalues: Any, **labelkwargs: Any) -> T:
|
| 159 |
+
"""Return the child for the given labelset.
|
| 160 |
+
|
| 161 |
+
All metrics can have labels, allowing grouping of related time series.
|
| 162 |
+
Taking a counter as an example:
|
| 163 |
+
|
| 164 |
+
from prometheus_client import Counter
|
| 165 |
+
|
| 166 |
+
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
|
| 167 |
+
c.labels('get', '/').inc()
|
| 168 |
+
c.labels('post', '/submit').inc()
|
| 169 |
+
|
| 170 |
+
Labels can also be provided as keyword arguments:
|
| 171 |
+
|
| 172 |
+
from prometheus_client import Counter
|
| 173 |
+
|
| 174 |
+
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
|
| 175 |
+
c.labels(method='get', endpoint='/').inc()
|
| 176 |
+
c.labels(method='post', endpoint='/submit').inc()
|
| 177 |
+
|
| 178 |
+
See the best practices on [naming](http://prometheus.io/docs/practices/naming/)
|
| 179 |
+
and [labels](http://prometheus.io/docs/practices/instrumentation/#use-labels).
|
| 180 |
+
"""
|
| 181 |
+
if not self._labelnames:
|
| 182 |
+
raise ValueError('No label names were set when constructing %s' % self)
|
| 183 |
+
|
| 184 |
+
if self._labelvalues:
|
| 185 |
+
raise ValueError('{} already has labels set ({}); can not chain calls to .labels()'.format(
|
| 186 |
+
self,
|
| 187 |
+
dict(zip(self._labelnames, self._labelvalues))
|
| 188 |
+
))
|
| 189 |
+
|
| 190 |
+
if labelvalues and labelkwargs:
|
| 191 |
+
raise ValueError("Can't pass both *args and **kwargs")
|
| 192 |
+
|
| 193 |
+
if labelkwargs:
|
| 194 |
+
if sorted(labelkwargs) != sorted(self._labelnames):
|
| 195 |
+
raise ValueError('Incorrect label names')
|
| 196 |
+
labelvalues = tuple(str(labelkwargs[l]) for l in self._labelnames)
|
| 197 |
+
else:
|
| 198 |
+
if len(labelvalues) != len(self._labelnames):
|
| 199 |
+
raise ValueError('Incorrect label count')
|
| 200 |
+
labelvalues = tuple(str(l) for l in labelvalues)
|
| 201 |
+
with self._lock:
|
| 202 |
+
if labelvalues not in self._metrics:
|
| 203 |
+
self._metrics[labelvalues] = self.__class__(
|
| 204 |
+
self._name,
|
| 205 |
+
documentation=self._documentation,
|
| 206 |
+
labelnames=self._labelnames,
|
| 207 |
+
unit=self._unit,
|
| 208 |
+
_labelvalues=labelvalues,
|
| 209 |
+
**self._kwargs
|
| 210 |
+
)
|
| 211 |
+
return self._metrics[labelvalues]
|
| 212 |
+
|
| 213 |
+
def remove(self, *labelvalues: Any) -> None:
|
| 214 |
+
if 'prometheus_multiproc_dir' in os.environ or 'PROMETHEUS_MULTIPROC_DIR' in os.environ:
|
| 215 |
+
warnings.warn(
|
| 216 |
+
"Removal of labels has not been implemented in multi-process mode yet.",
|
| 217 |
+
UserWarning)
|
| 218 |
+
|
| 219 |
+
if not self._labelnames:
|
| 220 |
+
raise ValueError('No label names were set when constructing %s' % self)
|
| 221 |
+
|
| 222 |
+
"""Remove the given labelset from the metric."""
|
| 223 |
+
if len(labelvalues) != len(self._labelnames):
|
| 224 |
+
raise ValueError('Incorrect label count (expected %d, got %s)' % (len(self._labelnames), labelvalues))
|
| 225 |
+
labelvalues = tuple(str(l) for l in labelvalues)
|
| 226 |
+
with self._lock:
|
| 227 |
+
del self._metrics[labelvalues]
|
| 228 |
+
|
| 229 |
+
def clear(self) -> None:
|
| 230 |
+
"""Remove all labelsets from the metric"""
|
| 231 |
+
if 'prometheus_multiproc_dir' in os.environ or 'PROMETHEUS_MULTIPROC_DIR' in os.environ:
|
| 232 |
+
warnings.warn(
|
| 233 |
+
"Clearing labels has not been implemented in multi-process mode yet",
|
| 234 |
+
UserWarning)
|
| 235 |
+
with self._lock:
|
| 236 |
+
self._metrics = {}
|
| 237 |
+
|
| 238 |
+
def _samples(self) -> Iterable[Sample]:
|
| 239 |
+
if self._is_parent():
|
| 240 |
+
return self._multi_samples()
|
| 241 |
+
else:
|
| 242 |
+
return self._child_samples()
|
| 243 |
+
|
| 244 |
+
def _multi_samples(self) -> Iterable[Sample]:
|
| 245 |
+
with self._lock:
|
| 246 |
+
metrics = self._metrics.copy()
|
| 247 |
+
for labels, metric in metrics.items():
|
| 248 |
+
series_labels = list(zip(self._labelnames, labels))
|
| 249 |
+
for suffix, sample_labels, value, timestamp, exemplar in metric._samples():
|
| 250 |
+
yield Sample(suffix, dict(series_labels + list(sample_labels.items())), value, timestamp, exemplar)
|
| 251 |
+
|
| 252 |
+
def _child_samples(self) -> Iterable[Sample]: # pragma: no cover
|
| 253 |
+
raise NotImplementedError('_child_samples() must be implemented by %r' % self)
|
| 254 |
+
|
| 255 |
+
def _metric_init(self): # pragma: no cover
|
| 256 |
+
"""
|
| 257 |
+
Initialize the metric object as a child, i.e. when it has labels (if any) set.
|
| 258 |
+
|
| 259 |
+
This is factored as a separate function to allow for deferred initialization.
|
| 260 |
+
"""
|
| 261 |
+
raise NotImplementedError('_metric_init() must be implemented by %r' % self)
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
class Counter(MetricWrapperBase):
|
| 265 |
+
"""A Counter tracks counts of events or running totals.
|
| 266 |
+
|
| 267 |
+
Example use cases for Counters:
|
| 268 |
+
- Number of requests processed
|
| 269 |
+
- Number of items that were inserted into a queue
|
| 270 |
+
- Total amount of data that a system has processed
|
| 271 |
+
|
| 272 |
+
Counters can only go up (and be reset when the process restarts). If your use case can go down,
|
| 273 |
+
you should use a Gauge instead.
|
| 274 |
+
|
| 275 |
+
An example for a Counter:
|
| 276 |
+
|
| 277 |
+
from prometheus_client import Counter
|
| 278 |
+
|
| 279 |
+
c = Counter('my_failures_total', 'Description of counter')
|
| 280 |
+
c.inc() # Increment by 1
|
| 281 |
+
c.inc(1.6) # Increment by given value
|
| 282 |
+
|
| 283 |
+
There are utilities to count exceptions raised:
|
| 284 |
+
|
| 285 |
+
@c.count_exceptions()
|
| 286 |
+
def f():
|
| 287 |
+
pass
|
| 288 |
+
|
| 289 |
+
with c.count_exceptions():
|
| 290 |
+
pass
|
| 291 |
+
|
| 292 |
+
# Count only one type of exception
|
| 293 |
+
with c.count_exceptions(ValueError):
|
| 294 |
+
pass
|
| 295 |
+
|
| 296 |
+
You can also reset the counter to zero in case your logical "process" restarts
|
| 297 |
+
without restarting the actual python process.
|
| 298 |
+
|
| 299 |
+
c.reset()
|
| 300 |
+
|
| 301 |
+
"""
|
| 302 |
+
_type = 'counter'
|
| 303 |
+
|
| 304 |
+
def _metric_init(self) -> None:
|
| 305 |
+
self._value = values.ValueClass(self._type, self._name, self._name + '_total', self._labelnames,
|
| 306 |
+
self._labelvalues, self._documentation)
|
| 307 |
+
self._created = time.time()
|
| 308 |
+
|
| 309 |
+
def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> None:
|
| 310 |
+
"""Increment counter by the given amount."""
|
| 311 |
+
self._raise_if_not_observable()
|
| 312 |
+
if amount < 0:
|
| 313 |
+
raise ValueError('Counters can only be incremented by non-negative amounts.')
|
| 314 |
+
self._value.inc(amount)
|
| 315 |
+
if exemplar:
|
| 316 |
+
_validate_exemplar(exemplar)
|
| 317 |
+
self._value.set_exemplar(Exemplar(exemplar, amount, time.time()))
|
| 318 |
+
|
| 319 |
+
def reset(self) -> None:
|
| 320 |
+
"""Reset the counter to zero. Use this when a logical process restarts without restarting the actual python process."""
|
| 321 |
+
self._value.set(0)
|
| 322 |
+
self._created = time.time()
|
| 323 |
+
|
| 324 |
+
def count_exceptions(self, exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = Exception) -> ExceptionCounter:
|
| 325 |
+
"""Count exceptions in a block of code or function.
|
| 326 |
+
|
| 327 |
+
Can be used as a function decorator or context manager.
|
| 328 |
+
Increments the counter when an exception of the given
|
| 329 |
+
type is raised up out of the code.
|
| 330 |
+
"""
|
| 331 |
+
self._raise_if_not_observable()
|
| 332 |
+
return ExceptionCounter(self, exception)
|
| 333 |
+
|
| 334 |
+
def _child_samples(self) -> Iterable[Sample]:
|
| 335 |
+
sample = Sample('_total', {}, self._value.get(), None, self._value.get_exemplar())
|
| 336 |
+
if _use_created:
|
| 337 |
+
return (
|
| 338 |
+
sample,
|
| 339 |
+
Sample('_created', {}, self._created, None, None)
|
| 340 |
+
)
|
| 341 |
+
return (sample,)
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
class Gauge(MetricWrapperBase):
|
| 345 |
+
"""Gauge metric, to report instantaneous values.
|
| 346 |
+
|
| 347 |
+
Examples of Gauges include:
|
| 348 |
+
- Inprogress requests
|
| 349 |
+
- Number of items in a queue
|
| 350 |
+
- Free memory
|
| 351 |
+
- Total memory
|
| 352 |
+
- Temperature
|
| 353 |
+
|
| 354 |
+
Gauges can go both up and down.
|
| 355 |
+
|
| 356 |
+
from prometheus_client import Gauge
|
| 357 |
+
|
| 358 |
+
g = Gauge('my_inprogress_requests', 'Description of gauge')
|
| 359 |
+
g.inc() # Increment by 1
|
| 360 |
+
g.dec(10) # Decrement by given value
|
| 361 |
+
g.set(4.2) # Set to a given value
|
| 362 |
+
|
| 363 |
+
There are utilities for common use cases:
|
| 364 |
+
|
| 365 |
+
g.set_to_current_time() # Set to current unixtime
|
| 366 |
+
|
| 367 |
+
# Increment when entered, decrement when exited.
|
| 368 |
+
@g.track_inprogress()
|
| 369 |
+
def f():
|
| 370 |
+
pass
|
| 371 |
+
|
| 372 |
+
with g.track_inprogress():
|
| 373 |
+
pass
|
| 374 |
+
|
| 375 |
+
A Gauge can also take its value from a callback:
|
| 376 |
+
|
| 377 |
+
d = Gauge('data_objects', 'Number of objects')
|
| 378 |
+
my_dict = {}
|
| 379 |
+
d.set_function(lambda: len(my_dict))
|
| 380 |
+
"""
|
| 381 |
+
_type = 'gauge'
|
| 382 |
+
_MULTIPROC_MODES = frozenset(('all', 'liveall', 'min', 'livemin', 'max', 'livemax', 'sum', 'livesum', 'mostrecent', 'livemostrecent'))
|
| 383 |
+
_MOST_RECENT_MODES = frozenset(('mostrecent', 'livemostrecent'))
|
| 384 |
+
|
| 385 |
+
def __init__(self,
|
| 386 |
+
name: str,
|
| 387 |
+
documentation: str,
|
| 388 |
+
labelnames: Iterable[str] = (),
|
| 389 |
+
namespace: str = '',
|
| 390 |
+
subsystem: str = '',
|
| 391 |
+
unit: str = '',
|
| 392 |
+
registry: Optional[CollectorRegistry] = REGISTRY,
|
| 393 |
+
_labelvalues: Optional[Sequence[str]] = None,
|
| 394 |
+
multiprocess_mode: Literal['all', 'liveall', 'min', 'livemin', 'max', 'livemax', 'sum', 'livesum', 'mostrecent', 'livemostrecent'] = 'all',
|
| 395 |
+
):
|
| 396 |
+
self._multiprocess_mode = multiprocess_mode
|
| 397 |
+
if multiprocess_mode not in self._MULTIPROC_MODES:
|
| 398 |
+
raise ValueError('Invalid multiprocess mode: ' + multiprocess_mode)
|
| 399 |
+
super().__init__(
|
| 400 |
+
name=name,
|
| 401 |
+
documentation=documentation,
|
| 402 |
+
labelnames=labelnames,
|
| 403 |
+
namespace=namespace,
|
| 404 |
+
subsystem=subsystem,
|
| 405 |
+
unit=unit,
|
| 406 |
+
registry=registry,
|
| 407 |
+
_labelvalues=_labelvalues,
|
| 408 |
+
)
|
| 409 |
+
self._kwargs['multiprocess_mode'] = self._multiprocess_mode
|
| 410 |
+
self._is_most_recent = self._multiprocess_mode in self._MOST_RECENT_MODES
|
| 411 |
+
|
| 412 |
+
def _metric_init(self) -> None:
|
| 413 |
+
self._value = values.ValueClass(
|
| 414 |
+
self._type, self._name, self._name, self._labelnames, self._labelvalues,
|
| 415 |
+
self._documentation, multiprocess_mode=self._multiprocess_mode
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
def inc(self, amount: float = 1) -> None:
|
| 419 |
+
"""Increment gauge by the given amount."""
|
| 420 |
+
if self._is_most_recent:
|
| 421 |
+
raise RuntimeError("inc must not be used with the mostrecent mode")
|
| 422 |
+
self._raise_if_not_observable()
|
| 423 |
+
self._value.inc(amount)
|
| 424 |
+
|
| 425 |
+
def dec(self, amount: float = 1) -> None:
|
| 426 |
+
"""Decrement gauge by the given amount."""
|
| 427 |
+
if self._is_most_recent:
|
| 428 |
+
raise RuntimeError("dec must not be used with the mostrecent mode")
|
| 429 |
+
self._raise_if_not_observable()
|
| 430 |
+
self._value.inc(-amount)
|
| 431 |
+
|
| 432 |
+
def set(self, value: float) -> None:
|
| 433 |
+
"""Set gauge to the given value."""
|
| 434 |
+
self._raise_if_not_observable()
|
| 435 |
+
if self._is_most_recent:
|
| 436 |
+
self._value.set(float(value), timestamp=time.time())
|
| 437 |
+
else:
|
| 438 |
+
self._value.set(float(value))
|
| 439 |
+
|
| 440 |
+
def set_to_current_time(self) -> None:
|
| 441 |
+
"""Set gauge to the current unixtime."""
|
| 442 |
+
self.set(time.time())
|
| 443 |
+
|
| 444 |
+
def track_inprogress(self) -> InprogressTracker:
|
| 445 |
+
"""Track inprogress blocks of code or functions.
|
| 446 |
+
|
| 447 |
+
Can be used as a function decorator or context manager.
|
| 448 |
+
Increments the gauge when the code is entered,
|
| 449 |
+
and decrements when it is exited.
|
| 450 |
+
"""
|
| 451 |
+
self._raise_if_not_observable()
|
| 452 |
+
return InprogressTracker(self)
|
| 453 |
+
|
| 454 |
+
def time(self) -> Timer:
|
| 455 |
+
"""Time a block of code or function, and set the duration in seconds.
|
| 456 |
+
|
| 457 |
+
Can be used as a function decorator or context manager.
|
| 458 |
+
"""
|
| 459 |
+
return Timer(self, 'set')
|
| 460 |
+
|
| 461 |
+
def set_function(self, f: Callable[[], float]) -> None:
|
| 462 |
+
"""Call the provided function to return the Gauge value.
|
| 463 |
+
|
| 464 |
+
The function must return a float, and may be called from
|
| 465 |
+
multiple threads. All other methods of the Gauge become NOOPs.
|
| 466 |
+
"""
|
| 467 |
+
|
| 468 |
+
self._raise_if_not_observable()
|
| 469 |
+
|
| 470 |
+
def samples(_: Gauge) -> Iterable[Sample]:
|
| 471 |
+
return (Sample('', {}, float(f()), None, None),)
|
| 472 |
+
|
| 473 |
+
self._child_samples = types.MethodType(samples, self) # type: ignore
|
| 474 |
+
|
| 475 |
+
def _child_samples(self) -> Iterable[Sample]:
|
| 476 |
+
return (Sample('', {}, self._value.get(), None, None),)
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
class Summary(MetricWrapperBase):
|
| 480 |
+
"""A Summary tracks the size and number of events.
|
| 481 |
+
|
| 482 |
+
Example use cases for Summaries:
|
| 483 |
+
- Response latency
|
| 484 |
+
- Request size
|
| 485 |
+
|
| 486 |
+
Example for a Summary:
|
| 487 |
+
|
| 488 |
+
from prometheus_client import Summary
|
| 489 |
+
|
| 490 |
+
s = Summary('request_size_bytes', 'Request size (bytes)')
|
| 491 |
+
s.observe(512) # Observe 512 (bytes)
|
| 492 |
+
|
| 493 |
+
Example for a Summary using time:
|
| 494 |
+
|
| 495 |
+
from prometheus_client import Summary
|
| 496 |
+
|
| 497 |
+
REQUEST_TIME = Summary('response_latency_seconds', 'Response latency (seconds)')
|
| 498 |
+
|
| 499 |
+
@REQUEST_TIME.time()
|
| 500 |
+
def create_response(request):
|
| 501 |
+
'''A dummy function'''
|
| 502 |
+
time.sleep(1)
|
| 503 |
+
|
| 504 |
+
Example for using the same Summary object as a context manager:
|
| 505 |
+
|
| 506 |
+
with REQUEST_TIME.time():
|
| 507 |
+
pass # Logic to be timed
|
| 508 |
+
"""
|
| 509 |
+
_type = 'summary'
|
| 510 |
+
_reserved_labelnames = ['quantile']
|
| 511 |
+
|
| 512 |
+
def _metric_init(self) -> None:
|
| 513 |
+
self._count = values.ValueClass(self._type, self._name, self._name + '_count', self._labelnames,
|
| 514 |
+
self._labelvalues, self._documentation)
|
| 515 |
+
self._sum = values.ValueClass(self._type, self._name, self._name + '_sum', self._labelnames, self._labelvalues, self._documentation)
|
| 516 |
+
self._created = time.time()
|
| 517 |
+
|
| 518 |
+
def observe(self, amount: float) -> None:
|
| 519 |
+
"""Observe the given amount.
|
| 520 |
+
|
| 521 |
+
The amount is usually positive or zero. Negative values are
|
| 522 |
+
accepted but prevent current versions of Prometheus from
|
| 523 |
+
properly detecting counter resets in the sum of
|
| 524 |
+
observations. See
|
| 525 |
+
https://prometheus.io/docs/practices/histograms/#count-and-sum-of-observations
|
| 526 |
+
for details.
|
| 527 |
+
"""
|
| 528 |
+
self._raise_if_not_observable()
|
| 529 |
+
self._count.inc(1)
|
| 530 |
+
self._sum.inc(amount)
|
| 531 |
+
|
| 532 |
+
def time(self) -> Timer:
|
| 533 |
+
"""Time a block of code or function, and observe the duration in seconds.
|
| 534 |
+
|
| 535 |
+
Can be used as a function decorator or context manager.
|
| 536 |
+
"""
|
| 537 |
+
return Timer(self, 'observe')
|
| 538 |
+
|
| 539 |
+
def _child_samples(self) -> Iterable[Sample]:
|
| 540 |
+
samples = [
|
| 541 |
+
Sample('_count', {}, self._count.get(), None, None),
|
| 542 |
+
Sample('_sum', {}, self._sum.get(), None, None),
|
| 543 |
+
]
|
| 544 |
+
if _use_created:
|
| 545 |
+
samples.append(Sample('_created', {}, self._created, None, None))
|
| 546 |
+
return tuple(samples)
|
| 547 |
+
|
| 548 |
+
|
| 549 |
+
class Histogram(MetricWrapperBase):
|
| 550 |
+
"""A Histogram tracks the size and number of events in buckets.
|
| 551 |
+
|
| 552 |
+
You can use Histograms for aggregatable calculation of quantiles.
|
| 553 |
+
|
| 554 |
+
Example use cases:
|
| 555 |
+
- Response latency
|
| 556 |
+
- Request size
|
| 557 |
+
|
| 558 |
+
Example for a Histogram:
|
| 559 |
+
|
| 560 |
+
from prometheus_client import Histogram
|
| 561 |
+
|
| 562 |
+
h = Histogram('request_size_bytes', 'Request size (bytes)')
|
| 563 |
+
h.observe(512) # Observe 512 (bytes)
|
| 564 |
+
|
| 565 |
+
Example for a Histogram using time:
|
| 566 |
+
|
| 567 |
+
from prometheus_client import Histogram
|
| 568 |
+
|
| 569 |
+
REQUEST_TIME = Histogram('response_latency_seconds', 'Response latency (seconds)')
|
| 570 |
+
|
| 571 |
+
@REQUEST_TIME.time()
|
| 572 |
+
def create_response(request):
|
| 573 |
+
'''A dummy function'''
|
| 574 |
+
time.sleep(1)
|
| 575 |
+
|
| 576 |
+
Example of using the same Histogram object as a context manager:
|
| 577 |
+
|
| 578 |
+
with REQUEST_TIME.time():
|
| 579 |
+
pass # Logic to be timed
|
| 580 |
+
|
| 581 |
+
The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds.
|
| 582 |
+
They can be overridden by passing `buckets` keyword argument to `Histogram`.
|
| 583 |
+
"""
|
| 584 |
+
_type = 'histogram'
|
| 585 |
+
_reserved_labelnames = ['le']
|
| 586 |
+
DEFAULT_BUCKETS = (.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10.0, INF)
|
| 587 |
+
|
| 588 |
+
def __init__(self,
|
| 589 |
+
name: str,
|
| 590 |
+
documentation: str,
|
| 591 |
+
labelnames: Iterable[str] = (),
|
| 592 |
+
namespace: str = '',
|
| 593 |
+
subsystem: str = '',
|
| 594 |
+
unit: str = '',
|
| 595 |
+
registry: Optional[CollectorRegistry] = REGISTRY,
|
| 596 |
+
_labelvalues: Optional[Sequence[str]] = None,
|
| 597 |
+
buckets: Sequence[Union[float, str]] = DEFAULT_BUCKETS,
|
| 598 |
+
):
|
| 599 |
+
self._prepare_buckets(buckets)
|
| 600 |
+
super().__init__(
|
| 601 |
+
name=name,
|
| 602 |
+
documentation=documentation,
|
| 603 |
+
labelnames=labelnames,
|
| 604 |
+
namespace=namespace,
|
| 605 |
+
subsystem=subsystem,
|
| 606 |
+
unit=unit,
|
| 607 |
+
registry=registry,
|
| 608 |
+
_labelvalues=_labelvalues,
|
| 609 |
+
)
|
| 610 |
+
self._kwargs['buckets'] = buckets
|
| 611 |
+
|
| 612 |
+
def _prepare_buckets(self, source_buckets: Sequence[Union[float, str]]) -> None:
|
| 613 |
+
buckets = [float(b) for b in source_buckets]
|
| 614 |
+
if buckets != sorted(buckets):
|
| 615 |
+
# This is probably an error on the part of the user,
|
| 616 |
+
# so raise rather than sorting for them.
|
| 617 |
+
raise ValueError('Buckets not in sorted order')
|
| 618 |
+
if buckets and buckets[-1] != INF:
|
| 619 |
+
buckets.append(INF)
|
| 620 |
+
if len(buckets) < 2:
|
| 621 |
+
raise ValueError('Must have at least two buckets')
|
| 622 |
+
self._upper_bounds = buckets
|
| 623 |
+
|
| 624 |
+
def _metric_init(self) -> None:
|
| 625 |
+
self._buckets: List[values.ValueClass] = []
|
| 626 |
+
self._created = time.time()
|
| 627 |
+
bucket_labelnames = self._labelnames + ('le',)
|
| 628 |
+
self._sum = values.ValueClass(self._type, self._name, self._name + '_sum', self._labelnames, self._labelvalues, self._documentation)
|
| 629 |
+
for b in self._upper_bounds:
|
| 630 |
+
self._buckets.append(values.ValueClass(
|
| 631 |
+
self._type,
|
| 632 |
+
self._name,
|
| 633 |
+
self._name + '_bucket',
|
| 634 |
+
bucket_labelnames,
|
| 635 |
+
self._labelvalues + (floatToGoString(b),),
|
| 636 |
+
self._documentation)
|
| 637 |
+
)
|
| 638 |
+
|
| 639 |
+
def observe(self, amount: float, exemplar: Optional[Dict[str, str]] = None) -> None:
|
| 640 |
+
"""Observe the given amount.
|
| 641 |
+
|
| 642 |
+
The amount is usually positive or zero. Negative values are
|
| 643 |
+
accepted but prevent current versions of Prometheus from
|
| 644 |
+
properly detecting counter resets in the sum of
|
| 645 |
+
observations. See
|
| 646 |
+
https://prometheus.io/docs/practices/histograms/#count-and-sum-of-observations
|
| 647 |
+
for details.
|
| 648 |
+
"""
|
| 649 |
+
self._raise_if_not_observable()
|
| 650 |
+
self._sum.inc(amount)
|
| 651 |
+
for i, bound in enumerate(self._upper_bounds):
|
| 652 |
+
if amount <= bound:
|
| 653 |
+
self._buckets[i].inc(1)
|
| 654 |
+
if exemplar:
|
| 655 |
+
_validate_exemplar(exemplar)
|
| 656 |
+
self._buckets[i].set_exemplar(Exemplar(exemplar, amount, time.time()))
|
| 657 |
+
break
|
| 658 |
+
|
| 659 |
+
def time(self) -> Timer:
|
| 660 |
+
"""Time a block of code or function, and observe the duration in seconds.
|
| 661 |
+
|
| 662 |
+
Can be used as a function decorator or context manager.
|
| 663 |
+
"""
|
| 664 |
+
return Timer(self, 'observe')
|
| 665 |
+
|
| 666 |
+
def _child_samples(self) -> Iterable[Sample]:
|
| 667 |
+
samples = []
|
| 668 |
+
acc = 0.0
|
| 669 |
+
for i, bound in enumerate(self._upper_bounds):
|
| 670 |
+
acc += self._buckets[i].get()
|
| 671 |
+
samples.append(Sample('_bucket', {'le': floatToGoString(bound)}, acc, None, self._buckets[i].get_exemplar()))
|
| 672 |
+
samples.append(Sample('_count', {}, acc, None, None))
|
| 673 |
+
if self._upper_bounds[0] >= 0:
|
| 674 |
+
samples.append(Sample('_sum', {}, self._sum.get(), None, None))
|
| 675 |
+
if _use_created:
|
| 676 |
+
samples.append(Sample('_created', {}, self._created, None, None))
|
| 677 |
+
return tuple(samples)
|
| 678 |
+
|
| 679 |
+
|
| 680 |
+
class Info(MetricWrapperBase):
|
| 681 |
+
"""Info metric, key-value pairs.
|
| 682 |
+
|
| 683 |
+
Examples of Info include:
|
| 684 |
+
- Build information
|
| 685 |
+
- Version information
|
| 686 |
+
- Potential target metadata
|
| 687 |
+
|
| 688 |
+
Example usage:
|
| 689 |
+
from prometheus_client import Info
|
| 690 |
+
|
| 691 |
+
i = Info('my_build', 'Description of info')
|
| 692 |
+
i.info({'version': '1.2.3', 'buildhost': 'foo@bar'})
|
| 693 |
+
|
| 694 |
+
Info metrics do not work in multiprocess mode.
|
| 695 |
+
"""
|
| 696 |
+
_type = 'info'
|
| 697 |
+
|
| 698 |
+
def _metric_init(self):
|
| 699 |
+
self._labelname_set = set(self._labelnames)
|
| 700 |
+
self._lock = Lock()
|
| 701 |
+
self._value = {}
|
| 702 |
+
|
| 703 |
+
def info(self, val: Dict[str, str]) -> None:
|
| 704 |
+
"""Set info metric."""
|
| 705 |
+
if self._labelname_set.intersection(val.keys()):
|
| 706 |
+
raise ValueError('Overlapping labels for Info metric, metric: {} child: {}'.format(
|
| 707 |
+
self._labelnames, val))
|
| 708 |
+
if any(i is None for i in val.values()):
|
| 709 |
+
raise ValueError('Label value cannot be None')
|
| 710 |
+
with self._lock:
|
| 711 |
+
self._value = dict(val)
|
| 712 |
+
|
| 713 |
+
def _child_samples(self) -> Iterable[Sample]:
|
| 714 |
+
with self._lock:
|
| 715 |
+
return (Sample('_info', self._value, 1.0, None, None),)
|
| 716 |
+
|
| 717 |
+
|
| 718 |
+
class Enum(MetricWrapperBase):
|
| 719 |
+
"""Enum metric, which of a set of states is true.
|
| 720 |
+
|
| 721 |
+
Example usage:
|
| 722 |
+
from prometheus_client import Enum
|
| 723 |
+
|
| 724 |
+
e = Enum('task_state', 'Description of enum',
|
| 725 |
+
states=['starting', 'running', 'stopped'])
|
| 726 |
+
e.state('running')
|
| 727 |
+
|
| 728 |
+
The first listed state will be the default.
|
| 729 |
+
Enum metrics do not work in multiprocess mode.
|
| 730 |
+
"""
|
| 731 |
+
_type = 'stateset'
|
| 732 |
+
|
| 733 |
+
def __init__(self,
|
| 734 |
+
name: str,
|
| 735 |
+
documentation: str,
|
| 736 |
+
labelnames: Sequence[str] = (),
|
| 737 |
+
namespace: str = '',
|
| 738 |
+
subsystem: str = '',
|
| 739 |
+
unit: str = '',
|
| 740 |
+
registry: Optional[CollectorRegistry] = REGISTRY,
|
| 741 |
+
_labelvalues: Optional[Sequence[str]] = None,
|
| 742 |
+
states: Optional[Sequence[str]] = None,
|
| 743 |
+
):
|
| 744 |
+
super().__init__(
|
| 745 |
+
name=name,
|
| 746 |
+
documentation=documentation,
|
| 747 |
+
labelnames=labelnames,
|
| 748 |
+
namespace=namespace,
|
| 749 |
+
subsystem=subsystem,
|
| 750 |
+
unit=unit,
|
| 751 |
+
registry=registry,
|
| 752 |
+
_labelvalues=_labelvalues,
|
| 753 |
+
)
|
| 754 |
+
if name in labelnames:
|
| 755 |
+
raise ValueError(f'Overlapping labels for Enum metric: {name}')
|
| 756 |
+
if not states:
|
| 757 |
+
raise ValueError(f'No states provided for Enum metric: {name}')
|
| 758 |
+
self._kwargs['states'] = self._states = states
|
| 759 |
+
|
| 760 |
+
def _metric_init(self) -> None:
|
| 761 |
+
self._value = 0
|
| 762 |
+
self._lock = Lock()
|
| 763 |
+
|
| 764 |
+
def state(self, state: str) -> None:
|
| 765 |
+
"""Set enum metric state."""
|
| 766 |
+
self._raise_if_not_observable()
|
| 767 |
+
with self._lock:
|
| 768 |
+
self._value = self._states.index(state)
|
| 769 |
+
|
| 770 |
+
def _child_samples(self) -> Iterable[Sample]:
|
| 771 |
+
with self._lock:
|
| 772 |
+
return [
|
| 773 |
+
Sample('', {self._name: s}, 1 if i == self._value else 0, None, None)
|
| 774 |
+
for i, s
|
| 775 |
+
in enumerate(self._states)
|
| 776 |
+
]
|