-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `pybind11_protobuf::runtime_config::ExtensionsWithUnknownFieldsPo…
…licy`. PiperOrigin-RevId: 584687154
- Loading branch information
1 parent
8359a09
commit 220e3e0
Showing
7 changed files
with
121 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
pybind11_protobuf/disallow_extensions_with_unknown_fields.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "pybind11_protobuf/runtime_config.h" | ||
|
||
namespace pybind11_protobuf::runtime_config { | ||
namespace { | ||
|
||
static int kSetConfigDone = []() { | ||
ExtensionsWithUnknownFieldsPolicy::StrongSetDisallow(); | ||
return 0; | ||
}(); | ||
|
||
} // namespace | ||
} // namespace pybind11_protobuf::runtime_config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef PYBIND11_PROTOBUF_RUNTIME_CONFIG_H_ | ||
#define PYBIND11_PROTOBUF_RUNTIME_CONFIG_H_ | ||
|
||
namespace pybind11_protobuf::runtime_config { | ||
|
||
// For background see "Protobuf Extensions" section in README.md. | ||
class ExtensionsWithUnknownFieldsPolicy { | ||
enum State { | ||
// Initial state. | ||
kWeakDisallow, | ||
|
||
// Primary use case: PyCLIF extensions might set this when being imported. | ||
kWeakEnableFallbackToSerializeParse, | ||
|
||
// Primary use case: `:disallow_extensions_with_unknown_fields` in `deps` | ||
// of a binary (or test). | ||
kStrongDisallow | ||
}; | ||
|
||
static State& GetStateSingleton() { | ||
static State singleton = kWeakDisallow; | ||
return singleton; | ||
} | ||
|
||
public: | ||
static void WeakEnableFallbackToSerializeParse() { | ||
State& policy = GetStateSingleton(); | ||
if (policy == kWeakDisallow) { | ||
policy = kWeakEnableFallbackToSerializeParse; | ||
} | ||
} | ||
|
||
static void StrongSetDisallow() { GetStateSingleton() = kStrongDisallow; } | ||
|
||
static bool UnknownFieldsAreDisallowed() { | ||
return GetStateSingleton() != kWeakEnableFallbackToSerializeParse; | ||
} | ||
}; | ||
|
||
} // namespace pybind11_protobuf::runtime_config | ||
|
||
#endif // PYBIND11_PROTOBUF_RUNTIME_CONFIG_H_ |