Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/site-packages/dill/__info__.py: 80%

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

5 statements  

1#!/usr/bin/env python 

2# 

3# Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 

4# Copyright (c) 2025 The Uncertainty Quantification Foundation. 

5# License: 3-clause BSD. The full license text is available at: 

6# - https://github.com/uqfoundation/dill/blob/master/LICENSE 

7''' 

8----------------------------- 

9dill: serialize all of Python 

10----------------------------- 

11 

12About Dill 

13========== 

14 

15``dill`` extends Python's ``pickle`` module for serializing and de-serializing 

16Python objects to the majority of the built-in Python types. Serialization 

17is the process of converting an object to a byte stream, and the inverse 

18of which is converting a byte stream back to a Python object hierarchy. 

19 

20``dill`` provides the user the same interface as the ``pickle`` module, and 

21also includes some additional features. In addition to pickling Python 

22objects, ``dill`` provides the ability to save the state of an interpreter 

23session in a single command. Hence, it would be feasible to save an 

24interpreter session, close the interpreter, ship the pickled file to 

25another computer, open a new interpreter, unpickle the session and 

26thus continue from the 'saved' state of the original interpreter 

27session. 

28 

29``dill`` can be used to store Python objects to a file, but the primary 

30usage is to send Python objects across the network as a byte stream. 

31``dill`` is quite flexible, and allows arbitrary user defined classes 

32and functions to be serialized. Thus ``dill`` is not intended to be 

33secure against erroneously or maliciously constructed data. It is 

34left to the user to decide whether the data they unpickle is from 

35a trustworthy source. 

36 

37``dill`` is part of ``pathos``, a Python framework for heterogeneous computing. 

38``dill`` is in active development, so any user feedback, bug reports, comments, 

39or suggestions are highly appreciated. A list of issues is located at 

40https://github.com/uqfoundation/dill/issues, with a legacy list maintained at 

41https://uqfoundation.github.io/project/pathos/query. 

42 

43 

44Major Features 

45============== 

46 

47``dill`` can pickle the following standard types: 

48 

49 - none, type, bool, int, float, complex, bytes, str, 

50 - tuple, list, dict, file, buffer, builtin, 

51 - Python classes, namedtuples, dataclasses, metaclasses, 

52 - instances of classes, 

53 - set, frozenset, array, functions, exceptions 

54 

55``dill`` can also pickle more 'exotic' standard types: 

56 

57 - functions with yields, nested functions, lambdas, 

58 - cell, method, unboundmethod, module, code, methodwrapper, 

59 - methoddescriptor, getsetdescriptor, memberdescriptor, wrapperdescriptor, 

60 - dictproxy, slice, notimplemented, ellipsis, quit 

61 

62``dill`` cannot yet pickle these standard types: 

63 

64 - frame, generator, traceback 

65 

66``dill`` also provides the capability to: 

67 

68 - save and load Python interpreter sessions 

69 - save and extract the source code from functions and classes 

70 - interactively diagnose pickling errors 

71 

72 

73Current Release 

74=============== 

75 

76The latest released version of ``dill`` is available from: 

77 

78 https://pypi.org/project/dill 

79 

80``dill`` is distributed under a 3-clause BSD license. 

81 

82 

83Development Version 

84=================== 

85 

86You can get the latest development version with all the shiny new features at: 

87 

88 https://github.com/uqfoundation 

89 

90If you have a new contribution, please submit a pull request. 

91 

92 

93Installation 

94============ 

95 

96``dill`` can be installed with ``pip``:: 

97 

98 $ pip install dill 

99 

100To optionally include the ``objgraph`` diagnostic tool in the install:: 

101 

102 $ pip install dill[graph] 

103 

104To optionally include the ``gprof2dot`` diagnostic tool in the install:: 

105 

106 $ pip install dill[profile] 

107 

108For windows users, to optionally install session history tools:: 

109 

110 $ pip install dill[readline] 

111 

112 

113Requirements 

114============ 

115 

116``dill`` requires: 

117 

118 - ``python`` (or ``pypy``), **>=3.9** 

119 - ``setuptools``, **>=42** 

120 

121Optional requirements: 

122 

123 - ``objgraph``, **>=1.7.2** 

124 - ``gprof2dot``, **>=2022.7.29** 

125 - ``pyreadline``, **>=1.7.1** (on windows) 

126 

127 

128Basic Usage 

129=========== 

130 

131``dill`` is a drop-in replacement for ``pickle``. Existing code can be 

132updated to allow complete pickling using:: 

133 

134 >>> import dill as pickle 

135 

136or:: 

137 

138 >>> from dill import dumps, loads 

139 

140``dumps`` converts the object to a unique byte string, and ``loads`` performs 

141the inverse operation:: 

142 

143 >>> squared = lambda x: x**2 

144 >>> loads(dumps(squared))(3) 

145 9 

146 

147There are a number of options to control serialization which are provided 

148as keyword arguments to several ``dill`` functions: 

149 

150* with *protocol*, the pickle protocol level can be set. This uses the 

151 same value as the ``pickle`` module, *DEFAULT_PROTOCOL*. 

152* with *byref=True*, ``dill`` to behave a lot more like pickle with 

153 certain objects (like modules) pickled by reference as opposed to 

154 attempting to pickle the object itself. 

155* with *recurse=True*, objects referred to in the global dictionary are 

156 recursively traced and pickled, instead of the default behavior of 

157 attempting to store the entire global dictionary. 

158* with *fmode*, the contents of the file can be pickled along with the file 

159 handle, which is useful if the object is being sent over the wire to a 

160 remote system which does not have the original file on disk. Options are 

161 *HANDLE_FMODE* for just the handle, *CONTENTS_FMODE* for the file content 

162 and *FILE_FMODE* for content and handle. 

163* with *ignore=False*, objects reconstructed with types defined in the 

164 top-level script environment use the existing type in the environment 

165 rather than a possibly different reconstructed type. 

166 

167The default serialization can also be set globally in *dill.settings*. 

168Thus, we can modify how ``dill`` handles references to the global dictionary 

169locally or globally:: 

170 

171 >>> import dill.settings 

172 >>> dumps(absolute) == dumps(absolute, recurse=True) 

173 False 

174 >>> dill.settings['recurse'] = True 

175 >>> dumps(absolute) == dumps(absolute, recurse=True) 

176 True 

177 

178``dill`` also includes source code inspection, as an alternate to pickling:: 

179 

180 >>> import dill.source 

181 >>> print(dill.source.getsource(squared)) 

182 squared = lambda x:x**2 

183 

184To aid in debugging pickling issues, use *dill.detect* which provides 

185tools like pickle tracing:: 

186 

187 >>> import dill.detect 

188 >>> with dill.detect.trace(): 

189 >>> dumps(squared) 

190 ┬ F1: <function <lambda> at 0x7fe074f8c280> 

191 ├┬ F2: <function _create_function at 0x7fe074c49c10> 

192 │└ # F2 [34 B] 

193 ├┬ Co: <code object <lambda> at 0x7fe07501eb30, file "<stdin>", line 1> 

194 │├┬ F2: <function _create_code at 0x7fe074c49ca0> 

195 ││└ # F2 [19 B] 

196 │└ # Co [87 B] 

197 ├┬ D1: <dict object at 0x7fe0750d4680> 

198 │└ # D1 [22 B] 

199 ├┬ D2: <dict object at 0x7fe074c5a1c0> 

200 │└ # D2 [2 B] 

201 ├┬ D2: <dict object at 0x7fe074f903c0> 

202 │├┬ D2: <dict object at 0x7fe074f8ebc0> 

203 ││└ # D2 [2 B] 

204 │└ # D2 [23 B] 

205 └ # F1 [180 B] 

206 

207With trace, we see how ``dill`` stored the lambda (``F1``) by first storing 

208``_create_function``, the underlying code object (``Co``) and ``_create_code`` 

209(which is used to handle code objects), then we handle the reference to 

210the global dict (``D2``) plus other dictionaries (``D1`` and ``D2``) that 

211save the lambda object's state. A ``#`` marks when the object is actually stored. 

212 

213 

214More Information 

215================ 

216 

217Probably the best way to get started is to look at the documentation at 

218http://dill.rtfd.io. Also see ``dill.tests`` for a set of scripts that 

219demonstrate how ``dill`` can serialize different Python objects. You can 

220run the test suite with ``python -m dill.tests``. The contents of any 

221pickle file can be examined with ``undill``. As ``dill`` conforms to 

222the ``pickle`` interface, the examples and documentation found at 

223http://docs.python.org/library/pickle.html also apply to ``dill`` 

224if one will ``import dill as pickle``. The source code is also generally 

225well documented, so further questions may be resolved by inspecting the 

226code itself. Please feel free to submit a ticket on github, or ask a 

227question on stackoverflow (**@Mike McKerns**). 

228If you would like to share how you use ``dill`` in your work, please send 

229an email (to **mmckerns at uqfoundation dot org**). 

230 

231 

232Citation 

233======== 

234 

235If you use ``dill`` to do research that leads to publication, we ask that you 

236acknowledge use of ``dill`` by citing the following in your publication:: 

237 

238 M.M. McKerns, L. Strand, T. Sullivan, A. Fang, M.A.G. Aivazis, 

239 "Building a framework for predictive science", Proceedings of 

240 the 10th Python in Science Conference, 2011; 

241 http://arxiv.org/pdf/1202.1056 

242 

243 Michael McKerns and Michael Aivazis, 

244 "pathos: a framework for heterogeneous computing", 2010- ; 

245 https://uqfoundation.github.io/project/pathos 

246 

247Please see https://uqfoundation.github.io/project/pathos or 

248http://arxiv.org/pdf/1202.1056 for further information. 

249 

250''' 

251 

252__version__ = '0.4.1.dev0' 

253__author__ = 'Mike McKerns' 

254 

255__license__ = ''' 

256Copyright (c) 2004-2016 California Institute of Technology. 

257Copyright (c) 2016-2025 The Uncertainty Quantification Foundation. 

258All rights reserved. 

259 

260This software is available subject to the conditions and terms laid 

261out below. By downloading and using this software you are agreeing 

262to the following conditions. 

263 

264Redistribution and use in source and binary forms, with or without 

265modification, are permitted provided that the following conditions 

266are met: 

267 

268 - Redistributions of source code must retain the above copyright 

269 notice, this list of conditions and the following disclaimer. 

270 

271 - Redistributions in binary form must reproduce the above copyright 

272 notice, this list of conditions and the following disclaimer in the 

273 documentation and/or other materials provided with the distribution. 

274 

275 - Neither the names of the copyright holders nor the names of any of 

276 the contributors may be used to endorse or promote products derived 

277 from this software without specific prior written permission. 

278 

279THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 

280"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 

281TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 

282PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 

283CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 

284EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 

285PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 

286OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 

287WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 

288OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 

289ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

290 

291'''