소스 검색

Make PickleType hack work on SQLAlchemy 0.5.x also

Copied PickleType from 0.5.8, like it had been copied from 0.6.x into
a805d4bd.py
Gunnlaugur Thor Briem 15 년 전
부모
커밋
975b3981e7
2개의 변경된 파일45개의 추가작업 그리고 1개의 파일을 삭제
  1. 41 0
      celery/db/dfd042c7.py
  2. 4 1
      celery/db/models.py

+ 41 - 0
celery/db/dfd042c7.py

@@ -0,0 +1,41 @@
+"""
+dfd042c7
+SQLAlchemy 0.5.8 version of a805d4bd, see the docstring of that module for an explanation of this workaround.
+"""
+from sqlalchemy.types import PickleType as _PickleType
+from sqlalchemy import util
+
+
+class PickleType(_PickleType):
+
+    def process_bind_param(self, value, dialect):
+        dumps = self.pickler.dumps
+        protocol = self.protocol
+        if value is None:
+            return None
+        return dumps(value, protocol)
+
+    def process_result_value(self, value, dialect):
+        loads = self.pickler.loads
+        if value is None:
+            return None
+        return loads(str(value))
+
+    def copy_value(self, value):
+        if self.mutable:
+            return self.pickler.loads(self.pickler.dumps(value, self.protocol))
+        else:
+            return value
+
+    def compare_values(self, x, y):
+        if self.comparator:
+            return self.comparator(x, y)
+        elif self.mutable and not hasattr(x, '__eq__') and x is not None:
+            util.warn_deprecated("Objects stored with PickleType when mutable=True must implement __eq__() for reliable comparison.")
+            return self.pickler.dumps(x, self.protocol) == self.pickler.dumps(y, self.protocol)
+        else:
+            return x == y
+
+    def is_mutable(self):
+        return self.mutable
+

+ 4 - 1
celery/db/models.py

@@ -5,7 +5,10 @@ import sqlalchemy as sa
 from celery import states
 from celery.db.session import ResultModelBase
 # See docstring of a805d4bd for an explanation for this workaround ;)
-from celery.db.a805d4bd import PickleType
+if sa.__version__.startswith('0.5'):
+    from celery.db.dfd042c7 import PickleType
+else:
+    from celery.db.a805d4bd import PickleType
 
 
 class Task(ResultModelBase):