Show More
@@ -0,0 +1,39 b'' | |||||
|
1 | # encoding: utf-8 | |||
|
2 | ||||
|
3 | """Utilities to enable code objects to be pickled. | |||
|
4 | ||||
|
5 | Any process that import this module will be able to pickle code objects. This | |||
|
6 | includes the func_code attribute of any function. Once unpickled, new | |||
|
7 | functions can be built using new.function(code, globals()). Eventually | |||
|
8 | we need to automate all of this so that functions themselves can be pickled. | |||
|
9 | ||||
|
10 | Reference: A. Tremols, P Cogolo, "Python Cookbook," p 302-305 | |||
|
11 | """ | |||
|
12 | ||||
|
13 | __docformat__ = "restructuredtext en" | |||
|
14 | ||||
|
15 | #------------------------------------------------------------------------------- | |||
|
16 | # Copyright (C) 2008 The IPython Development Team | |||
|
17 | # | |||
|
18 | # Distributed under the terms of the BSD License. The full license is in | |||
|
19 | # the file COPYING, distributed as part of this software. | |||
|
20 | #------------------------------------------------------------------------------- | |||
|
21 | ||||
|
22 | #------------------------------------------------------------------------------- | |||
|
23 | # Imports | |||
|
24 | #------------------------------------------------------------------------------- | |||
|
25 | ||||
|
26 | import new, types, copy_reg | |||
|
27 | ||||
|
28 | def code_ctor(*args): | |||
|
29 | return new.code(*args) | |||
|
30 | ||||
|
31 | def reduce_code(co): | |||
|
32 | if co.co_freevars or co.co_cellvars: | |||
|
33 | raise ValueError("Sorry, cannot pickle code objects with closures") | |||
|
34 | return code_ctor, (co.co_argcount, co.co_nlocals, co.co_stacksize, | |||
|
35 | co.co_flags, co.co_code, co.co_consts, co.co_names, | |||
|
36 | co.co_varnames, co.co_filename, co.co_name, co.co_firstlineno, | |||
|
37 | co.co_lnotab) | |||
|
38 | ||||
|
39 | copy_reg.pickle(types.CodeType, reduce_code) No newline at end of file |
@@ -21,15 +21,13 b' __test__ = {}' | |||||
21 |
|
21 | |||
22 | import cPickle as pickle |
|
22 | import cPickle as pickle | |
23 |
|
23 | |||
24 | # from twisted.python import components |
|
|||
25 | # from zope.interface import Interface, implements |
|
|||
26 |
|
||||
27 | try: |
|
24 | try: | |
28 | import numpy |
|
25 | import numpy | |
29 | except ImportError: |
|
26 | except ImportError: | |
30 | pass |
|
27 | pass | |
31 |
|
28 | |||
32 | from IPython.kernel.error import SerializationError |
|
29 | class SerializationError(Exception): | |
|
30 | pass | |||
33 |
|
31 | |||
34 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
35 | # Classes and functions |
|
33 | # Classes and functions |
@@ -18,8 +18,8 b' __docformat__ = "restructuredtext en"' | |||||
18 | from types import FunctionType |
|
18 | from types import FunctionType | |
19 |
|
19 | |||
20 | # contents of codeutil should either be in here, or codeutil belongs in IPython/util |
|
20 | # contents of codeutil should either be in here, or codeutil belongs in IPython/util | |
21 | from IPython.kernel import codeutil |
|
|||
22 | from IPython.zmq.parallel.dependency import dependent |
|
21 | from IPython.zmq.parallel.dependency import dependent | |
|
22 | import codeutil | |||
23 |
|
23 | |||
24 | class CannedObject(object): |
|
24 | class CannedObject(object): | |
25 | def __init__(self, obj, keys=[]): |
|
25 | def __init__(self, obj, keys=[]): |
General Comments 0
You need to be logged in to leave comments.
Login now