1From 64de7911d2938fc3601fec39c08008465b9d4f6f Mon Sep 17 00:00:00 2001
2From: Nick Cao <nickcao@nichi.co>
3Date: Tue, 7 Feb 2023 17:12:50 +0800
4Subject: [PATCH] python: enum: fix build for Python 3.11
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9Python 3.9 introduced Py_SET_SIZE function to set size instead of
10relying on Py_SIZE() as a macro [3.9].
11
12Python 3.10 started to encourage to use Py_SET_SIZE instead of
13assigning into return value of Py_SIZE [3.10].
14
15Python 3.11 flips the switch, turn Py_SIZE into a function [3.11],
16thus Py_SIZE(obj) will be a rvalue. We need to use Py_SET_SIZE
17to set size now.
18
19[3.9]: https://docs.python.org/3.9/c-api/structures.html#c.Py_SET_SIZE
20[3.10]: https://docs.python.org/3.10/c-api/structures.html#c.Py_SIZE
21[3.11]: https://docs.python.org/3.11/c-api/structures.html#c.Py_SIZE
22
23Adapted from https://github.com/mchehab/zbar/pull/231
24
25Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
26Signed-off-by: Nick Cao <nickcao@nichi.co>
27---
28 python/enum.c | 8 ++++++++
29 1 file changed, 8 insertions(+)
30
31diff --git a/python/enum.c b/python/enum.c
32index dfe1b1e..4833a20 100644
33--- a/python/enum.c
34+++ b/python/enum.c
35@@ -52,7 +52,11 @@ enumitem_new (PyTypeObject *type,
36
37 /* we assume the "fast path" for a single-digit ints (see longobject.c) */
38 /* this also holds if we get a small_int preallocated long */
39+#if PY_VERSION_HEX >= 0x030900A4
40+ Py_SET_SIZE(&self->val, Py_SIZE(longval));
41+#else
42 Py_SIZE(&self->val) = Py_SIZE(longval);
43+#endif
44 self->val.ob_digit[0] = longval->ob_digit[0];
45 Py_DECREF(longval);
46 #else
47@@ -143,7 +147,11 @@ zbarEnumItem_New (PyObject *byname,
48
49 /* we assume the "fast path" for a single-digit ints (see longobject.c) */
50 /* this also holds if we get a small_int preallocated long */
51+#if PY_VERSION_HEX >= 0x030900A4
52+ Py_SET_SIZE(&self->val, Py_SIZE(longval));
53+#else
54 Py_SIZE(&self->val) = Py_SIZE(longval);
55+#endif
56 self->val.ob_digit[0] = longval->ob_digit[0];
57 Py_DECREF(longval);
58
59--
602.39.1
61