Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 33 additions & 42 deletions sdks/python/apache_beam/internal/code_object_pickler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,83 +126,74 @@ def get_lambda_from_dictionary():
return get_lambda_from_dictionary()


prefix = __name__

test_cases = [
(top_level_function, f"{prefix}.top_level_function"
".__code__"),
(top_level_lambda, f"{prefix}.top_level_lambda"
".__code__"),
(
top_level_function,
"apache_beam.internal.code_object_pickler_test.top_level_function"
".__code__"),
(
top_level_lambda,
"apache_beam.internal.code_object_pickler_test.top_level_lambda"
".__code__"),
(
get_nested_function(),
(
"apache_beam.internal.code_object_pickler_test.get_nested_function"
get_nested_function(), (
f"{prefix}.get_nested_function"
".__code__.co_consts[nested_function]")),
(
get_lambda_from_dictionary(),
(
"apache_beam.internal.code_object_pickler_test"
f"{prefix}"
".get_lambda_from_dictionary.__code__.co_consts[<lambda>, ('x',)]")
),
(
get_lambda_from_dictionary_same_args(),
(
"apache_beam.internal.code_object_pickler_test"
f"{prefix}"
".get_lambda_from_dictionary_same_args.__code__.co_consts"
"[<lambda>, ('x',), " + hashlib.md5(
get_lambda_from_dictionary_same_args().__code__.co_code).
hexdigest() + "]")),
(
function_with_lambda_default_argument(),
(
"apache_beam.internal.code_object_pickler_test"
f"{prefix}"
".function_with_lambda_default_argument.__defaults__[0].__code__")),
(
function_with_function_default_argument(),
"apache_beam.internal.code_object_pickler_test.top_level_function"
f"{prefix}.top_level_function"
".__code__"),
(
add_one,
"apache_beam.internal.code_object_pickler_test.function_decorator"
".__code__.co_consts[<lambda>]"),
(add_one, f"{prefix}.function_decorator"
".__code__.co_consts[<lambda>]"),
(
ClassWithFunction.process,
"apache_beam.internal.code_object_pickler_test.ClassWithFunction"
f"{prefix}.ClassWithFunction"
".process.__code__"),
(
ClassWithStaticMethod.static_method,
"apache_beam.internal.code_object_pickler_test.ClassWithStaticMethod"
f"{prefix}.ClassWithStaticMethod"
".static_method.__code__"),
(
ClassWithClassMethod.class_method,
"apache_beam.internal.code_object_pickler_test.ClassWithClassMethod"
f"{prefix}.ClassWithClassMethod"
".class_method.__code__"),
(
ClassWithNestedFunction().process(),
(
"apache_beam.internal.code_object_pickler_test"
".ClassWithNestedFunction.process.__code__.co_consts"
f"{prefix}.ClassWithNestedFunction.process.__code__.co_consts"
"[nested_function]")),
(
ClassWithLambda().process(),
"apache_beam.internal.code_object_pickler_test.ClassWithLambda.process"
".__code__.co_consts[<lambda>]"),
f"{prefix}.ClassWithLambda.process.__code__.co_consts[<lambda>]"),
(
ClassWithNestedClass.InnerClass().process,
"apache_beam.internal.code_object_pickler_test.ClassWithNestedClass"
".InnerClass.process.__code__"),
f"{prefix}.ClassWithNestedClass.InnerClass.process.__code__"),
(
ClassWithNestedLambda().process(),
(
"apache_beam.internal.code_object_pickler_test"
f"{prefix}"
".ClassWithNestedLambda.process.__code__.co_consts"
"[get_lambda_from_dictionary].co_consts[<lambda>, ('x',)]")),
(
ClassWithNestedLambda.process,
"apache_beam.internal.code_object_pickler_test.ClassWithNestedLambda"
".process.__code__"),
f"{prefix}.ClassWithNestedLambda.process.__code__"),
]


Expand All @@ -225,35 +216,35 @@ def test_roundtrip(self, callable, unused_path):


class GetCodeFromCodeObjectIdentifierTest(unittest.TestCase):
def empty_path_raises_exception(self):
def test_empty_path_raises_exception(self):
with self.assertRaisesRegex(ValueError, "Path must not be empty"):
code_object_pickler.test_get_code_from_identifier("")
code_object_pickler.get_code_from_identifier("")

def invalid_default_index_raises_exception(self):
def test_invalid_default_index_raises_exception(self):
with self.assertRaisesRegex(ValueError, "out of bounds"):
code_object_pickler.test_get_code_from_identifier(
"apache_beam.internal.test_cases.module_with_default_argument."
code_object_pickler.get_code_from_identifier(
"apache_beam.internal.test_data.module_with_default_argument."
"function_with_lambda_default_argument.__defaults__[1]")

def invalid_single_name_path_raises_exception(self):
def test_invalid_single_name_path_raises_exception(self):
with self.assertRaisesRegex(AttributeError,
"Could not find code object with path"):
code_object_pickler.get_code_from_identifier(
"apache_beam.internal.test_cases.module_3."
"apache_beam.internal.test_data.module_3."
"my_function.__code__.co_consts[something]")

def invalid_lambda_with_args_path_raises_exception(self):
def test_invalid_lambda_with_args_path_raises_exception(self):
with self.assertRaisesRegex(AttributeError,
"Could not find code object with path"):
code_object_pickler.get_code_from_identifier(
"apache_beam.internal.test_cases.module_3."
"apache_beam.internal.test_data.module_3."
"my_function.__code__.co_consts[<lambda>, ('x',)]")

def invalid_lambda_with_hash_path_raises_exception(self):
def test_invalid_lambda_with_hash_path_raises_exception(self):
with self.assertRaisesRegex(AttributeError,
"Could not find code object with path"):
code_object_pickler.get_code_from_identifier(
"apache_beam.internal.test_cases.module_3."
"apache_beam.internal.test_data.module_3."
"my_function.__code__.co_consts[<lambda>, ('',), 1234567890]")

def test_adding_local_variable_in_class_preserves_object(self):
Expand Down
Loading