internal change

PiperOrigin-RevId: 922907524
This commit is contained in:
Runze Wang 2026-05-28 12:16:05 -07:00 committed by Copybara-Service
parent 4576ba1c2f
commit 12baad3caa
11 changed files with 71 additions and 51 deletions

View file

@ -112,6 +112,7 @@ py_extension(
features = UPB_DEFAULT_FEATURES,
target_compatible_with = select(_message_target_compatible_with),
deps = [
":breaking_changes",
"//src/google/protobuf:descriptor_upb_reflection_proto",
"//third_party/utf8_range",
"//upb/base",

View file

@ -108,6 +108,12 @@ def build_targets(name):
"define": "allow_oversize_protos=true",
},
)
cc_library(
name = "breaking_changes",
hdrs = ["google/protobuf/breaking_changes.h"],
visibility = ["//python:__subpackages__"],
)
cc_binary(
name = "google/protobuf/pyext/_message.so",
srcs = native.glob([
@ -136,6 +142,7 @@ def build_targets(name):
],
deps = [
":proto_api",
":breaking_changes",
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf:protobuf_lite",
@ -536,6 +543,7 @@ def build_targets(name):
":python_src_files",
"README.md",
"google/__init__.py",
"google/protobuf/breaking_changes.h",
],
strip_prefix = "",
visibility = ["//python/dist:__pkg__"],
@ -554,6 +562,7 @@ def build_targets(name):
"MANIFEST.in",
"README.md",
"build_targets.bzl",
"google/protobuf/breaking_changes.h",
"google/protobuf/proto_api.h",
"google/protobuf/pyext/README",
"google/protobuf/python_protobuf.h",

View file

@ -7,6 +7,7 @@
#include "python/descriptor.h"
#include "google/protobuf/breaking_changes.h"
#include "python/convert.h"
#include "python/descriptor_containers.h"
#include "python/descriptor_pool.h"
@ -138,7 +139,7 @@ static PyObject* PyUpb_DescriptorBase_GetCached(PyObject** cached,
upb_Message_ClearFieldByDef(opts2, field);
}
#if UPB_FUTURE_FREEZE_OPTIONS
#if PROTOBUF_PY_FUTURE_FREEZE_OPTIONS
upb_Message_Freeze(opts2, opts2_layout);
#endif
*cached = PyUpb_Message_Get(opts2, m, py_arena);

View file

@ -7,11 +7,14 @@
#include "python/descriptor_containers.h"
#include "google/protobuf/breaking_changes.h"
#include "python/descriptor.h"
#include "python/protobuf.h"
#include "upb/port/def.inc"
#include "upb/reflection/def.h"
// Must be last.
#include "upb/port/def.inc"
// Implements __repr__ as str(dict(self)).
static PyObject* PyUpb_DescriptorMap_Repr(PyObject* _self) {
PyObject* dict = PyDict_New();
@ -231,7 +234,7 @@ static PyUpb_CompareResult PyUpb_GenericSequence_IsEqual(
}
if (!PyList_Check(other)) {
#if UPB_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
#if PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
return kPyUpb_CompareNotImplemented;
#else
return kPyUpb_CompareNotEqual;
@ -273,7 +276,7 @@ static PyObject* PyUpb_GenericSequence_RichCompare(PyObject* _self,
PyUpb_CompareResult eq = PyUpb_GenericSequence_IsEqual(self, other);
switch (eq) {
case kPyUpb_CompareNotImplemented:
#if UPB_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
#if PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
Py_RETURN_NOTIMPLEMENTED;
#else
UPB_UNREACHABLE(); // Unreachable when this breaking change is disabled.
@ -551,7 +554,7 @@ static PyUpb_CompareResult PyUpb_ByNameMap_IsEqual(PyUpb_ByNameMap* self,
}
if (!PyDict_Check(other)) {
#if UPB_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
#if PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
return kPyUpb_CompareNotImplemented;
#else
return kPyUpb_CompareNotEqual;
@ -579,7 +582,7 @@ static PyObject* PyUpb_ByNameMap_RichCompare(PyObject* _self, PyObject* other,
PyUpb_CompareResult eq = PyUpb_ByNameMap_IsEqual(self, other);
switch (eq) {
case kPyUpb_CompareNotImplemented:
#if UPB_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
#if PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
Py_RETURN_NOTIMPLEMENTED;
#else
UPB_UNREACHABLE(); // Unreachable when this breaking change is disabled.
@ -804,7 +807,7 @@ static PyUpb_CompareResult PyUpb_ByNumberMap_IsEqual(PyUpb_ByNumberMap* self,
}
if (!PyDict_Check(other)) {
#if UPB_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
#if PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
return kPyUpb_CompareNotImplemented;
#else
return kPyUpb_CompareNotEqual;
@ -832,7 +835,7 @@ static PyObject* PyUpb_ByNumberMap_RichCompare(PyObject* _self, PyObject* other,
PyUpb_CompareResult eq = PyUpb_ByNumberMap_IsEqual(self, other);
switch (eq) {
case kPyUpb_CompareNotImplemented:
#if UPB_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
#if PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
Py_RETURN_NOTIMPLEMENTED;
#else
UPB_UNREACHABLE(); // Unreachable when this breaking change is disabled.

View file

@ -0,0 +1,40 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef THIRD_PARTY_PY_GOOGLE_PROTOBUF_BREAKING_CHANGES_H_
#define THIRD_PARTY_PY_GOOGLE_PROTOBUF_BREAKING_CHANGES_H_
// Future versions of Protobuf Python will include breaking changes.
// This file tracks Python-specific breaking changes that need to wait,
// unlike upb-core breaking changes which can land immediately.
#ifdef PROTOBUF_PY_FUTURE_BREAKING_CHANGES
// Removes non-standard clamping behavior in RepeatedContainer.pop()
// Owner: runze@
#define PROTOBUF_PY_FUTURE_REMOVE_POP_CLAMP 1
// Fix PyProto C++ and upb implementations to return NotImplemented in
// descriptor container equality checks for unrecognized types.
// Owner: runze@
#define PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED 1
// Make GetOptions() return immutable options.
// Owner: runze@
#define PROTOBUF_PY_FUTURE_FREEZE_OPTIONS 1
#else
#define PROTOBUF_PY_FUTURE_REMOVE_POP_CLAMP 0
#define PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED 0
#define PROTOBUF_PY_FUTURE_FREEZE_OPTIONS 0
#endif
#endif // THIRD_PARTY_PY_GOOGLE_PROTOBUF_BREAKING_CHANGES_H_

View file

@ -39,6 +39,7 @@
#include "absl/strings/string_view.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/port_def.inc"
#include "google/protobuf/breaking_changes.h"
#include "google/protobuf/pyext/descriptor.h"
#include "google/protobuf/pyext/descriptor_pool.h"
#include "google/protobuf/pyext/scoped_pyobject_ptr.h"
@ -346,7 +347,7 @@ static CompareResult DescriptorSequence_Equal(PyContainer* self,
return CompareResult::kEqual;
}
#if PROTOBUF_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
#if PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
// Any other object is not implemented.
return CompareResult::kNotImplemented;
#else
@ -400,7 +401,7 @@ static CompareResult DescriptorMapping_Equal(PyContainer* self,
return CompareResult::kEqual;
}
#if PROTOBUF_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
#if PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
// Any other object is not implemented.
return CompareResult::kNotImplemented;
#else
@ -424,7 +425,7 @@ static PyObject* RichCompare(PyContainer* self, PyObject* other, int opid) {
}
switch (result) {
case CompareResult::kNotImplemented:
#if PROTOBUF_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
#if PROTOBUF_PY_FUTURE_CONTAINER_EQ_RETURNS_NOTIMPLEMENTED
Py_RETURN_NOTIMPLEMENTED;
#else
return nullptr; // Unreachable when this breaking change is disabled.

View file

@ -7,9 +7,12 @@
#include "python/repeated.h"
#include "google/protobuf/breaking_changes.h"
#include "python/convert.h"
#include "python/message.h"
#include "python/protobuf.h"
// Must be last.
#include "upb/port/def.inc"
static PyObject* PyUpb_RepeatedCompositeContainer_Append(PyObject* _self,
@ -452,10 +455,10 @@ static PyObject* PyUpb_RepeatedContainer_Pop(PyObject* _self, PyObject* args) {
if (!arr) return NULL;
size_t size = upb_Array_Size(arr);
if (index < 0) index += size;
#if UPB_FUTURE_REMOVE_POP_CLAMP
#if PROTOBUF_PY_FUTURE_REMOVE_POP_CLAMP
#else
if (index >= size) index = size - 1;
#endif // UPB_FUTURE_REMOVE_POP_CLAMP
#endif // PROTOBUF_PY_FUTURE_REMOVE_POP_CLAMP
PyObject* ret = PyUpb_RepeatedContainer_Item(_self, index);
if (!ret) return NULL;
upb_Array_Delete(self->ptr.arr, index, 1);