Skip to content
Snippets Groups Projects
Commit 9fd82dbb authored by Davies Liu's avatar Davies Liu Committed by Josh Rosen
Browse files

[SPARK-1687] [PySpark] fix unit tests related to pickable namedtuple

serializer is imported multiple times during doctests, so it's better to make _hijack_namedtuple() safe to be called multiple times.

Author: Davies Liu <davies.liu@gmail.com>

Closes #1771 from davies/fix and squashes the following commits:

1a9e336 [Davies Liu] fix unit tests
parent 8e7d5ba1
No related branches found
No related tags found
No related merge requests found
...@@ -297,8 +297,11 @@ def _hack_namedtuple(cls): ...@@ -297,8 +297,11 @@ def _hack_namedtuple(cls):
def _hijack_namedtuple(): def _hijack_namedtuple():
""" Hack namedtuple() to make it picklable """ """ Hack namedtuple() to make it picklable """
global _old_namedtuple # or it will put in closure # hijack only one time
if hasattr(collections.namedtuple, "__hijack"):
return
global _old_namedtuple # or it will put in closure
def _copy_func(f): def _copy_func(f):
return types.FunctionType(f.func_code, f.func_globals, f.func_name, return types.FunctionType(f.func_code, f.func_globals, f.func_name,
f.func_defaults, f.func_closure) f.func_defaults, f.func_closure)
...@@ -313,6 +316,7 @@ def _hijack_namedtuple(): ...@@ -313,6 +316,7 @@ def _hijack_namedtuple():
collections.namedtuple.func_globals["_old_namedtuple"] = _old_namedtuple collections.namedtuple.func_globals["_old_namedtuple"] = _old_namedtuple
collections.namedtuple.func_globals["_hack_namedtuple"] = _hack_namedtuple collections.namedtuple.func_globals["_hack_namedtuple"] = _hack_namedtuple
collections.namedtuple.func_code = namedtuple.func_code collections.namedtuple.func_code = namedtuple.func_code
collections.namedtuple.__hijack = 1
# hack the cls already generated by namedtuple # hack the cls already generated by namedtuple
# those created in other module can be pickled as normal, # those created in other module can be pickled as normal,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment