Setup fix for python/upb for the enforcement of closed enums in editions.

This will be a breaking change in the 30.0 release.

PiperOrigin-RevId: 676683430
pull/18354/head
Mike Kruskal 2 months ago committed by Copybara-Service
parent 7d3e80ce9c
commit ce6dbe478a
  1. 5
      python/convert.c
  2. 6
      python/google/protobuf/internal/message_test.py
  3. 11
      upb/port/def.inc
  4. 2
      upb/port/undef.inc

@ -11,6 +11,7 @@
#include "python/protobuf.h"
#include "upb/message/compare.h"
#include "upb/message/map.h"
#include "upb/reflection/def.h"
#include "upb/reflection/message.h"
#include "utf8_range.h"
@ -179,8 +180,12 @@ static bool PyUpb_PyToUpbEnum(PyObject* obj, const upb_EnumDef* e,
} else {
int32_t i32;
if (!PyUpb_GetInt32(obj, &i32)) return false;
#ifdef UPB_FUTURE_PYTHON_CLOSED_ENUM_ENFORCEMENT
if (upb_EnumDef_IsClosed(e) && !upb_EnumDef_CheckNumber(e, i32)) {
#else
if (upb_FileDef_Syntax(upb_EnumDef_File(e)) == kUpb_Syntax_Proto2 &&
!upb_EnumDef_CheckNumber(e, i32)) {
#endif
PyErr_Format(PyExc_ValueError, "invalid enumerator %d", (int)i32);
return false;
}

@ -1422,6 +1422,12 @@ class Proto2Test(unittest.TestCase):
m.repeated_nested_enum[0] = 2
with self.assertRaises(ValueError):
m.repeated_nested_enum[0] = 123456
else:
m.optional_nested_enum = 1234567
m.repeated_nested_enum.append(1234567)
m.repeated_nested_enum.append(2)
m.repeated_nested_enum[0] = 2
m.repeated_nested_enum[0] = 123456
# Unknown enum value can be parsed but is ignored.
m2 = unittest_proto3_arena_pb2.TestAllTypes()

@ -429,3 +429,14 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#define UPB_LINKARR_APPEND(name)
#endif
// Future versions of upb will include breaking changes to some APIs.
// This macro can be set to enable these API changes ahead of time, so that
// user code can be updated before upgrading versions of protobuf.
#ifdef UPB_FUTURE_BREAKING_CHANGES
// Properly enforce closed enums in python.
// Owner: mkruskal@
#define UPB_FUTURE_PYTHON_CLOSED_ENUM_ENFORCEMENT 1
#endif

@ -61,3 +61,5 @@
#undef UPB_LINKARR_APPEND
#undef UPB_LINKARR_START
#undef UPB_LINKARR_STOP
#undef UPB_FUTURE_BREAKING_CHANGES
#undef UPB_FUTURE_PYTHON_CLOSED_ENUM_ENFORCEMENT

Loading…
Cancel
Save