Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/dill/source.py: 6%

613 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +0000

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-2022 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# 

9# inspired by inspect.py from Python-2.7.6 

10# inspect.py author: 'Ka-Ping Yee <ping@lfw.org>' 

11# inspect.py merged into original dill.source by Mike McKerns 4/13/14 

12""" 

13Extensions to python's 'inspect' module, which can be used 

14to retrieve information from live python objects. The methods 

15defined in this module are augmented to facilitate access to 

16source code of interactively defined functions and classes, 

17as well as provide access to source code for objects defined 

18in a file. 

19""" 

20 

21__all__ = ['findsource', 'getsourcelines', 'getsource', 'indent', 'outdent', \ 

22 '_wrap', 'dumpsource', 'getname', '_namespace', 'getimport', \ 

23 '_importable', 'importable','isdynamic', 'isfrommain'] 

24 

25import linecache 

26import re 

27from inspect import (getblock, getfile, getmodule, getsourcefile, indentsize, 

28 isbuiltin, isclass, iscode, isframe, isfunction, ismethod, 

29 ismodule, istraceback) 

30from tokenize import TokenError 

31 

32from ._dill import IS_IPYTHON 

33 

34 

35def isfrommain(obj): 

36 "check if object was built in __main__" 

37 module = getmodule(obj) 

38 if module and module.__name__ == '__main__': 

39 return True 

40 return False 

41 

42 

43def isdynamic(obj): 

44 "check if object was built in the interpreter" 

45 try: file = getfile(obj) 

46 except TypeError: file = None 

47 if file == '<stdin>' and isfrommain(obj): 

48 return True 

49 return False 

50 

51 

52def _matchlambda(func, line): 

53 """check if lambda object 'func' matches raw line of code 'line'""" 

54 from .detect import code as getcode 

55 from .detect import freevars, globalvars, varnames 

56 dummy = lambda : '__this_is_a_big_dummy_function__' 

57 # process the line (removing leading whitespace, etc) 

58 lhs,rhs = line.split('lambda ',1)[-1].split(":", 1) #FIXME: if !1 inputs 

59 try: #FIXME: unsafe 

60 _ = eval("lambda %s : %s" % (lhs,rhs), globals(),locals()) 

61 except Exception: _ = dummy 

62 # get code objects, for comparison 

63 _, code = getcode(_).co_code, getcode(func).co_code 

64 # check if func is in closure 

65 _f = [line.count(i) for i in freevars(func).keys()] 

66 if not _f: # not in closure 

67 # check if code matches 

68 if _ == code: return True 

69 return False 

70 # weak check on freevars 

71 if not all(_f): return False #XXX: VERY WEAK 

72 # weak check on varnames and globalvars 

73 _f = varnames(func) 

74 _f = [line.count(i) for i in _f[0]+_f[1]] 

75 if _f and not all(_f): return False #XXX: VERY WEAK 

76 _f = [line.count(i) for i in globalvars(func).keys()] 

77 if _f and not all(_f): return False #XXX: VERY WEAK 

78 # check if func is a double lambda 

79 if (line.count('lambda ') > 1) and (lhs in freevars(func).keys()): 

80 _lhs,_rhs = rhs.split('lambda ',1)[-1].split(":",1) #FIXME: if !1 inputs 

81 try: #FIXME: unsafe 

82 _f = eval("lambda %s : %s" % (_lhs,_rhs), globals(),locals()) 

83 except Exception: _f = dummy 

84 # get code objects, for comparison 

85 _, code = getcode(_f).co_code, getcode(func).co_code 

86 if len(_) != len(code): return False 

87 #NOTE: should be same code same order, but except for 't' and '\x88' 

88 _ = set((i,j) for (i,j) in zip(_,code) if i != j) 

89 if len(_) != 1: return False #('t','\x88') 

90 return True 

91 # check indentsize 

92 if not indentsize(line): return False #FIXME: is this a good check??? 

93 # check if code 'pattern' matches 

94 #XXX: or pattern match against dis.dis(code)? (or use uncompyle2?) 

95 _ = _.split(_[0]) # 't' #XXX: remove matching values if starts the same? 

96 _f = code.split(code[0]) # '\x88' 

97 #NOTE: should be same code different order, with different first element 

98 _ = dict(re.match(r'([\W\D\S])(.*)', _[i]).groups() for i in range(1,len(_))) 

99 _f = dict(re.match(r'([\W\D\S])(.*)', _f[i]).groups() for i in range(1,len(_f))) 

100 if (_.keys() == _f.keys()) and (sorted(_.values()) == sorted(_f.values())): 

101 return True 

102 return False 

103 

104 

105def findsource(object): 

106 """Return the entire source file and starting line number for an object. 

107 For interactively-defined objects, the 'file' is the interpreter's history. 

108 

109 The argument may be a module, class, method, function, traceback, frame, 

110 or code object. The source code is returned as a list of all the lines 

111 in the file and the line number indexes a line in that list. An IOError 

112 is raised if the source code cannot be retrieved, while a TypeError is 

113 raised for objects where the source code is unavailable (e.g. builtins).""" 

114 

115 module = getmodule(object) 

116 try: file = getfile(module) 

117 except TypeError: file = None 

118 is_module_main = (module and module.__name__ == '__main__' and not file) 

119 if IS_IPYTHON and is_module_main: 

120 #FIXME: quick fix for functions and classes in IPython interpreter 

121 try: 

122 file = getfile(object) 

123 sourcefile = getsourcefile(object) 

124 except TypeError: 

125 if isclass(object): 

126 for object_method in filter(isfunction, object.__dict__.values()): 

127 # look for a method of the class 

128 file_candidate = getfile(object_method) 

129 if not file_candidate.startswith('<ipython-input-'): 

130 continue 

131 file = file_candidate 

132 sourcefile = getsourcefile(object_method) 

133 break 

134 if file: 

135 lines = linecache.getlines(file) 

136 else: 

137 # fallback to use history 

138 history = '\n'.join(get_ipython().history_manager.input_hist_parsed) 

139 lines = [line + '\n' for line in history.splitlines()] 

140 # use readline when working in interpreter (i.e. __main__ and not file) 

141 elif is_module_main: 

142 try: 

143 import readline 

144 err = '' 

145 except ImportError: 

146 import sys 

147 err = sys.exc_info()[1].args[0] 

148 if sys.platform[:3] == 'win': 

149 err += ", please install 'pyreadline'" 

150 if err: 

151 raise IOError(err) 

152 lbuf = readline.get_current_history_length() 

153 lines = [readline.get_history_item(i)+'\n' for i in range(1,lbuf)] 

154 else: 

155 try: # special handling for class instances 

156 if not isclass(object) and isclass(type(object)): # __class__ 

157 file = getfile(module) 

158 sourcefile = getsourcefile(module) 

159 else: # builtins fail with a TypeError 

160 file = getfile(object) 

161 sourcefile = getsourcefile(object) 

162 except (TypeError, AttributeError): # fail with better error 

163 file = getfile(object) 

164 sourcefile = getsourcefile(object) 

165 if not sourcefile and file[:1] + file[-1:] != '<>': 

166 raise IOError('source code not available') 

167 file = sourcefile if sourcefile else file 

168 

169 module = getmodule(object, file) 

170 if module: 

171 lines = linecache.getlines(file, module.__dict__) 

172 else: 

173 lines = linecache.getlines(file) 

174 

175 if not lines: 

176 raise IOError('could not extract source code') 

177 

178 #FIXME: all below may fail if exec used (i.e. exec('f = lambda x:x') ) 

179 if ismodule(object): 

180 return lines, 0 

181 

182 #NOTE: beneficial if search goes from end to start of buffer history 

183 name = pat1 = obj = '' 

184 pat2 = r'^(\s*@)' 

185# pat1b = r'^(\s*%s\W*=)' % name #FIXME: finds 'f = decorate(f)', not exec 

186 if ismethod(object): 

187 name = object.__name__ 

188 if name == '<lambda>': pat1 = r'(.*(?<!\w)lambda(:|\s))' 

189 else: pat1 = r'^(\s*def\s)' 

190 object = object.__func__ 

191 if isfunction(object): 

192 name = object.__name__ 

193 if name == '<lambda>': 

194 pat1 = r'(.*(?<!\w)lambda(:|\s))' 

195 obj = object #XXX: better a copy? 

196 else: pat1 = r'^(\s*def\s)' 

197 object = object.__code__ 

198 if istraceback(object): 

199 object = object.tb_frame 

200 if isframe(object): 

201 object = object.f_code 

202 if iscode(object): 

203 if not hasattr(object, 'co_firstlineno'): 

204 raise IOError('could not find function definition') 

205 stdin = object.co_filename == '<stdin>' 

206 if stdin: 

207 lnum = len(lines) - 1 # can't get lnum easily, so leverage pat 

208 if not pat1: pat1 = r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)' 

209 else: 

210 lnum = object.co_firstlineno - 1 

211 pat1 = r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)' 

212 pat1 = re.compile(pat1); pat2 = re.compile(pat2) 

213 #XXX: candidate_lnum = [n for n in range(lnum) if pat1.match(lines[n])] 

214 while lnum > 0: #XXX: won't find decorators in <stdin> ? 

215 line = lines[lnum] 

216 if pat1.match(line): 

217 if not stdin: break # co_firstlineno does the job 

218 if name == '<lambda>': # hackery needed to confirm a match 

219 if _matchlambda(obj, line): break 

220 else: # not a lambda, just look for the name 

221 if name in line: # need to check for decorator... 

222 hats = 0 

223 for _lnum in range(lnum-1,-1,-1): 

224 if pat2.match(lines[_lnum]): hats += 1 

225 else: break 

226 lnum = lnum - hats 

227 break 

228 lnum = lnum - 1 

229 return lines, lnum 

230 

231 try: # turn instances into classes 

232 if not isclass(object) and isclass(type(object)): # __class__ 

233 object = object.__class__ #XXX: sometimes type(class) is better? 

234 #XXX: we don't find how the instance was built 

235 except AttributeError: pass 

236 if isclass(object): 

237 name = object.__name__ 

238 pat = re.compile(r'^(\s*)class\s*' + name + r'\b') 

239 # make some effort to find the best matching class definition: 

240 # use the one with the least indentation, which is the one 

241 # that's most probably not inside a function definition. 

242 candidates = [] 

243 for i in range(len(lines)-1,-1,-1): 

244 match = pat.match(lines[i]) 

245 if match: 

246 # if it's at toplevel, it's already the best one 

247 if lines[i][0] == 'c': 

248 return lines, i 

249 # else add whitespace to candidate list 

250 candidates.append((match.group(1), i)) 

251 if candidates: 

252 # this will sort by whitespace, and by line number, 

253 # less whitespace first #XXX: should sort high lnum before low 

254 candidates.sort() 

255 return lines, candidates[0][1] 

256 else: 

257 raise IOError('could not find class definition') 

258 raise IOError('could not find code object') 

259 

260 

261def getblocks(object, lstrip=False, enclosing=False, locate=False): 

262 """Return a list of source lines and starting line number for an object. 

263 Interactively-defined objects refer to lines in the interpreter's history. 

264 

265 If enclosing=True, then also return any enclosing code. 

266 If lstrip=True, ensure there is no indentation in the first line of code. 

267 If locate=True, then also return the line number for the block of code. 

268 

269 DEPRECATED: use 'getsourcelines' instead 

270 """ 

271 lines, lnum = findsource(object) 

272 

273 if ismodule(object): 

274 if lstrip: lines = _outdent(lines) 

275 return ([lines], [0]) if locate is True else [lines] 

276 

277 #XXX: 'enclosing' means: closures only? or classes and files? 

278 indent = indentsize(lines[lnum]) 

279 block = getblock(lines[lnum:]) #XXX: catch any TokenError here? 

280 

281 if not enclosing or not indent: 

282 if lstrip: block = _outdent(block) 

283 return ([block], [lnum]) if locate is True else [block] 

284 

285 pat1 = r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))'; pat1 = re.compile(pat1) 

286 pat2 = r'^(\s*@)'; pat2 = re.compile(pat2) 

287 #pat3 = r'^(\s*class\s)'; pat3 = re.compile(pat3) #XXX: enclosing class? 

288 #FIXME: bound methods need enclosing class (and then instantiation) 

289 # *or* somehow apply a partial using the instance 

290 

291 skip = 0 

292 line = 0 

293 blocks = []; _lnum = [] 

294 target = ''.join(block) 

295 while line <= lnum: #XXX: repeat lnum? or until line < lnum? 

296 # see if starts with ('def','lambda') and contains our target block 

297 if pat1.match(lines[line]): 

298 if not skip: 

299 try: code = getblock(lines[line:]) 

300 except TokenError: code = [lines[line]] 

301 if indentsize(lines[line]) > indent: #XXX: should be >= ? 

302 line += len(code) - skip 

303 elif target in ''.join(code): 

304 blocks.append(code) # save code block as the potential winner 

305 _lnum.append(line - skip) # save the line number for the match 

306 line += len(code) - skip 

307 else: 

308 line += 1 

309 skip = 0 

310 # find skip: the number of consecutive decorators 

311 elif pat2.match(lines[line]): 

312 try: code = getblock(lines[line:]) 

313 except TokenError: code = [lines[line]] 

314 skip = 1 

315 for _line in code[1:]: # skip lines that are decorators 

316 if not pat2.match(_line): break 

317 skip += 1 

318 line += skip 

319 # no match: reset skip and go to the next line 

320 else: 

321 line +=1 

322 skip = 0 

323 

324 if not blocks: 

325 blocks = [block] 

326 _lnum = [lnum] 

327 if lstrip: blocks = [_outdent(block) for block in blocks] 

328 # return last match 

329 return (blocks, _lnum) if locate is True else blocks 

330 

331 

332def getsourcelines(object, lstrip=False, enclosing=False): 

333 """Return a list of source lines and starting line number for an object. 

334 Interactively-defined objects refer to lines in the interpreter's history. 

335 

336 The argument may be a module, class, method, function, traceback, frame, 

337 or code object. The source code is returned as a list of the lines 

338 corresponding to the object and the line number indicates where in the 

339 original source file the first line of code was found. An IOError is 

340 raised if the source code cannot be retrieved, while a TypeError is 

341 raised for objects where the source code is unavailable (e.g. builtins). 

342 

343 If lstrip=True, ensure there is no indentation in the first line of code. 

344 If enclosing=True, then also return any enclosing code.""" 

345 code, n = getblocks(object, lstrip=lstrip, enclosing=enclosing, locate=True) 

346 return code[-1], n[-1] 

347 

348 

349#NOTE: broke backward compatibility 4/16/14 (was lstrip=True, force=True) 

350def getsource(object, alias='', lstrip=False, enclosing=False, \ 

351 force=False, builtin=False): 

352 """Return the text of the source code for an object. The source code for 

353 interactively-defined objects are extracted from the interpreter's history. 

354 

355 The argument may be a module, class, method, function, traceback, frame, 

356 or code object. The source code is returned as a single string. An 

357 IOError is raised if the source code cannot be retrieved, while a 

358 TypeError is raised for objects where the source code is unavailable 

359 (e.g. builtins). 

360 

361 If alias is provided, then add a line of code that renames the object. 

362 If lstrip=True, ensure there is no indentation in the first line of code. 

363 If enclosing=True, then also return any enclosing code. 

364 If force=True, catch (TypeError,IOError) and try to use import hooks. 

365 If builtin=True, force an import for any builtins 

366 """ 

367 # hascode denotes a callable 

368 hascode = _hascode(object) 

369 # is a class instance type (and not in builtins) 

370 instance = _isinstance(object) 

371 

372 # get source lines; if fail, try to 'force' an import 

373 try: # fails for builtins, and other assorted object types 

374 lines, lnum = getsourcelines(object, enclosing=enclosing) 

375 except (TypeError, IOError): # failed to get source, resort to import hooks 

376 if not force: # don't try to get types that findsource can't get 

377 raise 

378 if not getmodule(object): # get things like 'None' and '1' 

379 if not instance: return getimport(object, alias, builtin=builtin) 

380 # special handling (numpy arrays, ...) 

381 _import = getimport(object, builtin=builtin) 

382 name = getname(object, force=True) 

383 _alias = "%s = " % alias if alias else "" 

384 if alias == name: _alias = "" 

385 return _import+_alias+"%s\n" % name 

386 else: #FIXME: could use a good bit of cleanup, since using getimport... 

387 if not instance: return getimport(object, alias, builtin=builtin) 

388 # now we are dealing with an instance... 

389 name = object.__class__.__name__ 

390 module = object.__module__ 

391 if module in ['builtins','__builtin__']: 

392 return getimport(object, alias, builtin=builtin) 

393 else: #FIXME: leverage getimport? use 'from module import name'? 

394 lines, lnum = ["%s = __import__('%s', fromlist=['%s']).%s\n" % (name,module,name,name)], 0 

395 obj = eval(lines[0].lstrip(name + ' = ')) 

396 lines, lnum = getsourcelines(obj, enclosing=enclosing) 

397 

398 # strip leading indent (helps ensure can be imported) 

399 if lstrip or alias: 

400 lines = _outdent(lines) 

401 

402 # instantiate, if there's a nice repr #XXX: BAD IDEA??? 

403 if instance: #and force: #XXX: move into findsource or getsourcelines ? 

404 if '(' in repr(object): lines.append('%r\n' % object) 

405 #else: #XXX: better to somehow to leverage __reduce__ ? 

406 # reconstructor,args = object.__reduce__() 

407 # _ = reconstructor(*args) 

408 else: # fall back to serialization #XXX: bad idea? 

409 #XXX: better not duplicate work? #XXX: better new/enclose=True? 

410 lines = dumpsource(object, alias='', new=force, enclose=False) 

411 lines, lnum = [line+'\n' for line in lines.split('\n')][:-1], 0 

412 #else: object.__code__ # raise AttributeError 

413 

414 # add an alias to the source code 

415 if alias: 

416 if hascode: 

417 skip = 0 

418 for line in lines: # skip lines that are decorators 

419 if not line.startswith('@'): break 

420 skip += 1 

421 #XXX: use regex from findsource / getsourcelines ? 

422 if lines[skip].lstrip().startswith('def '): # we have a function 

423 if alias != object.__name__: 

424 lines.append('\n%s = %s\n' % (alias, object.__name__)) 

425 elif 'lambda ' in lines[skip]: # we have a lambda 

426 if alias != lines[skip].split('=')[0].strip(): 

427 lines[skip] = '%s = %s' % (alias, lines[skip]) 

428 else: # ...try to use the object's name 

429 if alias != object.__name__: 

430 lines.append('\n%s = %s\n' % (alias, object.__name__)) 

431 else: # class or class instance 

432 if instance: 

433 if alias != lines[-1].split('=')[0].strip(): 

434 lines[-1] = ('%s = ' % alias) + lines[-1] 

435 else: 

436 name = getname(object, force=True) or object.__name__ 

437 if alias != name: 

438 lines.append('\n%s = %s\n' % (alias, name)) 

439 return ''.join(lines) 

440 

441 

442def _hascode(object): 

443 '''True if object has an attribute that stores it's __code__''' 

444 return getattr(object,'__code__',None) or getattr(object,'func_code',None) 

445 

446def _isinstance(object): 

447 '''True if object is a class instance type (and is not a builtin)''' 

448 if _hascode(object) or isclass(object) or ismodule(object): 

449 return False 

450 if istraceback(object) or isframe(object) or iscode(object): 

451 return False 

452 # special handling (numpy arrays, ...) 

453 if not getmodule(object) and getmodule(type(object)).__name__ in ['numpy']: 

454 return True 

455# # check if is instance of a builtin 

456# if not getmodule(object) and getmodule(type(object)).__name__ in ['__builtin__','builtins']: 

457# return False 

458 _types = ('<class ',"<type 'instance'>") 

459 if not repr(type(object)).startswith(_types): #FIXME: weak hack 

460 return False 

461 if not getmodule(object) or object.__module__ in ['builtins','__builtin__'] or getname(object, force=True) in ['array']: 

462 return False 

463 return True # by process of elimination... it's what we want 

464 

465 

466def _intypes(object): 

467 '''check if object is in the 'types' module''' 

468 import types 

469 # allow user to pass in object or object.__name__ 

470 if type(object) is not type(''): 

471 object = getname(object, force=True) 

472 if object == 'ellipsis': object = 'EllipsisType' 

473 return True if hasattr(types, object) else False 

474 

475 

476def _isstring(object): #XXX: isstringlike better? 

477 '''check if object is a string-like type''' 

478 return isinstance(object, (str, bytes)) 

479 

480 

481def indent(code, spaces=4): 

482 '''indent a block of code with whitespace (default is 4 spaces)''' 

483 indent = indentsize(code) 

484 if type(spaces) is int: spaces = ' '*spaces 

485 # if '\t' is provided, will indent with a tab 

486 nspaces = indentsize(spaces) 

487 # blank lines (etc) need to be ignored 

488 lines = code.split('\n') 

489## stq = "'''"; dtq = '"""' 

490## in_stq = in_dtq = False 

491 for i in range(len(lines)): 

492 #FIXME: works... but shouldn't indent 2nd+ lines of multiline doc 

493 _indent = indentsize(lines[i]) 

494 if indent > _indent: continue 

495 lines[i] = spaces+lines[i] 

496## #FIXME: may fail when stq and dtq in same line (depends on ordering) 

497## nstq, ndtq = lines[i].count(stq), lines[i].count(dtq) 

498## if not in_dtq and not in_stq: 

499## lines[i] = spaces+lines[i] # we indent 

500## # entering a comment block 

501## if nstq%2: in_stq = not in_stq 

502## if ndtq%2: in_dtq = not in_dtq 

503## # leaving a comment block 

504## elif in_dtq and ndtq%2: in_dtq = not in_dtq 

505## elif in_stq and nstq%2: in_stq = not in_stq 

506## else: pass 

507 if lines[-1].strip() == '': lines[-1] = '' 

508 return '\n'.join(lines) 

509 

510 

511def _outdent(lines, spaces=None, all=True): 

512 '''outdent lines of code, accounting for docs and line continuations''' 

513 indent = indentsize(lines[0]) 

514 if spaces is None or spaces > indent or spaces < 0: spaces = indent 

515 for i in range(len(lines) if all else 1): 

516 #FIXME: works... but shouldn't outdent 2nd+ lines of multiline doc 

517 _indent = indentsize(lines[i]) 

518 if spaces > _indent: _spaces = _indent 

519 else: _spaces = spaces 

520 lines[i] = lines[i][_spaces:] 

521 return lines 

522 

523def outdent(code, spaces=None, all=True): 

524 '''outdent a block of code (default is to strip all leading whitespace)''' 

525 indent = indentsize(code) 

526 if spaces is None or spaces > indent or spaces < 0: spaces = indent 

527 #XXX: will this delete '\n' in some cases? 

528 if not all: return code[spaces:] 

529 return '\n'.join(_outdent(code.split('\n'), spaces=spaces, all=all)) 

530 

531 

532#XXX: not sure what the point of _wrap is... 

533__globals__ = globals() 

534__locals__ = locals() 

535def _wrap(f): 

536 """ encapsulate a function and it's __import__ """ 

537 def func(*args, **kwds): 

538 try: 

539 # _ = eval(getsource(f, force=True)) #XXX: safer but less robust 

540 exec(getimportable(f, alias='_'), __globals__, __locals__) 

541 except Exception: 

542 raise ImportError('cannot import name ' + f.__name__) 

543 return _(*args, **kwds) 

544 func.__name__ = f.__name__ 

545 func.__doc__ = f.__doc__ 

546 return func 

547 

548 

549def _enclose(object, alias=''): #FIXME: needs alias to hold returned object 

550 """create a function enclosure around the source of some object""" 

551 #XXX: dummy and stub should append a random string 

552 dummy = '__this_is_a_big_dummy_enclosing_function__' 

553 stub = '__this_is_a_stub_variable__' 

554 code = 'def %s():\n' % dummy 

555 code += indent(getsource(object, alias=stub, lstrip=True, force=True)) 

556 code += indent('return %s\n' % stub) 

557 if alias: code += '%s = ' % alias 

558 code += '%s(); del %s\n' % (dummy, dummy) 

559 #code += "globals().pop('%s',lambda :None)()\n" % dummy 

560 return code 

561 

562 

563def dumpsource(object, alias='', new=False, enclose=True): 

564 """'dump to source', where the code includes a pickled object. 

565 

566 If new=True and object is a class instance, then create a new 

567 instance using the unpacked class source code. If enclose, then 

568 create the object inside a function enclosure (thus minimizing 

569 any global namespace pollution). 

570 """ 

571 from dill import dumps 

572 pik = repr(dumps(object)) 

573 code = 'import dill\n' 

574 if enclose: 

575 stub = '__this_is_a_stub_variable__' #XXX: *must* be same _enclose.stub 

576 pre = '%s = ' % stub 

577 new = False #FIXME: new=True doesn't work with enclose=True 

578 else: 

579 stub = alias 

580 pre = '%s = ' % stub if alias else alias 

581 

582 # if a 'new' instance is not needed, then just dump and load 

583 if not new or not _isinstance(object): 

584 code += pre + 'dill.loads(%s)\n' % pik 

585 else: #XXX: other cases where source code is needed??? 

586 code += getsource(object.__class__, alias='', lstrip=True, force=True) 

587 mod = repr(object.__module__) # should have a module (no builtins here) 

588 code += pre + 'dill.loads(%s.replace(b%s,bytes(__name__,"UTF-8")))\n' % (pik,mod) 

589 #code += 'del %s' % object.__class__.__name__ #NOTE: kills any existing! 

590 

591 if enclose: 

592 # generation of the 'enclosure' 

593 dummy = '__this_is_a_big_dummy_object__' 

594 dummy = _enclose(dummy, alias=alias) 

595 # hack to replace the 'dummy' with the 'real' code 

596 dummy = dummy.split('\n') 

597 code = dummy[0]+'\n' + indent(code) + '\n'.join(dummy[-3:]) 

598 

599 return code #XXX: better 'dumpsourcelines', returning list of lines? 

600 

601 

602def getname(obj, force=False, fqn=False): #XXX: throw(?) to raise error on fail? 

603 """get the name of the object. for lambdas, get the name of the pointer """ 

604 if fqn: return '.'.join(_namespace(obj)) 

605 module = getmodule(obj) 

606 if not module: # things like "None" and "1" 

607 if not force: return None 

608 return repr(obj) 

609 try: 

610 #XXX: 'wrong' for decorators and curried functions ? 

611 # if obj.func_closure: ...use logic from getimportable, etc ? 

612 name = obj.__name__ 

613 if name == '<lambda>': 

614 return getsource(obj).split('=',1)[0].strip() 

615 # handle some special cases 

616 if module.__name__ in ['builtins','__builtin__']: 

617 if name == 'ellipsis': name = 'EllipsisType' 

618 return name 

619 except AttributeError: #XXX: better to just throw AttributeError ? 

620 if not force: return None 

621 name = repr(obj) 

622 if name.startswith('<'): # or name.split('('): 

623 return None 

624 return name 

625 

626 

627def _namespace(obj): 

628 """_namespace(obj); return namespace hierarchy (as a list of names) 

629 for the given object. For an instance, find the class hierarchy. 

630 

631 For example: 

632 

633 >>> from functools import partial 

634 >>> p = partial(int, base=2) 

635 >>> _namespace(p) 

636 [\'functools\', \'partial\'] 

637 """ 

638 # mostly for functions and modules and such 

639 #FIXME: 'wrong' for decorators and curried functions 

640 try: #XXX: needs some work and testing on different types 

641 module = qual = str(getmodule(obj)).split()[1].strip('>').strip('"').strip("'") 

642 qual = qual.split('.') 

643 if ismodule(obj): 

644 return qual 

645 # get name of a lambda, function, etc 

646 name = getname(obj) or obj.__name__ # failing, raise AttributeError 

647 # check special cases (NoneType, ...) 

648 if module in ['builtins','__builtin__']: # BuiltinFunctionType 

649 if _intypes(name): return ['types'] + [name] 

650 return qual + [name] #XXX: can be wrong for some aliased objects 

651 except Exception: pass 

652 # special case: numpy.inf and numpy.nan (we don't want them as floats) 

653 if str(obj) in ['inf','nan','Inf','NaN']: # is more, but are they needed? 

654 return ['numpy'] + [str(obj)] 

655 # mostly for classes and class instances and such 

656 module = getattr(obj.__class__, '__module__', None) 

657 qual = str(obj.__class__) 

658 try: qual = qual[qual.index("'")+1:-2] 

659 except ValueError: pass # str(obj.__class__) made the 'try' unnecessary 

660 qual = qual.split(".") 

661 if module in ['builtins','__builtin__']: 

662 # check special cases (NoneType, Ellipsis, ...) 

663 if qual[-1] == 'ellipsis': qual[-1] = 'EllipsisType' 

664 if _intypes(qual[-1]): module = 'types' #XXX: BuiltinFunctionType 

665 qual = [module] + qual 

666 return qual 

667 

668 

669#NOTE: 05/25/14 broke backward compatability: added 'alias' as 3rd argument 

670def _getimport(head, tail, alias='', verify=True, builtin=False): 

671 """helper to build a likely import string from head and tail of namespace. 

672 ('head','tail') are used in the following context: "from head import tail" 

673 

674 If verify=True, then test the import string before returning it. 

675 If builtin=True, then force an import for builtins where possible. 

676 If alias is provided, then rename the object on import. 

677 """ 

678 # special handling for a few common types 

679 if tail in ['Ellipsis', 'NotImplemented'] and head in ['types']: 

680 head = len.__module__ 

681 elif tail in ['None'] and head in ['types']: 

682 _alias = '%s = ' % alias if alias else '' 

683 if alias == tail: _alias = '' 

684 return _alias+'%s\n' % tail 

685 # we don't need to import from builtins, so return '' 

686# elif tail in ['NoneType','int','float','long','complex']: return '' #XXX: ? 

687 if head in ['builtins','__builtin__']: 

688 # special cases (NoneType, Ellipsis, ...) #XXX: BuiltinFunctionType 

689 if tail == 'ellipsis': tail = 'EllipsisType' 

690 if _intypes(tail): head = 'types' 

691 elif not builtin: 

692 _alias = '%s = ' % alias if alias else '' 

693 if alias == tail: _alias = '' 

694 return _alias+'%s\n' % tail 

695 else: pass # handle builtins below 

696 # get likely import string 

697 if not head: _str = "import %s" % tail 

698 else: _str = "from %s import %s" % (head, tail) 

699 _alias = " as %s\n" % alias if alias else "\n" 

700 if alias == tail: _alias = "\n" 

701 _str += _alias 

702 # FIXME: fails on most decorators, currying, and such... 

703 # (could look for magic __wrapped__ or __func__ attr) 

704 # (could fix in 'namespace' to check obj for closure) 

705 if verify and not head.startswith('dill.'):# weird behavior for dill 

706 #print(_str) 

707 try: exec(_str) #XXX: check if == obj? (name collision) 

708 except ImportError: #XXX: better top-down or bottom-up recursion? 

709 _head = head.rsplit(".",1)[0] #(or get all, then compare == obj?) 

710 if not _head: raise 

711 if _head != head: 

712 _str = _getimport(_head, tail, alias, verify) 

713 return _str 

714 

715 

716#XXX: rename builtin to force? vice versa? verify to force? (as in getsource) 

717#NOTE: 05/25/14 broke backward compatability: added 'alias' as 2nd argument 

718def getimport(obj, alias='', verify=True, builtin=False, enclosing=False): 

719 """get the likely import string for the given object 

720 

721 obj is the object to inspect 

722 If verify=True, then test the import string before returning it. 

723 If builtin=True, then force an import for builtins where possible. 

724 If enclosing=True, get the import for the outermost enclosing callable. 

725 If alias is provided, then rename the object on import. 

726 """ 

727 if enclosing: 

728 from .detect import outermost 

729 _obj = outermost(obj) 

730 obj = _obj if _obj else obj 

731 # get the namespace 

732 qual = _namespace(obj) 

733 head = '.'.join(qual[:-1]) 

734 tail = qual[-1] 

735 # for named things... with a nice repr #XXX: move into _namespace? 

736 try: # look for '<...>' and be mindful it might be in lists, dicts, etc... 

737 name = repr(obj).split('<',1)[1].split('>',1)[1] 

738 name = None # we have a 'object'-style repr 

739 except Exception: # it's probably something 'importable' 

740 if head in ['builtins','__builtin__']: 

741 name = repr(obj) #XXX: catch [1,2], (1,2), set([1,2])... others? 

742 else: 

743 name = repr(obj).split('(')[0] 

744 #if not repr(obj).startswith('<'): name = repr(obj).split('(')[0] 

745 #else: name = None 

746 if name: # try using name instead of tail 

747 try: return _getimport(head, name, alias, verify, builtin) 

748 except ImportError: pass 

749 except SyntaxError: 

750 if head in ['builtins','__builtin__']: 

751 _alias = '%s = ' % alias if alias else '' 

752 if alias == name: _alias = '' 

753 return _alias+'%s\n' % name 

754 else: pass 

755 try: 

756 #if type(obj) is type(abs): _builtin = builtin # BuiltinFunctionType 

757 #else: _builtin = False 

758 return _getimport(head, tail, alias, verify, builtin) 

759 except ImportError: 

760 raise # could do some checking against obj 

761 except SyntaxError: 

762 if head in ['builtins','__builtin__']: 

763 _alias = '%s = ' % alias if alias else '' 

764 if alias == tail: _alias = '' 

765 return _alias+'%s\n' % tail 

766 raise # could do some checking against obj 

767 

768 

769def _importable(obj, alias='', source=None, enclosing=False, force=True, \ 

770 builtin=True, lstrip=True): 

771 """get an import string (or the source code) for the given object 

772 

773 This function will attempt to discover the name of the object, or the repr 

774 of the object, or the source code for the object. To attempt to force 

775 discovery of the source code, use source=True, to attempt to force the 

776 use of an import, use source=False; otherwise an import will be sought 

777 for objects not defined in __main__. The intent is to build a string 

778 that can be imported from a python file. obj is the object to inspect. 

779 If alias is provided, then rename the object with the given alias. 

780 

781 If source=True, use these options: 

782 If enclosing=True, then also return any enclosing code. 

783 If force=True, catch (TypeError,IOError) and try to use import hooks. 

784 If lstrip=True, ensure there is no indentation in the first line of code. 

785 

786 If source=False, use these options: 

787 If enclosing=True, get the import for the outermost enclosing callable. 

788 If force=True, then don't test the import string before returning it. 

789 If builtin=True, then force an import for builtins where possible. 

790 """ 

791 if source is None: 

792 source = True if isfrommain(obj) else False 

793 if source: # first try to get the source 

794 try: 

795 return getsource(obj, alias, enclosing=enclosing, \ 

796 force=force, lstrip=lstrip, builtin=builtin) 

797 except Exception: pass 

798 try: 

799 if not _isinstance(obj): 

800 return getimport(obj, alias, enclosing=enclosing, \ 

801 verify=(not force), builtin=builtin) 

802 # first 'get the import', then 'get the instance' 

803 _import = getimport(obj, enclosing=enclosing, \ 

804 verify=(not force), builtin=builtin) 

805 name = getname(obj, force=True) 

806 if not name: 

807 raise AttributeError("object has no atribute '__name__'") 

808 _alias = "%s = " % alias if alias else "" 

809 if alias == name: _alias = "" 

810 return _import+_alias+"%s\n" % name 

811 

812 except Exception: pass 

813 if not source: # try getsource, only if it hasn't been tried yet 

814 try: 

815 return getsource(obj, alias, enclosing=enclosing, \ 

816 force=force, lstrip=lstrip, builtin=builtin) 

817 except Exception: pass 

818 # get the name (of functions, lambdas, and classes) 

819 # or hope that obj can be built from the __repr__ 

820 #XXX: what to do about class instances and such? 

821 obj = getname(obj, force=force) 

822 # we either have __repr__ or __name__ (or None) 

823 if not obj or obj.startswith('<'): 

824 raise AttributeError("object has no atribute '__name__'") 

825 _alias = '%s = ' % alias if alias else '' 

826 if alias == obj: _alias = '' 

827 return _alias+'%s\n' % obj 

828 #XXX: possible failsafe... (for example, for instances when source=False) 

829 # "import dill; result = dill.loads(<pickled_object>); # repr(<object>)" 

830 

831def _closuredimport(func, alias='', builtin=False): 

832 """get import for closured objects; return a dict of 'name' and 'import'""" 

833 import re 

834 from .detect import freevars, outermost 

835 free_vars = freevars(func) 

836 func_vars = {} 

837 # split into 'funcs' and 'non-funcs' 

838 for name,obj in list(free_vars.items()): 

839 if not isfunction(obj): continue 

840 # get import for 'funcs' 

841 fobj = free_vars.pop(name) 

842 src = getsource(fobj) 

843 if src.lstrip().startswith('@'): # we have a decorator 

844 src = getimport(fobj, alias=alias, builtin=builtin) 

845 else: # we have to "hack" a bit... and maybe be lucky 

846 encl = outermost(func) 

847 # pattern: 'func = enclosing(fobj' 

848 pat = r'.*[\w\s]=\s*'+getname(encl)+r'\('+getname(fobj) 

849 mod = getname(getmodule(encl)) 

850 #HACK: get file containing 'outer' function; is func there? 

851 lines,_ = findsource(encl) 

852 candidate = [line for line in lines if getname(encl) in line and \ 

853 re.match(pat, line)] 

854 if not candidate: 

855 mod = getname(getmodule(fobj)) 

856 #HACK: get file containing 'inner' function; is func there? 

857 lines,_ = findsource(fobj) 

858 candidate = [line for line in lines \ 

859 if getname(fobj) in line and re.match(pat, line)] 

860 if not len(candidate): raise TypeError('import could not be found') 

861 candidate = candidate[-1] 

862 name = candidate.split('=',1)[0].split()[-1].strip() 

863 src = _getimport(mod, name, alias=alias, builtin=builtin) 

864 func_vars[name] = src 

865 if not func_vars: 

866 name = outermost(func) 

867 mod = getname(getmodule(name)) 

868 if not mod or name is func: # then it can be handled by getimport 

869 name = getname(func, force=True) #XXX: better key? 

870 src = getimport(func, alias=alias, builtin=builtin) 

871 else: 

872 lines,_ = findsource(name) 

873 # pattern: 'func = enclosing(' 

874 candidate = [line for line in lines if getname(name) in line and \ 

875 re.match(r'.*[\w\s]=\s*'+getname(name)+r'\(', line)] 

876 if not len(candidate): raise TypeError('import could not be found') 

877 candidate = candidate[-1] 

878 name = candidate.split('=',1)[0].split()[-1].strip() 

879 src = _getimport(mod, name, alias=alias, builtin=builtin) 

880 func_vars[name] = src 

881 return func_vars 

882 

883#XXX: should be able to use __qualname__ 

884def _closuredsource(func, alias=''): 

885 """get source code for closured objects; return a dict of 'name' 

886 and 'code blocks'""" 

887 #FIXME: this entire function is a messy messy HACK 

888 # - pollutes global namespace 

889 # - fails if name of freevars are reused 

890 # - can unnecessarily duplicate function code 

891 from .detect import freevars 

892 free_vars = freevars(func) 

893 func_vars = {} 

894 # split into 'funcs' and 'non-funcs' 

895 for name,obj in list(free_vars.items()): 

896 if not isfunction(obj): 

897 # get source for 'non-funcs' 

898 free_vars[name] = getsource(obj, force=True, alias=name) 

899 continue 

900 # get source for 'funcs' 

901 fobj = free_vars.pop(name) 

902 src = getsource(fobj, alias) # DO NOT include dependencies 

903 # if source doesn't start with '@', use name as the alias 

904 if not src.lstrip().startswith('@'): #FIXME: 'enclose' in dummy; 

905 src = importable(fobj,alias=name)# wrong ref 'name' 

906 org = getsource(func, alias, enclosing=False, lstrip=True) 

907 src = (src, org) # undecorated first, then target 

908 else: #NOTE: reproduces the code! 

909 org = getsource(func, enclosing=True, lstrip=False) 

910 src = importable(fobj, alias, source=True) # include dependencies 

911 src = (org, src) # target first, then decorated 

912 func_vars[name] = src 

913 src = ''.join(free_vars.values()) 

914 if not func_vars: #FIXME: 'enclose' in dummy; wrong ref 'name' 

915 org = getsource(func, alias, force=True, enclosing=False, lstrip=True) 

916 src = (src, org) # variables first, then target 

917 else: 

918 src = (src, None) # just variables (better '' instead of None?) 

919 func_vars[None] = src 

920 # FIXME: remove duplicates (however, order is important...) 

921 return func_vars 

922 

923def importable(obj, alias='', source=None, builtin=True): 

924 """get an importable string (i.e. source code or the import string) 

925 for the given object, including any required objects from the enclosing 

926 and global scope 

927 

928 This function will attempt to discover the name of the object, or the repr 

929 of the object, or the source code for the object. To attempt to force 

930 discovery of the source code, use source=True, to attempt to force the 

931 use of an import, use source=False; otherwise an import will be sought 

932 for objects not defined in __main__. The intent is to build a string 

933 that can be imported from a python file. 

934 

935 obj is the object to inspect. If alias is provided, then rename the 

936 object with the given alias. If builtin=True, then force an import for 

937 builtins where possible. 

938 """ 

939 #NOTE: we always 'force', and 'lstrip' as necessary 

940 #NOTE: for 'enclosing', use importable(outermost(obj)) 

941 if source is None: 

942 source = True if isfrommain(obj) else False 

943 elif builtin and isbuiltin(obj): 

944 source = False 

945 tried_source = tried_import = False 

946 while True: 

947 if not source: # we want an import 

948 try: 

949 if _isinstance(obj): # for instances, punt to _importable 

950 return _importable(obj, alias, source=False, builtin=builtin) 

951 src = _closuredimport(obj, alias=alias, builtin=builtin) 

952 if len(src) == 0: 

953 raise NotImplementedError('not implemented') 

954 if len(src) > 1: 

955 raise NotImplementedError('not implemented') 

956 return list(src.values())[0] 

957 except Exception: 

958 if tried_source: raise 

959 tried_import = True 

960 # we want the source 

961 try: 

962 src = _closuredsource(obj, alias=alias) 

963 if len(src) == 0: 

964 raise NotImplementedError('not implemented') 

965 # groan... an inline code stitcher 

966 def _code_stitcher(block): 

967 "stitch together the strings in tuple 'block'" 

968 if block[0] and block[-1]: block = '\n'.join(block) 

969 elif block[0]: block = block[0] 

970 elif block[-1]: block = block[-1] 

971 else: block = '' 

972 return block 

973 # get free_vars first 

974 _src = _code_stitcher(src.pop(None)) 

975 _src = [_src] if _src else [] 

976 # get func_vars 

977 for xxx in src.values(): 

978 xxx = _code_stitcher(xxx) 

979 if xxx: _src.append(xxx) 

980 # make a single source string 

981 if not len(_src): 

982 src = '' 

983 elif len(_src) == 1: 

984 src = _src[0] 

985 else: 

986 src = '\n'.join(_src) 

987 # get source code of objects referred to by obj in global scope 

988 from .detect import globalvars 

989 obj = globalvars(obj) #XXX: don't worry about alias? recurse? etc? 

990 obj = list(getsource(_obj,name,force=True) for (name,_obj) in obj.items() if not isbuiltin(_obj)) 

991 obj = '\n'.join(obj) if obj else '' 

992 # combine all referred-to source (global then enclosing) 

993 if not obj: return src 

994 if not src: return obj 

995 return obj + src 

996 except Exception: 

997 if tried_import: raise 

998 tried_source = True 

999 source = not source 

1000 # should never get here 

1001 return 

1002 

1003 

1004# backward compatability 

1005def getimportable(obj, alias='', byname=True, explicit=False): 

1006 return importable(obj,alias,source=(not byname),builtin=explicit) 

1007 #return outdent(_importable(obj,alias,source=(not byname),builtin=explicit)) 

1008def likely_import(obj, passive=False, explicit=False): 

1009 return getimport(obj, verify=(not passive), builtin=explicit) 

1010def _likely_import(first, last, passive=False, explicit=True): 

1011 return _getimport(first, last, verify=(not passive), builtin=explicit) 

1012_get_name = getname 

1013getblocks_from_history = getblocks 

1014 

1015 

1016 

1017# EOF