Files
pytorch/caffe2/python/test/python_protobuf_test.py
Dmytro Dzhulgakov 3af2d6d904 Enforce import order to make protobuf cpp implementation in python work (#18560)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18560

We have to import python protobuf here **before** we load cpp extension.
Otherwise it breaks under certain build conditions if cpp implementation of
protobuf is used. Presumably there's some registry in protobuf library and
python side has to initialize the dictionary first, before static
initialization in python extension does so. Otherwise, duplicated protobuf
descriptors will be created and it can lead to obscure errors like

  Parameter to MergeFrom() must be instance of same class: expected caffe2.NetDef got caffe2.NetDef.

I think it also fixes https://github.com/facebookarchive/caffe2/issues/1573

Reviewed By: ezyang, iroot900

Differential Revision: D14622054

fbshipit-source-id: 2499eb88ecdee85ff8d845859048f7ae5da2a480
2019-04-03 13:17:08 -07:00

25 lines
791 B
Python

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# make sure we use cpp implementation of protobuf
import os
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "cpp"
# import cpp extension first
from caffe2.python import core
# then import protobuf
from caffe2.proto import caffe2_pb2, metanet_pb2
import unittest
class TestCrossProtoCalls(unittest.TestCase):
def testSimple(self):
net = caffe2_pb2.NetDef()
meta = metanet_pb2.MetaNetDef()
# if metanet_pb2 wasn't initialized properly the following fails with a
# cryptic message: "Parameter to MergeFrom() must be instance of same
# class: expected caffe2.NetDef got caffe2.NetDef."
meta.nets.add(key="foo", value=net)