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-2025 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 >>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
173 >>> dill.temp.loadIO(dumpfile)
174 [1, 2, 3, 4, 5]
175 """
176 import dill as pickle
177 from io import BytesIO as StringIO
178 value = getattr(buffer, 'getvalue', buffer) # value or buffer.getvalue
179 if value != buffer: value = value() # buffer.getvalue()
180 return pickle.load(StringIO(value))
182def dumpIO(object, **kwds):
183 """dill.dump of object to a buffer.
184Loads with "dill.temp.loadIO". Returns the buffer object.
186 >>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
187 >>> dill.temp.loadIO(dumpfile)
188 [1, 2, 3, 4, 5]
189 """
190 import dill as pickle
191 from io import BytesIO as StringIO
192 file = StringIO()
193 pickle.dump(object, file)
194 file.flush()
195 return file
197def loadIO_source(buffer, **kwds):
198 """load an object that was stored with dill.temp.dumpIO_source
200 buffer: buffer object
201 alias: string name of stored object
203 >>> f = lambda x:x**2
204 >>> pyfile = dill.temp.dumpIO_source(f, alias='_f')
205 >>> _f = dill.temp.loadIO_source(pyfile)
206 >>> _f(4)
207 16
208 """
209 alias = kwds.pop('alias', None)
210 source = getattr(buffer, 'getvalue', buffer) # source or buffer.getvalue
211 if source != buffer: source = source() # buffer.getvalue()
212 source = source.decode() # buffer to string
213 if not alias:
214 tag = source.strip().splitlines()[-1].split()
215 if tag[0] != '#NAME:':
216 stub = source.splitlines()[0]
217 raise IOError("unknown name for code: %s" % stub)
218 alias = tag[-1]
219 local = {}
220 exec(source, local)
221 _ = eval("%s" % alias, local)
222 return _
224def dumpIO_source(object, **kwds):
225 """write object source to a buffer (instead of dill.dump)
226Loads by with dill.temp.loadIO_source. Returns the buffer object.
228 >>> f = lambda x:x**2
229 >>> pyfile = dill.temp.dumpIO_source(f, alias='_f')
230 >>> _f = dill.temp.loadIO_source(pyfile)
231 >>> _f(4)
232 16
234Optional kwds:
235 If 'alias' is specified, the object will be renamed to the given string.
236 """
237 from .source import importable, getname
238 from io import BytesIO as StringIO
239 alias = kwds.pop('alias', '') #XXX: include an alias so a name is known
240 name = str(alias) or getname(object)
241 name = "\n#NAME: %s\n" % name
242 #XXX: assumes kwds['dir'] is writable and on $PYTHONPATH
243 file = StringIO()
244 file.write(b(''.join([importable(object, alias=alias),name])))
245 file.flush()
246 return file
249del contextlib
252# EOF