tvalentyn commented on code in PR #34683:
URL: https://github.com/apache/beam/pull/34683#discussion_r2053105116


##########
sdks/python/apache_beam/internal/cloudpickle_pickler_test.py:
##########
@@ -27,12 +27,89 @@
 from apache_beam.internal import module_test
 from apache_beam.internal.cloudpickle_pickler import dumps
 from apache_beam.internal.cloudpickle_pickler import loads
+from apache_beam.utils import shared
+
+GLOBAL_DICT_REF = module_test.GLOBAL_DICT
+
+
+# Allow weakref to dict
+class DictWrapper(dict):
+  pass
+
+
+MAIN_MODULE_DICT = DictWrapper()
+
+
+def acquire_dict():
+  return DictWrapper()
 
 
 class PicklerTest(unittest.TestCase):
 
   NO_MAPPINGPROXYTYPE = not hasattr(types, "MappingProxyType")
 
+  def test_globals_main_are_pickled_by_value(self):
+    self.assertIsNot(MAIN_MODULE_DICT, loads(dumps(lambda: 
MAIN_MODULE_DICT))())
+
+  def test_globals_shared_are_pickled_by_reference(self):
+    shared_handler = shared.Shared()
+    original_dict = shared_handler.acquire(acquire_dict)
+
+    unpickled_dict = loads(
+        dumps(lambda: shared_handler.acquire(acquire_dict)))()
+
+    self.assertIs(original_dict, unpickled_dict)
+
+  def test_globals_module_are_pickled_by_value_when_directly_referenced(self):

Review Comment:
   ```suggestion
     def 
test_module_globals_are_pickled_by_value_when_directly_referenced(self):
   ```



##########
sdks/python/apache_beam/internal/module_test.py:
##########
@@ -23,6 +23,38 @@
 import sys
 from typing import Any
 
+GLOBAL_DICT = {}
+
+
+class UnPicklable:
+  def __init__(self, x):
+    self.x = x
+
+  def __reduce__(self):
+    return NotImplemented
+
+
+UNPICLABLE_INSTANCE = UnPicklable(5)

Review Comment:
   nit: UNPICKLABLE_INSTANCE



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@beam.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to