You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

665 lines
24 KiB

28 years ago
22 years ago
Merged revisions 67028,67040,67044,67046,67052,67065,67070,67077,67082 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r67028 | benjamin.peterson | 2008-10-25 18:27:07 -0500 (Sat, 25 Oct 2008) | 1 line don't use a catch-all ........ r67040 | armin.rigo | 2008-10-28 12:01:21 -0500 (Tue, 28 Oct 2008) | 5 lines Fix one of the tests: it relied on being present in an "output test" in order to actually test what it was supposed to test, i.e. that the code in the __del__ method did not crash. Use instead the new helper test_support.captured_output(). ........ r67044 | amaury.forgeotdarc | 2008-10-29 18:15:57 -0500 (Wed, 29 Oct 2008) | 3 lines Correct error message in io.open(): closefd=True is the only accepted value with a file name. ........ r67046 | thomas.heller | 2008-10-30 15:18:13 -0500 (Thu, 30 Oct 2008) | 2 lines Fixed a modulefinder crash on certain relative imports. ........ r67052 | christian.heimes | 2008-10-30 16:26:15 -0500 (Thu, 30 Oct 2008) | 1 line Issue #4237: io.FileIO() was raising invalid warnings caused by insufficient initialization of PyFileIOObject struct members. ........ r67065 | benjamin.peterson | 2008-10-30 18:59:18 -0500 (Thu, 30 Oct 2008) | 1 line move unprefixed error into .c file ........ r67070 | benjamin.peterson | 2008-10-31 15:41:44 -0500 (Fri, 31 Oct 2008) | 1 line rephrase has_key doc ........ r67077 | benjamin.peterson | 2008-11-03 09:14:51 -0600 (Mon, 03 Nov 2008) | 1 line #4048 make the parser module accept relative imports as valid ........ r67082 | hirokazu.yamamoto | 2008-11-03 12:03:06 -0600 (Mon, 03 Nov 2008) | 2 lines Issue #3774: Fixed an error when create a Tkinter menu item without command and then remove it. Written by Guilherme Polo (gpolo). ........
18 years ago
28 years ago
  1. """Find modules used by a script, using introspection."""
  2. from __future__ import generators
  3. import dis
  4. import imp
  5. import marshal
  6. import os
  7. import sys
  8. import types
  9. import struct
  10. READ_MODE = "rU"
  11. # XXX Clean up once str8's cstor matches bytes.
  12. LOAD_CONST = bytes([dis.opname.index('LOAD_CONST')])
  13. IMPORT_NAME = bytes([dis.opname.index('IMPORT_NAME')])
  14. STORE_NAME = bytes([dis.opname.index('STORE_NAME')])
  15. STORE_GLOBAL = bytes([dis.opname.index('STORE_GLOBAL')])
  16. STORE_OPS = [STORE_NAME, STORE_GLOBAL]
  17. HAVE_ARGUMENT = bytes([dis.HAVE_ARGUMENT])
  18. # Modulefinder does a good job at simulating Python's, but it can not
  19. # handle __path__ modifications packages make at runtime. Therefore there
  20. # is a mechanism whereby you can register extra paths in this map for a
  21. # package, and it will be honored.
  22. # Note this is a mapping is lists of paths.
  23. packagePathMap = {}
  24. # A Public interface
  25. def AddPackagePath(packagename, path):
  26. paths = packagePathMap.get(packagename, [])
  27. paths.append(path)
  28. packagePathMap[packagename] = paths
  29. replacePackageMap = {}
  30. # This ReplacePackage mechanism allows modulefinder to work around
  31. # situations in which a package injects itself under the name
  32. # of another package into sys.modules at runtime by calling
  33. # ReplacePackage("real_package_name", "faked_package_name")
  34. # before running ModuleFinder.
  35. def ReplacePackage(oldname, newname):
  36. replacePackageMap[oldname] = newname
  37. class Module:
  38. def __init__(self, name, file=None, path=None):
  39. self.__name__ = name
  40. self.__file__ = file
  41. self.__path__ = path
  42. self.__code__ = None
  43. # The set of global names that are assigned to in the module.
  44. # This includes those names imported through starimports of
  45. # Python modules.
  46. self.globalnames = {}
  47. # The set of starimports this module did that could not be
  48. # resolved, ie. a starimport from a non-Python module.
  49. self.starimports = {}
  50. def __repr__(self):
  51. s = "Module(%r" % (self.__name__,)
  52. if self.__file__ is not None:
  53. s = s + ", %r" % (self.__file__,)
  54. if self.__path__ is not None:
  55. s = s + ", %r" % (self.__path__,)
  56. s = s + ")"
  57. return s
  58. class ModuleFinder:
  59. def __init__(self, path=None, debug=0, excludes=[], replace_paths=[]):
  60. if path is None:
  61. path = sys.path
  62. self.path = path
  63. self.modules = {}
  64. self.badmodules = {}
  65. self.debug = debug
  66. self.indent = 0
  67. self.excludes = excludes
  68. self.replace_paths = replace_paths
  69. self.processed_paths = [] # Used in debugging only
  70. def msg(self, level, str, *args):
  71. if level <= self.debug:
  72. for i in range(self.indent):
  73. print(" ", end=' ')
  74. print(str, end=' ')
  75. for arg in args:
  76. print(repr(arg), end=' ')
  77. print()
  78. def msgin(self, *args):
  79. level = args[0]
  80. if level <= self.debug:
  81. self.indent = self.indent + 1
  82. self.msg(*args)
  83. def msgout(self, *args):
  84. level = args[0]
  85. if level <= self.debug:
  86. self.indent = self.indent - 1
  87. self.msg(*args)
  88. def run_script(self, pathname):
  89. self.msg(2, "run_script", pathname)
  90. with open(pathname, READ_MODE) as fp:
  91. stuff = ("", "r", imp.PY_SOURCE)
  92. self.load_module('__main__', fp, pathname, stuff)
  93. def load_file(self, pathname):
  94. dir, name = os.path.split(pathname)
  95. name, ext = os.path.splitext(name)
  96. with open(pathname, READ_MODE) as fp:
  97. stuff = (ext, "r", imp.PY_SOURCE)
  98. self.load_module(name, fp, pathname, stuff)
  99. def import_hook(self, name, caller=None, fromlist=None, level=-1):
  100. self.msg(3, "import_hook", name, caller, fromlist, level)
  101. parent = self.determine_parent(caller, level=level)
  102. q, tail = self.find_head_package(parent, name)
  103. m = self.load_tail(q, tail)
  104. if not fromlist:
  105. return q
  106. if m.__path__:
  107. self.ensure_fromlist(m, fromlist)
  108. return None
  109. def determine_parent(self, caller, level=-1):
  110. self.msgin(4, "determine_parent", caller, level)
  111. if not caller or level == 0:
  112. self.msgout(4, "determine_parent -> None")
  113. return None
  114. pname = caller.__name__
  115. if level >= 1: # relative import
  116. if caller.__path__:
  117. level -= 1
  118. if level == 0:
  119. parent = self.modules[pname]
  120. assert parent is caller
  121. self.msgout(4, "determine_parent ->", parent)
  122. return parent
  123. if pname.count(".") < level:
  124. raise ImportError("relative importpath too deep")
  125. pname = ".".join(pname.split(".")[:-level])
  126. parent = self.modules[pname]
  127. self.msgout(4, "determine_parent ->", parent)
  128. return parent
  129. if caller.__path__:
  130. parent = self.modules[pname]
  131. assert caller is parent
  132. self.msgout(4, "determine_parent ->", parent)
  133. return parent
  134. if '.' in pname:
  135. i = pname.rfind('.')
  136. pname = pname[:i]
  137. parent = self.modules[pname]
  138. assert parent.__name__ == pname
  139. self.msgout(4, "determine_parent ->", parent)
  140. return parent
  141. self.msgout(4, "determine_parent -> None")
  142. return None
  143. def find_head_package(self, parent, name):
  144. self.msgin(4, "find_head_package", parent, name)
  145. if '.' in name:
  146. i = name.find('.')
  147. head = name[:i]
  148. tail = name[i+1:]
  149. else:
  150. head = name
  151. tail = ""
  152. if parent:
  153. qname = "%s.%s" % (parent.__name__, head)
  154. else:
  155. qname = head
  156. q = self.import_module(head, qname, parent)
  157. if q:
  158. self.msgout(4, "find_head_package ->", (q, tail))
  159. return q, tail
  160. if parent:
  161. qname = head
  162. parent = None
  163. q = self.import_module(head, qname, parent)
  164. if q:
  165. self.msgout(4, "find_head_package ->", (q, tail))
  166. return q, tail
  167. self.msgout(4, "raise ImportError: No module named", qname)
  168. raise ImportError("No module named " + qname)
  169. def load_tail(self, q, tail):
  170. self.msgin(4, "load_tail", q, tail)
  171. m = q
  172. while tail:
  173. i = tail.find('.')
  174. if i < 0: i = len(tail)
  175. head, tail = tail[:i], tail[i+1:]
  176. mname = "%s.%s" % (m.__name__, head)
  177. m = self.import_module(head, mname, m)
  178. if not m:
  179. self.msgout(4, "raise ImportError: No module named", mname)
  180. raise ImportError("No module named " + mname)
  181. self.msgout(4, "load_tail ->", m)
  182. return m
  183. def ensure_fromlist(self, m, fromlist, recursive=0):
  184. self.msg(4, "ensure_fromlist", m, fromlist, recursive)
  185. for sub in fromlist:
  186. if sub == "*":
  187. if not recursive:
  188. all = self.find_all_submodules(m)
  189. if all:
  190. self.ensure_fromlist(m, all, 1)
  191. elif not hasattr(m, sub):
  192. subname = "%s.%s" % (m.__name__, sub)
  193. submod = self.import_module(sub, subname, m)
  194. if not submod:
  195. raise ImportError("No module named " + subname)
  196. def find_all_submodules(self, m):
  197. if not m.__path__:
  198. return
  199. modules = {}
  200. # 'suffixes' used to be a list hardcoded to [".py", ".pyc", ".pyo"].
  201. # But we must also collect Python extension modules - although
  202. # we cannot separate normal dlls from Python extensions.
  203. suffixes = []
  204. for triple in imp.get_suffixes():
  205. suffixes.append(triple[0])
  206. for dir in m.__path__:
  207. try:
  208. names = os.listdir(dir)
  209. except os.error:
  210. self.msg(2, "can't list directory", dir)
  211. continue
  212. for name in names:
  213. mod = None
  214. for suff in suffixes:
  215. n = len(suff)
  216. if name[-n:] == suff:
  217. mod = name[:-n]
  218. break
  219. if mod and mod != "__init__":
  220. modules[mod] = mod
  221. return modules.keys()
  222. def import_module(self, partname, fqname, parent):
  223. self.msgin(3, "import_module", partname, fqname, parent)
  224. try:
  225. m = self.modules[fqname]
  226. except KeyError:
  227. pass
  228. else:
  229. self.msgout(3, "import_module ->", m)
  230. return m
  231. if fqname in self.badmodules:
  232. self.msgout(3, "import_module -> None")
  233. return None
  234. if parent and parent.__path__ is None:
  235. self.msgout(3, "import_module -> None")
  236. return None
  237. try:
  238. fp, pathname, stuff = self.find_module(partname,
  239. parent and parent.__path__, parent)
  240. except ImportError:
  241. self.msgout(3, "import_module ->", None)
  242. return None
  243. try:
  244. m = self.load_module(fqname, fp, pathname, stuff)
  245. finally:
  246. if fp: fp.close()
  247. if parent:
  248. setattr(parent, partname, m)
  249. self.msgout(3, "import_module ->", m)
  250. return m
  251. def load_module(self, fqname, fp, pathname, file_info):
  252. suffix, mode, type = file_info
  253. self.msgin(2, "load_module", fqname, fp and "fp", pathname)
  254. if type == imp.PKG_DIRECTORY:
  255. m = self.load_package(fqname, pathname)
  256. self.msgout(2, "load_module ->", m)
  257. return m
  258. if type == imp.PY_SOURCE:
  259. co = compile(fp.read()+'\n', pathname, 'exec')
  260. elif type == imp.PY_COMPILED:
  261. if fp.read(4) != imp.get_magic():
  262. self.msgout(2, "raise ImportError: Bad magic number", pathname)
  263. raise ImportError("Bad magic number in %s" % pathname)
  264. fp.read(4)
  265. co = marshal.load(fp)
  266. else:
  267. co = None
  268. m = self.add_module(fqname)
  269. m.__file__ = pathname
  270. if co:
  271. if self.replace_paths:
  272. co = self.replace_paths_in_code(co)
  273. m.__code__ = co
  274. self.scan_code(co, m)
  275. self.msgout(2, "load_module ->", m)
  276. return m
  277. def _add_badmodule(self, name, caller):
  278. if name not in self.badmodules:
  279. self.badmodules[name] = {}
  280. if caller:
  281. self.badmodules[name][caller.__name__] = 1
  282. else:
  283. self.badmodules[name]["-"] = 1
  284. def _safe_import_hook(self, name, caller, fromlist, level=-1):
  285. # wrapper for self.import_hook() that won't raise ImportError
  286. if name in self.badmodules:
  287. self._add_badmodule(name, caller)
  288. return
  289. try:
  290. self.import_hook(name, caller, level=level)
  291. except ImportError as msg:
  292. self.msg(2, "ImportError:", str(msg))
  293. self._add_badmodule(name, caller)
  294. else:
  295. if fromlist:
  296. for sub in fromlist:
  297. if sub in self.badmodules:
  298. self._add_badmodule(sub, caller)
  299. continue
  300. try:
  301. self.import_hook(name, caller, [sub], level=level)
  302. except ImportError as msg:
  303. self.msg(2, "ImportError:", str(msg))
  304. fullname = name + "." + sub
  305. self._add_badmodule(fullname, caller)
  306. def scan_opcodes(self, co,
  307. unpack = struct.unpack):
  308. # Scan the code, and yield 'interesting' opcode combinations
  309. # Version for Python 2.4 and older
  310. code = co.co_code
  311. names = co.co_names
  312. consts = co.co_consts
  313. while code:
  314. c = code[0]
  315. if c in STORE_OPS:
  316. oparg, = unpack('<H', code[1:3])
  317. yield "store", (names[oparg],)
  318. code = code[3:]
  319. continue
  320. if c == LOAD_CONST and code[3] == IMPORT_NAME:
  321. oparg_1, oparg_2 = unpack('<xHxH', code[:6])
  322. yield "import", (consts[oparg_1], names[oparg_2])
  323. code = code[6:]
  324. continue
  325. if c >= HAVE_ARGUMENT:
  326. code = code[3:]
  327. else:
  328. code = code[1:]
  329. def scan_opcodes_25(self, co,
  330. unpack = struct.unpack):
  331. # Scan the code, and yield 'interesting' opcode combinations
  332. # Python 2.5 version (has absolute and relative imports)
  333. code = co.co_code
  334. names = co.co_names
  335. consts = co.co_consts
  336. LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME
  337. while code:
  338. c = bytes([code[0]])
  339. if c in STORE_OPS:
  340. oparg, = unpack('<H', code[1:3])
  341. yield "store", (names[oparg],)
  342. code = code[3:]
  343. continue
  344. if code[:9:3] == LOAD_LOAD_AND_IMPORT:
  345. oparg_1, oparg_2, oparg_3 = unpack('<xHxHxH', code[:9])
  346. level = consts[oparg_1]
  347. if level == 0: # absolute import
  348. yield "absolute_import", (consts[oparg_2], names[oparg_3])
  349. else: # relative import
  350. yield "relative_import", (level, consts[oparg_2], names[oparg_3])
  351. code = code[9:]
  352. continue
  353. if c >= HAVE_ARGUMENT:
  354. code = code[3:]
  355. else:
  356. code = code[1:]
  357. def scan_code(self, co, m):
  358. code = co.co_code
  359. if sys.version_info >= (2, 5):
  360. scanner = self.scan_opcodes_25
  361. else:
  362. scanner = self.scan_opcodes
  363. for what, args in scanner(co):
  364. if what == "store":
  365. name, = args
  366. m.globalnames[name] = 1
  367. elif what == "absolute_import":
  368. fromlist, name = args
  369. have_star = 0
  370. if fromlist is not None:
  371. if "*" in fromlist:
  372. have_star = 1
  373. fromlist = [f for f in fromlist if f != "*"]
  374. self._safe_import_hook(name, m, fromlist, level=0)
  375. if have_star:
  376. # We've encountered an "import *". If it is a Python module,
  377. # the code has already been parsed and we can suck out the
  378. # global names.
  379. mm = None
  380. if m.__path__:
  381. # At this point we don't know whether 'name' is a
  382. # submodule of 'm' or a global module. Let's just try
  383. # the full name first.
  384. mm = self.modules.get(m.__name__ + "." + name)
  385. if mm is None:
  386. mm = self.modules.get(name)
  387. if mm is not None:
  388. m.globalnames.update(mm.globalnames)
  389. m.starimports.update(mm.starimports)
  390. if mm.__code__ is None:
  391. m.starimports[name] = 1
  392. else:
  393. m.starimports[name] = 1
  394. elif what == "relative_import":
  395. level, fromlist, name = args
  396. if name:
  397. self._safe_import_hook(name, m, fromlist, level=level)
  398. else:
  399. parent = self.determine_parent(m, level=level)
  400. self._safe_import_hook(parent.__name__, None, fromlist, level=0)
  401. else:
  402. # We don't expect anything else from the generator.
  403. raise RuntimeError(what)
  404. for c in co.co_consts:
  405. if isinstance(c, type(co)):
  406. self.scan_code(c, m)
  407. def load_package(self, fqname, pathname):
  408. self.msgin(2, "load_package", fqname, pathname)
  409. newname = replacePackageMap.get(fqname)
  410. if newname:
  411. fqname = newname
  412. m = self.add_module(fqname)
  413. m.__file__ = pathname
  414. m.__path__ = [pathname]
  415. # As per comment at top of file, simulate runtime __path__ additions.
  416. m.__path__ = m.__path__ + packagePathMap.get(fqname, [])
  417. fp, buf, stuff = self.find_module("__init__", m.__path__)
  418. try:
  419. self.load_module(fqname, fp, buf, stuff)
  420. self.msgout(2, "load_package ->", m)
  421. return m
  422. finally:
  423. if fp:
  424. fp.close()
  425. def add_module(self, fqname):
  426. if fqname in self.modules:
  427. return self.modules[fqname]
  428. self.modules[fqname] = m = Module(fqname)
  429. return m
  430. def find_module(self, name, path, parent=None):
  431. if parent is not None:
  432. # assert path is not None
  433. fullname = parent.__name__+'.'+name
  434. else:
  435. fullname = name
  436. if fullname in self.excludes:
  437. self.msgout(3, "find_module -> Excluded", fullname)
  438. raise ImportError(name)
  439. if path is None:
  440. if name in sys.builtin_module_names:
  441. return (None, None, ("", "", imp.C_BUILTIN))
  442. path = self.path
  443. return imp.find_module(name, path)
  444. def report(self):
  445. """Print a report to stdout, listing the found modules with their
  446. paths, as well as modules that are missing, or seem to be missing.
  447. """
  448. print()
  449. print(" %-25s %s" % ("Name", "File"))
  450. print(" %-25s %s" % ("----", "----"))
  451. # Print modules found
  452. keys = sorted(self.modules.keys())
  453. for key in keys:
  454. m = self.modules[key]
  455. if m.__path__:
  456. print("P", end=' ')
  457. else:
  458. print("m", end=' ')
  459. print("%-25s" % key, m.__file__ or "")
  460. # Print missing modules
  461. missing, maybe = self.any_missing_maybe()
  462. if missing:
  463. print()
  464. print("Missing modules:")
  465. for name in missing:
  466. mods = sorted(self.badmodules[name].keys())
  467. print("?", name, "imported from", ', '.join(mods))
  468. # Print modules that may be missing, but then again, maybe not...
  469. if maybe:
  470. print()
  471. print("Submodules thay appear to be missing, but could also be", end=' ')
  472. print("global names in the parent package:")
  473. for name in maybe:
  474. mods = sorted(self.badmodules[name].keys())
  475. print("?", name, "imported from", ', '.join(mods))
  476. def any_missing(self):
  477. """Return a list of modules that appear to be missing. Use
  478. any_missing_maybe() if you want to know which modules are
  479. certain to be missing, and which *may* be missing.
  480. """
  481. missing, maybe = self.any_missing_maybe()
  482. return missing + maybe
  483. def any_missing_maybe(self):
  484. """Return two lists, one with modules that are certainly missing
  485. and one with modules that *may* be missing. The latter names could
  486. either be submodules *or* just global names in the package.
  487. The reason it can't always be determined is that it's impossible to
  488. tell which names are imported when "from module import *" is done
  489. with an extension module, short of actually importing it.
  490. """
  491. missing = []
  492. maybe = []
  493. for name in self.badmodules:
  494. if name in self.excludes:
  495. continue
  496. i = name.rfind(".")
  497. if i < 0:
  498. missing.append(name)
  499. continue
  500. subname = name[i+1:]
  501. pkgname = name[:i]
  502. pkg = self.modules.get(pkgname)
  503. if pkg is not None:
  504. if pkgname in self.badmodules[name]:
  505. # The package tried to import this module itself and
  506. # failed. It's definitely missing.
  507. missing.append(name)
  508. elif subname in pkg.globalnames:
  509. # It's a global in the package: definitely not missing.
  510. pass
  511. elif pkg.starimports:
  512. # It could be missing, but the package did an "import *"
  513. # from a non-Python module, so we simply can't be sure.
  514. maybe.append(name)
  515. else:
  516. # It's not a global in the package, the package didn't
  517. # do funny star imports, it's very likely to be missing.
  518. # The symbol could be inserted into the package from the
  519. # outside, but since that's not good style we simply list
  520. # it missing.
  521. missing.append(name)
  522. else:
  523. missing.append(name)
  524. missing.sort()
  525. maybe.sort()
  526. return missing, maybe
  527. def replace_paths_in_code(self, co):
  528. new_filename = original_filename = os.path.normpath(co.co_filename)
  529. for f, r in self.replace_paths:
  530. if original_filename.startswith(f):
  531. new_filename = r + original_filename[len(f):]
  532. break
  533. if self.debug and original_filename not in self.processed_paths:
  534. if new_filename != original_filename:
  535. self.msgout(2, "co_filename %r changed to %r" \
  536. % (original_filename,new_filename,))
  537. else:
  538. self.msgout(2, "co_filename %r remains unchanged" \
  539. % (original_filename,))
  540. self.processed_paths.append(original_filename)
  541. consts = list(co.co_consts)
  542. for i in range(len(consts)):
  543. if isinstance(consts[i], type(co)):
  544. consts[i] = self.replace_paths_in_code(consts[i])
  545. return types.CodeType(co.co_argcount, co.co_nlocals, co.co_stacksize,
  546. co.co_flags, co.co_code, tuple(consts), co.co_names,
  547. co.co_varnames, new_filename, co.co_name,
  548. co.co_firstlineno, co.co_lnotab,
  549. co.co_freevars, co.co_cellvars)
  550. def test():
  551. # Parse command line
  552. import getopt
  553. try:
  554. opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:")
  555. except getopt.error as msg:
  556. print(msg)
  557. return
  558. # Process options
  559. debug = 1
  560. domods = 0
  561. addpath = []
  562. exclude = []
  563. for o, a in opts:
  564. if o == '-d':
  565. debug = debug + 1
  566. if o == '-m':
  567. domods = 1
  568. if o == '-p':
  569. addpath = addpath + a.split(os.pathsep)
  570. if o == '-q':
  571. debug = 0
  572. if o == '-x':
  573. exclude.append(a)
  574. # Provide default arguments
  575. if not args:
  576. script = "hello.py"
  577. else:
  578. script = args[0]
  579. # Set the path based on sys.path and the script directory
  580. path = sys.path[:]
  581. path[0] = os.path.dirname(script)
  582. path = addpath + path
  583. if debug > 1:
  584. print("path:")
  585. for item in path:
  586. print(" ", repr(item))
  587. # Create the module finder and turn its crank
  588. mf = ModuleFinder(path, debug, exclude)
  589. for arg in args[1:]:
  590. if arg == '-m':
  591. domods = 1
  592. continue
  593. if domods:
  594. if arg[-2:] == '.*':
  595. mf.import_hook(arg[:-2], None, ["*"])
  596. else:
  597. mf.import_hook(arg)
  598. else:
  599. mf.load_file(arg)
  600. mf.run_script(script)
  601. mf.report()
  602. return mf # for -i debugging
  603. if __name__ == '__main__':
  604. try:
  605. mf = test()
  606. except KeyboardInterrupt:
  607. print("\n[interrupt]")