aboutsummaryrefslogtreecommitdiff
path: root/python/pyspark/cloudpickle.py
diff options
context:
space:
mode:
authorDavies Liu <davies@databricks.com>2015-07-29 22:30:49 -0700
committerDavies Liu <davies.liu@gmail.com>2015-07-29 22:30:49 -0700
commite044705b4402f86d0557ecd146f3565388c7eeb4 (patch)
treef8db4937fe17a3c9fdb651f605df057aecf9d597 /python/pyspark/cloudpickle.py
parentf5dd11339fc9a6d11350f63beeca7c14aec169b1 (diff)
downloadspark-e044705b4402f86d0557ecd146f3565388c7eeb4.tar.gz
spark-e044705b4402f86d0557ecd146f3565388c7eeb4.tar.bz2
spark-e044705b4402f86d0557ecd146f3565388c7eeb4.zip
[SPARK-9116] [SQL] [PYSPARK] support Python only UDT in __main__
Also we could create a Python UDT without having a Scala one, it's important for Python users. cc mengxr JoshRosen Author: Davies Liu <davies@databricks.com> Closes #7453 from davies/class_in_main and squashes the following commits: 4dfd5e1 [Davies Liu] add tests for Python and Scala UDT 793d9b2 [Davies Liu] Merge branch 'master' of github.com:apache/spark into class_in_main dc65f19 [Davies Liu] address comment a9a3c40 [Davies Liu] Merge branch 'master' of github.com:apache/spark into class_in_main a86e1fc [Davies Liu] fix serialization ad528ba [Davies Liu] Merge branch 'master' of github.com:apache/spark into class_in_main 63f52ef [Davies Liu] fix pylint check 655b8a9 [Davies Liu] Merge branch 'master' of github.com:apache/spark into class_in_main 316a394 [Davies Liu] support Python UDT with UTF 0bcb3ef [Davies Liu] fix bug in mllib de986d6 [Davies Liu] fix test 83d65ac [Davies Liu] fix bug in StructType 55bb86e [Davies Liu] support Python UDT in __main__ (without Scala one)
Diffstat (limited to 'python/pyspark/cloudpickle.py')
-rw-r--r--python/pyspark/cloudpickle.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/python/pyspark/cloudpickle.py b/python/pyspark/cloudpickle.py
index 9ef93071d2..3b64798580 100644
--- a/python/pyspark/cloudpickle.py
+++ b/python/pyspark/cloudpickle.py
@@ -350,7 +350,26 @@ class CloudPickler(Pickler):
if new_override:
d['__new__'] = obj.__new__
- self.save_reduce(typ, (obj.__name__, obj.__bases__, d), obj=obj)
+ self.save(_load_class)
+ self.save_reduce(typ, (obj.__name__, obj.__bases__, {"__doc__": obj.__doc__}), obj=obj)
+ d.pop('__doc__', None)
+ # handle property and staticmethod
+ dd = {}
+ for k, v in d.items():
+ if isinstance(v, property):
+ k = ('property', k)
+ v = (v.fget, v.fset, v.fdel, v.__doc__)
+ elif isinstance(v, staticmethod) and hasattr(v, '__func__'):
+ k = ('staticmethod', k)
+ v = v.__func__
+ elif isinstance(v, classmethod) and hasattr(v, '__func__'):
+ k = ('classmethod', k)
+ v = v.__func__
+ dd[k] = v
+ self.save(dd)
+ self.write(pickle.TUPLE2)
+ self.write(pickle.REDUCE)
+
else:
raise pickle.PicklingError("Can't pickle %r" % obj)
@@ -708,6 +727,23 @@ def _make_skel_func(code, closures, base_globals = None):
None, None, closure)
+def _load_class(cls, d):
+ """
+ Loads additional properties into class `cls`.
+ """
+ for k, v in d.items():
+ if isinstance(k, tuple):
+ typ, k = k
+ if typ == 'property':
+ v = property(*v)
+ elif typ == 'staticmethod':
+ v = staticmethod(v)
+ elif typ == 'classmethod':
+ v = classmethod(v)
+ setattr(cls, k, v)
+ return cls
+
+
"""Constructors for 3rd party libraries
Note: These can never be renamed due to client compatibility issues"""