added my Recipes

This commit is contained in:
2024-07-11 14:16:35 +02:00
parent 38bc4f53ac
commit 09b621d929
7118 changed files with 525762 additions and 3 deletions

View File

@@ -0,0 +1,153 @@
From aa8ee5e5e934908f0357364f6ec90a3ecda62880 Mon Sep 17 00:00:00 2001
From: Nicolas Schodet <nico@ni.fr.eu.org>
Date: Mon, 3 Jan 2022 02:37:01 +0100
Subject: [PATCH] Use Py_ssize_t when parsing buffer length, fix #426 (#427)
From python 3.9 documentation:
> For all # variants of formats (s#, y#, etc.), the macro
> PY_SSIZE_T_CLEAN must be defined before including Python.h. On Python
> 3.9 and older, the type of the length argument is Py_ssize_t if the
> PY_SSIZE_T_CLEAN macro is defined, or int otherwise.
From python 3.8 changes:
> Use of # variants of formats in parsing or building value (e.g.
> PyArg_ParseTuple(), Py_BuildValue(), PyObject_CallFunction(), etc.)
> without PY_SSIZE_T_CLEAN defined raises DeprecationWarning now. It
> will be removed in 3.10 or 4.0. Read Parsing arguments and building
> values for detail. (Contributed by Inada Naoki in bpo-36381.)
Fixes https://github.com/pybluez/pybluez/issues/426
---
Upstream-Status: Accepted
bluez/btmodule.c | 23 ++++++++++++++---------
msbt/_msbt.c | 6 ++++--
2 files changed, 18 insertions(+), 11 deletions(-)
diff --git a/bluez/btmodule.c b/bluez/btmodule.c
index 518b723..912a489 100644
--- a/bluez/btmodule.c
+++ b/bluez/btmodule.c
@@ -16,7 +16,8 @@ Local naming conventions:
- names starting with bt_ are module-level functions
*/
-
+#define PY_SSIZE_T_CLEAN 1
+#include "Python.h"
#include "btmodule.h"
#include "structmember.h"
@@ -732,7 +733,7 @@ sock_setsockopt(PySocketSockObject *s, PyObject *args)
int optname;
int res;
void *buf;
- int buflen;
+ Py_ssize_t buflen;
int flag;
if (PyArg_ParseTuple(args, "iii:setsockopt", &level, &optname, &flag)) {
@@ -2001,7 +2002,8 @@ static PyObject *
bt_hci_send_cmd(PyObject *self, PyObject *args)
{
PySocketSockObject *socko = NULL;
- int err, plen = 0;
+ int err;
+ Py_ssize_t plen = 0;
uint16_t ogf, ocf;
char *param = NULL;
int dd = 0;
@@ -2036,6 +2038,7 @@ bt_hci_send_req(PyObject *self, PyObject *args, PyObject *kwds)
int err;
int to=0;
char rparam[256];
+ Py_ssize_t req_clen;
struct hci_request req = { 0 };
int dd = 0;
@@ -2043,9 +2046,10 @@ bt_hci_send_req(PyObject *self, PyObject *args, PyObject *kwds)
"timeout", 0 };
if( !PyArg_ParseTupleAndKeywords(args, kwds, "OHHii|s#i", keywords,
- &socko, &req.ogf, &req.ocf, &req.event, &req.rlen,
- &req.cparam, &req.clen, &to) )
+ &socko, &req.ogf, &req.ocf, &req.event, &req.rlen,
+ &req.cparam, &req_clen, &to) )
return 0;
+ req.clen = req_clen;
req.rparam = rparam;
dd = socko->sock_fd;
@@ -2274,7 +2278,8 @@ Returns the name of the device, or raises an error on failure");
static PyObject * bt_hci_filter_ ## name (PyObject *self, PyObject *args )\
{ \
char *param; \
- int len, arg; \
+ Py_ssize_t len; \
+ int arg; \
if( !PyArg_ParseTuple(args,"s#i", &param, &len, &arg) ) \
return 0; \
if( len != sizeof(struct hci_filter) ) { \
@@ -2303,7 +2308,7 @@ DECL_HCI_FILTER_OP_1(test_opcode, "test opcode!")
static PyObject * bt_hci_filter_ ## name (PyObject *self, PyObject *args )\
{ \
char *param; \
- int len; \
+ Py_ssize_t len; \
if( !PyArg_ParseTuple(args,"s#", &param, &len) ) \
return 0; \
if( len != sizeof(struct hci_filter) ) { \
@@ -2364,7 +2369,7 @@ static PyObject *
bt_ba2str(PyObject *self, PyObject *args)
{
char *data=NULL;
- int len=0;
+ Py_ssize_t len=0;
char ba_str[19] = {0};
if (!PyArg_ParseTuple(args, "s#", &data, &len)) return 0;
ba2str((bdaddr_t*)data, ba_str);
@@ -2579,7 +2584,7 @@ bt_sdp_advertise_service( PyObject *self, PyObject *args )
*provider = NULL,
*description = NULL;
PyObject *service_classes, *profiles, *protocols;
- int namelen = 0, provlen = 0, desclen = 0;
+ Py_ssize_t namelen = 0, provlen = 0, desclen = 0;
uuid_t svc_uuid = { 0 };
int i;
char addrbuf[256] = { 0 };
diff --git a/msbt/_msbt.c b/msbt/_msbt.c
index b3d27ff..81f5ee9 100644
--- a/msbt/_msbt.c
+++ b/msbt/_msbt.c
@@ -2,6 +2,8 @@
#define UNICODE
#endif
+#define PY_SSIZE_T_CLEAN 1
+
#include <winsock2.h>
#include <ws2bth.h>
#include <BluetoothAPIs.h>
@@ -155,7 +157,7 @@ static PyObject *
msbt_bind(PyObject *self, PyObject *args)
{
wchar_t *addrstr = NULL;
- int addrstrlen = -1;
+ Py_ssize_t addrstrlen = -1;
int sockfd = -1;
int port = -1;
char buf[100] = { 0 };
@@ -765,7 +767,7 @@ msbt_set_service_raw(PyObject *self, PyObject *args)
WSAESETSERVICEOP op;
char *record = NULL;
- int reclen = -1;
+ Py_ssize_t reclen = -1;
BTH_SET_SERVICE *si = NULL;
int silen = -1;
ULONG sdpVersion = BTH_SDP_VERSION;
--
2.34.1

View File

@@ -0,0 +1,454 @@
From f83d38687fec2239c517037453ed4a2191279796 Mon Sep 17 00:00:00 2001
From: Victor Stinner <vstinner@python.org>
Date: Fri, 28 Jan 2022 00:14:04 +0100
Subject: [PATCH] Port to Python 3.11 (#410)
* Replace "arg == Py_None" with Py_IsNone(arg)
* Replace arg->ob_type with Py_TYPE(arg)
* Replace "Py_TYPE(obj) = type" with Py_SET_TYPE(obj, type).
* Copy pythoncapi_compat.h from:
https://github.com/pythoncapi/pythoncapi_compat
* pythoncapi_compat.h provides Py_SET_TYPE() and Py_IsNone() to old
Python versions.
Upstream-Status: Backport [https://github.com/pybluez/pybluez/commit/5096047f90a1f6a74ceb250aef6243e144170f92]
Signed-off-by: Alexander Kanavin <alex@linutronix.de>
---
bluez/btmodule.c | 14 +-
bluez/pythoncapi_compat.h | 364 ++++++++++++++++++++++++++++++++++++++
2 files changed, 372 insertions(+), 6 deletions(-)
create mode 100644 bluez/pythoncapi_compat.h
diff --git a/bluez/btmodule.c b/bluez/btmodule.c
index 912a489..b61f74a 100644
--- a/bluez/btmodule.c
+++ b/bluez/btmodule.c
@@ -21,6 +21,8 @@ Local naming conventions:
#include "btmodule.h"
#include "structmember.h"
+#include "pythoncapi_compat.h"
+
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
@@ -678,7 +680,7 @@ sock_settimeout(PySocketSockObject *s, PyObject *arg)
{
double timeout;
- if (arg == Py_None)
+ if (Py_IsNone(arg))
timeout = -1.0;
else {
timeout = PyFloat_AsDouble(arg);
@@ -1752,7 +1754,7 @@ bt_btohl(PyObject *self, PyObject *args)
else
return PyErr_Format(PyExc_TypeError,
"expected int/long, %s found",
- arg->ob_type->tp_name);
+ Py_TYPE(arg)->tp_name);
if (x == (unsigned long) -1 && PyErr_Occurred())
return NULL;
return PyInt_FromLong(btohl(x));
@@ -1816,7 +1818,7 @@ bt_htobl(PyObject *self, PyObject *args)
else
return PyErr_Format(PyExc_TypeError,
"expected int/long, %s found",
- arg->ob_type->tp_name);
+ Py_TYPE(arg)->tp_name);
return PyInt_FromLong(htobl(x));
}
@@ -1889,7 +1891,7 @@ bt_setdefaulttimeout(PyObject *self, PyObject *arg)
{
double timeout;
- if (arg == Py_None)
+ if (Py_IsNone(arg))
timeout = -1.0;
else {
timeout = PyFloat_AsDouble(arg);
@@ -2980,8 +2982,8 @@ PyMODINIT_FUNC
init_bluetooth(void)
#endif
{
- Py_TYPE(&sock_type) = &PyType_Type;
- Py_TYPE(&sdp_session_type) = &PyType_Type;
+ Py_SET_TYPE(&sock_type, &PyType_Type);
+ Py_SET_TYPE(&sdp_session_type, &PyType_Type);
#if PY_MAJOR_VERSION >= 3
PyObject *m = PyModule_Create(&moduledef);
#else
diff --git a/bluez/pythoncapi_compat.h b/bluez/pythoncapi_compat.h
new file mode 100644
index 0000000..e660b61
--- /dev/null
+++ b/bluez/pythoncapi_compat.h
@@ -0,0 +1,364 @@
+// Header file providing new functions of the Python C API to old Python
+// versions.
+//
+// File distributed under the MIT license.
+// Copyright Contributors to the pythoncapi_compat project.
+//
+// Homepage:
+// https://github.com/pythoncapi/pythoncapi_compat
+//
+// Latest version:
+// https://raw.githubusercontent.com/pythoncapi/pythoncapi_compat/master/pythoncapi_compat.h
+//
+// SPDX-License-Identifier: MIT
+
+#ifndef PYTHONCAPI_COMPAT
+#define PYTHONCAPI_COMPAT
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <Python.h>
+#include "frameobject.h" // PyFrameObject, PyFrame_GetBack()
+
+
+// Compatibility with Visual Studio 2013 and older which don't support
+// the inline keyword in C (only in C++): use __inline instead.
+#if (defined(_MSC_VER) && _MSC_VER < 1900 \
+ && !defined(__cplusplus) && !defined(inline))
+# define inline __inline
+# define PYTHONCAPI_COMPAT_MSC_INLINE
+ // These two macros are undefined at the end of this file
+#endif
+
+
+// Cast argument to PyObject* type.
+#ifndef _PyObject_CAST
+# define _PyObject_CAST(op) ((PyObject*)(op))
+#endif
+#ifndef _PyObject_CAST_CONST
+# define _PyObject_CAST_CONST(op) ((const PyObject*)(op))
+#endif
+
+
+// bpo-42262 added Py_NewRef() to Python 3.10.0a3
+#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef)
+static inline PyObject* _Py_NewRef(PyObject *obj)
+{
+ Py_INCREF(obj);
+ return obj;
+}
+#define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
+#endif
+
+
+// bpo-42262 added Py_XNewRef() to Python 3.10.0a3
+#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef)
+static inline PyObject* _Py_XNewRef(PyObject *obj)
+{
+ Py_XINCREF(obj);
+ return obj;
+}
+#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
+#endif
+
+
+// See https://bugs.python.org/issue42522
+#if !defined(_Py_StealRef)
+static inline PyObject* __Py_StealRef(PyObject *obj)
+{
+ Py_DECREF(obj);
+ return obj;
+}
+#define _Py_StealRef(obj) __Py_StealRef(_PyObject_CAST(obj))
+#endif
+
+
+// See https://bugs.python.org/issue42522
+#if !defined(_Py_XStealRef)
+static inline PyObject* __Py_XStealRef(PyObject *obj)
+{
+ Py_XDECREF(obj);
+ return obj;
+}
+#define _Py_XStealRef(obj) __Py_XStealRef(_PyObject_CAST(obj))
+#endif
+
+
+// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
+#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
+static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
+{
+ ob->ob_refcnt = refcnt;
+}
+#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
+#endif
+
+
+// Py_SETREF() and Py_XSETREF() were added to Python 3.5.2.
+// It is excluded from the limited C API.
+#if (PY_VERSION_HEX < 0x03050200 && !defined(Py_SETREF)) && !defined(Py_LIMITED_API)
+#define Py_SETREF(op, op2) \
+ do { \
+ PyObject *_py_tmp = _PyObject_CAST(op); \
+ (op) = (op2); \
+ Py_DECREF(_py_tmp); \
+ } while (0)
+
+#define Py_XSETREF(op, op2) \
+ do { \
+ PyObject *_py_tmp = _PyObject_CAST(op); \
+ (op) = (op2); \
+ Py_XDECREF(_py_tmp); \
+ } while (0)
+#endif
+
+
+// bpo-43753 added Py_Is(), Py_IsNone(), Py_IsTrue() and Py_IsFalse()
+// to Python 3.10.0b1.
+#if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_Is)
+# define Py_Is(x, y) ((x) == (y))
+#endif
+#if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsNone)
+# define Py_IsNone(x) Py_Is(x, Py_None)
+#endif
+#if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsTrue)
+# define Py_IsTrue(x) Py_Is(x, Py_True)
+#endif
+#if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsFalse)
+# define Py_IsFalse(x) Py_Is(x, Py_False)
+#endif
+
+
+// bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
+#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
+static inline void
+_Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
+{
+ ob->ob_type = type;
+}
+#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
+#endif
+
+
+// bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
+#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
+static inline void
+_Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
+{
+ ob->ob_size = size;
+}
+#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
+#endif
+
+
+// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
+#if PY_VERSION_HEX < 0x030900B1
+static inline PyCodeObject*
+PyFrame_GetCode(PyFrameObject *frame)
+{
+ assert(frame != NULL);
+ assert(frame->f_code != NULL);
+ return (PyCodeObject*)Py_NewRef(frame->f_code);
+}
+#endif
+
+static inline PyCodeObject*
+_PyFrame_GetCodeBorrow(PyFrameObject *frame)
+{
+ return (PyCodeObject *)_Py_StealRef(PyFrame_GetCode(frame));
+}
+
+
+// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
+#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
+static inline PyFrameObject*
+PyFrame_GetBack(PyFrameObject *frame)
+{
+ assert(frame != NULL);
+ return (PyFrameObject*)Py_XNewRef(frame->f_back);
+}
+#endif
+
+#if !defined(PYPY_VERSION)
+static inline PyFrameObject*
+_PyFrame_GetBackBorrow(PyFrameObject *frame)
+{
+ return (PyFrameObject *)_Py_XStealRef(PyFrame_GetBack(frame));
+}
+#endif
+
+
+// bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
+#if PY_VERSION_HEX < 0x030900A5
+static inline PyInterpreterState *
+PyThreadState_GetInterpreter(PyThreadState *tstate)
+{
+ assert(tstate != NULL);
+ return tstate->interp;
+}
+#endif
+
+
+// bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
+#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
+static inline PyFrameObject*
+PyThreadState_GetFrame(PyThreadState *tstate)
+{
+ assert(tstate != NULL);
+ return (PyFrameObject *)Py_XNewRef(tstate->frame);
+}
+#endif
+
+#if !defined(PYPY_VERSION)
+static inline PyFrameObject*
+_PyThreadState_GetFrameBorrow(PyThreadState *tstate)
+{
+ return (PyFrameObject *)_Py_XStealRef(PyThreadState_GetFrame(tstate));
+}
+#endif
+
+
+// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
+#if PY_VERSION_HEX < 0x030900A5
+static inline PyInterpreterState *
+PyInterpreterState_Get(void)
+{
+ PyThreadState *tstate;
+ PyInterpreterState *interp;
+
+ tstate = PyThreadState_GET();
+ if (tstate == NULL) {
+ Py_FatalError("GIL released (tstate is NULL)");
+ }
+ interp = tstate->interp;
+ if (interp == NULL) {
+ Py_FatalError("no current interpreter");
+ }
+ return interp;
+}
+#endif
+
+
+// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
+#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
+static inline uint64_t
+PyThreadState_GetID(PyThreadState *tstate)
+{
+ assert(tstate != NULL);
+ return tstate->id;
+}
+#endif
+
+
+// bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
+#if PY_VERSION_HEX < 0x030900A1
+static inline PyObject*
+PyObject_CallNoArgs(PyObject *func)
+{
+ return PyObject_CallFunctionObjArgs(func, NULL);
+}
+#endif
+
+
+// bpo-39245 made PyObject_CallOneArg() public (previously called
+// _PyObject_CallOneArg) in Python 3.9.0a4
+#if PY_VERSION_HEX < 0x030900A4
+static inline PyObject*
+PyObject_CallOneArg(PyObject *func, PyObject *arg)
+{
+ return PyObject_CallFunctionObjArgs(func, arg, NULL);
+}
+#endif
+
+
+// bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
+#if PY_VERSION_HEX < 0x030A00A3
+static inline int
+PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
+{
+ Py_XINCREF(value);
+ int res = PyModule_AddObject(module, name, value);
+ if (res < 0) {
+ Py_XDECREF(value);
+ }
+ return res;
+}
+#endif
+
+
+// bpo-40024 added PyModule_AddType() to Python 3.9.0a5
+#if PY_VERSION_HEX < 0x030900A5
+static inline int
+PyModule_AddType(PyObject *module, PyTypeObject *type)
+{
+ const char *name, *dot;
+
+ if (PyType_Ready(type) < 0) {
+ return -1;
+ }
+
+ // inline _PyType_Name()
+ name = type->tp_name;
+ assert(name != NULL);
+ dot = strrchr(name, '.');
+ if (dot != NULL) {
+ name = dot + 1;
+ }
+
+ return PyModule_AddObjectRef(module, name, (PyObject *)type);
+}
+#endif
+
+
+// bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
+// bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
+#if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
+static inline int
+PyObject_GC_IsTracked(PyObject* obj)
+{
+ return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
+}
+#endif
+
+// bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
+// bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
+#if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION)
+static inline int
+PyObject_GC_IsFinalized(PyObject *obj)
+{
+ return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1));
+}
+#endif
+
+
+// bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
+#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
+static inline int
+_Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
+ return ob->ob_type == type;
+}
+#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST_CONST(ob), type)
+#endif
+
+
+// Py_UNUSED() was added to Python 3.4.0b2.
+#if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
+# if defined(__GNUC__) || defined(__clang__)
+# define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
+# else
+# define Py_UNUSED(name) _unused_ ## name
+# endif
+#endif
+
+
+#ifdef PYTHONCAPI_COMPAT_MSC_INLINE
+# undef inline
+# undef PYTHONCAPI_COMPAT_MSC_INLINE
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif // PYTHONCAPI_COMPAT
--
2.30.2