Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed Nov 27, 2019
1 parent 1918b77 commit f59cda2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
7 changes: 4 additions & 3 deletions sdks/python/apache_beam/coders/fast_coders_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
class FastCoders(unittest.TestCase):

def test_using_fast_impl(self):
if cython_should_be_enabled():
utils.check_compiled('apache_beam.coders.coder_impl')
else:
cython_enabled = cython_should_be_enabled()
self.assertEqual(
cython_enabled, utils.is_compiled('apache_beam.coders.stream'))
if not cython_enabled:
self.skipTest('Cython is not enabled')
# pylint: disable=wrong-import-order, wrong-import-position
# pylint: disable=unused-import
Expand Down
7 changes: 4 additions & 3 deletions sdks/python/apache_beam/coders/typecoders_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ def is_deterministic(self):
class TypeCodersTest(unittest.TestCase):

def setUp(self):
if cython_should_be_enabled():
utils.check_compiled('apache_beam.coders.coder_impl')
else:
cython_enabled = cython_should_be_enabled()
self.assertEqual(
cython_enabled, utils.is_compiled('apache_beam.coders.coder_impl'))
if not cython_enabled:
self.skipTest('Cython is not enabled')

def test_register_non_type_coder(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1562,9 +1562,12 @@ def test_lull_logging(self):
# TODO(BEAM-1251): Remove this test skip after dropping Py 2 support.
if sys.version_info < (3, 4):
self.skipTest('Log-based assertions are supported after Python 3.4')
if cython_should_be_enabled():
utils.check_compiled('apache_beam.runners.worker.opcounters')
else:

cython_enabled = cython_should_be_enabled()
self.assertEqual(
cython_enabled,
utils.is_compiled('apache_beam.runners.worker.opcounters'))
if not cython_enabled:
self.skipTest('Cython is not enabled')

with self.assertLogs(level='WARNING') as logs:
Expand Down
13 changes: 3 additions & 10 deletions sdks/python/apache_beam/tools/microbenchmarks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

import unittest

from pkg_resources import DistributionNotFound
from pkg_resources import get_distribution

from apache_beam.testing.util import cython_should_be_enabled
from apache_beam.tools import coders_microbenchmark
from apache_beam.tools import utils
Expand All @@ -37,13 +34,9 @@ def test_coders_microbenchmark(self):
num_runs=1, input_size=10, seed=1, verbose=False)

def test_check_compiled(self):
if cython_should_be_enabled():
utils.check_compiled('apache_beam.runners.worker.opcounters')
# Unfortunately, if cython is not installed, that doesn't mean we
# can rule out compiled modules (e.g. if Beam was installed from a wheel).
# Technically the other way around could be true as well, e.g. if
# Cython was installed after Beam, but this is rarer and more easily
# remedied.
self.assertEqual(
cython_should_be_enabled(),
utils.is_compiled('apache_beam.runners.worker.opcounters'))


if __name__ == '__main__':
Expand Down
14 changes: 11 additions & 3 deletions sdks/python/apache_beam/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@
import numpy


def check_compiled(module):
"""Check whether given module has been compiled.
def is_compiled(module):
"""Return whether given module has been compiled.
Args:
module: string, module name
"""
check_module = importlib.import_module(module)
ext = os.path.splitext(check_module.__file__)[-1]
if ext in ('.py', '.pyc'):
return ext not in ('.py', '.pyc')


def check_compiled(module):
"""Check whether given module has been compiled.
Args:
module: string, module name
"""
if not is_compiled(module):
raise RuntimeError(
"Profiling uncompiled code.\n"
"To compile beam, run "
Expand Down

0 comments on commit f59cda2

Please sign in to comment.