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
diff --git a/python/convert.c b/python/convert.c
index 647228e..809f860 100644
--- a/python/convert.c
+++ b/python/convert.c
@@ -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 @@
   } 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;
     }
diff --git a/python/google/protobuf/internal/message_test.py b/python/google/protobuf/internal/message_test.py
index 2a723ea..3a9852b 100755
--- a/python/google/protobuf/internal/message_test.py
+++ b/python/google/protobuf/internal/message_test.py
@@ -1422,6 +1422,12 @@
       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()
diff --git a/upb/port/def.inc b/upb/port/def.inc
index 903bcaf..4c073b3 100644
--- a/upb/port/def.inc
+++ b/upb/port/def.inc
@@ -429,3 +429,14 @@
 #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
diff --git a/upb/port/undef.inc b/upb/port/undef.inc
index bdec74f..f94e276 100644
--- a/upb/port/undef.inc
+++ b/upb/port/undef.inc
@@ -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