| 24 | |
| 25 | |
| 26 | class DBUnpickler(pickle.Unpickler): |
| 27 | |
| 28 | def __init__(self, file, connection): |
| 29 | super().__init__(file) |
| 30 | self.connection = connection |
| 31 | |
| 32 | def persistent_load(self, pid): |
| 33 | # This method is invoked whenever a persistent ID is encountered. |
| 34 | # Here, pid is the tuple returned by DBPickler. |
| 35 | cursor = self.connection.cursor() |
| 36 | type_tag, key_id = pid |
| 37 | if type_tag == "MemoRecord": |
| 38 | # Fetch the referenced record from the database and return it. |
| 39 | cursor.execute("SELECT * FROM memos WHERE key=?", (str(key_id),)) |
| 40 | key, task = cursor.fetchone() |
| 41 | return MemoRecord(key, task) |
| 42 | else: |
| 43 | # Always raises an error if you cannot return the correct object. |
| 44 | # Otherwise, the unpickler will think None is the object referenced |
| 45 | # by the persistent ID. |
| 46 | raise pickle.UnpicklingError("unsupported persistent object") |
| 47 | |
| 48 | |
| 49 | def main(): |
no outgoing calls
no test coverage detected
searching dependent graphs…