def _Database(self):
   if descriptor._USE_C_DESCRIPTORS:
     # The C++ implementation does not allow mixing descriptors from
     # different pools.
     db = symbol_database.SymbolDatabase(pool=descriptor_pool.Default())
   else:
     db = symbol_database.SymbolDatabase()
   # Register representative types from unittest_pb2.
   db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR)
   db.RegisterMessage(unittest_pb2.TestAllTypes)
   db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage)
   db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup)
   db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup)
   db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR)
   db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR)
   db.RegisterServiceDescriptor(unittest_pb2._TESTSERVICE)
   return db
Пример #2
0
def pytest_runtest_setup(item):
    _FileInfo.registry.clear()

    # Replace the descriptor pool and symbol database to avoid tests
    # polluting one another.
    pool = type(descriptor_pool.Default())()
    sym_db = symbol_database.SymbolDatabase(pool=pool)
    item._mocks = [
        mock.patch.object(descriptor_pool, "Default", return_value=pool),
        mock.patch.object(symbol_database, "Default", return_value=sym_db),
    ]
    if descriptor_pool._USE_C_DESCRIPTORS:

        item._mocks.append(
            mock.patch(
                "google._upb._message.default_pool" if has_upb() else
                "google.protobuf.pyext._message.default_pool",
                pool,
            ))

    [i.start() for i in item._mocks]

    # Importing a pb2 module registers those messages with the pool.
    # However, our test harness is subbing out the default pool above,
    # which means that all the dependencies that messages may depend on
    # are now absent from the pool.
    #
    # Add any pb2 modules that may have been imported by the test's module to
    # the descriptor pool and symbol database.
    #
    # This is exceptionally tricky in the C implementation because there is
    # no way to add an existing descriptor to a pool; the only acceptable
    # approach is to add a file descriptor proto, which then creates *new*
    # descriptors. We therefore do that and then plop the replacement classes
    # onto the pb2 modules.
    reloaded = set()
    for name in dir(item.module):
        if name.endswith("_pb2") and not name.startswith("test_"):
            module = getattr(item.module, name)
            pool.AddSerializedFile(module.DESCRIPTOR.serialized_pb)
            fd = pool.FindFileByName(module.DESCRIPTOR.name)

            # Register all the messages to the symbol database and the
            # module. Do this recursively if there are nested messages.
            _register_messages(module, fd.message_types_by_name, sym_db)

            # Track which modules had new message classes loaded.
            # This is used below to wire the new classes into the marshal.
            reloaded.add(name)

    # If the marshal had previously registered the old message classes,
    # then reload the appropriate modules so the marshal is using the new ones.
    if "wrappers_pb2" in reloaded:
        importlib.reload(rules.wrappers)
    if "struct_pb2" in reloaded:
        importlib.reload(rules.struct)
    if reloaded.intersection({"timestamp_pb2", "duration_pb2"}):
        importlib.reload(rules.dates)
Пример #3
0
 def _Database(self):
   db = symbol_database.SymbolDatabase()
   # Register representative types from unittest_pb2.
   db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR)
   db.RegisterMessage(unittest_pb2.TestAllTypes)
   db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage)
   db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup)
   db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup)
   db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR)
   db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR)
   return db
Пример #4
0
 def _Database(self):
   # TODO(b/17734095): Remove this difference when the C++ implementation
   # supports multiple databases.
   if descriptor._USE_C_DESCRIPTORS:
     return symbol_database.Default()
   else:
     db = symbol_database.SymbolDatabase()
     # Register representative types from unittest_pb2.
     db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR)
     db.RegisterMessage(unittest_pb2.TestAllTypes)
     db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage)
     db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup)
     db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup)
     db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR)
     db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR)
     return db