Skip to content

Commit

Permalink
Deprecate SupportsUnknownEnumValues on Message reflection. Use FieldD…
Browse files Browse the repository at this point in the history
…escriptor or EnumDescriptor instead.

PiperOrigin-RevId: 514618684
  • Loading branch information
mkruskal-google authored and copybara-github committed Mar 7, 2023
1 parent 51f22eb commit 0b9134b
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codespell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
with:
check_filenames: true
skip: ./.git,./third_party,./conformance/third_party,*.snk,*.pb,*.pb.cc,*.pb.h,./src/google/protobuf/testdata,./objectivec/Tests,./python/compatibility_tests/v2.5.0/tests/google/protobuf/internal,./.github/workflows/codespell.yml
ignore_words_list: "alow,alse,atleast,ba,chec,cleare,copyable,cloneable,crate,dedup,dur,errorprone,falsy,files',fo,fundementals,hel,importd,inout,leapyear,nd,nin,ois,ons,parseable,process',ro,te,testof,ue,unparseable,wasn,wee,gae,keyserver,objext,od,optin,streem,sur,falsy"
ignore_words_list: "alow,alse,atleast,ba,chec,cleare,copyable,cloneable,crate,dedup,dur,errorprone,falsy,files',fo,fundementals,hel,importd,inout,leapyear,nd,nin,ois,ons,parseable,process',ro,te,testof,ue,unparseable,wasn,wee,gae,keyserver,objext,od,optin,streem,sur,falsy,cleary"
5 changes: 4 additions & 1 deletion python/google/protobuf/pyext/map_container.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,10 @@ int MapReflectionFriend::ScalarMapSetItem(PyObject* _self, PyObject* key,
self->version++;
}

if (!PythonToMapValueRef(self, v, reflection->SupportsUnknownEnumValues(),
if (!PythonToMapValueRef(self, v,
!self->parent_field_descriptor->message_type()
->map_value()
->legacy_enum_field_treated_as_closed(),
&value)) {
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion python/google/protobuf/pyext/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2300,7 +2300,7 @@ int InternalSetNonOneofScalar(
}
case FieldDescriptor::CPPTYPE_ENUM: {
PROTOBUF_CHECK_GET_INT32(arg, value, -1);
if (reflection->SupportsUnknownEnumValues()) {
if (!field_descriptor->legacy_enum_field_treated_as_closed()) {
reflection->SetEnumValue(message, field_descriptor, value);
} else {
const EnumDescriptor* enum_descriptor = field_descriptor->enum_type();
Expand Down
4 changes: 2 additions & 2 deletions python/google/protobuf/pyext/repeated_scalar_container.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static int AssignItem(PyObject* pself, Py_ssize_t index, PyObject* arg) {
}
case FieldDescriptor::CPPTYPE_ENUM: {
PROTOBUF_CHECK_GET_INT32(arg, value, -1);
if (reflection->SupportsUnknownEnumValues()) {
if (!field_descriptor->legacy_enum_field_treated_as_closed()) {
reflection->SetRepeatedEnumValue(message, field_descriptor, index,
value);
} else {
Expand Down Expand Up @@ -376,7 +376,7 @@ PyObject* Append(RepeatedScalarContainer* self, PyObject* item) {
}
case FieldDescriptor::CPPTYPE_ENUM: {
PROTOBUF_CHECK_GET_INT32(item, value, nullptr);
if (reflection->SupportsUnknownEnumValues()) {
if (!field_descriptor->legacy_enum_field_treated_as_closed()) {
reflection->AddEnumValue(message, field_descriptor, value);
} else {
const EnumDescriptor* enum_descriptor = field_descriptor->enum_type();
Expand Down
6 changes: 1 addition & 5 deletions src/google/protobuf/generated_message_reflection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1540,10 +1540,6 @@ bool IsIndexInHasBitSet(const uint32_t* has_bit_set, uint32_t has_bit_index) {
static_cast<uint32_t>(1)) != 0;
}

bool CreateUnknownEnumValues(const FileDescriptor* file) {
return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
}

void CheckInOrder(const FieldDescriptor* field, uint32_t* last) {
*last = *last <= static_cast<uint32_t>(field->number())
? static_cast<uint32_t>(field->number())
Expand Down Expand Up @@ -2486,7 +2482,7 @@ const FieldDescriptor* Reflection::FindKnownExtensionByNumber(
}

bool Reflection::SupportsUnknownEnumValues() const {
return CreateUnknownEnumValues(descriptor_->file());
return descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3;
}

// ===================================================================
Expand Down
2 changes: 2 additions & 0 deletions src/google/protobuf/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
#include "google/protobuf/stubs/common.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/port.h"
#include "absl/base/attributes.h"
#include "absl/base/call_once.h"
#include "absl/base/casts.h"
#include "absl/functional/function_ref.h"
Expand Down Expand Up @@ -961,6 +962,7 @@ class PROTOBUF_EXPORT Reflection final {
// reflection->SetEnumValue(message, field, new_value);
// }
// }
ABSL_DEPRECATED("Use EnumDescriptor::is_closed instead.")
bool SupportsUnknownEnumValues() const;

// Returns the MessageFactory associated with this message. This can be
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/text_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ class TextFormat::Parser::ParserImpl {

if (enum_value == nullptr) {
if (int_value != kint64max &&
reflection->SupportsUnknownEnumValues()) {
!field->legacy_enum_field_treated_as_closed()) {
SET_FIELD(EnumValue, int_value);
return true;
} else if (!allow_unknown_enum_) {
Expand Down

0 comments on commit 0b9134b

Please sign in to comment.