├── proto ├── __init__.py ├── game_service.proto └── game_service_pb2.py ├── .gitignore ├── google ├── protobuf │ ├── __init__.py │ ├── pyext │ │ ├── __init__.py │ │ ├── README │ │ ├── proto2_api_test.proto │ │ ├── python_protobuf.h │ │ ├── message_factory_cpp2_test.py │ │ ├── descriptor_cpp2_test.py │ │ ├── cpp_message.py │ │ ├── python.proto │ │ ├── scoped_pyobject_ptr.h │ │ ├── descriptor.h │ │ ├── reflection_cpp2_generated_test.py │ │ ├── repeated_scalar_container.h │ │ ├── extension_dict.h │ │ └── repeated_composite_container.h │ ├── compiler │ │ ├── __init__.py │ │ └── plugin_pb2.py │ ├── internal │ │ ├── __init__.py │ │ ├── decoder.pyc │ │ ├── encoder.pyc │ │ ├── __init__.pyc │ │ ├── containers.pyc │ │ ├── wire_format.pyc │ │ ├── python_message.pyc │ │ ├── type_checkers.pyc │ │ ├── enum_type_wrapper.pyc │ │ ├── message_listener.pyc │ │ ├── api_implementation.pyc │ │ ├── missing_enum_values.proto │ │ ├── more_extensions_dynamic.proto │ │ ├── more_extensions.proto │ │ ├── test_bad_identifiers.proto │ │ ├── more_messages.proto │ │ ├── factory_test1.proto │ │ ├── message_python_test.py │ │ ├── descriptor_python_test.py │ │ ├── message_factory_python_test.py │ │ ├── descriptor_pool_test2.proto │ │ ├── api_implementation_default_test.py │ │ ├── text_encoding_test.py │ │ ├── descriptor_database_test.py │ │ ├── descriptor_pool_test1.proto │ │ ├── message_listener.py │ │ ├── factory_test2.proto │ │ ├── enum_type_wrapper.py │ │ ├── api_implementation.py │ │ ├── api_implementation.cc │ │ ├── symbol_database_test.py │ │ ├── service_reflection_test.py │ │ ├── message_factory_test.py │ │ ├── wire_format.py │ │ ├── unknown_fields_test.py │ │ └── containers.py │ ├── __init__.pyc │ ├── message.pyc │ ├── service.pyc │ ├── descriptor.pyc │ ├── reflection.pyc │ ├── text_format.pyc │ ├── descriptor_pb2.pyc │ ├── text_encoding.pyc │ ├── descriptor_pool.pyc │ ├── symbol_database.pyc │ ├── descriptor_database.pyc │ ├── service_reflection.pyc │ ├── text_encoding.py │ ├── descriptor_database.py │ ├── symbol_database.py │ ├── message_factory.py │ ├── reflection.py │ └── service.py ├── __init__.py └── __init__.pyc ├── rpc ├── __init__.py ├── __init__.pyc ├── rpc_controller.py ├── tcp_server.py ├── rpc_channel.py ├── tcp_client.py └── tcp_connection.py ├── test ├── __init__.py ├── echo_client.py └── echo_server.py ├── __init__.py ├── README.md └── logger.py /proto/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.pyc 3 | -------------------------------------------------------------------------------- /google/protobuf/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /rpc/__init__.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 -------------------------------------------------------------------------------- /google/protobuf/pyext/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/__init__.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 -------------------------------------------------------------------------------- /google/protobuf/compiler/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /google/protobuf/internal/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # 最终往上层目录挪一层 -------------------------------------------------------------------------------- /google/__init__.py: -------------------------------------------------------------------------------- 1 | __import__('pkg_resources').declare_namespace(__name__) 2 | -------------------------------------------------------------------------------- /rpc/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/rpc/__init__.pyc -------------------------------------------------------------------------------- /google/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/__init__.pyc -------------------------------------------------------------------------------- /google/protobuf/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/__init__.pyc -------------------------------------------------------------------------------- /google/protobuf/message.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/message.pyc -------------------------------------------------------------------------------- /google/protobuf/service.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/service.pyc -------------------------------------------------------------------------------- /google/protobuf/descriptor.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/descriptor.pyc -------------------------------------------------------------------------------- /google/protobuf/reflection.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/reflection.pyc -------------------------------------------------------------------------------- /google/protobuf/text_format.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/text_format.pyc -------------------------------------------------------------------------------- /google/protobuf/descriptor_pb2.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/descriptor_pb2.pyc -------------------------------------------------------------------------------- /google/protobuf/text_encoding.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/text_encoding.pyc -------------------------------------------------------------------------------- /google/protobuf/descriptor_pool.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/descriptor_pool.pyc -------------------------------------------------------------------------------- /google/protobuf/internal/decoder.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/decoder.pyc -------------------------------------------------------------------------------- /google/protobuf/internal/encoder.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/encoder.pyc -------------------------------------------------------------------------------- /google/protobuf/symbol_database.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/symbol_database.pyc -------------------------------------------------------------------------------- /google/protobuf/descriptor_database.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/descriptor_database.pyc -------------------------------------------------------------------------------- /google/protobuf/internal/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/__init__.pyc -------------------------------------------------------------------------------- /google/protobuf/internal/containers.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/containers.pyc -------------------------------------------------------------------------------- /google/protobuf/service_reflection.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/service_reflection.pyc -------------------------------------------------------------------------------- /google/protobuf/internal/wire_format.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/wire_format.pyc -------------------------------------------------------------------------------- /google/protobuf/internal/python_message.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/python_message.pyc -------------------------------------------------------------------------------- /google/protobuf/internal/type_checkers.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/type_checkers.pyc -------------------------------------------------------------------------------- /google/protobuf/internal/enum_type_wrapper.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/enum_type_wrapper.pyc -------------------------------------------------------------------------------- /google/protobuf/internal/message_listener.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/message_listener.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # protobuf-rpc-echo 2 | a simple rpc echo demo use python and protobuf. 3 | 4 | csdn:http://blog.csdn.net/majianfei1023/article/details/71628784 5 | -------------------------------------------------------------------------------- /google/protobuf/internal/api_implementation.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techxiaofei/protobuf-rpc-echo/HEAD/google/protobuf/internal/api_implementation.pyc -------------------------------------------------------------------------------- /google/protobuf/pyext/README: -------------------------------------------------------------------------------- 1 | This is the 'v2' C++ implementation for python proto2. 2 | 3 | It is active when: 4 | 5 | PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp 6 | PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=2 7 | -------------------------------------------------------------------------------- /rpc/rpc_controller.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | from google.protobuf import service 3 | 4 | 5 | class RpcController(service.RpcController): 6 | 7 | def __init__(self, rpc_channel): 8 | self.rpc_channel = rpc_channel 9 | -------------------------------------------------------------------------------- /logger.py: -------------------------------------------------------------------------------- 1 | 2 | import logging 3 | 4 | __logger_table = {} 5 | 6 | 7 | def get_logger(name): 8 | if name in __logger_table: 9 | return __logger_table[name] 10 | 11 | logger = logging.getLogger(name) 12 | logger.setLevel(logging.DEBUG) 13 | console_handler = logging.StreamHandler() 14 | logger.addHandler(console_handler) 15 | __logger_table[name] = logger 16 | return logger 17 | -------------------------------------------------------------------------------- /proto/game_service.proto: -------------------------------------------------------------------------------- 1 | package pluto; 2 | 3 | option py_generic_services = true; 4 | 5 | message Void {} 6 | 7 | message RequestMessage 8 | { 9 | required string msg = 1; 10 | } 11 | 12 | message ResponseMessage 13 | { 14 | required string msg = 1; 15 | } 16 | 17 | //客户端发给服务器 18 | service IEchoService 19 | { 20 | rpc echo(RequestMessage) returns(Void); 21 | } 22 | 23 | //服务器发给客户端 24 | service IEchoClient 25 | { 26 | rpc echo_reply(ResponseMessage) returns(Void); 27 | } 28 | -------------------------------------------------------------------------------- /test/echo_client.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | import sys 3 | sys.path.append('../') 4 | 5 | from proto.game_service_pb2 import IEchoService_Stub,IEchoClient,RequestMessage 6 | from rpc.tcp_client import TcpClient 7 | import asyncore 8 | 9 | LISTEN_IP = "127.0.0.1" 10 | LISTEN_PORT = 1888 11 | 12 | # 被调用方,接收调用方(stub)的rpc请求 13 | class MyEchoClientReply(IEchoClient): 14 | def echo_reply(self, rpc_controller, request, done): 15 | print "MyEchoClientReply:%s"%request.msg 16 | 17 | 18 | if __name__ == "__main__": 19 | request = RequestMessage() 20 | request.msg = "just for test" 21 | 22 | client = TcpClient(LISTEN_IP, LISTEN_PORT, IEchoService_Stub, MyEchoClientReply) 23 | client.sync_connect() 24 | 25 | client.stub.echo(None, request, None) 26 | 27 | request2 = RequestMessage() 28 | request2.msg = "just for test2" 29 | client.stub.echo(None, request2, None) 30 | asyncore.loop() -------------------------------------------------------------------------------- /test/echo_server.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | import sys 3 | sys.path.append('../') 4 | 5 | import asyncore 6 | from proto.game_service_pb2 import IEchoClient_Stub, IEchoService,ResponseMessage 7 | from rpc.tcp_server import TcpServer 8 | import logger 9 | 10 | LISTEN_IP = "127.0.0.1" 11 | LISTEN_PORT = 1888 12 | 13 | 14 | # 被调用方的Service要自己实现具体的rpc处理逻辑 15 | class MyEchoService(IEchoService): 16 | 17 | def echo(self, controller, request, done): 18 | 19 | rpc_channel = controller.rpc_channel 20 | msg = request.msg 21 | 22 | response = ResponseMessage() 23 | response.msg = "echo:"+msg 24 | 25 | print "response.msg", response.msg 26 | 27 | # 此时,服务器是调用方,就调用stub.rpc,客户端时被调用方,实现rpc方法。 28 | client_stub = IEchoClient_Stub(rpc_channel) 29 | client_stub.echo_reply(controller, response, None) 30 | 31 | 32 | # 主服务器 33 | class GameServer(): 34 | def __init__(self): 35 | self.logger = logger.get_logger("GameServer") 36 | self.server = None 37 | 38 | def run(self): 39 | self.server = TcpServer(LISTEN_IP, LISTEN_PORT, MyEchoService) 40 | 41 | 42 | if __name__ == "__main__": 43 | gameserver = GameServer() 44 | gameserver.run() 45 | while True: 46 | asyncore.loop() 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /rpc/tcp_server.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | import socket 3 | import asyncore 4 | 5 | from rpc.tcp_connection import TcpConnection 6 | from rpc.rpc_channel import RpcChannel 7 | import logger 8 | 9 | 10 | class TcpServer(asyncore.dispatcher): 11 | '''负责accept链接,并建立一条TcpConnection通道''' 12 | def __init__(self, ip, port, service_factory): 13 | asyncore.dispatcher.__init__(self) 14 | self.logger = logger.get_logger('TcpServer') 15 | self.ip = ip 16 | self.port = port 17 | self.service_factory = service_factory 18 | 19 | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) 20 | self.set_reuse_addr() 21 | self.bind((self.ip, self.port)) 22 | self.listen(50) 23 | self.logger.info('Server Listening on: ' + str((self.ip, self.port))) 24 | 25 | def handle_accept(self): 26 | try: 27 | sock, addr = self.accept() 28 | except socket.error, e: 29 | self.logger.warning('accept error: ' + e.message) 30 | return 31 | except TypeError, e: 32 | self.logger.warning('accept error: ' + e.message) 33 | return 34 | 35 | self.logger.info('accept client from ' + str(addr)) 36 | conn = TcpConnection(sock, addr) 37 | self.handle_new_connection(conn) 38 | 39 | def stop(self): 40 | self.close() 41 | 42 | def handle_new_connection(self, conn): 43 | self.logger.info('handle_new_connection') 44 | service = self.service_factory() 45 | rpc_channel = RpcChannel(service, conn) # TODO, 多个connection的RpcChannel保存 46 | conn.attach_rpc_channel(rpc_channel) 47 | -------------------------------------------------------------------------------- /rpc/rpc_channel.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | from google.protobuf import service 3 | from rpc.rpc_controller import RpcController 4 | import struct 5 | import logger 6 | 7 | 8 | class RpcChannel(service.RpcChannel): 9 | def __init__(self, rpc_service, conn): 10 | super(RpcChannel, self).__init__() 11 | self.logger = logger.get_logger("RpcChannel") 12 | self.rpc_service = rpc_service 13 | self.conn = conn 14 | 15 | self.rpc_controller = RpcController(self) 16 | print "RpcChannel init" 17 | 18 | def set_rpc_service(self, rpc_service): 19 | self.rpc_service = rpc_service 20 | 21 | def CallMethod(self, method_descriptor, rpc_controller, 22 | request, response_class, done): 23 | print "RpcChannel CallMethod" 24 | index = method_descriptor.index 25 | data = request.SerializeToString() 26 | total_len = len(data) + 6 27 | self.logger.debug("CallMethod:%d,%d"%(total_len,index)) 28 | 29 | self.conn.send_data(''.join([struct.pack('!ih', total_len, index), data])) 30 | 31 | def input_data(self, data): 32 | total_len, index = struct.unpack('!ih', data[0:6]) 33 | self.logger.debug("input_data:%d,%d" % (total_len, index)) 34 | rpc_service = self.rpc_service 35 | s_descriptor = rpc_service.GetDescriptor() 36 | method = s_descriptor.methods[index] 37 | try: 38 | request = rpc_service.GetRequestClass(method)() 39 | serialized = data[6:total_len] 40 | request.ParseFromString(serialized) 41 | 42 | rpc_service.CallMethod(method, self.rpc_controller, request, None) 43 | 44 | except Exception, e: 45 | self.logger.error("Call rpc method failed!") 46 | print "error:",e 47 | self.logger.log_last_except() 48 | 49 | if (len(data) > total_len): 50 | data = data[total_len:] 51 | self.input_data(data) 52 | return True 53 | 54 | -------------------------------------------------------------------------------- /google/protobuf/pyext/proto2_api_test.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | import "google/protobuf/internal/cpp/proto1_api_test.proto"; 32 | 33 | package google.protobuf.python.internal; 34 | 35 | message TestNestedProto1APIMessage { 36 | optional int32 a = 1; 37 | optional TestMessage.NestedMessage b = 2; 38 | } 39 | -------------------------------------------------------------------------------- /rpc/tcp_client.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | import socket 3 | from rpc.tcp_connection import TcpConnection 4 | from rpc.rpc_channel import RpcChannel 5 | import logger 6 | 7 | 8 | class TcpClient(TcpConnection): 9 | 10 | def __init__(self, ip, port, stub_factory, service_factory): 11 | TcpConnection.__init__(self, None, (ip, port)) 12 | self.logger = logger.get_logger('TcpClient') 13 | self.stub_factory = stub_factory 14 | self.service_factory = service_factory 15 | self.channel = None 16 | self.stub = None 17 | self.service = None 18 | 19 | def close(self): 20 | self.disconnect() 21 | 22 | def sync_connect(self): 23 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 24 | try: 25 | sock.connect(self.peername) 26 | except socket.error, msg: 27 | sock.close() 28 | self.logger.warning("sync_connect failed %s with remote server %s", msg, self.peername) 29 | return False 30 | 31 | # after connected, do the nonblocking setting 32 | sock.setblocking(0) 33 | self.set_socket(sock) 34 | self.setsockopt() 35 | self.handle_connect() 36 | return True 37 | 38 | def async_connect(self): 39 | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) 40 | self.setsockopt() 41 | self.connect(self.peername) 42 | 43 | def handle_connect(self): 44 | self.logger.info('connection established.') 45 | self.status = TcpConnection.ST_ESTABLISHED 46 | 47 | #service是被动接收方,stub是主动发送方 48 | self.service = self.service_factory() 49 | self.channel = RpcChannel(self.service, self) 50 | self.stub = self.stub_factory(self.channel) 51 | self.attach_rpc_channel(self.channel) 52 | 53 | 54 | def handle_close(self): 55 | TcpConnection.handle_close(self) 56 | 57 | def writable(self): 58 | if self.status == TcpConnection.ST_ESTABLISHED: 59 | return TcpConnection.writable(self) 60 | else: 61 | return True 62 | -------------------------------------------------------------------------------- /google/protobuf/internal/missing_enum_values.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | package google.protobuf.python.internal; 32 | 33 | message TestEnumValues { 34 | enum NestedEnum { 35 | ZERO = 0; 36 | ONE = 1; 37 | } 38 | optional NestedEnum optional_nested_enum = 1; 39 | repeated NestedEnum repeated_nested_enum = 2; 40 | repeated NestedEnum packed_nested_enum = 3 [packed = true]; 41 | } 42 | 43 | message TestMissingEnumValues { 44 | enum NestedEnum { 45 | TWO = 2; 46 | } 47 | optional NestedEnum optional_nested_enum = 1; 48 | repeated NestedEnum repeated_nested_enum = 2; 49 | repeated NestedEnum packed_nested_enum = 3 [packed = true]; 50 | } 51 | -------------------------------------------------------------------------------- /google/protobuf/internal/more_extensions_dynamic.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: jasonh@google.com (Jason Hsueh) 32 | // 33 | // This file is used to test a corner case in the CPP implementation where the 34 | // generated C++ type is available for the extendee, but the extension is 35 | // defined in a file whose C++ type is not in the binary. 36 | 37 | 38 | import "google/protobuf/internal/more_extensions.proto"; 39 | 40 | package google.protobuf.internal; 41 | 42 | message DynamicMessageType { 43 | optional int32 a = 1; 44 | } 45 | 46 | extend ExtendedMessage { 47 | optional int32 dynamic_int32_extension = 100; 48 | optional DynamicMessageType dynamic_message_extension = 101; 49 | } 50 | -------------------------------------------------------------------------------- /google/protobuf/internal/more_extensions.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: robinson@google.com (Will Robinson) 32 | 33 | 34 | package google.protobuf.internal; 35 | 36 | 37 | message TopLevelMessage { 38 | optional ExtendedMessage submessage = 1; 39 | } 40 | 41 | 42 | message ExtendedMessage { 43 | extensions 1 to max; 44 | } 45 | 46 | 47 | message ForeignMessage { 48 | optional int32 foreign_message_int = 1; 49 | } 50 | 51 | 52 | extend ExtendedMessage { 53 | optional int32 optional_int_extension = 1; 54 | optional ForeignMessage optional_message_extension = 2; 55 | 56 | repeated int32 repeated_int_extension = 3; 57 | repeated ForeignMessage repeated_message_extension = 4; 58 | } 59 | -------------------------------------------------------------------------------- /google/protobuf/internal/test_bad_identifiers.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | 33 | 34 | package protobuf_unittest; 35 | 36 | option py_generic_services = true; 37 | 38 | message TestBadIdentifiers { 39 | extensions 100 to max; 40 | } 41 | 42 | // Make sure these reasonable extension names don't conflict with internal 43 | // variables. 44 | extend TestBadIdentifiers { 45 | optional string message = 100 [default="foo"]; 46 | optional string descriptor = 101 [default="bar"]; 47 | optional string reflection = 102 [default="baz"]; 48 | optional string service = 103 [default="qux"]; 49 | } 50 | 51 | message AnotherMessage {} 52 | service AnotherService {} 53 | -------------------------------------------------------------------------------- /google/protobuf/internal/more_messages.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: robinson@google.com (Will Robinson) 32 | 33 | 34 | package google.protobuf.internal; 35 | 36 | // A message where tag numbers are listed out of order, to allow us to test our 37 | // canonicalization of serialized output, which should always be in tag order. 38 | // We also mix in some extensions for extra fun. 39 | message OutOfOrderFields { 40 | optional sint32 optional_sint32 = 5; 41 | extensions 4 to 4; 42 | optional uint32 optional_uint32 = 3; 43 | extensions 2 to 2; 44 | optional int32 optional_int32 = 1; 45 | }; 46 | 47 | 48 | extend OutOfOrderFields { 49 | optional uint64 optional_uint64 = 4; 50 | optional int64 optional_int64 = 2; 51 | } 52 | -------------------------------------------------------------------------------- /google/protobuf/internal/factory_test1.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: matthewtoia@google.com (Matt Toia) 32 | 33 | 34 | package google.protobuf.python.internal; 35 | 36 | 37 | enum Factory1Enum { 38 | FACTORY_1_VALUE_0 = 0; 39 | FACTORY_1_VALUE_1 = 1; 40 | } 41 | 42 | message Factory1Message { 43 | optional Factory1Enum factory_1_enum = 1; 44 | enum NestedFactory1Enum { 45 | NESTED_FACTORY_1_VALUE_0 = 0; 46 | NESTED_FACTORY_1_VALUE_1 = 1; 47 | } 48 | optional NestedFactory1Enum nested_factory_1_enum = 2; 49 | message NestedFactory1Message { 50 | optional string value = 1; 51 | } 52 | optional NestedFactory1Message nested_factory_1_message = 3; 53 | optional int32 scalar_value = 4; 54 | repeated string list_value = 5; 55 | 56 | extensions 1000 to max; 57 | } 58 | -------------------------------------------------------------------------------- /google/protobuf/internal/message_python_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Tests for ..public.message for the pure Python implementation.""" 34 | 35 | import os 36 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python' 37 | 38 | # We must set the implementation version above before the google3 imports. 39 | # pylint: disable=g-import-not-at-top 40 | from google.apputils import basetest 41 | from google.protobuf.internal import api_implementation 42 | # Run all tests from the original module by putting them in our namespace. 43 | # pylint: disable=wildcard-import 44 | from google.protobuf.internal.message_test import * 45 | 46 | 47 | class ConfirmPurePythonTest(basetest.TestCase): 48 | 49 | def testImplementationSetting(self): 50 | self.assertEqual('python', api_implementation.Type()) 51 | 52 | 53 | if __name__ == '__main__': 54 | basetest.main() 55 | -------------------------------------------------------------------------------- /google/protobuf/internal/descriptor_python_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Unittest for descriptor.py for the pure Python implementation.""" 34 | 35 | import os 36 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python' 37 | 38 | # We must set the implementation version above before the google3 imports. 39 | # pylint: disable=g-import-not-at-top 40 | from google.apputils import basetest 41 | from google.protobuf.internal import api_implementation 42 | # Run all tests from the original module by putting them in our namespace. 43 | # pylint: disable=wildcard-import 44 | from google.protobuf.internal.descriptor_test import * 45 | 46 | 47 | class ConfirmPurePythonTest(basetest.TestCase): 48 | 49 | def testImplementationSetting(self): 50 | self.assertEqual('python', api_implementation.Type()) 51 | 52 | 53 | if __name__ == '__main__': 54 | basetest.main() 55 | -------------------------------------------------------------------------------- /google/protobuf/internal/message_factory_python_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Tests for ..public.message_factory for the pure Python implementation.""" 34 | 35 | import os 36 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python' 37 | 38 | # We must set the implementation version above before the google3 imports. 39 | # pylint: disable=g-import-not-at-top 40 | from google.apputils import basetest 41 | from google.protobuf.internal import api_implementation 42 | # Run all tests from the original module by putting them in our namespace. 43 | # pylint: disable=wildcard-import 44 | from google.protobuf.internal.message_factory_test import * 45 | 46 | 47 | class ConfirmPurePythonTest(basetest.TestCase): 48 | 49 | def testImplementationSetting(self): 50 | self.assertEqual('python', api_implementation.Type()) 51 | 52 | 53 | if __name__ == '__main__': 54 | basetest.main() 55 | -------------------------------------------------------------------------------- /google/protobuf/pyext/python_protobuf.h: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: qrczak@google.com (Marcin Kowalczyk) 32 | // 33 | // This module exposes the C proto inside the given Python proto, in 34 | // case the Python proto is implemented with a C proto. 35 | 36 | #ifndef GOOGLE_PROTOBUF_PYTHON_PYTHON_PROTOBUF_H__ 37 | #define GOOGLE_PROTOBUF_PYTHON_PYTHON_PROTOBUF_H__ 38 | 39 | #include 40 | 41 | namespace google { 42 | namespace protobuf { 43 | 44 | class Message; 45 | 46 | namespace python { 47 | 48 | // Return the pointer to the C proto inside the given Python proto, 49 | // or NULL when this is not a Python proto implemented with a C proto. 50 | const Message* GetCProtoInsidePyProto(PyObject* msg); 51 | Message* MutableCProtoInsidePyProto(PyObject* msg); 52 | 53 | } // namespace python 54 | } // namespace protobuf 55 | 56 | } // namespace google 57 | #endif // GOOGLE_PROTOBUF_PYTHON_PYTHON_PROTOBUF_H__ 58 | -------------------------------------------------------------------------------- /google/protobuf/pyext/message_factory_cpp2_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Tests for google.protobuf.message_factory.""" 34 | 35 | import os 36 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' 37 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION'] = '2' 38 | 39 | # We must set the implementation version above before the google3 imports. 40 | # pylint: disable=g-import-not-at-top 41 | from google.apputils import basetest 42 | from google.protobuf.internal import api_implementation 43 | # Run all tests from the original module by putting them in our namespace. 44 | # pylint: disable=wildcard-import 45 | from google.protobuf.internal.message_factory_test import * 46 | 47 | 48 | class ConfirmCppApi2Test(basetest.TestCase): 49 | 50 | def testImplementationSetting(self): 51 | self.assertEqual('cpp', api_implementation.Type()) 52 | self.assertEqual(2, api_implementation.Version()) 53 | 54 | 55 | if __name__ == '__main__': 56 | basetest.main() 57 | -------------------------------------------------------------------------------- /google/protobuf/pyext/descriptor_cpp2_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Tests for google.protobuf.pyext behavior.""" 34 | 35 | __author__ = 'anuraag@google.com (Anuraag Agrawal)' 36 | 37 | import os 38 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' 39 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION'] = '2' 40 | 41 | # We must set the implementation version above before the google3 imports. 42 | # pylint: disable=g-import-not-at-top 43 | from google.apputils import basetest 44 | from google.protobuf.internal import api_implementation 45 | # Run all tests from the original module by putting them in our namespace. 46 | # pylint: disable=wildcard-import 47 | from google.protobuf.internal.descriptor_test import * 48 | 49 | 50 | class ConfirmCppApi2Test(basetest.TestCase): 51 | 52 | def testImplementationSetting(self): 53 | self.assertEqual('cpp', api_implementation.Type()) 54 | self.assertEqual(2, api_implementation.Version()) 55 | 56 | 57 | if __name__ == '__main__': 58 | basetest.main() 59 | -------------------------------------------------------------------------------- /google/protobuf/internal/descriptor_pool_test2.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | package google.protobuf.python.internal; 32 | 33 | import "google/protobuf/internal/descriptor_pool_test1.proto"; 34 | 35 | 36 | message DescriptorPoolTest3 { 37 | 38 | extend DescriptorPoolTest1 { 39 | optional DescriptorPoolTest3 descriptor_pool_test = 1001; 40 | } 41 | 42 | enum NestedEnum { 43 | NU = 13; 44 | XI = 14; 45 | } 46 | 47 | optional NestedEnum nested_enum = 1 [default = XI]; 48 | 49 | message NestedMessage { 50 | enum NestedEnum { 51 | OMICRON = 15; 52 | PI = 16; 53 | } 54 | optional NestedEnum nested_enum = 1 [default = PI]; 55 | optional string nested_field = 2 [default = "nu"]; 56 | optional DeepNestedMessage deep_nested_message = 3; 57 | 58 | message DeepNestedMessage { 59 | enum NestedEnum { 60 | RHO = 17; 61 | SIGMA = 18; 62 | } 63 | optional NestedEnum nested_enum = 1 [default = RHO]; 64 | optional string nested_field = 2 [default = "sigma"]; 65 | } 66 | } 67 | 68 | optional NestedMessage nested_message = 2; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /google/protobuf/pyext/cpp_message.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """Protocol message implementation hooks for C++ implementation. 32 | 33 | Contains helper functions used to create protocol message classes from 34 | Descriptor objects at runtime backed by the protocol buffer C++ API. 35 | """ 36 | 37 | __author__ = 'tibell@google.com (Johan Tibell)' 38 | 39 | from google.protobuf.pyext import _message 40 | from google.protobuf import message 41 | 42 | 43 | def NewMessage(bases, message_descriptor, dictionary): 44 | """Creates a new protocol message *class*.""" 45 | new_bases = [] 46 | for base in bases: 47 | if base is message.Message: 48 | # _message.Message must come before message.Message as it 49 | # overrides methods in that class. 50 | new_bases.append(_message.Message) 51 | new_bases.append(base) 52 | return tuple(new_bases) 53 | 54 | 55 | def InitMessage(message_descriptor, cls): 56 | """Constructs a new message instance (called before instance's __init__).""" 57 | 58 | def SubInit(self, **kwargs): 59 | super(cls, self).__init__(message_descriptor, **kwargs) 60 | cls.__init__ = SubInit 61 | cls.AddDescriptors(message_descriptor) 62 | -------------------------------------------------------------------------------- /google/protobuf/pyext/python.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: tibell@google.com (Johan Tibell) 32 | // 33 | // These message definitions are used to exercises known corner cases 34 | // in the C++ implementation of the Python API. 35 | 36 | 37 | package google.protobuf.python.internal; 38 | 39 | // Protos optimized for SPEED use a strict superset of the generated code 40 | // of equivalent ones optimized for CODE_SIZE, so we should optimize all our 41 | // tests for speed unless explicitly testing code size optimization. 42 | option optimize_for = SPEED; 43 | 44 | message TestAllTypes { 45 | message NestedMessage { 46 | optional int32 bb = 1; 47 | optional ForeignMessage cc = 2; 48 | } 49 | 50 | repeated NestedMessage repeated_nested_message = 1; 51 | optional NestedMessage optional_nested_message = 2; 52 | optional int32 optional_int32 = 3; 53 | } 54 | 55 | message ForeignMessage { 56 | optional int32 c = 1; 57 | repeated int32 d = 2; 58 | } 59 | 60 | message TestAllExtensions { 61 | extensions 1 to max; 62 | } 63 | 64 | extend TestAllExtensions { 65 | optional TestAllTypes.NestedMessage optional_nested_message_extension = 1; 66 | } 67 | -------------------------------------------------------------------------------- /rpc/tcp_connection.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | import socket 3 | import asyncore 4 | import logger 5 | 6 | 7 | class TcpConnection(asyncore.dispatcher): 8 | 9 | DEFAULT_RECV_BUFFER = 4096 10 | ST_INIT = 0 11 | ST_ESTABLISHED = 1 12 | ST_DISCONNECTED = 2 13 | 14 | def __init__(self, sock, peername): 15 | asyncore.dispatcher.__init__(self, sock) 16 | self.logger = logger.get_logger('TcpConnection') 17 | self.peername = peername 18 | 19 | self.writebuff = '' 20 | self.recv_buff_size = TcpConnection.DEFAULT_RECV_BUFFER 21 | 22 | self.status = TcpConnection.ST_INIT 23 | if sock: 24 | self.status = TcpConnection.ST_ESTABLISHED 25 | self.setsockopt() 26 | 27 | self.rpc_channel = None 28 | 29 | def setsockopt(self): 30 | self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) 31 | 32 | def get_rpc_channel(self): 33 | return self.rpc_channel 34 | 35 | def attach_rpc_channel(self, channel_interface): 36 | self.rpc_channel = channel_interface 37 | 38 | def is_established(self): 39 | return self.status == TcpConnection.ST_ESTABLISHED 40 | 41 | def set_recv_buffer(self, size): 42 | self.recv_buff_size = size 43 | 44 | def disconnect(self): 45 | if self.status == TcpConnection.ST_DISCONNECTED: 46 | return 47 | 48 | if self.rpc_channel: 49 | self.rpc_channel.on_disconnected() 50 | self.rpc_channel = None 51 | 52 | if self.socket: 53 | asyncore.dispatcher.close(self) 54 | 55 | self.status = TcpConnection.ST_DISCONNECTED 56 | 57 | def getpeername(self): 58 | return self.peername 59 | 60 | def handle_close(self): 61 | self.logger.debug('handle_close') 62 | asyncore.dispatcher.handle_close(self) 63 | self.disconnect() 64 | 65 | def handle_expt(self): 66 | self.logger.debug('handle_expt') 67 | asyncore.dispatcher.handle_expt(self) 68 | self.disconnect() 69 | 70 | def handle_error(self): 71 | self.logger.debug('handle_error') 72 | asyncore.dispatcher.handle_error(self) 73 | self.disconnect() 74 | 75 | def handle_read(self): 76 | self.logger.debug('handle_read') 77 | data = self.recv(self.recv_buff_size) 78 | if data: 79 | if not self.rpc_channel: 80 | self.logger.debug("not rpc_channel") 81 | return 82 | self.rpc_channel.input_data(data) 83 | 84 | def handle_write(self): 85 | self.logger.debug('handle_write') 86 | if self.writebuff: 87 | size = self.send(self.writebuff) 88 | self.writebuff = self.writebuff[size:] 89 | 90 | def writable(self): 91 | return len(self.writebuff) > 0 92 | 93 | def send_data(self, data): 94 | self.writebuff += data -------------------------------------------------------------------------------- /google/protobuf/internal/api_implementation_default_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Test that the api_implementation defaults are what we expect.""" 34 | 35 | import os 36 | import sys 37 | # Clear environment implementation settings before the google3 imports. 38 | os.environ.pop('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION', None) 39 | os.environ.pop('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION', None) 40 | 41 | # pylint: disable=g-import-not-at-top 42 | from google.apputils import basetest 43 | from google.protobuf.internal import api_implementation 44 | 45 | 46 | class ApiImplementationDefaultTest(basetest.TestCase): 47 | 48 | if sys.version_info.major <= 2: 49 | 50 | def testThatPythonIsTheDefault(self): 51 | """If -DPYTHON_PROTO_*IMPL* was given at build time, this may fail.""" 52 | self.assertEqual('python', api_implementation.Type()) 53 | 54 | else: 55 | 56 | def testThatCppApiV2IsTheDefault(self): 57 | """If -DPYTHON_PROTO_*IMPL* was given at build time, this may fail.""" 58 | self.assertEqual('cpp', api_implementation.Type()) 59 | self.assertEqual(2, api_implementation.Version()) 60 | 61 | 62 | if __name__ == '__main__': 63 | basetest.main() 64 | -------------------------------------------------------------------------------- /google/protobuf/internal/text_encoding_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Tests for google.protobuf.text_encoding.""" 34 | 35 | from google.apputils import basetest 36 | from google.protobuf import text_encoding 37 | 38 | TEST_VALUES = [ 39 | ("foo\\rbar\\nbaz\\t", 40 | "foo\\rbar\\nbaz\\t", 41 | b"foo\rbar\nbaz\t"), 42 | ("\\'full of \\\"sound\\\" and \\\"fury\\\"\\'", 43 | "\\'full of \\\"sound\\\" and \\\"fury\\\"\\'", 44 | b"'full of \"sound\" and \"fury\"'"), 45 | ("signi\\\\fying\\\\ nothing\\\\", 46 | "signi\\\\fying\\\\ nothing\\\\", 47 | b"signi\\fying\\ nothing\\"), 48 | ("\\010\\t\\n\\013\\014\\r", 49 | "\x08\\t\\n\x0b\x0c\\r", 50 | b"\010\011\012\013\014\015")] 51 | 52 | 53 | class TextEncodingTestCase(basetest.TestCase): 54 | def testCEscape(self): 55 | for escaped, escaped_utf8, unescaped in TEST_VALUES: 56 | self.assertEquals(escaped, 57 | text_encoding.CEscape(unescaped, as_utf8=False)) 58 | self.assertEquals(escaped_utf8, 59 | text_encoding.CEscape(unescaped, as_utf8=True)) 60 | 61 | def testCUnescape(self): 62 | for escaped, escaped_utf8, unescaped in TEST_VALUES: 63 | self.assertEquals(unescaped, text_encoding.CUnescape(escaped)) 64 | self.assertEquals(unescaped, text_encoding.CUnescape(escaped_utf8)) 65 | 66 | 67 | if __name__ == "__main__": 68 | basetest.main() 69 | -------------------------------------------------------------------------------- /google/protobuf/internal/descriptor_database_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Tests for google.protobuf.descriptor_database.""" 34 | 35 | __author__ = 'matthewtoia@google.com (Matt Toia)' 36 | 37 | from google.apputils import basetest 38 | from google.protobuf import descriptor_pb2 39 | from google.protobuf.internal import factory_test2_pb2 40 | from google.protobuf import descriptor_database 41 | 42 | 43 | class DescriptorDatabaseTest(basetest.TestCase): 44 | 45 | def testAdd(self): 46 | db = descriptor_database.DescriptorDatabase() 47 | file_desc_proto = descriptor_pb2.FileDescriptorProto.FromString( 48 | factory_test2_pb2.DESCRIPTOR.serialized_pb) 49 | db.Add(file_desc_proto) 50 | 51 | self.assertEquals(file_desc_proto, db.FindFileByName( 52 | 'google/protobuf/internal/factory_test2.proto')) 53 | self.assertEquals(file_desc_proto, db.FindFileContainingSymbol( 54 | 'google.protobuf.python.internal.Factory2Message')) 55 | self.assertEquals(file_desc_proto, db.FindFileContainingSymbol( 56 | 'google.protobuf.python.internal.Factory2Message.NestedFactory2Message')) 57 | self.assertEquals(file_desc_proto, db.FindFileContainingSymbol( 58 | 'google.protobuf.python.internal.Factory2Enum')) 59 | self.assertEquals(file_desc_proto, db.FindFileContainingSymbol( 60 | 'google.protobuf.python.internal.Factory2Message.NestedFactory2Enum')) 61 | 62 | if __name__ == '__main__': 63 | basetest.main() 64 | -------------------------------------------------------------------------------- /google/protobuf/internal/descriptor_pool_test1.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | package google.protobuf.python.internal; 32 | 33 | 34 | message DescriptorPoolTest1 { 35 | extensions 1000 to max; 36 | 37 | enum NestedEnum { 38 | ALPHA = 1; 39 | BETA = 2; 40 | } 41 | 42 | optional NestedEnum nested_enum = 1 [default = BETA]; 43 | 44 | message NestedMessage { 45 | enum NestedEnum { 46 | EPSILON = 5; 47 | ZETA = 6; 48 | } 49 | optional NestedEnum nested_enum = 1 [default = ZETA]; 50 | optional string nested_field = 2 [default = "beta"]; 51 | optional DeepNestedMessage deep_nested_message = 3; 52 | 53 | message DeepNestedMessage { 54 | enum NestedEnum { 55 | ETA = 7; 56 | THETA = 8; 57 | } 58 | optional NestedEnum nested_enum = 1 [default = ETA]; 59 | optional string nested_field = 2 [default = "theta"]; 60 | } 61 | } 62 | 63 | optional NestedMessage nested_message = 2; 64 | } 65 | 66 | message DescriptorPoolTest2 { 67 | enum NestedEnum { 68 | GAMMA = 3; 69 | DELTA = 4; 70 | } 71 | 72 | optional NestedEnum nested_enum = 1 [default = GAMMA]; 73 | 74 | message NestedMessage { 75 | enum NestedEnum { 76 | IOTA = 9; 77 | KAPPA = 10; 78 | } 79 | optional NestedEnum nested_enum = 1 [default = IOTA]; 80 | optional string nested_field = 2 [default = "delta"]; 81 | optional DeepNestedMessage deep_nested_message = 3; 82 | 83 | message DeepNestedMessage { 84 | enum NestedEnum { 85 | LAMBDA = 11; 86 | MU = 12; 87 | } 88 | optional NestedEnum nested_enum = 1 [default = MU]; 89 | optional string nested_field = 2 [default = "lambda"]; 90 | } 91 | } 92 | 93 | optional NestedMessage nested_message = 2; 94 | } 95 | -------------------------------------------------------------------------------- /google/protobuf/internal/message_listener.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """Defines a listener interface for observing certain 32 | state transitions on Message objects. 33 | 34 | Also defines a null implementation of this interface. 35 | """ 36 | 37 | __author__ = 'robinson@google.com (Will Robinson)' 38 | 39 | 40 | class MessageListener(object): 41 | 42 | """Listens for modifications made to a message. Meant to be registered via 43 | Message._SetListener(). 44 | 45 | Attributes: 46 | dirty: If True, then calling Modified() would be a no-op. This can be 47 | used to avoid these calls entirely in the common case. 48 | """ 49 | 50 | def Modified(self): 51 | """Called every time the message is modified in such a way that the parent 52 | message may need to be updated. This currently means either: 53 | (a) The message was modified for the first time, so the parent message 54 | should henceforth mark the message as present. 55 | (b) The message's cached byte size became dirty -- i.e. the message was 56 | modified for the first time after a previous call to ByteSize(). 57 | Therefore the parent should also mark its byte size as dirty. 58 | Note that (a) implies (b), since new objects start out with a client cached 59 | size (zero). However, we document (a) explicitly because it is important. 60 | 61 | Modified() will *only* be called in response to one of these two events -- 62 | not every time the sub-message is modified. 63 | 64 | Note that if the listener's |dirty| attribute is true, then calling 65 | Modified at the moment would be a no-op, so it can be skipped. Performance- 66 | sensitive callers should check this attribute directly before calling since 67 | it will be true most of the time. 68 | """ 69 | 70 | raise NotImplementedError 71 | 72 | 73 | class NullMessageListener(object): 74 | 75 | """No-op MessageListener implementation.""" 76 | 77 | def Modified(self): 78 | pass 79 | -------------------------------------------------------------------------------- /google/protobuf/pyext/scoped_pyobject_ptr.h: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: tibell@google.com (Johan Tibell) 32 | 33 | #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_SCOPED_PYOBJECT_PTR_H__ 34 | #define GOOGLE_PROTOBUF_PYTHON_CPP_SCOPED_PYOBJECT_PTR_H__ 35 | 36 | #include 37 | 38 | namespace google { 39 | class ScopedPyObjectPtr { 40 | public: 41 | // Constructor. Defaults to intializing with NULL. 42 | // There is no way to create an uninitialized ScopedPyObjectPtr. 43 | explicit ScopedPyObjectPtr(PyObject* p = NULL) : ptr_(p) { } 44 | 45 | // Destructor. If there is a PyObject object, delete it. 46 | ~ScopedPyObjectPtr() { 47 | Py_XDECREF(ptr_); 48 | } 49 | 50 | // Reset. Deletes the current owned object, if any. 51 | // Then takes ownership of a new object, if given. 52 | // this->reset(this->get()) works. 53 | PyObject* reset(PyObject* p = NULL) { 54 | if (p != ptr_) { 55 | Py_XDECREF(ptr_); 56 | ptr_ = p; 57 | } 58 | return ptr_; 59 | } 60 | 61 | // Releases ownership of the object. 62 | PyObject* release() { 63 | PyObject* p = ptr_; 64 | ptr_ = NULL; 65 | return p; 66 | } 67 | 68 | operator PyObject*() { return ptr_; } 69 | 70 | PyObject* operator->() const { 71 | assert(ptr_ != NULL); 72 | return ptr_; 73 | } 74 | 75 | PyObject* get() const { return ptr_; } 76 | 77 | Py_ssize_t refcnt() const { return Py_REFCNT(ptr_); } 78 | 79 | void inc() const { Py_INCREF(ptr_); } 80 | 81 | // Comparison operators. 82 | // These return whether a ScopedPyObjectPtr and a raw pointer 83 | // refer to the same object, not just to two different but equal 84 | // objects. 85 | bool operator==(const PyObject* p) const { return ptr_ == p; } 86 | bool operator!=(const PyObject* p) const { return ptr_ != p; } 87 | 88 | private: 89 | PyObject* ptr_; 90 | 91 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ScopedPyObjectPtr); 92 | }; 93 | 94 | } // namespace google 95 | #endif // GOOGLE_PROTOBUF_PYTHON_CPP_SCOPED_PYOBJECT_PTR_H__ 96 | -------------------------------------------------------------------------------- /google/protobuf/pyext/descriptor.h: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: petar@google.com (Petar Petrov) 32 | 33 | #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__ 34 | #define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__ 35 | 36 | #include 37 | #include 38 | 39 | #include 40 | 41 | #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN) 42 | typedef int Py_ssize_t; 43 | #define PY_SSIZE_T_MAX INT_MAX 44 | #define PY_SSIZE_T_MIN INT_MIN 45 | #endif 46 | 47 | namespace google { 48 | namespace protobuf { 49 | namespace python { 50 | 51 | typedef struct CFieldDescriptor { 52 | PyObject_HEAD 53 | 54 | // The proto2 descriptor that this object represents. 55 | const google::protobuf::FieldDescriptor* descriptor; 56 | 57 | // Reference to the original field object in the Python DESCRIPTOR. 58 | PyObject* descriptor_field; 59 | } CFieldDescriptor; 60 | 61 | typedef struct { 62 | PyObject_HEAD 63 | 64 | const google::protobuf::DescriptorPool* pool; 65 | } CDescriptorPool; 66 | 67 | extern PyTypeObject CFieldDescriptor_Type; 68 | 69 | extern PyTypeObject CDescriptorPool_Type; 70 | 71 | namespace cdescriptor_pool { 72 | 73 | // Looks up a field by name. Returns a CDescriptor corresponding to 74 | // the field on success, or NULL on failure. 75 | // 76 | // Returns a new reference. 77 | PyObject* FindFieldByName(CDescriptorPool* self, PyObject* name); 78 | 79 | // Looks up an extension by name. Returns a CDescriptor corresponding 80 | // to the field on success, or NULL on failure. 81 | // 82 | // Returns a new reference. 83 | PyObject* FindExtensionByName(CDescriptorPool* self, PyObject* arg); 84 | 85 | } // namespace cdescriptor_pool 86 | 87 | PyObject* Python_NewCDescriptorPool(PyObject* ignored, PyObject* args); 88 | PyObject* Python_BuildFile(PyObject* ignored, PyObject* args); 89 | bool InitDescriptor(); 90 | google::protobuf::DescriptorPool* GetDescriptorPool(); 91 | 92 | } // namespace python 93 | } // namespace protobuf 94 | 95 | } // namespace google 96 | #endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__ 97 | -------------------------------------------------------------------------------- /google/protobuf/internal/factory_test2.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: matthewtoia@google.com (Matt Toia) 32 | 33 | 34 | package google.protobuf.python.internal; 35 | 36 | import "google/protobuf/internal/factory_test1.proto"; 37 | 38 | 39 | enum Factory2Enum { 40 | FACTORY_2_VALUE_0 = 0; 41 | FACTORY_2_VALUE_1 = 1; 42 | } 43 | 44 | message Factory2Message { 45 | required int32 mandatory = 1; 46 | optional Factory2Enum factory_2_enum = 2; 47 | enum NestedFactory2Enum { 48 | NESTED_FACTORY_2_VALUE_0 = 0; 49 | NESTED_FACTORY_2_VALUE_1 = 1; 50 | } 51 | optional NestedFactory2Enum nested_factory_2_enum = 3; 52 | message NestedFactory2Message { 53 | optional string value = 1; 54 | } 55 | optional NestedFactory2Message nested_factory_2_message = 4; 56 | optional Factory1Message factory_1_message = 5; 57 | optional Factory1Enum factory_1_enum = 6; 58 | optional Factory1Message.NestedFactory1Enum nested_factory_1_enum = 7; 59 | optional Factory1Message.NestedFactory1Message nested_factory_1_message = 8; 60 | optional Factory2Message circular_message = 9; 61 | optional string scalar_value = 10; 62 | repeated string list_value = 11; 63 | repeated group Grouped = 12 { 64 | optional string part_1 = 13; 65 | optional string part_2 = 14; 66 | } 67 | optional LoopMessage loop = 15; 68 | optional int32 int_with_default = 16 [default = 1776]; 69 | optional double double_with_default = 17 [default = 9.99]; 70 | optional string string_with_default = 18 [default = "hello world"]; 71 | optional bool bool_with_default = 19 [default = false]; 72 | optional Factory2Enum enum_with_default = 20 [default = FACTORY_2_VALUE_1]; 73 | optional bytes bytes_with_default = 21 [default = "a\373\000c"]; 74 | 75 | 76 | extend Factory1Message { 77 | optional string one_more_field = 1001; 78 | } 79 | 80 | oneof oneof_field { 81 | int32 oneof_int = 22; 82 | string oneof_string = 23; 83 | } 84 | } 85 | 86 | message LoopMessage { 87 | optional Factory2Message loop = 1; 88 | } 89 | 90 | extend Factory1Message { 91 | optional string another_field = 1002; 92 | } 93 | -------------------------------------------------------------------------------- /google/protobuf/internal/enum_type_wrapper.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """A simple wrapper around enum types to expose utility functions. 32 | 33 | Instances are created as properties with the same name as the enum they wrap 34 | on proto classes. For usage, see: 35 | reflection_test.py 36 | """ 37 | 38 | __author__ = 'rabsatt@google.com (Kevin Rabsatt)' 39 | 40 | 41 | class EnumTypeWrapper(object): 42 | """A utility for finding the names of enum values.""" 43 | 44 | DESCRIPTOR = None 45 | 46 | def __init__(self, enum_type): 47 | """Inits EnumTypeWrapper with an EnumDescriptor.""" 48 | self._enum_type = enum_type 49 | self.DESCRIPTOR = enum_type; 50 | 51 | def Name(self, number): 52 | """Returns a string containing the name of an enum value.""" 53 | if number in self._enum_type.values_by_number: 54 | return self._enum_type.values_by_number[number].name 55 | raise ValueError('Enum %s has no name defined for value %d' % ( 56 | self._enum_type.name, number)) 57 | 58 | def Value(self, name): 59 | """Returns the value coresponding to the given enum name.""" 60 | if name in self._enum_type.values_by_name: 61 | return self._enum_type.values_by_name[name].number 62 | raise ValueError('Enum %s has no value defined for name %s' % ( 63 | self._enum_type.name, name)) 64 | 65 | def keys(self): 66 | """Return a list of the string names in the enum. 67 | 68 | These are returned in the order they were defined in the .proto file. 69 | """ 70 | 71 | return [value_descriptor.name 72 | for value_descriptor in self._enum_type.values] 73 | 74 | def values(self): 75 | """Return a list of the integer values in the enum. 76 | 77 | These are returned in the order they were defined in the .proto file. 78 | """ 79 | 80 | return [value_descriptor.number 81 | for value_descriptor in self._enum_type.values] 82 | 83 | def items(self): 84 | """Return a list of the (name, value) pairs of the enum. 85 | 86 | These are returned in the order they were defined in the .proto file. 87 | """ 88 | return [(value_descriptor.name, value_descriptor.number) 89 | for value_descriptor in self._enum_type.values] 90 | -------------------------------------------------------------------------------- /google/protobuf/internal/api_implementation.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """Determine which implementation of the protobuf API is used in this process. 32 | """ 33 | 34 | import os 35 | import sys 36 | 37 | try: 38 | # pylint: disable=g-import-not-at-top 39 | from google.protobuf.internal import _api_implementation 40 | # The compile-time constants in the _api_implementation module can be used to 41 | # switch to a certain implementation of the Python API at build time. 42 | _api_version = _api_implementation.api_version 43 | del _api_implementation 44 | except ImportError: 45 | _api_version = 0 46 | 47 | _default_implementation_type = ( 48 | 'python' if _api_version == 0 else 'cpp') 49 | _default_version_str = ( 50 | '1' if _api_version <= 1 else '2') 51 | 52 | # This environment variable can be used to switch to a certain implementation 53 | # of the Python API, overriding the compile-time constants in the 54 | # _api_implementation module. Right now only 'python' and 'cpp' are valid 55 | # values. Any other value will be ignored. 56 | _implementation_type = os.getenv('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION', 57 | _default_implementation_type) 58 | 59 | if _implementation_type != 'python': 60 | _implementation_type = 'cpp' 61 | 62 | # This environment variable can be used to switch between the two 63 | # 'cpp' implementations, overriding the compile-time constants in the 64 | # _api_implementation module. Right now only 1 and 2 are valid values. Any other 65 | # value will be ignored. 66 | _implementation_version_str = os.getenv( 67 | 'PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION', 68 | _default_version_str) 69 | 70 | if _implementation_version_str not in ('1', '2'): 71 | raise ValueError( 72 | "unsupported PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION: '" + 73 | _implementation_version_str + "' (supported versions: 1, 2)" 74 | ) 75 | 76 | _implementation_version = int(_implementation_version_str) 77 | 78 | 79 | # Usage of this function is discouraged. Clients shouldn't care which 80 | # implementation of the API is in use. Note that there is no guarantee 81 | # that differences between APIs will be maintained. 82 | # Please don't use this function if possible. 83 | def Type(): 84 | return _implementation_type 85 | 86 | 87 | # See comment on 'Type' above. 88 | def Version(): 89 | return _implementation_version 90 | -------------------------------------------------------------------------------- /google/protobuf/pyext/reflection_cpp2_generated_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Protocol Buffers - Google's data interchange format 5 | # Copyright 2008 Google Inc. All rights reserved. 6 | # https://developers.google.com/protocol-buffers/ 7 | # 8 | # Redistribution and use in source and binary forms, with or without 9 | # modification, are permitted provided that the following conditions are 10 | # met: 11 | # 12 | # * Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # * Redistributions in binary form must reproduce the above 15 | # copyright notice, this list of conditions and the following disclaimer 16 | # in the documentation and/or other materials provided with the 17 | # distribution. 18 | # * Neither the name of Google Inc. nor the names of its 19 | # contributors may be used to endorse or promote products derived from 20 | # this software without specific prior written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | 34 | """Unittest for reflection.py, which tests the generated C++ implementation.""" 35 | 36 | __author__ = 'jasonh@google.com (Jason Hsueh)' 37 | 38 | import os 39 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' 40 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION'] = '2' 41 | 42 | from google.apputils import basetest 43 | from google.protobuf.internal import api_implementation 44 | from google.protobuf.internal import more_extensions_dynamic_pb2 45 | from google.protobuf.internal import more_extensions_pb2 46 | from google.protobuf.internal.reflection_test import * 47 | 48 | 49 | class ReflectionCppTest(basetest.TestCase): 50 | def testImplementationSetting(self): 51 | self.assertEqual('cpp', api_implementation.Type()) 52 | self.assertEqual(2, api_implementation.Version()) 53 | 54 | def testExtensionOfGeneratedTypeInDynamicFile(self): 55 | """Tests that a file built dynamically can extend a generated C++ type. 56 | 57 | The C++ implementation uses a DescriptorPool that has the generated 58 | DescriptorPool as an underlay. Typically, a type can only find 59 | extensions in its own pool. With the python C-extension, the generated C++ 60 | extendee may be available, but not the extension. This tests that the 61 | C-extension implements the correct special handling to make such extensions 62 | available. 63 | """ 64 | pb1 = more_extensions_pb2.ExtendedMessage() 65 | # Test that basic accessors work. 66 | self.assertFalse( 67 | pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension)) 68 | self.assertFalse( 69 | pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension)) 70 | pb1.Extensions[more_extensions_dynamic_pb2.dynamic_int32_extension] = 17 71 | pb1.Extensions[more_extensions_dynamic_pb2.dynamic_message_extension].a = 24 72 | self.assertTrue( 73 | pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension)) 74 | self.assertTrue( 75 | pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension)) 76 | 77 | # Now serialize the data and parse to a new message. 78 | pb2 = more_extensions_pb2.ExtendedMessage() 79 | pb2.MergeFromString(pb1.SerializeToString()) 80 | 81 | self.assertTrue( 82 | pb2.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension)) 83 | self.assertTrue( 84 | pb2.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension)) 85 | self.assertEqual( 86 | 17, pb2.Extensions[more_extensions_dynamic_pb2.dynamic_int32_extension]) 87 | self.assertEqual( 88 | 24, 89 | pb2.Extensions[more_extensions_dynamic_pb2.dynamic_message_extension].a) 90 | 91 | 92 | 93 | if __name__ == '__main__': 94 | basetest.main() 95 | -------------------------------------------------------------------------------- /google/protobuf/pyext/repeated_scalar_container.h: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: anuraag@google.com (Anuraag Agrawal) 32 | // Author: tibell@google.com (Johan Tibell) 33 | 34 | #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__ 35 | #define GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__ 36 | 37 | #include 38 | 39 | #include 40 | #ifndef _SHARED_PTR_H 41 | #include 42 | #endif 43 | 44 | 45 | namespace google { 46 | namespace protobuf { 47 | 48 | class Message; 49 | 50 | using internal::shared_ptr; 51 | 52 | namespace python { 53 | 54 | struct CFieldDescriptor; 55 | struct CMessage; 56 | 57 | typedef struct RepeatedScalarContainer { 58 | PyObject_HEAD; 59 | 60 | // This is the top-level C++ Message object that owns the whole 61 | // proto tree. Every Python RepeatedScalarContainer holds a 62 | // reference to it in order to keep it alive as long as there's a 63 | // Python object that references any part of the tree. 64 | shared_ptr owner; 65 | 66 | // Pointer to the C++ Message that contains this container. The 67 | // RepeatedScalarContainer does not own this pointer. 68 | Message* message; 69 | 70 | // Weak reference to a parent CMessage object (i.e. may be NULL.) 71 | // 72 | // Used to make sure all ancestors are also mutable when first 73 | // modifying the container. 74 | CMessage* parent; 75 | 76 | // Weak reference to the parent's descriptor that describes this 77 | // field. Used together with the parent's message when making a 78 | // default message instance mutable. 79 | CFieldDescriptor* parent_field; 80 | } RepeatedScalarContainer; 81 | 82 | extern PyTypeObject RepeatedScalarContainer_Type; 83 | 84 | namespace repeated_scalar_container { 85 | 86 | // Appends the scalar 'item' to the end of the container 'self'. 87 | // 88 | // Returns None if successful; returns NULL and sets an exception if 89 | // unsuccessful. 90 | PyObject* Append(RepeatedScalarContainer* self, PyObject* item); 91 | 92 | // Releases the messages in the container to a new message. 93 | // 94 | // Returns 0 on success, -1 on failure. 95 | int Release(RepeatedScalarContainer* self); 96 | 97 | // Appends all the elements in the input iterator to the container. 98 | // 99 | // Returns None if successful; returns NULL and sets an exception if 100 | // unsuccessful. 101 | PyObject* Extend(RepeatedScalarContainer* self, PyObject* value); 102 | 103 | // Set the owner field of self and any children of self. 104 | void SetOwner(RepeatedScalarContainer* self, 105 | const shared_ptr& new_owner); 106 | 107 | } // namespace repeated_scalar_container 108 | } // namespace python 109 | } // namespace protobuf 110 | 111 | } // namespace google 112 | #endif // GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__ 113 | -------------------------------------------------------------------------------- /google/protobuf/pyext/extension_dict.h: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: anuraag@google.com (Anuraag Agrawal) 32 | // Author: tibell@google.com (Johan Tibell) 33 | 34 | #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_H__ 35 | #define GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_H__ 36 | 37 | #include 38 | 39 | #include 40 | #ifndef _SHARED_PTR_H 41 | #include 42 | #endif 43 | 44 | 45 | namespace google { 46 | namespace protobuf { 47 | 48 | class Message; 49 | class FieldDescriptor; 50 | 51 | using internal::shared_ptr; 52 | 53 | namespace python { 54 | 55 | struct CMessage; 56 | struct CFieldDescriptor; 57 | 58 | typedef struct ExtensionDict { 59 | PyObject_HEAD; 60 | shared_ptr owner; 61 | CMessage* parent; 62 | Message* message; 63 | PyObject* values; 64 | } ExtensionDict; 65 | 66 | extern PyTypeObject ExtensionDict_Type; 67 | 68 | namespace extension_dict { 69 | 70 | // Gets the _cdescriptor reference to a CFieldDescriptor object given a 71 | // python descriptor object. 72 | // 73 | // Returns a new reference. 74 | CFieldDescriptor* InternalGetCDescriptorFromExtension(PyObject* extension); 75 | 76 | // Gets the number of extension values in this ExtensionDict as a python object. 77 | // 78 | // Returns a new reference. 79 | PyObject* len(ExtensionDict* self); 80 | 81 | // Releases extensions referenced outside this dictionary to keep outside 82 | // references alive. 83 | // 84 | // Returns 0 on success, -1 on failure. 85 | int ReleaseExtension(ExtensionDict* self, 86 | PyObject* extension, 87 | const google::protobuf::FieldDescriptor* descriptor); 88 | 89 | // Gets an extension from the dict for the given extension descriptor. 90 | // 91 | // Returns a new reference. 92 | PyObject* subscript(ExtensionDict* self, PyObject* key); 93 | 94 | // Assigns a value to an extension in the dict. Can only be used for singular 95 | // simple types. 96 | // 97 | // Returns 0 on success, -1 on failure. 98 | int ass_subscript(ExtensionDict* self, PyObject* key, PyObject* value); 99 | 100 | // Clears an extension from the dict. Will release the extension if there 101 | // is still an external reference left to it. 102 | // 103 | // Returns None on success. 104 | PyObject* ClearExtension(ExtensionDict* self, 105 | PyObject* extension); 106 | 107 | // Checks if the dict has an extension. 108 | // 109 | // Returns a new python boolean reference. 110 | PyObject* HasExtension(ExtensionDict* self, PyObject* extension); 111 | 112 | // Gets an extension from the dict given the extension name as opposed to 113 | // descriptor. 114 | // 115 | // Returns a new reference. 116 | PyObject* _FindExtensionByName(ExtensionDict* self, PyObject* name); 117 | 118 | } // namespace extension_dict 119 | } // namespace python 120 | } // namespace protobuf 121 | 122 | } // namespace google 123 | #endif // GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_H__ 124 | -------------------------------------------------------------------------------- /google/protobuf/internal/api_implementation.cc: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | #include 32 | 33 | namespace google { 34 | namespace protobuf { 35 | namespace python { 36 | 37 | // Version constant. 38 | // This is either 0 for python, 1 for CPP V1, 2 for CPP V2. 39 | // 40 | // 0 is default and is equivalent to 41 | // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python 42 | // 43 | // 1 is set with -DPYTHON_PROTO2_CPP_IMPL_V1 and is equivalent to 44 | // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp 45 | // and 46 | // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=1 47 | // 48 | // 2 is set with -DPYTHON_PROTO2_CPP_IMPL_V2 and is equivalent to 49 | // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp 50 | // and 51 | // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=2 52 | #ifdef PYTHON_PROTO2_CPP_IMPL_V1 53 | #if PY_MAJOR_VERSION >= 3 54 | #error "PYTHON_PROTO2_CPP_IMPL_V1 is not supported under Python 3." 55 | #endif 56 | static int kImplVersion = 1; 57 | #else 58 | #ifdef PYTHON_PROTO2_CPP_IMPL_V2 59 | static int kImplVersion = 2; 60 | #else 61 | #ifdef PYTHON_PROTO2_PYTHON_IMPL 62 | static int kImplVersion = 0; 63 | #else 64 | 65 | // The defaults are set here. Python 3 uses the fast C++ APIv2 by default. 66 | // Python 2 still uses the Python version by default until some compatibility 67 | // issues can be worked around. 68 | #if PY_MAJOR_VERSION >= 3 69 | static int kImplVersion = 2; 70 | #else 71 | static int kImplVersion = 0; 72 | #endif 73 | 74 | #endif // PYTHON_PROTO2_PYTHON_IMPL 75 | #endif // PYTHON_PROTO2_CPP_IMPL_V2 76 | #endif // PYTHON_PROTO2_CPP_IMPL_V1 77 | 78 | static const char* kImplVersionName = "api_version"; 79 | 80 | static const char* kModuleName = "_api_implementation"; 81 | static const char kModuleDocstring[] = 82 | "_api_implementation is a module that exposes compile-time constants that\n" 83 | "determine the default API implementation to use for Python proto2.\n" 84 | "\n" 85 | "It complements api_implementation.py by setting defaults using compile-time\n" 86 | "constants defined in C, such that one can set defaults at compilation\n" 87 | "(e.g. with blaze flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2)."; 88 | 89 | #if PY_MAJOR_VERSION >= 3 90 | static struct PyModuleDef _module = { 91 | PyModuleDef_HEAD_INIT, 92 | kModuleName, 93 | kModuleDocstring, 94 | -1, 95 | NULL, 96 | NULL, 97 | NULL, 98 | NULL, 99 | NULL 100 | }; 101 | #define INITFUNC PyInit__api_implementation 102 | #define INITFUNC_ERRORVAL NULL 103 | #else 104 | #define INITFUNC init_api_implementation 105 | #define INITFUNC_ERRORVAL 106 | #endif 107 | 108 | extern "C" { 109 | PyMODINIT_FUNC INITFUNC() { 110 | #if PY_MAJOR_VERSION >= 3 111 | PyObject *module = PyModule_Create(&_module); 112 | #else 113 | PyObject *module = Py_InitModule3( 114 | const_cast(kModuleName), 115 | NULL, 116 | const_cast(kModuleDocstring)); 117 | #endif 118 | if (module == NULL) { 119 | return INITFUNC_ERRORVAL; 120 | } 121 | 122 | // Adds the module variable "api_version". 123 | if (PyModule_AddIntConstant( 124 | module, 125 | const_cast(kImplVersionName), 126 | kImplVersion)) 127 | #if PY_MAJOR_VERSION < 3 128 | return; 129 | #else 130 | { Py_DECREF(module); return NULL; } 131 | 132 | return module; 133 | #endif 134 | } 135 | } 136 | 137 | } // namespace python 138 | } // namespace protobuf 139 | } // namespace google 140 | -------------------------------------------------------------------------------- /google/protobuf/text_encoding.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | #PY25 compatible for GAE. 32 | # 33 | """Encoding related utilities.""" 34 | 35 | import re 36 | import sys ##PY25 37 | 38 | # Lookup table for utf8 39 | _cescape_utf8_to_str = [chr(i) for i in xrange(0, 256)] 40 | _cescape_utf8_to_str[9] = r'\t' # optional escape 41 | _cescape_utf8_to_str[10] = r'\n' # optional escape 42 | _cescape_utf8_to_str[13] = r'\r' # optional escape 43 | _cescape_utf8_to_str[39] = r"\'" # optional escape 44 | 45 | _cescape_utf8_to_str[34] = r'\"' # necessary escape 46 | _cescape_utf8_to_str[92] = r'\\' # necessary escape 47 | 48 | # Lookup table for non-utf8, with necessary escapes at (o >= 127 or o < 32) 49 | _cescape_byte_to_str = ([r'\%03o' % i for i in xrange(0, 32)] + 50 | [chr(i) for i in xrange(32, 127)] + 51 | [r'\%03o' % i for i in xrange(127, 256)]) 52 | _cescape_byte_to_str[9] = r'\t' # optional escape 53 | _cescape_byte_to_str[10] = r'\n' # optional escape 54 | _cescape_byte_to_str[13] = r'\r' # optional escape 55 | _cescape_byte_to_str[39] = r"\'" # optional escape 56 | 57 | _cescape_byte_to_str[34] = r'\"' # necessary escape 58 | _cescape_byte_to_str[92] = r'\\' # necessary escape 59 | 60 | 61 | def CEscape(text, as_utf8): 62 | """Escape a bytes string for use in an ascii protocol buffer. 63 | 64 | text.encode('string_escape') does not seem to satisfy our needs as it 65 | encodes unprintable characters using two-digit hex escapes whereas our 66 | C++ unescaping function allows hex escapes to be any length. So, 67 | "\0011".encode('string_escape') ends up being "\\x011", which will be 68 | decoded in C++ as a single-character string with char code 0x11. 69 | 70 | Args: 71 | text: A byte string to be escaped 72 | as_utf8: Specifies if result should be returned in UTF-8 encoding 73 | Returns: 74 | Escaped string 75 | """ 76 | # PY3 hack: make Ord work for str and bytes: 77 | # //platforms/networking/data uses unicode here, hence basestring. 78 | Ord = ord if isinstance(text, basestring) else lambda x: x 79 | if as_utf8: 80 | return ''.join(_cescape_utf8_to_str[Ord(c)] for c in text) 81 | return ''.join(_cescape_byte_to_str[Ord(c)] for c in text) 82 | 83 | 84 | _CUNESCAPE_HEX = re.compile(r'(\\+)x([0-9a-fA-F])(?![0-9a-fA-F])') 85 | _cescape_highbit_to_str = ([chr(i) for i in range(0, 127)] + 86 | [r'\%03o' % i for i in range(127, 256)]) 87 | 88 | 89 | def CUnescape(text): 90 | """Unescape a text string with C-style escape sequences to UTF-8 bytes.""" 91 | 92 | def ReplaceHex(m): 93 | # Only replace the match if the number of leading back slashes is odd. i.e. 94 | # the slash itself is not escaped. 95 | if len(m.group(1)) & 1: 96 | return m.group(1) + 'x0' + m.group(2) 97 | return m.group(0) 98 | 99 | # This is required because the 'string_escape' encoding doesn't 100 | # allow single-digit hex escapes (like '\xf'). 101 | result = _CUNESCAPE_HEX.sub(ReplaceHex, text) 102 | 103 | if sys.version_info[0] < 3: ##PY25 104 | ##!PY25 if str is bytes: # PY2 105 | return result.decode('string_escape') 106 | result = ''.join(_cescape_highbit_to_str[ord(c)] for c in result) 107 | return (result.encode('ascii') # Make it bytes to allow decode. 108 | .decode('unicode_escape') 109 | # Make it bytes again to return the proper type. 110 | .encode('raw_unicode_escape')) 111 | -------------------------------------------------------------------------------- /google/protobuf/internal/symbol_database_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Tests for google.protobuf.symbol_database.""" 34 | 35 | from google.apputils import basetest 36 | from google.protobuf import unittest_pb2 37 | from google.protobuf import symbol_database 38 | 39 | 40 | class SymbolDatabaseTest(basetest.TestCase): 41 | 42 | def _Database(self): 43 | db = symbol_database.SymbolDatabase() 44 | # Register representative types from unittest_pb2. 45 | db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR) 46 | db.RegisterMessage(unittest_pb2.TestAllTypes) 47 | db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage) 48 | db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup) 49 | db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup) 50 | db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR) 51 | db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR) 52 | return db 53 | 54 | def testGetPrototype(self): 55 | instance = self._Database().GetPrototype( 56 | unittest_pb2.TestAllTypes.DESCRIPTOR) 57 | self.assertTrue(instance is unittest_pb2.TestAllTypes) 58 | 59 | def testGetMessages(self): 60 | messages = self._Database().GetMessages( 61 | ['google/protobuf/unittest.proto']) 62 | self.assertTrue( 63 | unittest_pb2.TestAllTypes is 64 | messages['protobuf_unittest.TestAllTypes']) 65 | 66 | def testGetSymbol(self): 67 | self.assertEquals( 68 | unittest_pb2.TestAllTypes, self._Database().GetSymbol( 69 | 'protobuf_unittest.TestAllTypes')) 70 | self.assertEquals( 71 | unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol( 72 | 'protobuf_unittest.TestAllTypes.NestedMessage')) 73 | self.assertEquals( 74 | unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol( 75 | 'protobuf_unittest.TestAllTypes.OptionalGroup')) 76 | self.assertEquals( 77 | unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol( 78 | 'protobuf_unittest.TestAllTypes.RepeatedGroup')) 79 | 80 | def testEnums(self): 81 | # Check registration of types in the pool. 82 | self.assertEquals( 83 | 'protobuf_unittest.ForeignEnum', 84 | self._Database().pool.FindEnumTypeByName( 85 | 'protobuf_unittest.ForeignEnum').full_name) 86 | self.assertEquals( 87 | 'protobuf_unittest.TestAllTypes.NestedEnum', 88 | self._Database().pool.FindEnumTypeByName( 89 | 'protobuf_unittest.TestAllTypes.NestedEnum').full_name) 90 | 91 | def testFindMessageTypeByName(self): 92 | self.assertEquals( 93 | 'protobuf_unittest.TestAllTypes', 94 | self._Database().pool.FindMessageTypeByName( 95 | 'protobuf_unittest.TestAllTypes').full_name) 96 | self.assertEquals( 97 | 'protobuf_unittest.TestAllTypes.NestedMessage', 98 | self._Database().pool.FindMessageTypeByName( 99 | 'protobuf_unittest.TestAllTypes.NestedMessage').full_name) 100 | 101 | def testFindFindContainingSymbol(self): 102 | # Lookup based on either enum or message. 103 | self.assertEquals( 104 | 'google/protobuf/unittest.proto', 105 | self._Database().pool.FindFileContainingSymbol( 106 | 'protobuf_unittest.TestAllTypes.NestedEnum').name) 107 | self.assertEquals( 108 | 'google/protobuf/unittest.proto', 109 | self._Database().pool.FindFileContainingSymbol( 110 | 'protobuf_unittest.TestAllTypes').name) 111 | 112 | def testFindFileByName(self): 113 | self.assertEquals( 114 | 'google/protobuf/unittest.proto', 115 | self._Database().pool.FindFileByName( 116 | 'google/protobuf/unittest.proto').name) 117 | 118 | 119 | if __name__ == '__main__': 120 | basetest.main() 121 | -------------------------------------------------------------------------------- /google/protobuf/descriptor_database.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """Provides a container for DescriptorProtos.""" 32 | 33 | __author__ = 'matthewtoia@google.com (Matt Toia)' 34 | 35 | 36 | class Error(Exception): 37 | pass 38 | 39 | 40 | class DescriptorDatabaseConflictingDefinitionError(Error): 41 | """Raised when a proto is added with the same name & different descriptor.""" 42 | 43 | 44 | class DescriptorDatabase(object): 45 | """A container accepting FileDescriptorProtos and maps DescriptorProtos.""" 46 | 47 | def __init__(self): 48 | self._file_desc_protos_by_file = {} 49 | self._file_desc_protos_by_symbol = {} 50 | 51 | def Add(self, file_desc_proto): 52 | """Adds the FileDescriptorProto and its types to this database. 53 | 54 | Args: 55 | file_desc_proto: The FileDescriptorProto to add. 56 | Raises: 57 | DescriptorDatabaseException: if an attempt is made to add a proto 58 | with the same name but different definition than an exisiting 59 | proto in the database. 60 | """ 61 | proto_name = file_desc_proto.name 62 | if proto_name not in self._file_desc_protos_by_file: 63 | self._file_desc_protos_by_file[proto_name] = file_desc_proto 64 | elif self._file_desc_protos_by_file[proto_name] != file_desc_proto: 65 | raise DescriptorDatabaseConflictingDefinitionError( 66 | '%s already added, but with different descriptor.' % proto_name) 67 | 68 | package = file_desc_proto.package 69 | for message in file_desc_proto.message_type: 70 | self._file_desc_protos_by_symbol.update( 71 | (name, file_desc_proto) for name in _ExtractSymbols(message, package)) 72 | for enum in file_desc_proto.enum_type: 73 | self._file_desc_protos_by_symbol[ 74 | '.'.join((package, enum.name))] = file_desc_proto 75 | 76 | def FindFileByName(self, name): 77 | """Finds the file descriptor proto by file name. 78 | 79 | Typically the file name is a relative path ending to a .proto file. The 80 | proto with the given name will have to have been added to this database 81 | using the Add method or else an error will be raised. 82 | 83 | Args: 84 | name: The file name to find. 85 | 86 | Returns: 87 | The file descriptor proto matching the name. 88 | 89 | Raises: 90 | KeyError if no file by the given name was added. 91 | """ 92 | 93 | return self._file_desc_protos_by_file[name] 94 | 95 | def FindFileContainingSymbol(self, symbol): 96 | """Finds the file descriptor proto containing the specified symbol. 97 | 98 | The symbol should be a fully qualified name including the file descriptor's 99 | package and any containing messages. Some examples: 100 | 101 | 'some.package.name.Message' 102 | 'some.package.name.Message.NestedEnum' 103 | 104 | The file descriptor proto containing the specified symbol must be added to 105 | this database using the Add method or else an error will be raised. 106 | 107 | Args: 108 | symbol: The fully qualified symbol name. 109 | 110 | Returns: 111 | The file descriptor proto containing the symbol. 112 | 113 | Raises: 114 | KeyError if no file contains the specified symbol. 115 | """ 116 | 117 | return self._file_desc_protos_by_symbol[symbol] 118 | 119 | 120 | def _ExtractSymbols(desc_proto, package): 121 | """Pulls out all the symbols from a descriptor proto. 122 | 123 | Args: 124 | desc_proto: The proto to extract symbols from. 125 | package: The package containing the descriptor type. 126 | 127 | Yields: 128 | The fully qualified name found in the descriptor. 129 | """ 130 | 131 | message_name = '.'.join((package, desc_proto.name)) 132 | yield message_name 133 | for nested_type in desc_proto.nested_type: 134 | for symbol in _ExtractSymbols(nested_type, message_name): 135 | yield symbol 136 | for enum_type in desc_proto.enum_type: 137 | yield '.'.join((message_name, enum_type.name)) 138 | -------------------------------------------------------------------------------- /google/protobuf/internal/service_reflection_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Tests for google.protobuf.internal.service_reflection.""" 34 | 35 | __author__ = 'petar@google.com (Petar Petrov)' 36 | 37 | from google.apputils import basetest 38 | from google.protobuf import unittest_pb2 39 | from google.protobuf import service_reflection 40 | from google.protobuf import service 41 | 42 | 43 | class FooUnitTest(basetest.TestCase): 44 | 45 | def testService(self): 46 | class MockRpcChannel(service.RpcChannel): 47 | def CallMethod(self, method, controller, request, response, callback): 48 | self.method = method 49 | self.controller = controller 50 | self.request = request 51 | callback(response) 52 | 53 | class MockRpcController(service.RpcController): 54 | def SetFailed(self, msg): 55 | self.failure_message = msg 56 | 57 | self.callback_response = None 58 | 59 | class MyService(unittest_pb2.TestService): 60 | pass 61 | 62 | self.callback_response = None 63 | 64 | def MyCallback(response): 65 | self.callback_response = response 66 | 67 | rpc_controller = MockRpcController() 68 | channel = MockRpcChannel() 69 | srvc = MyService() 70 | srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback) 71 | self.assertEqual('Method Foo not implemented.', 72 | rpc_controller.failure_message) 73 | self.assertEqual(None, self.callback_response) 74 | 75 | rpc_controller.failure_message = None 76 | 77 | service_descriptor = unittest_pb2.TestService.GetDescriptor() 78 | srvc.CallMethod(service_descriptor.methods[1], rpc_controller, 79 | unittest_pb2.BarRequest(), MyCallback) 80 | self.assertEqual('Method Bar not implemented.', 81 | rpc_controller.failure_message) 82 | self.assertEqual(None, self.callback_response) 83 | 84 | class MyServiceImpl(unittest_pb2.TestService): 85 | def Foo(self, rpc_controller, request, done): 86 | self.foo_called = True 87 | def Bar(self, rpc_controller, request, done): 88 | self.bar_called = True 89 | 90 | srvc = MyServiceImpl() 91 | rpc_controller.failure_message = None 92 | srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback) 93 | self.assertEqual(None, rpc_controller.failure_message) 94 | self.assertEqual(True, srvc.foo_called) 95 | 96 | rpc_controller.failure_message = None 97 | srvc.CallMethod(service_descriptor.methods[1], rpc_controller, 98 | unittest_pb2.BarRequest(), MyCallback) 99 | self.assertEqual(None, rpc_controller.failure_message) 100 | self.assertEqual(True, srvc.bar_called) 101 | 102 | def testServiceStub(self): 103 | class MockRpcChannel(service.RpcChannel): 104 | def CallMethod(self, method, controller, request, 105 | response_class, callback): 106 | self.method = method 107 | self.controller = controller 108 | self.request = request 109 | callback(response_class()) 110 | 111 | self.callback_response = None 112 | 113 | def MyCallback(response): 114 | self.callback_response = response 115 | 116 | channel = MockRpcChannel() 117 | stub = unittest_pb2.TestService_Stub(channel) 118 | rpc_controller = 'controller' 119 | request = 'request' 120 | 121 | # GetDescriptor now static, still works as instance method for compatability 122 | self.assertEqual(unittest_pb2.TestService_Stub.GetDescriptor(), 123 | stub.GetDescriptor()) 124 | 125 | # Invoke method. 126 | stub.Foo(rpc_controller, request, MyCallback) 127 | 128 | self.assertTrue(isinstance(self.callback_response, 129 | unittest_pb2.FooResponse)) 130 | self.assertEqual(request, channel.request) 131 | self.assertEqual(rpc_controller, channel.controller) 132 | self.assertEqual(stub.GetDescriptor().methods[0], channel.method) 133 | 134 | 135 | if __name__ == '__main__': 136 | basetest.main() 137 | -------------------------------------------------------------------------------- /google/protobuf/internal/message_factory_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # 3 | # Protocol Buffers - Google's data interchange format 4 | # Copyright 2008 Google Inc. All rights reserved. 5 | # https://developers.google.com/protocol-buffers/ 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are 9 | # met: 10 | # 11 | # * Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # * Redistributions in binary form must reproduce the above 14 | # copyright notice, this list of conditions and the following disclaimer 15 | # in the documentation and/or other materials provided with the 16 | # distribution. 17 | # * Neither the name of Google Inc. nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | """Tests for google.protobuf.message_factory.""" 34 | 35 | __author__ = 'matthewtoia@google.com (Matt Toia)' 36 | 37 | from google.apputils import basetest 38 | from google.protobuf import descriptor_pb2 39 | from google.protobuf.internal import factory_test1_pb2 40 | from google.protobuf.internal import factory_test2_pb2 41 | from google.protobuf import descriptor_database 42 | from google.protobuf import descriptor_pool 43 | from google.protobuf import message_factory 44 | 45 | 46 | class MessageFactoryTest(basetest.TestCase): 47 | 48 | def setUp(self): 49 | self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString( 50 | factory_test1_pb2.DESCRIPTOR.serialized_pb) 51 | self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString( 52 | factory_test2_pb2.DESCRIPTOR.serialized_pb) 53 | 54 | def _ExerciseDynamicClass(self, cls): 55 | msg = cls() 56 | msg.mandatory = 42 57 | msg.nested_factory_2_enum = 0 58 | msg.nested_factory_2_message.value = 'nested message value' 59 | msg.factory_1_message.factory_1_enum = 1 60 | msg.factory_1_message.nested_factory_1_enum = 0 61 | msg.factory_1_message.nested_factory_1_message.value = ( 62 | 'nested message value') 63 | msg.factory_1_message.scalar_value = 22 64 | msg.factory_1_message.list_value.extend([u'one', u'two', u'three']) 65 | msg.factory_1_message.list_value.append(u'four') 66 | msg.factory_1_enum = 1 67 | msg.nested_factory_1_enum = 0 68 | msg.nested_factory_1_message.value = 'nested message value' 69 | msg.circular_message.mandatory = 1 70 | msg.circular_message.circular_message.mandatory = 2 71 | msg.circular_message.scalar_value = 'one deep' 72 | msg.scalar_value = 'zero deep' 73 | msg.list_value.extend([u'four', u'three', u'two']) 74 | msg.list_value.append(u'one') 75 | msg.grouped.add() 76 | msg.grouped[0].part_1 = 'hello' 77 | msg.grouped[0].part_2 = 'world' 78 | msg.grouped.add(part_1='testing', part_2='123') 79 | msg.loop.loop.mandatory = 2 80 | msg.loop.loop.loop.loop.mandatory = 4 81 | serialized = msg.SerializeToString() 82 | converted = factory_test2_pb2.Factory2Message.FromString(serialized) 83 | reserialized = converted.SerializeToString() 84 | self.assertEquals(serialized, reserialized) 85 | result = cls.FromString(reserialized) 86 | self.assertEquals(msg, result) 87 | 88 | def testGetPrototype(self): 89 | db = descriptor_database.DescriptorDatabase() 90 | pool = descriptor_pool.DescriptorPool(db) 91 | db.Add(self.factory_test1_fd) 92 | db.Add(self.factory_test2_fd) 93 | factory = message_factory.MessageFactory() 94 | cls = factory.GetPrototype(pool.FindMessageTypeByName( 95 | 'google.protobuf.python.internal.Factory2Message')) 96 | self.assertIsNot(cls, factory_test2_pb2.Factory2Message) 97 | self._ExerciseDynamicClass(cls) 98 | cls2 = factory.GetPrototype(pool.FindMessageTypeByName( 99 | 'google.protobuf.python.internal.Factory2Message')) 100 | self.assertIs(cls, cls2) 101 | 102 | def testGetMessages(self): 103 | # performed twice because multiple calls with the same input must be allowed 104 | for _ in range(2): 105 | messages = message_factory.GetMessages([self.factory_test2_fd, 106 | self.factory_test1_fd]) 107 | self.assertContainsSubset( 108 | ['google.protobuf.python.internal.Factory2Message', 109 | 'google.protobuf.python.internal.Factory1Message'], 110 | messages.keys()) 111 | self._ExerciseDynamicClass( 112 | messages['google.protobuf.python.internal.Factory2Message']) 113 | self.assertContainsSubset( 114 | ['google.protobuf.python.internal.Factory2Message.one_more_field', 115 | 'google.protobuf.python.internal.another_field'], 116 | (messages['google.protobuf.python.internal.Factory1Message'] 117 | ._extensions_by_name.keys())) 118 | factory_msg1 = messages['google.protobuf.python.internal.Factory1Message'] 119 | msg1 = messages['google.protobuf.python.internal.Factory1Message']() 120 | ext1 = factory_msg1._extensions_by_name[ 121 | 'google.protobuf.python.internal.Factory2Message.one_more_field'] 122 | ext2 = factory_msg1._extensions_by_name[ 123 | 'google.protobuf.python.internal.another_field'] 124 | msg1.Extensions[ext1] = 'test1' 125 | msg1.Extensions[ext2] = 'test2' 126 | self.assertEquals('test1', msg1.Extensions[ext1]) 127 | self.assertEquals('test2', msg1.Extensions[ext2]) 128 | 129 | 130 | if __name__ == '__main__': 131 | basetest.main() 132 | -------------------------------------------------------------------------------- /proto/game_service_pb2.py: -------------------------------------------------------------------------------- 1 | # Generated by the protocol buffer compiler. DO NOT EDIT! 2 | # source: game_service.proto 3 | 4 | import sys 5 | _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) 6 | from google.protobuf import descriptor as _descriptor 7 | from google.protobuf import message as _message 8 | from google.protobuf import reflection as _reflection 9 | from google.protobuf import symbol_database as _symbol_database 10 | from google.protobuf import service as _service 11 | from google.protobuf import service_reflection 12 | from google.protobuf import descriptor_pb2 13 | # @@protoc_insertion_point(imports) 14 | 15 | _sym_db = _symbol_database.Default() 16 | 17 | 18 | 19 | 20 | DESCRIPTOR = _descriptor.FileDescriptor( 21 | name='game_service.proto', 22 | package='pluto', 23 | serialized_pb=_b('\n\x12game_service.proto\x12\x05pluto\"\x06\n\x04Void\"\x1d\n\x0eRequestMessage\x12\x0b\n\x03msg\x18\x01 \x02(\t\"\x1e\n\x0fResponseMessage\x12\x0b\n\x03msg\x18\x01 \x02(\t2:\n\x0cIEchoService\x12*\n\x04\x65\x63ho\x12\x15.pluto.RequestMessage\x1a\x0b.pluto.Void2@\n\x0bIEchoClient\x12\x31\n\necho_reply\x12\x16.pluto.ResponseMessage\x1a\x0b.pluto.VoidB\x03\x90\x01\x01') 24 | ) 25 | _sym_db.RegisterFileDescriptor(DESCRIPTOR) 26 | 27 | 28 | 29 | 30 | _VOID = _descriptor.Descriptor( 31 | name='Void', 32 | full_name='pluto.Void', 33 | filename=None, 34 | file=DESCRIPTOR, 35 | containing_type=None, 36 | fields=[ 37 | ], 38 | extensions=[ 39 | ], 40 | nested_types=[], 41 | enum_types=[ 42 | ], 43 | options=None, 44 | is_extendable=False, 45 | extension_ranges=[], 46 | oneofs=[ 47 | ], 48 | serialized_start=29, 49 | serialized_end=35, 50 | ) 51 | 52 | 53 | _REQUESTMESSAGE = _descriptor.Descriptor( 54 | name='RequestMessage', 55 | full_name='pluto.RequestMessage', 56 | filename=None, 57 | file=DESCRIPTOR, 58 | containing_type=None, 59 | fields=[ 60 | _descriptor.FieldDescriptor( 61 | name='msg', full_name='pluto.RequestMessage.msg', index=0, 62 | number=1, type=9, cpp_type=9, label=2, 63 | has_default_value=False, default_value=_b("").decode('utf-8'), 64 | message_type=None, enum_type=None, containing_type=None, 65 | is_extension=False, extension_scope=None, 66 | options=None), 67 | ], 68 | extensions=[ 69 | ], 70 | nested_types=[], 71 | enum_types=[ 72 | ], 73 | options=None, 74 | is_extendable=False, 75 | extension_ranges=[], 76 | oneofs=[ 77 | ], 78 | serialized_start=37, 79 | serialized_end=66, 80 | ) 81 | 82 | 83 | _RESPONSEMESSAGE = _descriptor.Descriptor( 84 | name='ResponseMessage', 85 | full_name='pluto.ResponseMessage', 86 | filename=None, 87 | file=DESCRIPTOR, 88 | containing_type=None, 89 | fields=[ 90 | _descriptor.FieldDescriptor( 91 | name='msg', full_name='pluto.ResponseMessage.msg', index=0, 92 | number=1, type=9, cpp_type=9, label=2, 93 | has_default_value=False, default_value=_b("").decode('utf-8'), 94 | message_type=None, enum_type=None, containing_type=None, 95 | is_extension=False, extension_scope=None, 96 | options=None), 97 | ], 98 | extensions=[ 99 | ], 100 | nested_types=[], 101 | enum_types=[ 102 | ], 103 | options=None, 104 | is_extendable=False, 105 | extension_ranges=[], 106 | oneofs=[ 107 | ], 108 | serialized_start=68, 109 | serialized_end=98, 110 | ) 111 | 112 | DESCRIPTOR.message_types_by_name['Void'] = _VOID 113 | DESCRIPTOR.message_types_by_name['RequestMessage'] = _REQUESTMESSAGE 114 | DESCRIPTOR.message_types_by_name['ResponseMessage'] = _RESPONSEMESSAGE 115 | 116 | Void = _reflection.GeneratedProtocolMessageType('Void', (_message.Message,), dict( 117 | DESCRIPTOR = _VOID, 118 | __module__ = 'game_service_pb2' 119 | # @@protoc_insertion_point(class_scope:pluto.Void) 120 | )) 121 | _sym_db.RegisterMessage(Void) 122 | 123 | RequestMessage = _reflection.GeneratedProtocolMessageType('RequestMessage', (_message.Message,), dict( 124 | DESCRIPTOR = _REQUESTMESSAGE, 125 | __module__ = 'game_service_pb2' 126 | # @@protoc_insertion_point(class_scope:pluto.RequestMessage) 127 | )) 128 | _sym_db.RegisterMessage(RequestMessage) 129 | 130 | ResponseMessage = _reflection.GeneratedProtocolMessageType('ResponseMessage', (_message.Message,), dict( 131 | DESCRIPTOR = _RESPONSEMESSAGE, 132 | __module__ = 'game_service_pb2' 133 | # @@protoc_insertion_point(class_scope:pluto.ResponseMessage) 134 | )) 135 | _sym_db.RegisterMessage(ResponseMessage) 136 | 137 | 138 | DESCRIPTOR.has_options = True 139 | DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), _b('\220\001\001')) 140 | 141 | _IECHOSERVICE = _descriptor.ServiceDescriptor( 142 | name='IEchoService', 143 | full_name='pluto.IEchoService', 144 | file=DESCRIPTOR, 145 | index=0, 146 | options=None, 147 | serialized_start=100, 148 | serialized_end=158, 149 | methods=[ 150 | _descriptor.MethodDescriptor( 151 | name='echo', 152 | full_name='pluto.IEchoService.echo', 153 | index=0, 154 | containing_service=None, 155 | input_type=_REQUESTMESSAGE, 156 | output_type=_VOID, 157 | options=None, 158 | ), 159 | ]) 160 | 161 | IEchoService = service_reflection.GeneratedServiceType('IEchoService', (_service.Service,), dict( 162 | DESCRIPTOR = _IECHOSERVICE, 163 | __module__ = 'game_service_pb2' 164 | )) 165 | 166 | IEchoService_Stub = service_reflection.GeneratedServiceStubType('IEchoService_Stub', (IEchoService,), dict( 167 | DESCRIPTOR = _IECHOSERVICE, 168 | __module__ = 'game_service_pb2' 169 | )) 170 | 171 | 172 | 173 | _IECHOCLIENT = _descriptor.ServiceDescriptor( 174 | name='IEchoClient', 175 | full_name='pluto.IEchoClient', 176 | file=DESCRIPTOR, 177 | index=1, 178 | options=None, 179 | serialized_start=160, 180 | serialized_end=224, 181 | methods=[ 182 | _descriptor.MethodDescriptor( 183 | name='echo_reply', 184 | full_name='pluto.IEchoClient.echo_reply', 185 | index=0, 186 | containing_service=None, 187 | input_type=_RESPONSEMESSAGE, 188 | output_type=_VOID, 189 | options=None, 190 | ), 191 | ]) 192 | 193 | IEchoClient = service_reflection.GeneratedServiceType('IEchoClient', (_service.Service,), dict( 194 | DESCRIPTOR = _IECHOCLIENT, 195 | __module__ = 'game_service_pb2' 196 | )) 197 | 198 | IEchoClient_Stub = service_reflection.GeneratedServiceStubType('IEchoClient_Stub', (IEchoClient,), dict( 199 | DESCRIPTOR = _IECHOCLIENT, 200 | __module__ = 'game_service_pb2' 201 | )) 202 | 203 | 204 | # @@protoc_insertion_point(module_scope) 205 | -------------------------------------------------------------------------------- /google/protobuf/symbol_database.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """A database of Python protocol buffer generated symbols. 32 | 33 | SymbolDatabase makes it easy to create new instances of a registered type, given 34 | only the type's protocol buffer symbol name. Once all symbols are registered, 35 | they can be accessed using either the MessageFactory interface which 36 | SymbolDatabase exposes, or the DescriptorPool interface of the underlying 37 | pool. 38 | 39 | Example usage: 40 | 41 | db = symbol_database.SymbolDatabase() 42 | 43 | # Register symbols of interest, from one or multiple files. 44 | db.RegisterFileDescriptor(my_proto_pb2.DESCRIPTOR) 45 | db.RegisterMessage(my_proto_pb2.MyMessage) 46 | db.RegisterEnumDescriptor(my_proto_pb2.MyEnum.DESCRIPTOR) 47 | 48 | # The database can be used as a MessageFactory, to generate types based on 49 | # their name: 50 | types = db.GetMessages(['my_proto.proto']) 51 | my_message_instance = types['MyMessage']() 52 | 53 | # The database's underlying descriptor pool can be queried, so it's not 54 | # necessary to know a type's filename to be able to generate it: 55 | filename = db.pool.FindFileContainingSymbol('MyMessage') 56 | my_message_instance = db.GetMessages([filename])['MyMessage']() 57 | 58 | # This functionality is also provided directly via a convenience method: 59 | my_message_instance = db.GetSymbol('MyMessage')() 60 | """ 61 | 62 | 63 | from google.protobuf import descriptor_pool 64 | 65 | 66 | class SymbolDatabase(object): 67 | """A database of Python generated symbols. 68 | 69 | SymbolDatabase also models message_factory.MessageFactory. 70 | 71 | The symbol database can be used to keep a global registry of all protocol 72 | buffer types used within a program. 73 | """ 74 | 75 | def __init__(self): 76 | """Constructor.""" 77 | 78 | self._symbols = {} 79 | self._symbols_by_file = {} 80 | self.pool = descriptor_pool.DescriptorPool() 81 | 82 | def RegisterMessage(self, message): 83 | """Registers the given message type in the local database. 84 | 85 | Args: 86 | message: a message.Message, to be registered. 87 | 88 | Returns: 89 | The provided message. 90 | """ 91 | 92 | desc = message.DESCRIPTOR 93 | self._symbols[desc.full_name] = message 94 | if desc.file.name not in self._symbols_by_file: 95 | self._symbols_by_file[desc.file.name] = {} 96 | self._symbols_by_file[desc.file.name][desc.full_name] = message 97 | self.pool.AddDescriptor(desc) 98 | return message 99 | 100 | def RegisterEnumDescriptor(self, enum_descriptor): 101 | """Registers the given enum descriptor in the local database. 102 | 103 | Args: 104 | enum_descriptor: a descriptor.EnumDescriptor. 105 | 106 | Returns: 107 | The provided descriptor. 108 | """ 109 | self.pool.AddEnumDescriptor(enum_descriptor) 110 | return enum_descriptor 111 | 112 | def RegisterFileDescriptor(self, file_descriptor): 113 | """Registers the given file descriptor in the local database. 114 | 115 | Args: 116 | file_descriptor: a descriptor.FileDescriptor. 117 | 118 | Returns: 119 | The provided descriptor. 120 | """ 121 | self.pool.AddFileDescriptor(file_descriptor) 122 | 123 | def GetSymbol(self, symbol): 124 | """Tries to find a symbol in the local database. 125 | 126 | Currently, this method only returns message.Message instances, however, if 127 | may be extended in future to support other symbol types. 128 | 129 | Args: 130 | symbol: A str, a protocol buffer symbol. 131 | 132 | Returns: 133 | A Python class corresponding to the symbol. 134 | 135 | Raises: 136 | KeyError: if the symbol could not be found. 137 | """ 138 | 139 | return self._symbols[symbol] 140 | 141 | def GetPrototype(self, descriptor): 142 | """Builds a proto2 message class based on the passed in descriptor. 143 | 144 | Passing a descriptor with a fully qualified name matching a previous 145 | invocation will cause the same class to be returned. 146 | 147 | Args: 148 | descriptor: The descriptor to build from. 149 | 150 | Returns: 151 | A class describing the passed in descriptor. 152 | """ 153 | 154 | return self.GetSymbol(descriptor.full_name) 155 | 156 | def GetMessages(self, files): 157 | """Gets all the messages from a specified file. 158 | 159 | This will find and resolve dependencies, failing if they are not registered 160 | in the symbol database. 161 | 162 | 163 | Args: 164 | files: The file names to extract messages from. 165 | 166 | Returns: 167 | A dictionary mapping proto names to the message classes. This will include 168 | any dependent messages as well as any messages defined in the same file as 169 | a specified message. 170 | 171 | Raises: 172 | KeyError: if a file could not be found. 173 | """ 174 | 175 | result = {} 176 | for f in files: 177 | result.update(self._symbols_by_file[f]) 178 | return result 179 | 180 | _DEFAULT = SymbolDatabase() 181 | 182 | 183 | def Default(): 184 | """Returns the default SymbolDatabase.""" 185 | return _DEFAULT 186 | -------------------------------------------------------------------------------- /google/protobuf/message_factory.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | #PY25 compatible for GAE. 32 | # 33 | # Copyright 2012 Google Inc. All Rights Reserved. 34 | 35 | """Provides a factory class for generating dynamic messages. 36 | 37 | The easiest way to use this class is if you have access to the FileDescriptor 38 | protos containing the messages you want to create you can just do the following: 39 | 40 | message_classes = message_factory.GetMessages(iterable_of_file_descriptors) 41 | my_proto_instance = message_classes['some.proto.package.MessageName']() 42 | """ 43 | 44 | __author__ = 'matthewtoia@google.com (Matt Toia)' 45 | 46 | import sys ##PY25 47 | from google.protobuf import descriptor_database 48 | from google.protobuf import descriptor_pool 49 | from google.protobuf import message 50 | from google.protobuf import reflection 51 | 52 | 53 | class MessageFactory(object): 54 | """Factory for creating Proto2 messages from descriptors in a pool.""" 55 | 56 | def __init__(self, pool=None): 57 | """Initializes a new factory.""" 58 | self.pool = (pool or descriptor_pool.DescriptorPool( 59 | descriptor_database.DescriptorDatabase())) 60 | 61 | # local cache of all classes built from protobuf descriptors 62 | self._classes = {} 63 | 64 | def GetPrototype(self, descriptor): 65 | """Builds a proto2 message class based on the passed in descriptor. 66 | 67 | Passing a descriptor with a fully qualified name matching a previous 68 | invocation will cause the same class to be returned. 69 | 70 | Args: 71 | descriptor: The descriptor to build from. 72 | 73 | Returns: 74 | A class describing the passed in descriptor. 75 | """ 76 | if descriptor.full_name not in self._classes: 77 | descriptor_name = descriptor.name 78 | if sys.version_info[0] < 3: ##PY25 79 | ##!PY25 if str is bytes: # PY2 80 | descriptor_name = descriptor.name.encode('ascii', 'ignore') 81 | result_class = reflection.GeneratedProtocolMessageType( 82 | descriptor_name, 83 | (message.Message,), 84 | {'DESCRIPTOR': descriptor, '__module__': None}) 85 | # If module not set, it wrongly points to the reflection.py module. 86 | self._classes[descriptor.full_name] = result_class 87 | for field in descriptor.fields: 88 | if field.message_type: 89 | self.GetPrototype(field.message_type) 90 | for extension in result_class.DESCRIPTOR.extensions: 91 | if extension.containing_type.full_name not in self._classes: 92 | self.GetPrototype(extension.containing_type) 93 | extended_class = self._classes[extension.containing_type.full_name] 94 | extended_class.RegisterExtension(extension) 95 | return self._classes[descriptor.full_name] 96 | 97 | def GetMessages(self, files): 98 | """Gets all the messages from a specified file. 99 | 100 | This will find and resolve dependencies, failing if the descriptor 101 | pool cannot satisfy them. 102 | 103 | Args: 104 | files: The file names to extract messages from. 105 | 106 | Returns: 107 | A dictionary mapping proto names to the message classes. This will include 108 | any dependent messages as well as any messages defined in the same file as 109 | a specified message. 110 | """ 111 | result = {} 112 | for file_name in files: 113 | file_desc = self.pool.FindFileByName(file_name) 114 | for name, msg in file_desc.message_types_by_name.iteritems(): 115 | if file_desc.package: 116 | full_name = '.'.join([file_desc.package, name]) 117 | else: 118 | full_name = msg.name 119 | result[full_name] = self.GetPrototype( 120 | self.pool.FindMessageTypeByName(full_name)) 121 | 122 | # While the extension FieldDescriptors are created by the descriptor pool, 123 | # the python classes created in the factory need them to be registered 124 | # explicitly, which is done below. 125 | # 126 | # The call to RegisterExtension will specifically check if the 127 | # extension was already registered on the object and either 128 | # ignore the registration if the original was the same, or raise 129 | # an error if they were different. 130 | 131 | for name, extension in file_desc.extensions_by_name.iteritems(): 132 | if extension.containing_type.full_name not in self._classes: 133 | self.GetPrototype(extension.containing_type) 134 | extended_class = self._classes[extension.containing_type.full_name] 135 | extended_class.RegisterExtension(extension) 136 | return result 137 | 138 | 139 | _FACTORY = MessageFactory() 140 | 141 | 142 | def GetMessages(file_protos): 143 | """Builds a dictionary of all the messages available in a set of files. 144 | 145 | Args: 146 | file_protos: A sequence of file protos to build messages out of. 147 | 148 | Returns: 149 | A dictionary mapping proto names to the message classes. This will include 150 | any dependent messages as well as any messages defined in the same file as 151 | a specified message. 152 | """ 153 | for file_proto in file_protos: 154 | _FACTORY.pool.Add(file_proto) 155 | return _FACTORY.GetMessages([file_proto.name for file_proto in file_protos]) 156 | -------------------------------------------------------------------------------- /google/protobuf/pyext/repeated_composite_container.h: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: anuraag@google.com (Anuraag Agrawal) 32 | // Author: tibell@google.com (Johan Tibell) 33 | 34 | #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__ 35 | #define GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__ 36 | 37 | #include 38 | 39 | #include 40 | #ifndef _SHARED_PTR_H 41 | #include 42 | #endif 43 | #include 44 | #include 45 | 46 | 47 | namespace google { 48 | namespace protobuf { 49 | 50 | class FieldDescriptor; 51 | class Message; 52 | 53 | using internal::shared_ptr; 54 | 55 | namespace python { 56 | 57 | struct CMessage; 58 | struct CFieldDescriptor; 59 | 60 | // A RepeatedCompositeContainer can be in one of two states: attached 61 | // or released. 62 | // 63 | // When in the attached state all modifications to the container are 64 | // done both on the 'message' and on the 'child_messages' 65 | // list. In this state all Messages refered to by the children in 66 | // 'child_messages' are owner by the 'owner'. 67 | // 68 | // When in the released state 'message', 'owner', 'parent', and 69 | // 'parent_field' are NULL. 70 | typedef struct RepeatedCompositeContainer { 71 | PyObject_HEAD; 72 | 73 | // This is the top-level C++ Message object that owns the whole 74 | // proto tree. Every Python RepeatedCompositeContainer holds a 75 | // reference to it in order to keep it alive as long as there's a 76 | // Python object that references any part of the tree. 77 | shared_ptr owner; 78 | 79 | // Weak reference to parent object. May be NULL. Used to make sure 80 | // the parent is writable before modifying the 81 | // RepeatedCompositeContainer. 82 | CMessage* parent; 83 | 84 | // A descriptor used to modify the underlying 'message'. 85 | CFieldDescriptor* parent_field; 86 | 87 | // Pointer to the C++ Message that contains this container. The 88 | // RepeatedCompositeContainer does not own this pointer. 89 | // 90 | // If NULL, this message has been released from its parent (by 91 | // calling Clear() or ClearField() on the parent. 92 | Message* message; 93 | 94 | // A callable that is used to create new child messages. 95 | PyObject* subclass_init; 96 | 97 | // A list of child messages. 98 | PyObject* child_messages; 99 | } RepeatedCompositeContainer; 100 | 101 | extern PyTypeObject RepeatedCompositeContainer_Type; 102 | 103 | namespace repeated_composite_container { 104 | 105 | // Returns the number of items in this repeated composite container. 106 | static Py_ssize_t Length(RepeatedCompositeContainer* self); 107 | 108 | // Appends a new CMessage to the container and returns it. The 109 | // CMessage is initialized using the content of kwargs. 110 | // 111 | // Returns a new reference if successful; returns NULL and sets an 112 | // exception if unsuccessful. 113 | PyObject* Add(RepeatedCompositeContainer* self, 114 | PyObject* args, 115 | PyObject* kwargs); 116 | 117 | // Appends all the CMessages in the input iterator to the container. 118 | // 119 | // Returns None if successful; returns NULL and sets an exception if 120 | // unsuccessful. 121 | PyObject* Extend(RepeatedCompositeContainer* self, PyObject* value); 122 | 123 | // Appends a new message to the container for each message in the 124 | // input iterator, merging each data element in. Equivalent to extend. 125 | // 126 | // Returns None if successful; returns NULL and sets an exception if 127 | // unsuccessful. 128 | PyObject* MergeFrom(RepeatedCompositeContainer* self, PyObject* other); 129 | 130 | // Accesses messages in the container. 131 | // 132 | // Returns a new reference to the message for an integer parameter. 133 | // Returns a new reference to a list of messages for a slice. 134 | PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* slice); 135 | 136 | // Deletes items from the container (cannot be used for assignment). 137 | // 138 | // Returns 0 on success, -1 on failure. 139 | int AssignSubscript(RepeatedCompositeContainer* self, 140 | PyObject* slice, 141 | PyObject* value); 142 | 143 | // Releases the messages in the container to the given message. 144 | // 145 | // Returns 0 on success, -1 on failure. 146 | int ReleaseToMessage(RepeatedCompositeContainer* self, 147 | google::protobuf::Message* new_message); 148 | 149 | // Releases the messages in the container to a new message. 150 | // 151 | // Returns 0 on success, -1 on failure. 152 | int Release(RepeatedCompositeContainer* self); 153 | 154 | // Returns 0 on success, -1 on failure. 155 | int SetOwner(RepeatedCompositeContainer* self, 156 | const shared_ptr& new_owner); 157 | 158 | // Removes the last element of the repeated message field 'field' on 159 | // the Message 'message', and transfers the ownership of the released 160 | // Message to 'cmessage'. 161 | // 162 | // Corresponds to reflection api method ReleaseMessage. 163 | void ReleaseLastTo(const FieldDescriptor* field, 164 | Message* message, 165 | CMessage* cmessage); 166 | 167 | } // namespace repeated_composite_container 168 | } // namespace python 169 | } // namespace protobuf 170 | 171 | } // namespace google 172 | #endif // GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__ 173 | -------------------------------------------------------------------------------- /google/protobuf/compiler/plugin_pb2.py: -------------------------------------------------------------------------------- 1 | # Generated by the protocol buffer compiler. DO NOT EDIT! 2 | # source: google/protobuf/compiler/plugin.proto 3 | 4 | from google.protobuf import descriptor as _descriptor 5 | from google.protobuf import message as _message 6 | from google.protobuf import reflection as _reflection 7 | from google.protobuf import descriptor_pb2 8 | # @@protoc_insertion_point(imports) 9 | 10 | 11 | import google.protobuf.descriptor_pb2 12 | 13 | 14 | DESCRIPTOR = _descriptor.FileDescriptor( 15 | name='google/protobuf/compiler/plugin.proto', 16 | package='google.protobuf.compiler', 17 | serialized_pb='\n%google/protobuf/compiler/plugin.proto\x12\x18google.protobuf.compiler\x1a google/protobuf/descriptor.proto\"}\n\x14\x43odeGeneratorRequest\x12\x18\n\x10\x66ile_to_generate\x18\x01 \x03(\t\x12\x11\n\tparameter\x18\x02 \x01(\t\x12\x38\n\nproto_file\x18\x0f \x03(\x0b\x32$.google.protobuf.FileDescriptorProto\"\xaa\x01\n\x15\x43odeGeneratorResponse\x12\r\n\x05\x65rror\x18\x01 \x01(\t\x12\x42\n\x04\x66ile\x18\x0f \x03(\x0b\x32\x34.google.protobuf.compiler.CodeGeneratorResponse.File\x1a>\n\x04\x46ile\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x17\n\x0finsertion_point\x18\x02 \x01(\t\x12\x0f\n\x07\x63ontent\x18\x0f \x01(\tB,\n\x1c\x63om.google.protobuf.compilerB\x0cPluginProtos') 18 | 19 | 20 | 21 | 22 | _CODEGENERATORREQUEST = _descriptor.Descriptor( 23 | name='CodeGeneratorRequest', 24 | full_name='google.protobuf.compiler.CodeGeneratorRequest', 25 | filename=None, 26 | file=DESCRIPTOR, 27 | containing_type=None, 28 | fields=[ 29 | _descriptor.FieldDescriptor( 30 | name='file_to_generate', full_name='google.protobuf.compiler.CodeGeneratorRequest.file_to_generate', index=0, 31 | number=1, type=9, cpp_type=9, label=3, 32 | has_default_value=False, default_value=[], 33 | message_type=None, enum_type=None, containing_type=None, 34 | is_extension=False, extension_scope=None, 35 | options=None), 36 | _descriptor.FieldDescriptor( 37 | name='parameter', full_name='google.protobuf.compiler.CodeGeneratorRequest.parameter', index=1, 38 | number=2, type=9, cpp_type=9, label=1, 39 | has_default_value=False, default_value=unicode("", "utf-8"), 40 | message_type=None, enum_type=None, containing_type=None, 41 | is_extension=False, extension_scope=None, 42 | options=None), 43 | _descriptor.FieldDescriptor( 44 | name='proto_file', full_name='google.protobuf.compiler.CodeGeneratorRequest.proto_file', index=2, 45 | number=15, type=11, cpp_type=10, label=3, 46 | has_default_value=False, default_value=[], 47 | message_type=None, enum_type=None, containing_type=None, 48 | is_extension=False, extension_scope=None, 49 | options=None), 50 | ], 51 | extensions=[ 52 | ], 53 | nested_types=[], 54 | enum_types=[ 55 | ], 56 | options=None, 57 | is_extendable=False, 58 | extension_ranges=[], 59 | serialized_start=101, 60 | serialized_end=226, 61 | ) 62 | 63 | 64 | _CODEGENERATORRESPONSE_FILE = _descriptor.Descriptor( 65 | name='File', 66 | full_name='google.protobuf.compiler.CodeGeneratorResponse.File', 67 | filename=None, 68 | file=DESCRIPTOR, 69 | containing_type=None, 70 | fields=[ 71 | _descriptor.FieldDescriptor( 72 | name='name', full_name='google.protobuf.compiler.CodeGeneratorResponse.File.name', index=0, 73 | number=1, type=9, cpp_type=9, label=1, 74 | has_default_value=False, default_value=unicode("", "utf-8"), 75 | message_type=None, enum_type=None, containing_type=None, 76 | is_extension=False, extension_scope=None, 77 | options=None), 78 | _descriptor.FieldDescriptor( 79 | name='insertion_point', full_name='google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point', index=1, 80 | number=2, type=9, cpp_type=9, label=1, 81 | has_default_value=False, default_value=unicode("", "utf-8"), 82 | message_type=None, enum_type=None, containing_type=None, 83 | is_extension=False, extension_scope=None, 84 | options=None), 85 | _descriptor.FieldDescriptor( 86 | name='content', full_name='google.protobuf.compiler.CodeGeneratorResponse.File.content', index=2, 87 | number=15, type=9, cpp_type=9, label=1, 88 | has_default_value=False, default_value=unicode("", "utf-8"), 89 | message_type=None, enum_type=None, containing_type=None, 90 | is_extension=False, extension_scope=None, 91 | options=None), 92 | ], 93 | extensions=[ 94 | ], 95 | nested_types=[], 96 | enum_types=[ 97 | ], 98 | options=None, 99 | is_extendable=False, 100 | extension_ranges=[], 101 | serialized_start=337, 102 | serialized_end=399, 103 | ) 104 | 105 | _CODEGENERATORRESPONSE = _descriptor.Descriptor( 106 | name='CodeGeneratorResponse', 107 | full_name='google.protobuf.compiler.CodeGeneratorResponse', 108 | filename=None, 109 | file=DESCRIPTOR, 110 | containing_type=None, 111 | fields=[ 112 | _descriptor.FieldDescriptor( 113 | name='error', full_name='google.protobuf.compiler.CodeGeneratorResponse.error', index=0, 114 | number=1, type=9, cpp_type=9, label=1, 115 | has_default_value=False, default_value=unicode("", "utf-8"), 116 | message_type=None, enum_type=None, containing_type=None, 117 | is_extension=False, extension_scope=None, 118 | options=None), 119 | _descriptor.FieldDescriptor( 120 | name='file', full_name='google.protobuf.compiler.CodeGeneratorResponse.file', index=1, 121 | number=15, type=11, cpp_type=10, label=3, 122 | has_default_value=False, default_value=[], 123 | message_type=None, enum_type=None, containing_type=None, 124 | is_extension=False, extension_scope=None, 125 | options=None), 126 | ], 127 | extensions=[ 128 | ], 129 | nested_types=[_CODEGENERATORRESPONSE_FILE, ], 130 | enum_types=[ 131 | ], 132 | options=None, 133 | is_extendable=False, 134 | extension_ranges=[], 135 | serialized_start=229, 136 | serialized_end=399, 137 | ) 138 | 139 | _CODEGENERATORREQUEST.fields_by_name['proto_file'].message_type = google.protobuf.descriptor_pb2._FILEDESCRIPTORPROTO 140 | _CODEGENERATORRESPONSE_FILE.containing_type = _CODEGENERATORRESPONSE; 141 | _CODEGENERATORRESPONSE.fields_by_name['file'].message_type = _CODEGENERATORRESPONSE_FILE 142 | DESCRIPTOR.message_types_by_name['CodeGeneratorRequest'] = _CODEGENERATORREQUEST 143 | DESCRIPTOR.message_types_by_name['CodeGeneratorResponse'] = _CODEGENERATORRESPONSE 144 | 145 | class CodeGeneratorRequest(_message.Message): 146 | __metaclass__ = _reflection.GeneratedProtocolMessageType 147 | DESCRIPTOR = _CODEGENERATORREQUEST 148 | 149 | # @@protoc_insertion_point(class_scope:google.protobuf.compiler.CodeGeneratorRequest) 150 | 151 | class CodeGeneratorResponse(_message.Message): 152 | __metaclass__ = _reflection.GeneratedProtocolMessageType 153 | 154 | class File(_message.Message): 155 | __metaclass__ = _reflection.GeneratedProtocolMessageType 156 | DESCRIPTOR = _CODEGENERATORRESPONSE_FILE 157 | 158 | # @@protoc_insertion_point(class_scope:google.protobuf.compiler.CodeGeneratorResponse.File) 159 | DESCRIPTOR = _CODEGENERATORRESPONSE 160 | 161 | # @@protoc_insertion_point(class_scope:google.protobuf.compiler.CodeGeneratorResponse) 162 | 163 | 164 | DESCRIPTOR.has_options = True 165 | DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), '\n\034com.google.protobuf.compilerB\014PluginProtos') 166 | # @@protoc_insertion_point(module_scope) 167 | -------------------------------------------------------------------------------- /google/protobuf/reflection.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | # This code is meant to work on Python 2.4 and above only. 32 | 33 | """Contains a metaclass and helper functions used to create 34 | protocol message classes from Descriptor objects at runtime. 35 | 36 | Recall that a metaclass is the "type" of a class. 37 | (A class is to a metaclass what an instance is to a class.) 38 | 39 | In this case, we use the GeneratedProtocolMessageType metaclass 40 | to inject all the useful functionality into the classes 41 | output by the protocol compiler at compile-time. 42 | 43 | The upshot of all this is that the real implementation 44 | details for ALL pure-Python protocol buffers are *here in 45 | this file*. 46 | """ 47 | 48 | __author__ = 'robinson@google.com (Will Robinson)' 49 | 50 | 51 | from google.protobuf.internal import api_implementation 52 | from google.protobuf import descriptor as descriptor_mod 53 | from google.protobuf import message 54 | 55 | _FieldDescriptor = descriptor_mod.FieldDescriptor 56 | 57 | 58 | if api_implementation.Type() == 'cpp': 59 | if api_implementation.Version() == 2: 60 | from google.protobuf.pyext import cpp_message 61 | _NewMessage = cpp_message.NewMessage 62 | _InitMessage = cpp_message.InitMessage 63 | else: 64 | from google.protobuf.internal import cpp_message 65 | _NewMessage = cpp_message.NewMessage 66 | _InitMessage = cpp_message.InitMessage 67 | else: 68 | from google.protobuf.internal import python_message 69 | _NewMessage = python_message.NewMessage 70 | _InitMessage = python_message.InitMessage 71 | 72 | 73 | class GeneratedProtocolMessageType(type): 74 | 75 | """Metaclass for protocol message classes created at runtime from Descriptors. 76 | 77 | We add implementations for all methods described in the Message class. We 78 | also create properties to allow getting/setting all fields in the protocol 79 | message. Finally, we create slots to prevent users from accidentally 80 | "setting" nonexistent fields in the protocol message, which then wouldn't get 81 | serialized / deserialized properly. 82 | 83 | The protocol compiler currently uses this metaclass to create protocol 84 | message classes at runtime. Clients can also manually create their own 85 | classes at runtime, as in this example: 86 | 87 | mydescriptor = Descriptor(.....) 88 | class MyProtoClass(Message): 89 | __metaclass__ = GeneratedProtocolMessageType 90 | DESCRIPTOR = mydescriptor 91 | myproto_instance = MyProtoClass() 92 | myproto.foo_field = 23 93 | ... 94 | 95 | The above example will not work for nested types. If you wish to include them, 96 | use reflection.MakeClass() instead of manually instantiating the class in 97 | order to create the appropriate class structure. 98 | """ 99 | 100 | # Must be consistent with the protocol-compiler code in 101 | # proto2/compiler/internal/generator.*. 102 | _DESCRIPTOR_KEY = 'DESCRIPTOR' 103 | 104 | def __new__(cls, name, bases, dictionary): 105 | """Custom allocation for runtime-generated class types. 106 | 107 | We override __new__ because this is apparently the only place 108 | where we can meaningfully set __slots__ on the class we're creating(?). 109 | (The interplay between metaclasses and slots is not very well-documented). 110 | 111 | Args: 112 | name: Name of the class (ignored, but required by the 113 | metaclass protocol). 114 | bases: Base classes of the class we're constructing. 115 | (Should be message.Message). We ignore this field, but 116 | it's required by the metaclass protocol 117 | dictionary: The class dictionary of the class we're 118 | constructing. dictionary[_DESCRIPTOR_KEY] must contain 119 | a Descriptor object describing this protocol message 120 | type. 121 | 122 | Returns: 123 | Newly-allocated class. 124 | """ 125 | descriptor = dictionary[GeneratedProtocolMessageType._DESCRIPTOR_KEY] 126 | bases = _NewMessage(bases, descriptor, dictionary) 127 | superclass = super(GeneratedProtocolMessageType, cls) 128 | 129 | new_class = superclass.__new__(cls, name, bases, dictionary) 130 | setattr(descriptor, '_concrete_class', new_class) 131 | return new_class 132 | 133 | def __init__(cls, name, bases, dictionary): 134 | """Here we perform the majority of our work on the class. 135 | We add enum getters, an __init__ method, implementations 136 | of all Message methods, and properties for all fields 137 | in the protocol type. 138 | 139 | Args: 140 | name: Name of the class (ignored, but required by the 141 | metaclass protocol). 142 | bases: Base classes of the class we're constructing. 143 | (Should be message.Message). We ignore this field, but 144 | it's required by the metaclass protocol 145 | dictionary: The class dictionary of the class we're 146 | constructing. dictionary[_DESCRIPTOR_KEY] must contain 147 | a Descriptor object describing this protocol message 148 | type. 149 | """ 150 | descriptor = dictionary[GeneratedProtocolMessageType._DESCRIPTOR_KEY] 151 | _InitMessage(descriptor, cls) 152 | superclass = super(GeneratedProtocolMessageType, cls) 153 | superclass.__init__(name, bases, dictionary) 154 | 155 | 156 | def ParseMessage(descriptor, byte_str): 157 | """Generate a new Message instance from this Descriptor and a byte string. 158 | 159 | Args: 160 | descriptor: Protobuf Descriptor object 161 | byte_str: Serialized protocol buffer byte string 162 | 163 | Returns: 164 | Newly created protobuf Message object. 165 | """ 166 | result_class = MakeClass(descriptor) 167 | new_msg = result_class() 168 | new_msg.ParseFromString(byte_str) 169 | return new_msg 170 | 171 | 172 | def MakeClass(descriptor): 173 | """Construct a class object for a protobuf described by descriptor. 174 | 175 | Composite descriptors are handled by defining the new class as a member of the 176 | parent class, recursing as deep as necessary. 177 | This is the dynamic equivalent to: 178 | 179 | class Parent(message.Message): 180 | __metaclass__ = GeneratedProtocolMessageType 181 | DESCRIPTOR = descriptor 182 | class Child(message.Message): 183 | __metaclass__ = GeneratedProtocolMessageType 184 | DESCRIPTOR = descriptor.nested_types[0] 185 | 186 | Sample usage: 187 | file_descriptor = descriptor_pb2.FileDescriptorProto() 188 | file_descriptor.ParseFromString(proto2_string) 189 | msg_descriptor = descriptor.MakeDescriptor(file_descriptor.message_type[0]) 190 | msg_class = reflection.MakeClass(msg_descriptor) 191 | msg = msg_class() 192 | 193 | Args: 194 | descriptor: A descriptor.Descriptor object describing the protobuf. 195 | Returns: 196 | The Message class object described by the descriptor. 197 | """ 198 | attributes = {} 199 | for name, nested_type in descriptor.nested_types_by_name.items(): 200 | attributes[name] = MakeClass(nested_type) 201 | 202 | attributes[GeneratedProtocolMessageType._DESCRIPTOR_KEY] = descriptor 203 | 204 | return GeneratedProtocolMessageType(str(descriptor.name), (message.Message,), 205 | attributes) 206 | -------------------------------------------------------------------------------- /google/protobuf/internal/wire_format.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """Constants and static functions to support protocol buffer wire format.""" 32 | 33 | __author__ = 'robinson@google.com (Will Robinson)' 34 | 35 | import struct 36 | from google.protobuf import descriptor 37 | from google.protobuf import message 38 | 39 | 40 | TAG_TYPE_BITS = 3 # Number of bits used to hold type info in a proto tag. 41 | TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1 # 0x7 42 | 43 | # These numbers identify the wire type of a protocol buffer value. 44 | # We use the least-significant TAG_TYPE_BITS bits of the varint-encoded 45 | # tag-and-type to store one of these WIRETYPE_* constants. 46 | # These values must match WireType enum in google/protobuf/wire_format.h. 47 | WIRETYPE_VARINT = 0 48 | WIRETYPE_FIXED64 = 1 49 | WIRETYPE_LENGTH_DELIMITED = 2 50 | WIRETYPE_START_GROUP = 3 51 | WIRETYPE_END_GROUP = 4 52 | WIRETYPE_FIXED32 = 5 53 | _WIRETYPE_MAX = 5 54 | 55 | 56 | # Bounds for various integer types. 57 | INT32_MAX = int((1 << 31) - 1) 58 | INT32_MIN = int(-(1 << 31)) 59 | UINT32_MAX = (1 << 32) - 1 60 | 61 | INT64_MAX = (1 << 63) - 1 62 | INT64_MIN = -(1 << 63) 63 | UINT64_MAX = (1 << 64) - 1 64 | 65 | # "struct" format strings that will encode/decode the specified formats. 66 | FORMAT_UINT32_LITTLE_ENDIAN = '> TAG_TYPE_BITS), (tag & TAG_TYPE_MASK) 98 | 99 | 100 | def ZigZagEncode(value): 101 | """ZigZag Transform: Encodes signed integers so that they can be 102 | effectively used with varint encoding. See wire_format.h for 103 | more details. 104 | """ 105 | if value >= 0: 106 | return value << 1 107 | return (value << 1) ^ (~0) 108 | 109 | 110 | def ZigZagDecode(value): 111 | """Inverse of ZigZagEncode().""" 112 | if not value & 0x1: 113 | return value >> 1 114 | return (value >> 1) ^ (~0) 115 | 116 | 117 | 118 | # The *ByteSize() functions below return the number of bytes required to 119 | # serialize "field number + type" information and then serialize the value. 120 | 121 | 122 | def Int32ByteSize(field_number, int32): 123 | return Int64ByteSize(field_number, int32) 124 | 125 | 126 | def Int32ByteSizeNoTag(int32): 127 | return _VarUInt64ByteSizeNoTag(0xffffffffffffffff & int32) 128 | 129 | 130 | def Int64ByteSize(field_number, int64): 131 | # Have to convert to uint before calling UInt64ByteSize(). 132 | return UInt64ByteSize(field_number, 0xffffffffffffffff & int64) 133 | 134 | 135 | def UInt32ByteSize(field_number, uint32): 136 | return UInt64ByteSize(field_number, uint32) 137 | 138 | 139 | def UInt64ByteSize(field_number, uint64): 140 | return TagByteSize(field_number) + _VarUInt64ByteSizeNoTag(uint64) 141 | 142 | 143 | def SInt32ByteSize(field_number, int32): 144 | return UInt32ByteSize(field_number, ZigZagEncode(int32)) 145 | 146 | 147 | def SInt64ByteSize(field_number, int64): 148 | return UInt64ByteSize(field_number, ZigZagEncode(int64)) 149 | 150 | 151 | def Fixed32ByteSize(field_number, fixed32): 152 | return TagByteSize(field_number) + 4 153 | 154 | 155 | def Fixed64ByteSize(field_number, fixed64): 156 | return TagByteSize(field_number) + 8 157 | 158 | 159 | def SFixed32ByteSize(field_number, sfixed32): 160 | return TagByteSize(field_number) + 4 161 | 162 | 163 | def SFixed64ByteSize(field_number, sfixed64): 164 | return TagByteSize(field_number) + 8 165 | 166 | 167 | def FloatByteSize(field_number, flt): 168 | return TagByteSize(field_number) + 4 169 | 170 | 171 | def DoubleByteSize(field_number, double): 172 | return TagByteSize(field_number) + 8 173 | 174 | 175 | def BoolByteSize(field_number, b): 176 | return TagByteSize(field_number) + 1 177 | 178 | 179 | def EnumByteSize(field_number, enum): 180 | return UInt32ByteSize(field_number, enum) 181 | 182 | 183 | def StringByteSize(field_number, string): 184 | return BytesByteSize(field_number, string.encode('utf-8')) 185 | 186 | 187 | def BytesByteSize(field_number, b): 188 | return (TagByteSize(field_number) 189 | + _VarUInt64ByteSizeNoTag(len(b)) 190 | + len(b)) 191 | 192 | 193 | def GroupByteSize(field_number, message): 194 | return (2 * TagByteSize(field_number) # START and END group. 195 | + message.ByteSize()) 196 | 197 | 198 | def MessageByteSize(field_number, message): 199 | return (TagByteSize(field_number) 200 | + _VarUInt64ByteSizeNoTag(message.ByteSize()) 201 | + message.ByteSize()) 202 | 203 | 204 | def MessageSetItemByteSize(field_number, msg): 205 | # First compute the sizes of the tags. 206 | # There are 2 tags for the beginning and ending of the repeated group, that 207 | # is field number 1, one with field number 2 (type_id) and one with field 208 | # number 3 (message). 209 | total_size = (2 * TagByteSize(1) + TagByteSize(2) + TagByteSize(3)) 210 | 211 | # Add the number of bytes for type_id. 212 | total_size += _VarUInt64ByteSizeNoTag(field_number) 213 | 214 | message_size = msg.ByteSize() 215 | 216 | # The number of bytes for encoding the length of the message. 217 | total_size += _VarUInt64ByteSizeNoTag(message_size) 218 | 219 | # The size of the message. 220 | total_size += message_size 221 | return total_size 222 | 223 | 224 | def TagByteSize(field_number): 225 | """Returns the bytes required to serialize a tag with this field number.""" 226 | # Just pass in type 0, since the type won't affect the tag+type size. 227 | return _VarUInt64ByteSizeNoTag(PackTag(field_number, 0)) 228 | 229 | 230 | # Private helper function for the *ByteSize() functions above. 231 | 232 | def _VarUInt64ByteSizeNoTag(uint64): 233 | """Returns the number of bytes required to serialize a single varint 234 | using boundary value comparisons. (unrolled loop optimization -WPierce) 235 | uint64 must be unsigned. 236 | """ 237 | if uint64 <= 0x7f: return 1 238 | if uint64 <= 0x3fff: return 2 239 | if uint64 <= 0x1fffff: return 3 240 | if uint64 <= 0xfffffff: return 4 241 | if uint64 <= 0x7ffffffff: return 5 242 | if uint64 <= 0x3ffffffffff: return 6 243 | if uint64 <= 0x1ffffffffffff: return 7 244 | if uint64 <= 0xffffffffffffff: return 8 245 | if uint64 <= 0x7fffffffffffffff: return 9 246 | if uint64 > UINT64_MAX: 247 | raise message.EncodeError('Value out of range: %d' % uint64) 248 | return 10 249 | 250 | 251 | NON_PACKABLE_TYPES = ( 252 | descriptor.FieldDescriptor.TYPE_STRING, 253 | descriptor.FieldDescriptor.TYPE_GROUP, 254 | descriptor.FieldDescriptor.TYPE_MESSAGE, 255 | descriptor.FieldDescriptor.TYPE_BYTES 256 | ) 257 | 258 | 259 | def IsTypePackable(field_type): 260 | """Return true iff packable = true is valid for fields of this type. 261 | 262 | Args: 263 | field_type: a FieldDescriptor::Type value. 264 | 265 | Returns: 266 | True iff fields of this type are packable. 267 | """ 268 | return field_type not in NON_PACKABLE_TYPES 269 | -------------------------------------------------------------------------------- /google/protobuf/service.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """DEPRECATED: Declares the RPC service interfaces. 32 | 33 | This module declares the abstract interfaces underlying proto2 RPC 34 | services. These are intended to be independent of any particular RPC 35 | implementation, so that proto2 services can be used on top of a variety 36 | of implementations. Starting with version 2.3.0, RPC implementations should 37 | not try to build on these, but should instead provide code generator plugins 38 | which generate code specific to the particular RPC implementation. This way 39 | the generated code can be more appropriate for the implementation in use 40 | and can avoid unnecessary layers of indirection. 41 | """ 42 | 43 | __author__ = 'petar@google.com (Petar Petrov)' 44 | 45 | 46 | class RpcException(Exception): 47 | """Exception raised on failed blocking RPC method call.""" 48 | pass 49 | 50 | 51 | class Service(object): 52 | 53 | """Abstract base interface for protocol-buffer-based RPC services. 54 | 55 | Services themselves are abstract classes (implemented either by servers or as 56 | stubs), but they subclass this base interface. The methods of this 57 | interface can be used to call the methods of the service without knowing 58 | its exact type at compile time (analogous to the Message interface). 59 | """ 60 | 61 | def GetDescriptor(): 62 | """Retrieves this service's descriptor.""" 63 | raise NotImplementedError 64 | 65 | def CallMethod(self, method_descriptor, rpc_controller, 66 | request, done): 67 | """Calls a method of the service specified by method_descriptor. 68 | 69 | If "done" is None then the call is blocking and the response 70 | message will be returned directly. Otherwise the call is asynchronous 71 | and "done" will later be called with the response value. 72 | 73 | In the blocking case, RpcException will be raised on error. 74 | 75 | Preconditions: 76 | * method_descriptor.service == GetDescriptor 77 | * request is of the exact same classes as returned by 78 | GetRequestClass(method). 79 | * After the call has started, the request must not be modified. 80 | * "rpc_controller" is of the correct type for the RPC implementation being 81 | used by this Service. For stubs, the "correct type" depends on the 82 | RpcChannel which the stub is using. 83 | 84 | Postconditions: 85 | * "done" will be called when the method is complete. This may be 86 | before CallMethod() returns or it may be at some point in the future. 87 | * If the RPC failed, the response value passed to "done" will be None. 88 | Further details about the failure can be found by querying the 89 | RpcController. 90 | """ 91 | raise NotImplementedError 92 | 93 | def GetRequestClass(self, method_descriptor): 94 | """Returns the class of the request message for the specified method. 95 | 96 | CallMethod() requires that the request is of a particular subclass of 97 | Message. GetRequestClass() gets the default instance of this required 98 | type. 99 | 100 | Example: 101 | method = service.GetDescriptor().FindMethodByName("Foo") 102 | request = stub.GetRequestClass(method)() 103 | request.ParseFromString(input) 104 | service.CallMethod(method, request, callback) 105 | """ 106 | raise NotImplementedError 107 | 108 | def GetResponseClass(self, method_descriptor): 109 | """Returns the class of the response message for the specified method. 110 | 111 | This method isn't really needed, as the RpcChannel's CallMethod constructs 112 | the response protocol message. It's provided anyway in case it is useful 113 | for the caller to know the response type in advance. 114 | """ 115 | raise NotImplementedError 116 | 117 | 118 | class RpcController(object): 119 | 120 | """An RpcController mediates a single method call. 121 | 122 | The primary purpose of the controller is to provide a way to manipulate 123 | settings specific to the RPC implementation and to find out about RPC-level 124 | errors. The methods provided by the RpcController interface are intended 125 | to be a "least common denominator" set of features which we expect all 126 | implementations to support. Specific implementations may provide more 127 | advanced features (e.g. deadline propagation). 128 | """ 129 | 130 | # Client-side methods below 131 | 132 | def Reset(self): 133 | """Resets the RpcController to its initial state. 134 | 135 | After the RpcController has been reset, it may be reused in 136 | a new call. Must not be called while an RPC is in progress. 137 | """ 138 | raise NotImplementedError 139 | 140 | def Failed(self): 141 | """Returns true if the call failed. 142 | 143 | After a call has finished, returns true if the call failed. The possible 144 | reasons for failure depend on the RPC implementation. Failed() must not 145 | be called before a call has finished. If Failed() returns true, the 146 | contents of the response message are undefined. 147 | """ 148 | raise NotImplementedError 149 | 150 | def ErrorText(self): 151 | """If Failed is true, returns a human-readable description of the error.""" 152 | raise NotImplementedError 153 | 154 | def StartCancel(self): 155 | """Initiate cancellation. 156 | 157 | Advises the RPC system that the caller desires that the RPC call be 158 | canceled. The RPC system may cancel it immediately, may wait awhile and 159 | then cancel it, or may not even cancel the call at all. If the call is 160 | canceled, the "done" callback will still be called and the RpcController 161 | will indicate that the call failed at that time. 162 | """ 163 | raise NotImplementedError 164 | 165 | # Server-side methods below 166 | 167 | def SetFailed(self, reason): 168 | """Sets a failure reason. 169 | 170 | Causes Failed() to return true on the client side. "reason" will be 171 | incorporated into the message returned by ErrorText(). If you find 172 | you need to return machine-readable information about failures, you 173 | should incorporate it into your response protocol buffer and should 174 | NOT call SetFailed(). 175 | """ 176 | raise NotImplementedError 177 | 178 | def IsCanceled(self): 179 | """Checks if the client cancelled the RPC. 180 | 181 | If true, indicates that the client canceled the RPC, so the server may 182 | as well give up on replying to it. The server should still call the 183 | final "done" callback. 184 | """ 185 | raise NotImplementedError 186 | 187 | def NotifyOnCancel(self, callback): 188 | """Sets a callback to invoke on cancel. 189 | 190 | Asks that the given callback be called when the RPC is canceled. The 191 | callback will always be called exactly once. If the RPC completes without 192 | being canceled, the callback will be called after completion. If the RPC 193 | has already been canceled when NotifyOnCancel() is called, the callback 194 | will be called immediately. 195 | 196 | NotifyOnCancel() must be called no more than once per request. 197 | """ 198 | raise NotImplementedError 199 | 200 | 201 | class RpcChannel(object): 202 | 203 | """Abstract interface for an RPC channel. 204 | 205 | An RpcChannel represents a communication line to a service which can be used 206 | to call that service's methods. The service may be running on another 207 | machine. Normally, you should not use an RpcChannel directly, but instead 208 | construct a stub {@link Service} wrapping it. Example: 209 | 210 | Example: 211 | RpcChannel channel = rpcImpl.Channel("remotehost.example.com:1234") 212 | RpcController controller = rpcImpl.Controller() 213 | MyService service = MyService_Stub(channel) 214 | service.MyMethod(controller, request, callback) 215 | """ 216 | 217 | def CallMethod(self, method_descriptor, rpc_controller, 218 | request, response_class, done): 219 | """Calls the method identified by the descriptor. 220 | 221 | Call the given method of the remote service. The signature of this 222 | procedure looks the same as Service.CallMethod(), but the requirements 223 | are less strict in one important way: the request object doesn't have to 224 | be of any specific class as long as its descriptor is method.input_type. 225 | """ 226 | raise NotImplementedError 227 | -------------------------------------------------------------------------------- /google/protobuf/internal/unknown_fields_test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Protocol Buffers - Google's data interchange format 5 | # Copyright 2008 Google Inc. All rights reserved. 6 | # https://developers.google.com/protocol-buffers/ 7 | # 8 | # Redistribution and use in source and binary forms, with or without 9 | # modification, are permitted provided that the following conditions are 10 | # met: 11 | # 12 | # * Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # * Redistributions in binary form must reproduce the above 15 | # copyright notice, this list of conditions and the following disclaimer 16 | # in the documentation and/or other materials provided with the 17 | # distribution. 18 | # * Neither the name of Google Inc. nor the names of its 19 | # contributors may be used to endorse or promote products derived from 20 | # this software without specific prior written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | 34 | """Test for preservation of unknown fields in the pure Python implementation.""" 35 | 36 | __author__ = 'bohdank@google.com (Bohdan Koval)' 37 | 38 | from google.apputils import basetest 39 | from google.protobuf import unittest_mset_pb2 40 | from google.protobuf import unittest_pb2 41 | from google.protobuf.internal import encoder 42 | from google.protobuf.internal import missing_enum_values_pb2 43 | from google.protobuf.internal import test_util 44 | from google.protobuf.internal import type_checkers 45 | 46 | 47 | class UnknownFieldsTest(basetest.TestCase): 48 | 49 | def setUp(self): 50 | self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR 51 | self.all_fields = unittest_pb2.TestAllTypes() 52 | test_util.SetAllFields(self.all_fields) 53 | self.all_fields_data = self.all_fields.SerializeToString() 54 | self.empty_message = unittest_pb2.TestEmptyMessage() 55 | self.empty_message.ParseFromString(self.all_fields_data) 56 | self.unknown_fields = self.empty_message._unknown_fields 57 | 58 | def GetField(self, name): 59 | field_descriptor = self.descriptor.fields_by_name[name] 60 | wire_type = type_checkers.FIELD_TYPE_TO_WIRE_TYPE[field_descriptor.type] 61 | field_tag = encoder.TagBytes(field_descriptor.number, wire_type) 62 | result_dict = {} 63 | for tag_bytes, value in self.unknown_fields: 64 | if tag_bytes == field_tag: 65 | decoder = unittest_pb2.TestAllTypes._decoders_by_tag[tag_bytes][0] 66 | decoder(value, 0, len(value), self.all_fields, result_dict) 67 | return result_dict[field_descriptor] 68 | 69 | def testEnum(self): 70 | value = self.GetField('optional_nested_enum') 71 | self.assertEqual(self.all_fields.optional_nested_enum, value) 72 | 73 | def testRepeatedEnum(self): 74 | value = self.GetField('repeated_nested_enum') 75 | self.assertEqual(self.all_fields.repeated_nested_enum, value) 76 | 77 | def testVarint(self): 78 | value = self.GetField('optional_int32') 79 | self.assertEqual(self.all_fields.optional_int32, value) 80 | 81 | def testFixed32(self): 82 | value = self.GetField('optional_fixed32') 83 | self.assertEqual(self.all_fields.optional_fixed32, value) 84 | 85 | def testFixed64(self): 86 | value = self.GetField('optional_fixed64') 87 | self.assertEqual(self.all_fields.optional_fixed64, value) 88 | 89 | def testLengthDelimited(self): 90 | value = self.GetField('optional_string') 91 | self.assertEqual(self.all_fields.optional_string, value) 92 | 93 | def testGroup(self): 94 | value = self.GetField('optionalgroup') 95 | self.assertEqual(self.all_fields.optionalgroup, value) 96 | 97 | def testSerialize(self): 98 | data = self.empty_message.SerializeToString() 99 | 100 | # Don't use assertEqual because we don't want to dump raw binary data to 101 | # stdout. 102 | self.assertTrue(data == self.all_fields_data) 103 | 104 | def testCopyFrom(self): 105 | message = unittest_pb2.TestEmptyMessage() 106 | message.CopyFrom(self.empty_message) 107 | self.assertEqual(self.unknown_fields, message._unknown_fields) 108 | 109 | def testMergeFrom(self): 110 | message = unittest_pb2.TestAllTypes() 111 | message.optional_int32 = 1 112 | message.optional_uint32 = 2 113 | source = unittest_pb2.TestEmptyMessage() 114 | source.ParseFromString(message.SerializeToString()) 115 | 116 | message.ClearField('optional_int32') 117 | message.optional_int64 = 3 118 | message.optional_uint32 = 4 119 | destination = unittest_pb2.TestEmptyMessage() 120 | destination.ParseFromString(message.SerializeToString()) 121 | unknown_fields = destination._unknown_fields[:] 122 | 123 | destination.MergeFrom(source) 124 | self.assertEqual(unknown_fields + source._unknown_fields, 125 | destination._unknown_fields) 126 | 127 | def testClear(self): 128 | self.empty_message.Clear() 129 | self.assertEqual(0, len(self.empty_message._unknown_fields)) 130 | 131 | def testByteSize(self): 132 | self.assertEqual(self.all_fields.ByteSize(), self.empty_message.ByteSize()) 133 | 134 | def testUnknownExtensions(self): 135 | message = unittest_pb2.TestEmptyMessageWithExtensions() 136 | message.ParseFromString(self.all_fields_data) 137 | self.assertEqual(self.empty_message._unknown_fields, 138 | message._unknown_fields) 139 | 140 | def testListFields(self): 141 | # Make sure ListFields doesn't return unknown fields. 142 | self.assertEqual(0, len(self.empty_message.ListFields())) 143 | 144 | def testSerializeMessageSetWireFormatUnknownExtension(self): 145 | # Create a message using the message set wire format with an unknown 146 | # message. 147 | raw = unittest_mset_pb2.RawMessageSet() 148 | 149 | # Add an unknown extension. 150 | item = raw.item.add() 151 | item.type_id = 1545009 152 | message1 = unittest_mset_pb2.TestMessageSetExtension1() 153 | message1.i = 12345 154 | item.message = message1.SerializeToString() 155 | 156 | serialized = raw.SerializeToString() 157 | 158 | # Parse message using the message set wire format. 159 | proto = unittest_mset_pb2.TestMessageSet() 160 | proto.MergeFromString(serialized) 161 | 162 | # Verify that the unknown extension is serialized unchanged 163 | reserialized = proto.SerializeToString() 164 | new_raw = unittest_mset_pb2.RawMessageSet() 165 | new_raw.MergeFromString(reserialized) 166 | self.assertEqual(raw, new_raw) 167 | 168 | def testEquals(self): 169 | message = unittest_pb2.TestEmptyMessage() 170 | message.ParseFromString(self.all_fields_data) 171 | self.assertEqual(self.empty_message, message) 172 | 173 | self.all_fields.ClearField('optional_string') 174 | message.ParseFromString(self.all_fields.SerializeToString()) 175 | self.assertNotEqual(self.empty_message, message) 176 | 177 | 178 | class UnknownFieldsTest(basetest.TestCase): 179 | 180 | def setUp(self): 181 | self.descriptor = missing_enum_values_pb2.TestEnumValues.DESCRIPTOR 182 | 183 | self.message = missing_enum_values_pb2.TestEnumValues() 184 | self.message.optional_nested_enum = ( 185 | missing_enum_values_pb2.TestEnumValues.ZERO) 186 | self.message.repeated_nested_enum.extend([ 187 | missing_enum_values_pb2.TestEnumValues.ZERO, 188 | missing_enum_values_pb2.TestEnumValues.ONE, 189 | ]) 190 | self.message.packed_nested_enum.extend([ 191 | missing_enum_values_pb2.TestEnumValues.ZERO, 192 | missing_enum_values_pb2.TestEnumValues.ONE, 193 | ]) 194 | self.message_data = self.message.SerializeToString() 195 | self.missing_message = missing_enum_values_pb2.TestMissingEnumValues() 196 | self.missing_message.ParseFromString(self.message_data) 197 | self.unknown_fields = self.missing_message._unknown_fields 198 | 199 | def GetField(self, name): 200 | field_descriptor = self.descriptor.fields_by_name[name] 201 | wire_type = type_checkers.FIELD_TYPE_TO_WIRE_TYPE[field_descriptor.type] 202 | field_tag = encoder.TagBytes(field_descriptor.number, wire_type) 203 | result_dict = {} 204 | for tag_bytes, value in self.unknown_fields: 205 | if tag_bytes == field_tag: 206 | decoder = missing_enum_values_pb2.TestEnumValues._decoders_by_tag[ 207 | tag_bytes][0] 208 | decoder(value, 0, len(value), self.message, result_dict) 209 | return result_dict[field_descriptor] 210 | 211 | def testUnknownEnumValue(self): 212 | self.assertFalse(self.missing_message.HasField('optional_nested_enum')) 213 | value = self.GetField('optional_nested_enum') 214 | self.assertEqual(self.message.optional_nested_enum, value) 215 | 216 | def testUnknownRepeatedEnumValue(self): 217 | value = self.GetField('repeated_nested_enum') 218 | self.assertEqual(self.message.repeated_nested_enum, value) 219 | 220 | def testUnknownPackedEnumValue(self): 221 | value = self.GetField('packed_nested_enum') 222 | self.assertEqual(self.message.packed_nested_enum, value) 223 | 224 | def testRoundTrip(self): 225 | new_message = missing_enum_values_pb2.TestEnumValues() 226 | new_message.ParseFromString(self.missing_message.SerializeToString()) 227 | self.assertEqual(self.message, new_message) 228 | 229 | 230 | if __name__ == '__main__': 231 | basetest.main() 232 | -------------------------------------------------------------------------------- /google/protobuf/internal/containers.py: -------------------------------------------------------------------------------- 1 | # Protocol Buffers - Google's data interchange format 2 | # Copyright 2008 Google Inc. All rights reserved. 3 | # https://developers.google.com/protocol-buffers/ 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # * Redistributions in binary form must reproduce the above 12 | # copyright notice, this list of conditions and the following disclaimer 13 | # in the documentation and/or other materials provided with the 14 | # distribution. 15 | # * Neither the name of Google Inc. nor the names of its 16 | # contributors may be used to endorse or promote products derived from 17 | # this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """Contains container classes to represent different protocol buffer types. 32 | 33 | This file defines container classes which represent categories of protocol 34 | buffer field types which need extra maintenance. Currently these categories 35 | are: 36 | - Repeated scalar fields - These are all repeated fields which aren't 37 | composite (e.g. they are of simple types like int32, string, etc). 38 | - Repeated composite fields - Repeated fields which are composite. This 39 | includes groups and nested messages. 40 | """ 41 | 42 | __author__ = 'petar@google.com (Petar Petrov)' 43 | 44 | 45 | class BaseContainer(object): 46 | 47 | """Base container class.""" 48 | 49 | # Minimizes memory usage and disallows assignment to other attributes. 50 | __slots__ = ['_message_listener', '_values'] 51 | 52 | def __init__(self, message_listener): 53 | """ 54 | Args: 55 | message_listener: A MessageListener implementation. 56 | The RepeatedScalarFieldContainer will call this object's 57 | Modified() method when it is modified. 58 | """ 59 | self._message_listener = message_listener 60 | self._values = [] 61 | 62 | def __getitem__(self, key): 63 | """Retrieves item by the specified key.""" 64 | return self._values[key] 65 | 66 | def __len__(self): 67 | """Returns the number of elements in the container.""" 68 | return len(self._values) 69 | 70 | def __ne__(self, other): 71 | """Checks if another instance isn't equal to this one.""" 72 | # The concrete classes should define __eq__. 73 | return not self == other 74 | 75 | def __hash__(self): 76 | raise TypeError('unhashable object') 77 | 78 | def __repr__(self): 79 | return repr(self._values) 80 | 81 | def sort(self, *args, **kwargs): 82 | # Continue to support the old sort_function keyword argument. 83 | # This is expected to be a rare occurrence, so use LBYL to avoid 84 | # the overhead of actually catching KeyError. 85 | if 'sort_function' in kwargs: 86 | kwargs['cmp'] = kwargs.pop('sort_function') 87 | self._values.sort(*args, **kwargs) 88 | 89 | 90 | class RepeatedScalarFieldContainer(BaseContainer): 91 | 92 | """Simple, type-checked, list-like container for holding repeated scalars.""" 93 | 94 | # Disallows assignment to other attributes. 95 | __slots__ = ['_type_checker'] 96 | 97 | def __init__(self, message_listener, type_checker): 98 | """ 99 | Args: 100 | message_listener: A MessageListener implementation. 101 | The RepeatedScalarFieldContainer will call this object's 102 | Modified() method when it is modified. 103 | type_checker: A type_checkers.ValueChecker instance to run on elements 104 | inserted into this container. 105 | """ 106 | super(RepeatedScalarFieldContainer, self).__init__(message_listener) 107 | self._type_checker = type_checker 108 | 109 | def append(self, value): 110 | """Appends an item to the list. Similar to list.append().""" 111 | self._values.append(self._type_checker.CheckValue(value)) 112 | if not self._message_listener.dirty: 113 | self._message_listener.Modified() 114 | 115 | def insert(self, key, value): 116 | """Inserts the item at the specified position. Similar to list.insert().""" 117 | self._values.insert(key, self._type_checker.CheckValue(value)) 118 | if not self._message_listener.dirty: 119 | self._message_listener.Modified() 120 | 121 | def extend(self, elem_seq): 122 | """Extends by appending the given sequence. Similar to list.extend().""" 123 | if not elem_seq: 124 | return 125 | 126 | new_values = [] 127 | for elem in elem_seq: 128 | new_values.append(self._type_checker.CheckValue(elem)) 129 | self._values.extend(new_values) 130 | self._message_listener.Modified() 131 | 132 | def MergeFrom(self, other): 133 | """Appends the contents of another repeated field of the same type to this 134 | one. We do not check the types of the individual fields. 135 | """ 136 | self._values.extend(other._values) 137 | self._message_listener.Modified() 138 | 139 | def remove(self, elem): 140 | """Removes an item from the list. Similar to list.remove().""" 141 | self._values.remove(elem) 142 | self._message_listener.Modified() 143 | 144 | def __setitem__(self, key, value): 145 | """Sets the item on the specified position.""" 146 | if isinstance(key, slice): # PY3 147 | if key.step is not None: 148 | raise ValueError('Extended slices not supported') 149 | self.__setslice__(key.start, key.stop, value) 150 | else: 151 | self._values[key] = self._type_checker.CheckValue(value) 152 | self._message_listener.Modified() 153 | 154 | def __getslice__(self, start, stop): 155 | """Retrieves the subset of items from between the specified indices.""" 156 | return self._values[start:stop] 157 | 158 | def __setslice__(self, start, stop, values): 159 | """Sets the subset of items from between the specified indices.""" 160 | new_values = [] 161 | for value in values: 162 | new_values.append(self._type_checker.CheckValue(value)) 163 | self._values[start:stop] = new_values 164 | self._message_listener.Modified() 165 | 166 | def __delitem__(self, key): 167 | """Deletes the item at the specified position.""" 168 | del self._values[key] 169 | self._message_listener.Modified() 170 | 171 | def __delslice__(self, start, stop): 172 | """Deletes the subset of items from between the specified indices.""" 173 | del self._values[start:stop] 174 | self._message_listener.Modified() 175 | 176 | def __eq__(self, other): 177 | """Compares the current instance with another one.""" 178 | if self is other: 179 | return True 180 | # Special case for the same type which should be common and fast. 181 | if isinstance(other, self.__class__): 182 | return other._values == self._values 183 | # We are presumably comparing against some other sequence type. 184 | return other == self._values 185 | 186 | 187 | class RepeatedCompositeFieldContainer(BaseContainer): 188 | 189 | """Simple, list-like container for holding repeated composite fields.""" 190 | 191 | # Disallows assignment to other attributes. 192 | __slots__ = ['_message_descriptor'] 193 | 194 | def __init__(self, message_listener, message_descriptor): 195 | """ 196 | Note that we pass in a descriptor instead of the generated directly, 197 | since at the time we construct a _RepeatedCompositeFieldContainer we 198 | haven't yet necessarily initialized the type that will be contained in the 199 | container. 200 | 201 | Args: 202 | message_listener: A MessageListener implementation. 203 | The RepeatedCompositeFieldContainer will call this object's 204 | Modified() method when it is modified. 205 | message_descriptor: A Descriptor instance describing the protocol type 206 | that should be present in this container. We'll use the 207 | _concrete_class field of this descriptor when the client calls add(). 208 | """ 209 | super(RepeatedCompositeFieldContainer, self).__init__(message_listener) 210 | self._message_descriptor = message_descriptor 211 | 212 | def add(self, **kwargs): 213 | """Adds a new element at the end of the list and returns it. Keyword 214 | arguments may be used to initialize the element. 215 | """ 216 | new_element = self._message_descriptor._concrete_class(**kwargs) 217 | new_element._SetListener(self._message_listener) 218 | self._values.append(new_element) 219 | if not self._message_listener.dirty: 220 | self._message_listener.Modified() 221 | return new_element 222 | 223 | def extend(self, elem_seq): 224 | """Extends by appending the given sequence of elements of the same type 225 | as this one, copying each individual message. 226 | """ 227 | message_class = self._message_descriptor._concrete_class 228 | listener = self._message_listener 229 | values = self._values 230 | for message in elem_seq: 231 | new_element = message_class() 232 | new_element._SetListener(listener) 233 | new_element.MergeFrom(message) 234 | values.append(new_element) 235 | listener.Modified() 236 | 237 | def MergeFrom(self, other): 238 | """Appends the contents of another repeated field of the same type to this 239 | one, copying each individual message. 240 | """ 241 | self.extend(other._values) 242 | 243 | def remove(self, elem): 244 | """Removes an item from the list. Similar to list.remove().""" 245 | self._values.remove(elem) 246 | self._message_listener.Modified() 247 | 248 | def __getslice__(self, start, stop): 249 | """Retrieves the subset of items from between the specified indices.""" 250 | return self._values[start:stop] 251 | 252 | def __delitem__(self, key): 253 | """Deletes the item at the specified position.""" 254 | del self._values[key] 255 | self._message_listener.Modified() 256 | 257 | def __delslice__(self, start, stop): 258 | """Deletes the subset of items from between the specified indices.""" 259 | del self._values[start:stop] 260 | self._message_listener.Modified() 261 | 262 | def __eq__(self, other): 263 | """Compares the current instance with another one.""" 264 | if self is other: 265 | return True 266 | if not isinstance(other, self.__class__): 267 | raise TypeError('Can only compare repeated composite fields against ' 268 | 'other repeated composite fields.') 269 | return self._values == other._values 270 | --------------------------------------------------------------------------------