Skip to content

Commit

Permalink
Down integrate to GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Hao Nguyen committed May 17, 2019
1 parent 5f3f3d6 commit 6654023
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 94 deletions.
4 changes: 2 additions & 2 deletions benchmarks/python/py_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@


def run_one_test(filename):
data = open(filename).read()
data = open(filename, "rb").read()
benchmark_dataset = benchmarks_pb2.BenchmarkDataset()
benchmark_dataset.ParseFromString(data)
total_bytes = 0
Expand Down Expand Up @@ -69,7 +69,7 @@ def init(filename):
message_list=[]
counter = 0
total_bytes = 0
data = open(filename).read()
data = open(filename, "rb").read()
benchmark_dataset = benchmarks_pb2.BenchmarkDataset()
benchmark_dataset.ParseFromString(data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ package protobuf_unittest;
import "google/protobuf/unittest.proto";
import "google/protobuf/descriptor.proto";

option java_generic_services = true; // auto-added
option java_generic_services = true; // auto-added
option java_multiple_files = true;
option java_outer_classname = "MultipleFilesTestProto";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ syntax = "proto2";
// See: http://go/proto2-generic-services-default
package io_protocol_tests;

option java_generic_services = true; // auto-added
option java_generic_services = true; // auto-added
option java_package = "com.google.protobuf";
option java_outer_classname = "TestBadIdentifiersProto";

Expand Down
1 change: 1 addition & 0 deletions protobuf.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ Description: Google's Data Interchange Format
Version: @VERSION@
Libs: -L${libdir} -lprotobuf @PTHREAD_LIBS@
Libs.private: @LIBS@

Cflags: -I${includedir} @PTHREAD_CFLAGS@
Conflicts: protobuf-lite
24 changes: 13 additions & 11 deletions python/google/protobuf/descriptor_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def _IsMessageSetExtension(field):
field.label == descriptor.FieldDescriptor.LABEL_OPTIONAL)




class DescriptorPool(object):
"""A collection of protobufs dynamically constructed by descriptor protos."""

Expand Down Expand Up @@ -161,18 +163,18 @@ def _CheckConflictRegister(self, desc, desc_name, file_name):

if not isinstance(desc, descriptor_type) or (
old_file != file_name):
warn_msg = ('Conflict register for file "' + file_name +
'": ' + desc_name +
' is already defined in file "' +
old_file + '". Please fix the conflict by adding '
'package name on the proto file, or use different '
'name for the duplication. This warning will '
'turn into error soon.')
error_msg = ('Conflict register for file "' + file_name +
'": ' + desc_name +
' is already defined in file "' +
old_file + '". Please fix the conflict by adding '
'package name on the proto file, or use different '
'name for the duplication.')
if isinstance(desc, descriptor.EnumValueDescriptor):
warn_msg += ('\nNote: enum values appear as '
'siblings of the enum type instead of '
'children of it.')
warnings.warn(warn_msg, RuntimeWarning)
error_msg += ('\nNote: enum values appear as '
'siblings of the enum type instead of '
'children of it.')

raise TypeError(error_msg)

return

Expand Down
5 changes: 2 additions & 3 deletions python/google/protobuf/internal/descriptor_database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,18 @@ def testConflictRegister(self):
db.Add(unittest_fd)
conflict_fd = descriptor_pb2.FileDescriptorProto.FromString(
unittest_pb2.DESCRIPTOR.serialized_pb)
conflict_fd.name = 'other_file'
conflict_fd.name = 'other_file2'
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter('always')
db.Add(conflict_fd)
self.assertTrue(len(w))
self.assertIs(w[0].category, RuntimeWarning)
self.assertIn('Conflict register for file "other_file": ',
self.assertIn('Conflict register for file "other_file2": ',
str(w[0].message))
self.assertIn('already defined in file '
'"google/protobuf/unittest.proto"',
str(w[0].message))


if __name__ == '__main__':
unittest.main()
41 changes: 14 additions & 27 deletions python/google/protobuf/internal/descriptor_pool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

import copy
import os
import warnings

try:
import unittest2 as unittest #PY26
Expand Down Expand Up @@ -532,33 +531,21 @@ def testConflictRegister(self):
if api_implementation.Type() == 'cpp':
pass
else:
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter('always')
pool = copy.deepcopy(self.pool)
# No warnings to add the same descriptors.
file_descriptor = unittest_pb2.DESCRIPTOR
pool.AddDescriptor(
file_descriptor.message_types_by_name['TestAllTypes'])
pool.AddEnumDescriptor(
file_descriptor.enum_types_by_name['ForeignEnum'])
pool.AddServiceDescriptor(
file_descriptor.services_by_name['TestService'])
pool.AddExtensionDescriptor(
file_descriptor.extensions_by_name['optional_int32_extension'])
self.assertEqual(len(w), 0)
# Check warnings for conflict descriptors with the same name.
pool.Add(unittest_fd)
pool.Add(conflict_fd)
pool.FindFileByName(unittest_fd.name)
pool = copy.deepcopy(self.pool)
file_descriptor = unittest_pb2.DESCRIPTOR
pool.AddDescriptor(
file_descriptor.message_types_by_name['TestAllTypes'])
pool.AddEnumDescriptor(
file_descriptor.enum_types_by_name['ForeignEnum'])
pool.AddServiceDescriptor(
file_descriptor.services_by_name['TestService'])
pool.AddExtensionDescriptor(
file_descriptor.extensions_by_name['optional_int32_extension'])
pool.Add(unittest_fd)
pool.Add(conflict_fd)
pool.FindFileByName(unittest_fd.name)
with self.assertRaises(TypeError):
pool.FindFileByName(conflict_fd.name)
self.assertTrue(len(w))
self.assertIs(w[0].category, RuntimeWarning)
self.assertIn('Conflict register for file "other_file": ',
str(w[0].message))
self.assertIn('already defined in file '
'"google/protobuf/unittest.proto"',
str(w[0].message))


@testing_refleaks.TestCase
Expand Down
6 changes: 5 additions & 1 deletion python/google/protobuf/internal/python_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,11 @@ def field_setter(self, new_value):
# pylint: disable=protected-access
# Testing the value for truthiness captures all of the proto3 defaults
# (0, 0.0, enum 0, and False).
new_value = type_checker.CheckValue(new_value)
try:
new_value = type_checker.CheckValue(new_value)
except TypeError as e:
raise TypeError(
'Cannot set %s to %.1024r: %s' % (field.full_name, new_value, e))
if clear_when_set_to_default and not new_value:
self._fields.pop(field, None)
else:
Expand Down
14 changes: 9 additions & 5 deletions src/google/protobuf/compiler/js/js_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2426,18 +2426,22 @@ void Generator::GenerateObjectTypedef(const GeneratorOptions& options,
"method.\n"
" * @record\n"
" */\n"
"$typeName$ = function() {};\n\n",
"$typeName$ = function() {\n",
"messageName", desc->name(), "typeName", type_name);

for (int i = 0; i < desc->field_count(); i++) {
if (i > 0) {
printer->Print("\n");
}
printer->Print(
"/** @type {$fieldType$|undefined} */\n"
"$typeName$.prototype.$fieldName$;\n\n",
"typeName", type_name, "fieldName",
JSObjectFieldName(options, desc->field(i)),
" /** @type {$fieldType$|undefined} */\n"
" this.$fieldName$;\n",
"fieldName", JSObjectFieldName(options, desc->field(i)),
// TODO(b/121097361): Add type checking for field values.
"fieldType", "?");
}

printer->Print("};\n\n");
}

void Generator::GenerateClassFromObject(const GeneratorOptions& options,
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,7 @@ const MethodDescriptor* DescriptorPool::FindMethodByName(

const FieldDescriptor* DescriptorPool::FindExtensionByNumber(
const Descriptor* extendee, int number) const {
if (extendee->extension_range_count() == 0) return nullptr;
// A faster path to reduce lock contention in finding extensions, assuming
// most extensions will be cache hit.
if (mutex_ != nullptr) {
Expand Down
18 changes: 11 additions & 7 deletions src/google/protobuf/descriptor_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddFile(

for (int i = 0; i < file.message_type_size(); i++) {
if (!AddSymbol(path + file.message_type(i).name(), value)) return false;
if (!AddNestedExtensions(file.message_type(i), value)) return false;
if (!AddNestedExtensions(file.name(), file.message_type(i), value))
return false;
}
for (int i = 0; i < file.enum_type_size(); i++) {
if (!AddSymbol(path + file.enum_type(i).name(), value)) return false;
}
for (int i = 0; i < file.extension_size(); i++) {
if (!AddSymbol(path + file.extension(i).name(), value)) return false;
if (!AddExtension(file.extension(i), value)) return false;
if (!AddExtension(file.name(), file.extension(i), value)) return false;
}
for (int i = 0; i < file.service_size(); i++) {
if (!AddSymbol(path + file.service(i).name(), value)) return false;
Expand Down Expand Up @@ -203,19 +204,22 @@ bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddSymbol(

template <typename Value>
bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddNestedExtensions(
const DescriptorProto& message_type, Value value) {
const std::string& filename, const DescriptorProto& message_type,
Value value) {
for (int i = 0; i < message_type.nested_type_size(); i++) {
if (!AddNestedExtensions(message_type.nested_type(i), value)) return false;
if (!AddNestedExtensions(filename, message_type.nested_type(i), value))
return false;
}
for (int i = 0; i < message_type.extension_size(); i++) {
if (!AddExtension(message_type.extension(i), value)) return false;
if (!AddExtension(filename, message_type.extension(i), value)) return false;
}
return true;
}

template <typename Value>
bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddExtension(
const FieldDescriptorProto& field, Value value) {
const std::string& filename, const FieldDescriptorProto& field,
Value value) {
if (!field.extendee().empty() && field.extendee()[0] == '.') {
// The extension is fully-qualified. We can use it as a lookup key in
// the by_symbol_ table.
Expand All @@ -226,7 +230,7 @@ bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddExtension(
GOOGLE_LOG(ERROR) << "Extension conflicts with extension already in database: "
"extend "
<< field.extendee() << " { " << field.name() << " = "
<< field.number() << " }";
<< field.number() << " } from:" << filename;
return false;
}
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/google/protobuf/descriptor_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ class PROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase {
// to the index.
bool AddFile(const FileDescriptorProto& file, Value value);
bool AddSymbol(const std::string& name, Value value);
bool AddNestedExtensions(const DescriptorProto& message_type, Value value);
bool AddExtension(const FieldDescriptorProto& field, Value value);
bool AddNestedExtensions(const std::string& filename,
const DescriptorProto& message_type, Value value);
bool AddExtension(const std::string& filename,
const FieldDescriptorProto& field, Value value);

Value FindFile(const std::string& filename);
Value FindSymbol(const std::string& name);
Expand Down
Loading

0 comments on commit 6654023

Please sign in to comment.