Skip to content

Commit a014dbb

Browse files
habermancopybara-github
authored andcommitted
Removed old Python bug workaround now that we have dropped Python <3.8.
PiperOrigin-RevId: 758825465
1 parent 6193a8c commit a014dbb

File tree

2 files changed

+5
-21
lines changed

2 files changed

+5
-21
lines changed

python/message.c

+2-19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "python/descriptor.h"
1212
#include "python/extension_dict.h"
1313
#include "python/map.h"
14+
#include "python/protobuf.h"
1415
#include "python/repeated.h"
1516
#include "upb/base/string_view.h"
1617
#include "upb/message/compare.h"
@@ -45,12 +46,6 @@ typedef struct {
4546
size_t type_basicsize; // sizeof(PyHeapTypeObject)
4647
traverseproc type_traverse; // PyTypeObject.tp_traverse
4748
inquiry type_clear; // PyTypeObject.tp_clear
48-
49-
// While we can refer to PY_VERSION_HEX in the limited API, this will give us
50-
// the version of Python we were compiled against, which may be different
51-
// than the version we are dynamically linked against. Here we want the
52-
// version that is actually running in this process.
53-
long python_version_hex; // PY_VERSION_HEX
5449
} PyUpb_CPythonBits;
5550

5651
// A global containing the values for this process.
@@ -144,7 +139,6 @@ static bool PyUpb_CPythonBits_Init(PyUpb_CPythonBits* bits) {
144139

145140
sys = PyImport_ImportModule("sys");
146141
hex_version = PyObject_GetAttrString(sys, "hexversion");
147-
bits->python_version_hex = PyLong_AsLong(hex_version);
148142
ret = true;
149143

150144
err:
@@ -836,18 +830,7 @@ static void PyUpb_Message_Dealloc(PyObject* _self) {
836830
}
837831

838832
Py_DECREF(self->arena);
839-
840-
// We do not use PyUpb_Dealloc() here because Message is a base type and for
841-
// base types there is a bug we have to work around in this case (see below).
842-
PyTypeObject* tp = Py_TYPE(self);
843-
freefunc tp_free = PyType_GetSlot(tp, Py_tp_free);
844-
tp_free(self);
845-
846-
if (cpython_bits.python_version_hex >= 0x03080000) {
847-
// Prior to Python 3.8 there is a bug where deallocating the type here would
848-
// lead to a double-decref: https://bugs.python.org/issue37879
849-
Py_DECREF(tp);
850-
}
833+
PyUpb_Dealloc(self);
851834
}
852835

853836
PyObject* PyUpb_Message_Get(upb_Message* u_msg, const upb_MessageDef* m,

python/protobuf.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#ifndef PYUPB_PROTOBUF_H__
99
#define PYUPB_PROTOBUF_H__
1010

11+
#include <assert.h>
1112
#include <stdbool.h>
1213

1314
#include "python/descriptor.h"
1415
#include "python/python_api.h"
1516
#include "upb/hash/int_table.h"
17+
#include "upb/reflection/def.h"
1618

1719
#define PYUPB_PROTOBUF_PUBLIC_PACKAGE "google.protobuf"
1820
#define PYUPB_PROTOBUF_INTERNAL_PACKAGE "google.protobuf.internal"
@@ -183,8 +185,7 @@ PyObject* PyUpb_Forbidden_New(PyObject* cls, PyObject* args, PyObject* kwds);
183185

184186
// Our standard dealloc func. It follows the guidance defined in:
185187
// https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
186-
// However it tests Py_TPFLAGS_HEAPTYPE dynamically so that a single dealloc
187-
// function can work for any type.
188+
// It requires that the type is a heap type, which all of our types are.
188189
static inline void PyUpb_Dealloc(void* self) {
189190
PyTypeObject* tp = Py_TYPE(self);
190191
assert(PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE);

0 commit comments

Comments
 (0)