Skip to content

Commit

Permalink
[SPARK-1687] [PySpark] fix unit tests related to pickable namedtuple
Browse files Browse the repository at this point in the history
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 apache#1771 from davies/fix and squashes the following commits:

1a9e336 [Davies Liu] fix unit tests
  • Loading branch information
davies authored and JoshRosen committed Aug 4, 2014
1 parent 8e7d5ba commit 9fd82db
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion python/pyspark/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,11 @@ def __reduce__(self):

def _hijack_namedtuple():
""" 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):
return types.FunctionType(f.func_code, f.func_globals, f.func_name,
f.func_defaults, f.func_closure)
Expand All @@ -313,6 +316,7 @@ def namedtuple(name, fields, verbose=False, rename=False):
collections.namedtuple.func_globals["_old_namedtuple"] = _old_namedtuple
collections.namedtuple.func_globals["_hack_namedtuple"] = _hack_namedtuple
collections.namedtuple.func_code = namedtuple.func_code
collections.namedtuple.__hijack = 1

# hack the cls already generated by namedtuple
# those created in other module can be pickled as normal,
Expand Down

0 comments on commit 9fd82db

Please sign in to comment.