Skip to content

Commit

Permalink
Updating __init__ and test___init__ for Py3.
Browse files Browse the repository at this point in the history
These were discovered while merging these into the gcloud-python
library.
  • Loading branch information
dhermes committed Sep 3, 2015
1 parent 35ab37d commit 7a8e37e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gcloud_bigtable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
try:
from grpc._adapter import _c
except ImportError as exc:
if 'libgrpc.so' in exc.message:
if 'libgrpc.so' in str(exc):
raise ImportError('gRPC libraries could not be located. Please see '
'instructions to locate these files. You\'ll want '
'to set your LD_LIBRARY_PATH variable to help '
Expand Down
21 changes: 11 additions & 10 deletions gcloud_bigtable/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setUpClass(cls):
def tearDownClass(cls):
del cls._load_module_args

def _module_patch_helper(self, test_method):
def _module_patch_helper(self, function_to_test):
import sys

removed_mods = {}
Expand All @@ -42,7 +42,7 @@ def _module_patch_helper(self, test_method):
try:
sys.modules.pop('gcloud_bigtable', None)
# Test method should re-import gcloud_bigtable
test_method()
function_to_test()
finally:
for mod_name, value in removed_mods.items():
sys.modules[mod_name] = value
Expand All @@ -54,7 +54,7 @@ def test_success(self):

TEST_CASE = self

def test_method():
def function_to_test():
c_mod = types.ModuleType('grpc._adapter._c')
sys.modules['grpc._adapter._c'] = c_mod

Expand All @@ -70,7 +70,7 @@ def test_method():
gcloud_bigtable = imp.load_module(*TEST_CASE._load_module_args)
TEST_CASE.assertTrue(gcloud_bigtable._c is c_mod)

self._module_patch_helper(test_method)
self._module_patch_helper(function_to_test)

@staticmethod
def _create_fake_grpc_adapter_package(contents):
Expand All @@ -82,12 +82,12 @@ def _create_fake_grpc_adapter_package(contents):
curr_dir = os.path.join(temp_dir, 'grpc')
os.mkdir(curr_dir)
with open(os.path.join(curr_dir, '__init__.py'), 'wb') as file_obj:
file_obj.write('')
file_obj.write(b'')

curr_dir = os.path.join(curr_dir, '_adapter')
os.mkdir(curr_dir)
with open(os.path.join(curr_dir, '__init__.py'), 'wb') as file_obj:
file_obj.write('')
file_obj.write(b'')

filename = os.path.join(curr_dir, '_c.py')
with open(filename, 'wb') as file_obj:
Expand All @@ -101,11 +101,12 @@ def _import_fail_helper(self, orig_exc_message):

# Be sure the message contains libgrpc.so
c_module_contents = 'raise ImportError(%r)\n' % (orig_exc_message,)
c_module_contents = c_module_contents.encode('ascii')
temp_dir = self._create_fake_grpc_adapter_package(c_module_contents)

TEST_CASE = self

def test_method():
def function_to_test():
sys.path.insert(0, temp_dir)
try:
sys.modules.pop('grpc')
Expand All @@ -116,13 +117,13 @@ def test_method():
imp.load_module(*TEST_CASE._load_module_args)
except ImportError as exc:
if 'libgrpc.so' in orig_exc_message:
TEST_CASE.assertNotEqual(exc.message, orig_exc_message)
TEST_CASE.assertNotEqual(str(exc), orig_exc_message)
else:
TEST_CASE.assertEqual(exc.message, orig_exc_message)
TEST_CASE.assertEqual(str(exc), orig_exc_message)
finally:
sys.path.remove(temp_dir)

self._module_patch_helper(test_method)
self._module_patch_helper(function_to_test)

def test_system_library_cause(self):
# Be sure the message contains libgrpc.so
Expand Down

0 comments on commit 7a8e37e

Please sign in to comment.