Use PyImport_ImportModuleLevel() instead of PyImport_Import() for descriptor_pb2. PyImport_ImportModuleLevel() custom globals and locals pointing to some namespace we control, not normal-looking __builtins__

Change PYUPB_DESCRIPTOR_MODULE to "google.protobuf.descriptor_pb2" because protobuf python is using google.protobuf.descriptor_pb2 everywhere

Also check import result of PYUPB_DESCRIPTOR_MODULE

PiperOrigin-RevId: 565409795
pull/14088/head
Jie Luo 2 years ago committed by Copybara-Service
parent a0f7918cae
commit bc0a87423e
  1. 4
      upb/python/descriptor.c
  2. 3
      upb/python/protobuf.h

@ -112,7 +112,9 @@ static PyObject* PyUpb_DescriptorBase_GetOptions(PyUpb_DescriptorBase* self,
if (!self->options) {
// Load descriptors protos if they are not loaded already. We have to do
// this lazily, otherwise, it would lead to circular imports.
PyObject* mod = PyImport_ImportModule(PYUPB_DESCRIPTOR_MODULE);
PyObject* mod = PyImport_ImportModuleLevel(PYUPB_DESCRIPTOR_MODULE, NULL,
NULL, NULL, 0);
if (mod == NULL) return NULL;
Py_DECREF(mod);
// Find the correct options message.

@ -41,7 +41,6 @@
#define PYUPB_PROTOBUF_PUBLIC_PACKAGE "google.protobuf"
#define PYUPB_PROTOBUF_INTERNAL_PACKAGE "google.protobuf.internal"
#define PYUPB_DESCRIPTOR_PROTO_PACKAGE "google.protobuf"
#define PYUPB_DESCRIPTOR_MODULE "google.protobuf.descriptor_pb2"
#define PYUPB_MODULE_NAME "google._upb._message"
// end:github_only
@ -49,10 +48,10 @@
// #define PYUPB_PROTOBUF_PUBLIC_PACKAGE "google3.net.proto2.python.public"
// #define PYUPB_PROTOBUF_INTERNAL_PACKAGE "google3.net.proto2.python.internal"
// #define PYUPB_DESCRIPTOR_PROTO_PACKAGE "proto2"
// #define PYUPB_DESCRIPTOR_MODULE "google3.net.proto2.proto.descriptor_pb2"
// #define PYUPB_MODULE_NAME "google3.third_party.upb.python._message"
// end:google_only
#define PYUPB_DESCRIPTOR_MODULE "google.protobuf.descriptor_pb2"
#define PYUPB_RETURN_OOM return PyErr_SetNone(PyExc_MemoryError), NULL
struct PyUpb_WeakMap;

Loading…
Cancel
Save