Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/site-packages/dill/temp.py: 16%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2#
3# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
4# Copyright (c) 2008-2016 California Institute of Technology.
5# Copyright (c) 2016-2026 The Uncertainty Quantification Foundation.
6# License: 3-clause BSD. The full license text is available at:
7# - https://github.com/uqfoundation/dill/blob/master/LICENSE
8"""
9Methods for serialized objects (or source code) stored in temporary files
10and file-like objects.
11"""
12#XXX: better instead to have functions write to any given file-like object ?
13#XXX: currently, all file-like objects are created by the function...
15__all__ = ['dump_source', 'dump', 'dumpIO_source', 'dumpIO',\
16 'load_source', 'load', 'loadIO_source', 'loadIO',\
17 'capture']
19import contextlib
22@contextlib.contextmanager
23def capture(stream='stdout'):
24 """builds a context that temporarily replaces the given stream name
26 >>> with capture('stdout') as out:
27 ... print ("foo!")
28 ...
29 >>> print (out.getvalue())
30 foo!
32 """
33 import sys
34 from io import StringIO
35 orig = getattr(sys, stream)
36 setattr(sys, stream, StringIO())
37 try:
38 yield getattr(sys, stream)
39 finally:
40 setattr(sys, stream, orig)
43def b(x): # deal with b'foo' versus 'foo'
44 import codecs
45 return codecs.latin_1_encode(x)[0]
47def load_source(file, **kwds):
48 """load an object that was stored with dill.temp.dump_source
50 file: filehandle
51 alias: string name of stored object
52 mode: mode to open the file, one of: {'r', 'rb'}
54 >>> f = lambda x: x**2
55 >>> pyfile = dill.temp.dump_source(f, alias='_f')
56 >>> _f = dill.temp.load_source(pyfile)
57 >>> _f(4)
58 16
59 """
60 alias = kwds.pop('alias', None)
61 mode = kwds.pop('mode', 'r')
62 fname = getattr(file, 'name', file) # fname=file.name or fname=file (if str)
63 source = open(fname, mode=mode, **kwds).read()
64 if not alias:
65 tag = source.strip().splitlines()[-1].split()
66 if tag[0] != '#NAME:':
67 stub = source.splitlines()[0]
68 raise IOError("unknown name for code: %s" % stub)
69 alias = tag[-1]
70 local = {}
71 exec(source, local)
72 _ = eval("%s" % alias, local)
73 return _
75def dump_source(object, **kwds):
76 """write object source to a NamedTemporaryFile (instead of dill.dump)
77Loads with "import" or "dill.temp.load_source". Returns the filehandle.
79 >>> f = lambda x: x**2
80 >>> pyfile = dill.temp.dump_source(f, alias='_f')
81 >>> _f = dill.temp.load_source(pyfile)
82 >>> _f(4)
83 16
85 >>> f = lambda x: x**2
86 >>> pyfile = dill.temp.dump_source(f, dir='.')
87 >>> modulename = os.path.basename(pyfile.name).split('.py')[0]
88 >>> exec('from %s import f as _f' % modulename)
89 >>> _f(4)
90 16
92Optional kwds:
93 If 'alias' is specified, the object will be renamed to the given string.
95 If 'prefix' is specified, the file name will begin with that prefix,
96 otherwise a default prefix is used.
98 If 'dir' is specified, the file will be created in that directory,
99 otherwise a default directory is used.
101 If 'text' is specified and true, the file is opened in text
102 mode. Else (the default) the file is opened in binary mode. On
103 some operating systems, this makes no difference.
105NOTE: Keep the return value for as long as you want your file to exist !
106 """ #XXX: write a "load_source"?
107 from .source import importable, getname
108 import tempfile
109 kwds.setdefault('delete', True)
110 kwds.pop('suffix', '') # this is *always* '.py'
111 alias = kwds.pop('alias', '') #XXX: include an alias so a name is known
112 name = str(alias) or getname(object)
113 name = "\n#NAME: %s\n" % name
114 #XXX: assumes kwds['dir'] is writable and on $PYTHONPATH
115 file = tempfile.NamedTemporaryFile(suffix='.py', **kwds)
116 file.write(b(''.join([importable(object, alias=alias),name])))
117 file.flush()
118 return file
120def load(file, **kwds):
121 """load an object that was stored with dill.temp.dump
123 file: filehandle
124 mode: mode to open the file, one of: {'r', 'rb'}
126 >>> dumpfile = dill.temp.dump([1, 2, 3, 4, 5])
127 >>> dill.temp.load(dumpfile)
128 [1, 2, 3, 4, 5]
129 """
130 import dill as pickle
131 mode = kwds.pop('mode', 'rb')
132 name = getattr(file, 'name', file) # name=file.name or name=file (if str)
133 return pickle.load(open(name, mode=mode, **kwds))
135def dump(object, **kwds):
136 """dill.dump of object to a NamedTemporaryFile.
137Loads with "dill.temp.load". Returns the filehandle.
139 >>> dumpfile = dill.temp.dump([1, 2, 3, 4, 5])
140 >>> dill.temp.load(dumpfile)
141 [1, 2, 3, 4, 5]
143Optional kwds:
144 If 'suffix' is specified, the file name will end with that suffix,
145 otherwise there will be no suffix.
147 If 'prefix' is specified, the file name will begin with that prefix,
148 otherwise a default prefix is used.
150 If 'dir' is specified, the file will be created in that directory,
151 otherwise a default directory is used.
153 If 'text' is specified and true, the file is opened in text
154 mode. Else (the default) the file is opened in binary mode. On
155 some operating systems, this makes no difference.
157NOTE: Keep the return value for as long as you want your file to exist !
158 """
159 import dill as pickle
160 import tempfile
161 kwds.setdefault('delete', True)
162 file = tempfile.NamedTemporaryFile(**kwds)
163 pickle.dump(object, file)
164 file.flush()
165 return file
167def loadIO(buffer, **kwds):
168 """load an object that was stored with dill.temp.dumpIO
170 buffer: buffer object
172 Additional keyword arguments are passed to dill.load.
174 >>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
175 >>> dill.temp.loadIO(dumpfile)
176 [1, 2, 3, 4, 5]
177 """
178 import dill as pickle
179 from io import BytesIO as StringIO
180 value = getattr(buffer, 'getvalue', buffer) # value or buffer.getvalue
181 if value != buffer: value = value() # buffer.getvalue()
182 return pickle.load(StringIO(value), **kwds)
184def dumpIO(object, **kwds):
185 """dill.dump of object to a buffer.
186Loads with "dill.temp.loadIO". Returns the buffer object.
188Additional keyword arguments are passed to dill.dump.
190 >>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
191 >>> dill.temp.loadIO(dumpfile)
192 [1, 2, 3, 4, 5]
193 """
194 import dill as pickle
195 from io import BytesIO as StringIO
196 file = StringIO()
197 pickle.dump(object, file, **kwds)
198 file.flush()
199 return file
201def loadIO_source(buffer, **kwds):
202 """load an object that was stored with dill.temp.dumpIO_source
204 buffer: buffer object
205 alias: string name of stored object
207 >>> f = lambda x:x**2
208 >>> pyfile = dill.temp.dumpIO_source(f, alias='_f')
209 >>> _f = dill.temp.loadIO_source(pyfile)
210 >>> _f(4)
211 16
212 """
213 alias = kwds.pop('alias', None)
214 source = getattr(buffer, 'getvalue', buffer) # source or buffer.getvalue
215 if source != buffer: source = source() # buffer.getvalue()
216 source = source.decode() # buffer to string
217 if not alias:
218 tag = source.strip().splitlines()[-1].split()
219 if tag[0] != '#NAME:':
220 stub = source.splitlines()[0]
221 raise IOError("unknown name for code: %s" % stub)
222 alias = tag[-1]
223 local = {}
224 exec(source, local)
225 _ = eval("%s" % alias, local)
226 return _
228def dumpIO_source(object, **kwds):
229 """write object source to a buffer (instead of dill.dump)
230Loads by with dill.temp.loadIO_source. Returns the buffer object.
232 >>> f = lambda x:x**2
233 >>> pyfile = dill.temp.dumpIO_source(f, alias='_f')
234 >>> _f = dill.temp.loadIO_source(pyfile)
235 >>> _f(4)
236 16
238Optional kwds:
239 If 'alias' is specified, the object will be renamed to the given string.
240 """
241 from .source import importable, getname
242 from io import BytesIO as StringIO
243 alias = kwds.pop('alias', '') #XXX: include an alias so a name is known
244 name = str(alias) or getname(object)
245 name = "\n#NAME: %s\n" % name
246 #XXX: assumes kwds['dir'] is writable and on $PYTHONPATH
247 file = StringIO()
248 file.write(b(''.join([importable(object, alias=alias),name])))
249 file.flush()
250 return file
253del contextlib
256# EOF