Skip to content

Commit

Permalink
Merge pull request #9576 from acozzette/sync-stage
Browse files Browse the repository at this point in the history
Integrate from Piper for C++, Java, and Python
  • Loading branch information
acozzette authored Mar 4, 2022
2 parents bb4302e + f59a584 commit 0a246e2
Show file tree
Hide file tree
Showing 25 changed files with 192 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Unreleased Changes (C++/Java/Python/PHP/Objective-C/C#/Ruby/JavaScript)
* Add "ensure_ascii" parameter to json_format.MessageToJson. This allows smaller
JSON serializations with UTF-8 or other non-ASCII encodings.
* Added experimental support for directly assigning numpy scalars and array.
* Improve the calculation of public_dependencies in DescriptorPool.

Compiler
* Migrate IsDefault(const std::string*) and UnsafeSetDefault(const std::string*)
Expand Down
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,10 @@ python_EXTRA_DIST= \
python/google/protobuf/internal/factory_test2.proto \
python/google/protobuf/internal/file_options_test.proto \
python/google/protobuf/internal/generator_test.py \
python/google/protobuf/internal/import_test.py \
python/google/protobuf/internal/import_test_package/__init__.py \
python/google/protobuf/internal/import_test_package/import_public.proto \
python/google/protobuf/internal/import_test_package/import_public_nested.proto \
python/google/protobuf/internal/import_test_package/inner.proto \
python/google/protobuf/internal/import_test_package/outer.proto \
python/google/protobuf/internal/json_format_test.py \
Expand Down
4 changes: 0 additions & 4 deletions java/core/src/main/java/com/google/protobuf/TextFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -1554,10 +1554,6 @@ public static <T extends Message> T parse(
public static class Parser {
private int debugStringSilentMarker;

int getSilentMarkerCount() {
return debugStringSilentMarker;
}

/**
* A valid silent marker appears between a field name and its value. If there is a ":" in
* between, the silent marker will only appear after the colon. This is called after a field
Expand Down
1 change: 1 addition & 0 deletions java/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ junit_tests(
":util",
"//java/core",
"//java/core:generic_test_protos_java_proto",
"@maven//:com_google_code_gson_gson",
"@maven//:com_google_guava_guava",
"@maven//:com_google_truth_truth",
"@maven//:junit_junit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1319,11 +1319,11 @@ void merge(Reader json, Message.Builder builder) throws IOException {
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw new InvalidProtocolBufferException(e.getMessage());
throw new InvalidProtocolBufferException(e.getMessage(), e);
}
} catch (RuntimeException e) {
// We convert all exceptions from JSON parsing to our own exceptions.
throw new InvalidProtocolBufferException(e.getMessage());
throw new InvalidProtocolBufferException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static com.google.common.truth.Truth.assertWithMessage;

import com.google.common.collect.ImmutableSet;
import com.google.gson.JsonSyntaxException;
import com.google.protobuf.Any;
import com.google.protobuf.BoolValue;
import com.google.protobuf.ByteString;
Expand Down Expand Up @@ -1861,7 +1862,7 @@ public void testRecursionLimit() throws Exception {

// Test that we are not leaking out JSON exceptions.
@Test
public void testJsonException() throws Exception {
public void testJsonException_forwardsIOException() throws Exception {
InputStream throwingInputStream =
new InputStream() {
@Override
Expand All @@ -1879,7 +1880,10 @@ public int read() throws IOException {
} catch (IOException e) {
assertThat(e).hasMessageThat().isEqualTo("12345");
}
}

@Test
public void testJsonException_forwardsJsonException() throws Exception {
Reader invalidJsonReader = new StringReader("{ xxx - yyy }");
// When the JSON parser throws parser exceptions, JsonFormat should turn
// that into InvalidProtocolBufferException.
Expand All @@ -1888,7 +1892,7 @@ public int read() throws IOException {
JsonFormat.parser().merge(invalidJsonReader, builder);
assertWithMessage("Exception is expected.").fail();
} catch (InvalidProtocolBufferException e) {
// Expected.
assertThat(e.getCause()).isInstanceOf(JsonSyntaxException.class);
}
}

Expand Down
14 changes: 9 additions & 5 deletions python/google/protobuf/descriptor_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,21 +1235,25 @@ def _ExtractSymbols(self, descriptors):
for enum in desc.enum_types:
yield (_PrefixWithDot(enum.full_name), enum)

def _GetDeps(self, dependencies):
def _GetDeps(self, dependencies, visited=None):
"""Recursively finds dependencies for file protos.
Args:
dependencies: The names of the files being depended on.
visited: The names of files already found.
Yields:
Each direct and indirect dependency.
"""

visited = visited or set()
for dependency in dependencies:
dep_desc = self.FindFileByName(dependency)
yield dep_desc
for parent_dep in dep_desc.dependencies:
yield parent_dep
if dependency not in visited:
visited.add(dependency)
dep_desc = self.FindFileByName(dependency)
yield dep_desc
public_files = [d.name for d in dep_desc.public_dependencies]
yield from self._GetDeps(public_files, visited)

def _GetTypeFromScope(self, package, type_name, scope):
"""Finds a given type name in the current scope.
Expand Down
49 changes: 49 additions & 0 deletions python/google/protobuf/internal/import_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# Protocol Buffers - Google's data interchange format
# Copyright 2008 Google Inc. All rights reserved.
# https://developers.google.com/protocol-buffers/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Unittest for nested public imports."""

import unittest

from google.protobuf.internal.import_test_package import outer_pb2


class ImportTest(unittest.TestCase):

def testPackageInitializationImport(self):
"""Test that we can import nested import public messages."""

msg = outer_pb2.Outer()
self.assertEqual(58, msg.import_public_nested.value)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// A proto file which is imported by inner.proto to test public importing.

syntax = "proto2";

package google.protobuf.python.internal.import_test_package;

option optimize_for = SPEED;

// Test nested public import
import public "google/protobuf/internal/import_test_package/import_public_nested.proto";
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// A proto file which is imported by import_public.proto to test nested public
// importing.

syntax = "proto2";

package google.protobuf.python.internal.import_test_package;

message ImportPublicNestedMessage {
optional int32 value = 1 [default = 58];
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ syntax = "proto2";

package google.protobuf.python.internal.import_test_package;

// Test public import
import public "google/protobuf/internal/import_test_package/import_public.proto";

message Inner {
optional int32 value = 1 [default = 57];
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ import "google/protobuf/internal/import_test_package/inner.proto";

message Outer {
optional Inner inner = 1;
optional ImportPublicNestedMessage import_public_nested = 2;
}
2 changes: 2 additions & 0 deletions python/google/protobuf/pyext/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ bool CheckAndGetInteger(PyObject* arg, T* value) {
if (std::numeric_limits<T>::min() == 0) {
// Unsigned case.
unsigned PY_LONG_LONG ulong_result = PyLong_AsUnsignedLongLong(arg_py_int);
Py_DECREF(arg_py_int);
if (VerifyIntegerCastAndRange<T, unsigned PY_LONG_LONG>(arg,
ulong_result)) {
*value = static_cast<T>(ulong_result);
Expand All @@ -601,6 +602,7 @@ bool CheckAndGetInteger(PyObject* arg, T* value) {
}
} else {
// Signed case.
Py_DECREF(arg_py_int);
PY_LONG_LONG long_result = PyLong_AsLongLong(arg);
if (VerifyIntegerCastAndRange<T, PY_LONG_LONG>(arg, long_result)) {
*value = static_cast<T>(long_result);
Expand Down
2 changes: 2 additions & 0 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def GenerateUnittestProtos():
GenProto('google/protobuf/internal/factory_test1.proto', False)
GenProto('google/protobuf/internal/factory_test2.proto', False)
GenProto('google/protobuf/internal/file_options_test.proto', False)
GenProto('google/protobuf/internal/import_test_package/import_public.proto', False)
GenProto('google/protobuf/internal/import_test_package/import_public_nested.proto', False)
GenProto('google/protobuf/internal/import_test_package/inner.proto', False)
GenProto('google/protobuf/internal/import_test_package/outer.proto', False)
GenProto('google/protobuf/internal/missing_enum_values.proto', False)
Expand Down
4 changes: 4 additions & 0 deletions src/google/protobuf/compiler/cpp/cpp_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ inline std::string MakeVarintCachedSizeFieldName(const FieldDescriptor* field) {
return StrCat("_", FieldName(field), "_cached_byte_size_");
}

// Note: A lot of libraries detect Any protos based on Descriptor::full_name()
// while the two functions below use FileDescriptor::name(). In a sane world the
// two approaches should be equivalent. But if you are dealing with descriptors
// from untrusted sources, you might need to match semantics across libraries.
bool IsAnyMessage(const FileDescriptor* descriptor, const Options& options);
bool IsAnyMessage(const Descriptor* descriptor, const Options& options);

Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/compiler/cpp/cpp_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ class MessageGenerator {
int max_has_bit_index_;

// A map from field index to inlined_string index. For non-inlined-string
// fields, the element is -1.
// fields, the element is -1. If there is no inlined string in the message,
// this is empty.
std::vector<int> inlined_string_indices_;
// The count of inlined_string fields in the message.
int max_inlined_string_index_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,9 @@ TailCallTableInfo::TailCallTableInfo(
"_fl::Offset{offsetof(", ClassName(descriptor), ", _oneof_case_)}"));
}

int inlined_string_count = 0;
for (const FieldDescriptor* field : ordered_fields) {
if (IsString(field, options) && IsStringInlined(field, options)) {
++inlined_string_count;
}
}
// If this message has any inlined string fields, store the donation state
// offset in the second auxiliary entry.
if (inlined_string_count > 0) {
if (!inlined_string_indices.empty()) {
aux_entries.resize(2); // pad if necessary
aux_entries[1] =
StrCat("_fl::Offset{offsetof(", ClassName(descriptor),
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <string>
#include <utility>
#include <vector>

#include <google/protobuf/compiler/parser.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor_database.h>
Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/compiler/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
#include <google/protobuf/stubs/casts.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/io/tokenizer.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/wire_format.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/tokenizer.h>
#include <google/protobuf/wire_format.h>
#include <google/protobuf/stubs/map_util.h>
#include <google/protobuf/stubs/hash.h>

Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
#include <string>
#include <utility>

#include <google/protobuf/io/tokenizer.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/repeated_field.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/tokenizer.h>
#include <google/protobuf/repeated_field.h>

// Must be included last.
#include <google/protobuf/port_def.inc>
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/extension_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class PROTOBUF_EXPORT ExtensionSet {
public:
constexpr ExtensionSet();
explicit ExtensionSet(Arena* arena);
ExtensionSet(ArenaInitialized, Arena* arena) : ExtensionSet(arena) {}
~ExtensionSet();

// These are called at startup by protocol-compiler-generated code to
Expand Down
6 changes: 4 additions & 2 deletions src/google/protobuf/generated_message_tctable_lite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <google/protobuf/extension_set.h>
#include <google/protobuf/generated_message_tctable_decl.h>
#include <google/protobuf/generated_message_tctable_impl.h>
#include <google/protobuf/inlined_string_field.h>
#include <google/protobuf/message_lite.h>
#include <google/protobuf/parse_context.h>
#include <google/protobuf/wire_format_lite.h>
Expand Down Expand Up @@ -1698,8 +1699,9 @@ const char* TcParser::MpString(PROTOBUF_TC_PARAM_DECL) {
break;
}

case field_layout::kRepIString:
break; // note: skipped above
case field_layout::kRepIString: {
break;
}
}

if (ptr == nullptr || !is_valid) {
Expand Down
Loading

0 comments on commit 0a246e2

Please sign in to comment.