modulegraph.py 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301
  1. """
  2. Find modules used by a script, using bytecode analysis.
  3. Based on the stdlib modulefinder by Thomas Heller and Just van Rossum,
  4. but uses a graph data structure and 2.3 features
  5. XXX: Verify all calls to _import_hook (and variants) to ensure that
  6. imports are done in the right way.
  7. """
  8. #FIXME: To decrease the likelihood of ModuleGraph exceeding the recursion limit
  9. #and hence unpredictably raising fatal exceptions, increase the recursion
  10. #limit at PyInstaller startup (i.e., in the
  11. #PyInstaller.building.build_main.build() function). For details, see:
  12. # https://github.com/pyinstaller/pyinstaller/issues/1919#issuecomment-216016176
  13. import pkg_resources
  14. import ast
  15. import codecs
  16. import imp
  17. import marshal
  18. import os
  19. import pkgutil
  20. import sys
  21. import re
  22. from collections import deque, namedtuple
  23. import warnings
  24. from altgraph.ObjectGraph import ObjectGraph
  25. from altgraph import GraphError
  26. from . import util
  27. from . import zipio
  28. from ._compat import BytesIO, StringIO, pathname2url, _READ_MODE
  29. BOM = codecs.BOM_UTF8.decode('utf-8')
  30. class BUILTIN_MODULE:
  31. def is_package(fqname):
  32. return False
  33. class NAMESPACE_PACKAGE:
  34. def __init__(self, namespace_dirs):
  35. self.namespace_dirs = namespace_dirs
  36. def is_package(self, fqname):
  37. return True
  38. #FIXME: Leverage this rather than magic numbers below.
  39. ABSOLUTE_OR_RELATIVE_IMPORT_LEVEL = -1
  40. """
  41. Constant instructing the builtin `__import__()` function to attempt both
  42. absolute and relative imports.
  43. """
  44. #FIXME: Leverage this rather than magic numbers below.
  45. ABSOLUTE_IMPORT_LEVEL = 0
  46. """
  47. Constant instructing the builtin `__import__()` function to attempt only
  48. absolute imports.
  49. """
  50. #FIXME: Leverage this rather than magic numbers below.
  51. DEFAULT_IMPORT_LEVEL = (
  52. ABSOLUTE_OR_RELATIVE_IMPORT_LEVEL if sys.version_info[0] == 2 else
  53. ABSOLUTE_IMPORT_LEVEL)
  54. """
  55. Constant instructing the builtin `__import__()` function to attempt the default
  56. import style specific to the active Python interpreter.
  57. Specifically, under:
  58. * Python 2, this defaults to attempting both absolute and relative imports.
  59. * Python 3, this defaults to attempting only absolute imports.
  60. """
  61. # TODO: Refactor all uses of explicit filetypes in this module *AND* of the
  62. # imp.get_suffixes() function to use this dictionary instead. Unfortunately,
  63. # tests for explicit filetypes (e.g., ".py") are non-portable. Under Windows,
  64. # for example, both the ".py" *AND* ".pyw" filetypes signify valid uncompiled
  65. # Python modules.
  66. # TODO: The imp.get_suffixes() function (in fact, the entire "imp" package) has
  67. # been deprecated as of Python 3.3 by the importlib.machinery.all_suffixes()
  68. # function, which largely performs the same role. Unfortunately, the latter
  69. # function was only introduced with Python 3.3. Since PyInstaller requires
  70. # Python >= 3.3 when running under Python 3, refactor this as follows:
  71. #
  72. # * Under Python 2, continue calling imp.get_suffixes().
  73. # * Under Python 3, call importlib.machinery.all_suffixes() instead.
  74. _IMPORTABLE_FILETYPE_TO_METADATA = {
  75. filetype: (filetype, open_mode, imp_type)
  76. for filetype, open_mode, imp_type in imp.get_suffixes()
  77. }
  78. # Reverse sort by length so when comparing filenames the longest match first
  79. _IMPORTABLE_FILETYPE_EXTS = sorted(_IMPORTABLE_FILETYPE_TO_METADATA,
  80. key=lambda p: len(p), reverse=True)
  81. """
  82. Dictionary mapping the filetypes of importable files to the 3-tuple of metadata
  83. describing such files returned by the `imp.get_suffixes()` function whose first
  84. element is that filetype.
  85. This dictionary simplifies platform-portable importation of importable files,
  86. including:
  87. * Uncompiled modules suffixed by `.py` (as well as `.pyw` under Windows).
  88. * Compiled modules suffixed by either `.pyc` or `.pyo`.
  89. * C extensions suffixed by the platform-specific shared library filetype (e.g.,
  90. `.so` under Linux, `.dll` under Windows).
  91. The keys of this dictionary are `.`-prefixed filetypes (e.g., `.py`, `.so`) or
  92. `-`-prefixed filetypes (e.g., `-cpython-37m.dll`[1]);
  93. the values of this dictionary are 3-tuples whose:
  94. 1. First element is the same `.` or `-` prefixed filetype.
  95. 1. Second element is the mode to be passed to the `open()` built-in to open
  96. files of that filetype under the current platform and Python interpreter
  97. (e.g., `rU` for the `.py` filetype under Python 2, `r` for the same
  98. filetype under Python 3).
  99. 1. Third element is a magic number specific to the `imp` module (e.g.,
  100. `imp.C_EXTENSION` for filetypes corresponding to C extensions).
  101. [1] For example of `-cpython-m37.dll` search on
  102. https://packages.msys2.org/package/mingw-w64-x86_64-python3?repo=mingw64
  103. """
  104. # Modulegraph does a good job at simulating Python's, but it can not
  105. # handle packagepath modifications packages make at runtime. Therefore there
  106. # is a mechanism whereby you can register extra paths in this map for a
  107. # package, and it will be honored.
  108. #
  109. # Note this is a mapping is lists of paths.
  110. _packagePathMap = {}
  111. # Prefix used in magic .pth files used by setuptools to create namespace
  112. # packages without an __init__.py file.
  113. #
  114. # The value is a list of such prefixes as the prefix varies with versions of
  115. # setuptools.
  116. _SETUPTOOLS_NAMESPACEPKG_PTHs=(
  117. # setuptools 31.0.0
  118. ("import sys, types, os;has_mfs = sys.version_info > (3, 5);"
  119. "p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('"),
  120. # distribute 0.6.10
  121. ("import sys,types,os; p = os.path.join("
  122. "sys._getframe(1).f_locals['sitedir'], *('"),
  123. # setuptools 0.6c9, distribute 0.6.12
  124. ("import sys,new,os; p = os.path.join(sys._getframe("
  125. "1).f_locals['sitedir'], *('"),
  126. # setuptools 28.1.0
  127. ("import sys, types, os;p = os.path.join("
  128. "sys._getframe(1).f_locals['sitedir'], *('"),
  129. # setuptools 28.7.0
  130. ("import sys, types, os;pep420 = sys.version_info > (3, 3);"
  131. "p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('"),
  132. )
  133. class InvalidRelativeImportError (ImportError):
  134. pass
  135. def _namespace_package_path(fqname, pathnames, path=None):
  136. """
  137. Return the __path__ for the python package in *fqname*.
  138. This function uses setuptools metadata to extract information
  139. about namespace packages from installed eggs.
  140. """
  141. working_set = pkg_resources.WorkingSet(path)
  142. path = list(pathnames)
  143. for dist in working_set:
  144. if dist.has_metadata('namespace_packages.txt'):
  145. namespaces = dist.get_metadata(
  146. 'namespace_packages.txt').splitlines()
  147. if fqname in namespaces:
  148. nspath = os.path.join(dist.location, *fqname.split('.'))
  149. if nspath not in path:
  150. path.append(nspath)
  151. return path
  152. _strs = re.compile(r'''^\s*["']([A-Za-z0-9_]+)["'],?\s*''') # "<- emacs happy
  153. def _eval_str_tuple(value):
  154. """
  155. Input is the repr of a tuple of strings, output
  156. is that tuple.
  157. This only works with a tuple where the members are
  158. python identifiers.
  159. """
  160. if not (value.startswith('(') and value.endswith(')')):
  161. raise ValueError(value)
  162. orig_value = value
  163. value = value[1:-1]
  164. result = []
  165. while value:
  166. m = _strs.match(value)
  167. if m is None:
  168. raise ValueError(orig_value)
  169. result.append(m.group(1))
  170. value = value[len(m.group(0)):]
  171. return tuple(result)
  172. def _path_from_importerror(exc, default):
  173. # This is a hack, but sadly enough the necessary information
  174. # isn't available otherwise.
  175. m = re.match(r'^No module named (\S+)$', str(exc))
  176. if m is not None:
  177. return m.group(1)
  178. return default
  179. def os_listdir(path):
  180. """
  181. Deprecated name
  182. """
  183. warnings.warn(
  184. "Use zipio.listdir instead of os_listdir",
  185. DeprecationWarning)
  186. return zipio.listdir(path)
  187. def _code_to_file(co):
  188. """ Convert code object to a .pyc pseudo-file """
  189. if sys.version_info >= (3, 7):
  190. header = imp.get_magic() + (b'\0' * 12)
  191. elif sys.version_info >= (3, 4):
  192. header = imp.get_magic() + (b'\0' * 8)
  193. else:
  194. header = imp.get_magic() + (b'\0' * 4)
  195. return BytesIO(header + marshal.dumps(co))
  196. def moduleInfoForPath(path):
  197. for (ext, readmode, typ) in imp.get_suffixes():
  198. if path.endswith(ext):
  199. return os.path.basename(path)[:-len(ext)], readmode, typ
  200. return None
  201. def AddPackagePath(packagename, path):
  202. warnings.warn(
  203. "Use addPackagePath instead of AddPackagePath",
  204. DeprecationWarning)
  205. addPackagePath(packagename, path)
  206. def addPackagePath(packagename, path):
  207. paths = _packagePathMap.get(packagename, [])
  208. paths.append(path)
  209. _packagePathMap[packagename] = paths
  210. _replacePackageMap = {}
  211. # This ReplacePackage mechanism allows modulefinder to work around the
  212. # way the _xmlplus package injects itself under the name "xml" into
  213. # sys.modules at runtime by calling ReplacePackage("_xmlplus", "xml")
  214. # before running ModuleGraph.
  215. def ReplacePackage(oldname, newname):
  216. warnings.warn("use replacePackage instead of ReplacePackage",
  217. DeprecationWarning)
  218. replacePackage(oldname, newname)
  219. def replacePackage(oldname, newname):
  220. _replacePackageMap[oldname] = newname
  221. #FIXME: What is this? Do we actually need this? This appears to provide
  222. #significantly more fine-grained metadata than PyInstaller will ever require.
  223. #It consumes a great deal of space (slots or no slots), since we store an
  224. #instance of this class for each edge of the graph.
  225. class DependencyInfo (namedtuple("DependencyInfo",
  226. ["conditional", "function", "tryexcept", "fromlist"])):
  227. __slots__ = ()
  228. def _merged(self, other):
  229. if (not self.conditional and not self.function and not self.tryexcept) \
  230. or (not other.conditional and not other.function and not other.tryexcept):
  231. return DependencyInfo(
  232. conditional=False,
  233. function=False,
  234. tryexcept=False,
  235. fromlist=self.fromlist and other.fromlist)
  236. else:
  237. return DependencyInfo(
  238. conditional=self.conditional or other.conditional,
  239. function=self.function or other.function,
  240. tryexcept=self.tryexcept or other.tryexcept,
  241. fromlist=self.fromlist and other.fromlist)
  242. #FIXME: Shift the following Node class hierarchy into a new
  243. #"PyInstaller.lib.modulegraph.node" module. This module is much too long.
  244. #FIXME: Refactor "_deferred_imports" from a tuple into a proper lightweight
  245. #class leveraging "__slots__". If not for backward compatibility, we'd just
  246. #leverage a named tuple -- but this should do just as well.
  247. #FIXME: Move the "packagepath" attribute into the "Package" class. Only
  248. #packages define the "__path__" special attribute. The codebase currently
  249. #erroneously tests whether "module.packagepath is not None" to determine
  250. #whether a node is a package or not. However, "isinstance(module, Package)" is
  251. #a significantly more reliable test. Refactor the former into the latter.
  252. class Node(object):
  253. """
  254. Abstract base class (ABC) of all objects added to a `ModuleGraph`.
  255. Attributes
  256. ----------
  257. code : codeobject
  258. Code object of the pure-Python module corresponding to this graph node
  259. if any _or_ `None` otherwise.
  260. graphident : str
  261. Synonym of `identifier` required by the `ObjectGraph` superclass of the
  262. `ModuleGraph` class. For readability, the `identifier` attribute should
  263. typically be used instead.
  264. filename : str
  265. Absolute path of this graph node's corresponding module, package, or C
  266. extension if any _or_ `None` otherwise.
  267. identifier : str
  268. Fully-qualified name of this graph node's corresponding module,
  269. package, or C extension.
  270. packagepath : str
  271. List of the absolute paths of all directories comprising this graph
  272. node's corresponding package. If this is a:
  273. * Non-namespace package, this list contains exactly one path.
  274. * Namespace package, this list contains one or more paths.
  275. _deferred_imports : list
  276. List of all target modules imported by the source module corresponding
  277. to this graph node whole importations have been deferred for subsequent
  278. processing in between calls to the `_ModuleGraph._scan_code()` and
  279. `_ModuleGraph._process_imports()` methods for this source module _or_
  280. `None` otherwise. Each element of this list is a 3-tuple
  281. `(have_star, _safe_import_hook_args, _safe_import_hook_kwargs)`
  282. collecting the importation of a target module from this source module
  283. for subsequent processing, where:
  284. * `have_star` is a boolean `True` only if this is a `from`-style star
  285. import (e.g., resembling `from {target_module_name} import *`).
  286. * `_safe_import_hook_args` is a (typically non-empty) sequence of all
  287. positional arguments to be passed to the `_safe_import_hook()` method
  288. to add this importation to the graph.
  289. * `_safe_import_hook_kwargs` is a (typically empty) dictionary of all
  290. keyword arguments to be passed to the `_safe_import_hook()` method
  291. to add this importation to the graph.
  292. Unlike functional languages, Python imposes a maximum depth on the
  293. interpreter stack (and hence recursion). On breaching this depth,
  294. Python raises a fatal `RuntimeError` exception. Since `ModuleGraph`
  295. parses imports recursively rather than iteratively, this depth _was_
  296. commonly breached before the introduction of this list. Python
  297. environments installing a large number of modules (e.g., Anaconda) were
  298. particularly susceptible. Why? Because `ModuleGraph` concurrently
  299. descended through both the abstract syntax trees (ASTs) of all source
  300. modules being parsed _and_ the graph of all target modules imported by
  301. these source modules being built. The stack thus consisted of
  302. alternating layers of AST and graph traversal. To unwind such
  303. alternation and effectively halve the stack depth, `ModuleGraph` now
  304. descends through the abstract syntax tree (AST) of each source module
  305. being parsed and adds all importations originating within this module
  306. to this list _before_ descending into the graph of these importations.
  307. See pyinstaller/pyinstaller/#1289 for further details.
  308. _global_attr_names : set
  309. Set of the unqualified names of all global attributes (e.g., classes,
  310. variables) defined in the pure-Python module corresponding to this
  311. graph node if any _or_ the empty set otherwise. This includes the names
  312. of all attributes imported via `from`-style star imports from other
  313. existing modules (e.g., `from {target_module_name} import *`). This
  314. set is principally used to differentiate the non-ignorable importation
  315. of non-existent submodules in a package from the ignorable importation
  316. of existing global attributes defined in that package's pure-Python
  317. `__init__` submodule in `from`-style imports (e.g., `bar` in
  318. `from foo import bar`, which may be either a submodule or attribute of
  319. `foo`), as such imports ambiguously allow both. This set is _not_ used
  320. to differentiate submodules from attributes in `import`-style imports
  321. (e.g., `bar` in `import foo.bar`, which _must_ be a submodule of
  322. `foo`), as such imports unambiguously allow only submodules.
  323. _starimported_ignored_module_names : set
  324. Set of the fully-qualified names of all existing unparsable modules
  325. that the existing parsable module corresponding to this graph node
  326. attempted to perform one or more "star imports" from. If this module
  327. either does _not_ exist or does but is unparsable, this is the empty
  328. set. Equivalently, this set contains each fully-qualified name
  329. `{trg_module_name}` for which:
  330. * This module contains an import statement of the form
  331. `from {trg_module_name} import *`.
  332. * The module whose name is `{trg_module_name}` exists but is _not_
  333. parsable by `ModuleGraph` (e.g., due to _not_ being pure-Python).
  334. **This set is currently defined but otherwise ignored.**
  335. _submodule_basename_to_node : dict
  336. Dictionary mapping from the unqualified name of each submodule
  337. contained by the parent module corresponding to this graph node to that
  338. submodule's graph node. If this dictionary is non-empty, this parent
  339. module is typically but _not_ always a package (e.g., the non-package
  340. `os` module containing the `os.path` submodule).
  341. """
  342. __slots__ = [
  343. 'code',
  344. 'filename',
  345. 'graphident',
  346. 'identifier',
  347. 'packagepath',
  348. '_deferred_imports',
  349. '_global_attr_names',
  350. '_starimported_ignored_module_names',
  351. '_submodule_basename_to_node',
  352. ]
  353. def __init__(self, identifier):
  354. """
  355. Initialize this graph node.
  356. Parameters
  357. ----------
  358. identifier : str
  359. Fully-qualified name of this graph node's corresponding module,
  360. package, or C extension.
  361. """
  362. self.code = None
  363. self.filename = None
  364. self.graphident = identifier
  365. self.identifier = identifier
  366. self.packagepath = None
  367. self._deferred_imports = None
  368. self._global_attr_names = set()
  369. self._starimported_ignored_module_names = set()
  370. self._submodule_basename_to_node = dict()
  371. def is_global_attr(self, attr_name):
  372. """
  373. `True` only if the pure-Python module corresponding to this graph node
  374. defines a global attribute (e.g., class, variable) with the passed
  375. name.
  376. If this module is actually a package, this method instead returns
  377. `True` only if this package's pure-Python `__init__` submodule defines
  378. such a global attribute. In this case, note that this package may still
  379. contain an importable submodule of the same name. Callers should
  380. attempt to import this attribute as a submodule of this package
  381. _before_ assuming this attribute to be an ignorable global. See
  382. "Examples" below for further details.
  383. Parameters
  384. ----------
  385. attr_name : str
  386. Unqualified name of the attribute to be tested.
  387. Returns
  388. ----------
  389. bool
  390. `True` only if this module defines this global attribute.
  391. Examples
  392. ----------
  393. Consider a hypothetical module `foo` containing submodules `bar` and
  394. `__init__` where the latter assigns `bar` to be a global variable
  395. (possibly star-exported via the special `__all__` global variable):
  396. >>> # In "foo.__init__":
  397. >>> bar = 3.1415
  398. Python 2 and 3 both permissively permit this. This method returns
  399. `True` in this case (i.e., when called on the `foo` package's graph
  400. node, passed the attribute name `bar`) despite the importability of the
  401. `foo.bar` submodule.
  402. """
  403. return attr_name in self._global_attr_names
  404. def is_submodule(self, submodule_basename):
  405. """
  406. `True` only if the parent module corresponding to this graph node
  407. contains the submodule with the passed name.
  408. If `True`, this parent module is typically but _not_ always a package
  409. (e.g., the non-package `os` module containing the `os.path` submodule).
  410. Parameters
  411. ----------
  412. submodule_basename : str
  413. Unqualified name of the submodule to be tested.
  414. Returns
  415. ----------
  416. bool
  417. `True` only if this parent module contains this submodule.
  418. """
  419. return submodule_basename in self._submodule_basename_to_node
  420. def add_global_attr(self, attr_name):
  421. """
  422. Record the global attribute (e.g., class, variable) with the passed
  423. name to be defined by the pure-Python module corresponding to this
  424. graph node.
  425. If this module is actually a package, this method instead records this
  426. attribute to be defined by this package's pure-Python `__init__`
  427. submodule.
  428. Parameters
  429. ----------
  430. attr_name : str
  431. Unqualified name of the attribute to be added.
  432. """
  433. self._global_attr_names.add(attr_name)
  434. def add_global_attrs_from_module(self, target_module):
  435. """
  436. Record all global attributes (e.g., classes, variables) defined by the
  437. target module corresponding to the passed graph node to also be defined
  438. by the source module corresponding to this graph node.
  439. If the source module is actually a package, this method instead records
  440. these attributes to be defined by this package's pure-Python `__init__`
  441. submodule.
  442. Parameters
  443. ----------
  444. target_module : Node
  445. Graph node of the target module to import attributes from.
  446. """
  447. self._global_attr_names.update(target_module._global_attr_names)
  448. def add_submodule(self, submodule_basename, submodule_node):
  449. """
  450. Add the submodule with the passed name and previously imported graph
  451. node to the parent module corresponding to this graph node.
  452. This parent module is typically but _not_ always a package (e.g., the
  453. non-package `os` module containing the `os.path` submodule).
  454. Parameters
  455. ----------
  456. submodule_basename : str
  457. Unqualified name of the submodule to add to this parent module.
  458. submodule_node : Node
  459. Graph node of this submodule.
  460. """
  461. self._submodule_basename_to_node[submodule_basename] = submodule_node
  462. def get_submodule(self, submodule_basename):
  463. """
  464. Graph node of the submodule with the passed name in the parent module
  465. corresponding to this graph node.
  466. If this parent module does _not_ contain this submodule, an exception
  467. is raised. Else, this parent module is typically but _not_ always a
  468. package (e.g., the non-package `os` module containing the `os.path`
  469. submodule).
  470. Parameters
  471. ----------
  472. module_basename : str
  473. Unqualified name of the submodule to retrieve.
  474. Returns
  475. ----------
  476. Node
  477. Graph node of this submodule.
  478. """
  479. return self._submodule_basename_to_node[submodule_basename]
  480. def get_submodule_or_none(self, submodule_basename):
  481. """
  482. Graph node of the submodule with the passed unqualified name in the
  483. parent module corresponding to this graph node if this module contains
  484. this submodule _or_ `None`.
  485. This parent module is typically but _not_ always a package (e.g., the
  486. non-package `os` module containing the `os.path` submodule).
  487. Parameters
  488. ----------
  489. submodule_basename : str
  490. Unqualified name of the submodule to retrieve.
  491. Returns
  492. ----------
  493. Node
  494. Graph node of this submodule if this parent module contains this
  495. submodule _or_ `None`.
  496. """
  497. return self._submodule_basename_to_node.get(submodule_basename)
  498. def remove_global_attr_if_found(self, attr_name):
  499. """
  500. Record the global attribute (e.g., class, variable) with the passed
  501. name if previously recorded as defined by the pure-Python module
  502. corresponding to this graph node to be subsequently undefined by the
  503. same module.
  504. If this module is actually a package, this method instead records this
  505. attribute to be undefined by this package's pure-Python `__init__`
  506. submodule.
  507. This method is intended to be called on globals previously defined by
  508. this module that are subsequently undefined via the `del` built-in by
  509. this module, thus "forgetting" or "undoing" these globals.
  510. For safety, there exists no corresponding `remove_global_attr()`
  511. method. While defining this method is trivial, doing so would invite
  512. `KeyError` exceptions on scanning valid Python that lexically deletes a
  513. global in a scope under this module's top level (e.g., in a function)
  514. _before_ defining this global at this top level. Since `ModuleGraph`
  515. cannot and should not (re)implement a full-blown Python interpreter,
  516. ignoring out-of-order deletions is the only sane policy.
  517. Parameters
  518. ----------
  519. attr_name : str
  520. Unqualified name of the attribute to be removed.
  521. """
  522. if self.is_global_attr(attr_name):
  523. self._global_attr_names.remove(attr_name)
  524. def __cmp__(self, other):
  525. try:
  526. otherIdent = getattr(other, 'graphident')
  527. except AttributeError:
  528. return NotImplemented
  529. return cmp(self.graphident, otherIdent) # noqa: F821
  530. def __eq__(self, other):
  531. try:
  532. otherIdent = getattr(other, 'graphident')
  533. except AttributeError:
  534. return False
  535. return self.graphident == otherIdent
  536. def __ne__(self, other):
  537. try:
  538. otherIdent = getattr(other, 'graphident')
  539. except AttributeError:
  540. return True
  541. return self.graphident != otherIdent
  542. def __lt__(self, other):
  543. try:
  544. otherIdent = getattr(other, 'graphident')
  545. except AttributeError:
  546. return NotImplemented
  547. return self.graphident < otherIdent
  548. def __le__(self, other):
  549. try:
  550. otherIdent = getattr(other, 'graphident')
  551. except AttributeError:
  552. return NotImplemented
  553. return self.graphident <= otherIdent
  554. def __gt__(self, other):
  555. try:
  556. otherIdent = getattr(other, 'graphident')
  557. except AttributeError:
  558. return NotImplemented
  559. return self.graphident > otherIdent
  560. def __ge__(self, other):
  561. try:
  562. otherIdent = getattr(other, 'graphident')
  563. except AttributeError:
  564. return NotImplemented
  565. return self.graphident >= otherIdent
  566. def __hash__(self):
  567. return hash(self.graphident)
  568. def infoTuple(self):
  569. return (self.identifier,)
  570. def __repr__(self):
  571. return '%s%r' % (type(self).__name__, self.infoTuple())
  572. # TODO: This indirection is, frankly, unnecessary. The
  573. # ModuleGraph.alias_module() should directly add the desired AliasNode instance
  574. # to the graph rather than indirectly adding an Alias instance to the
  575. # "lazynodes" dictionary.
  576. class Alias(str):
  577. """
  578. Placeholder aliasing an existing source module to a non-existent target
  579. module (i.e., the desired alias).
  580. For obscure reasons, this class subclasses `str`. Each instance of this
  581. class is the fully-qualified name of the existing source module being
  582. aliased. Unlike the related `AliasNode` class, instances of this class are
  583. _not_ actual nodes and hence _not_ added to the graph; they only facilitate
  584. communication between the `ModuleGraph.alias_module()` and
  585. `ModuleGraph.find_node()` methods.
  586. """
  587. class AliasNode(Node):
  588. """
  589. Graph node representing the aliasing of an existing source module under a
  590. non-existent target module name (i.e., the desired alias).
  591. """
  592. def __init__(self, name, node):
  593. """
  594. Initialize this alias.
  595. Parameters
  596. ----------
  597. name : str
  598. Fully-qualified name of the non-existent target module to be
  599. created (as an alias of the existing source module).
  600. node : Node
  601. Graph node of the existing source module being aliased.
  602. """
  603. super(AliasNode, self).__init__(name)
  604. #FIXME: Why only some? Why not *EVERYTHING* except "graphident", which
  605. #must remain equal to "name" for lookup purposes? This is, after all,
  606. #an alias. The idea is for the two nodes to effectively be the same.
  607. # Copy some attributes from this source module into this target alias.
  608. for attr_name in (
  609. 'identifier', 'packagepath',
  610. '_global_attr_names', '_starimported_ignored_module_names',
  611. '_submodule_basename_to_node'):
  612. if hasattr(node, attr_name):
  613. setattr(self, attr_name, getattr(node, attr_name))
  614. def infoTuple(self):
  615. return (self.graphident, self.identifier)
  616. class BadModule(Node):
  617. pass
  618. class ExcludedModule(BadModule):
  619. pass
  620. class MissingModule(BadModule):
  621. pass
  622. class InvalidRelativeImport (BadModule):
  623. def __init__(self, relative_path, from_name):
  624. identifier = relative_path
  625. if relative_path.endswith('.'):
  626. identifier += from_name
  627. else:
  628. identifier += '.' + from_name
  629. super(InvalidRelativeImport, self).__init__(identifier)
  630. self.relative_path = relative_path
  631. self.from_name = from_name
  632. def infoTuple(self):
  633. return (self.relative_path, self.from_name)
  634. class Script(Node):
  635. def __init__(self, filename):
  636. super(Script, self).__init__(filename)
  637. self.filename = filename
  638. def infoTuple(self):
  639. return (self.filename,)
  640. class BaseModule(Node):
  641. def __init__(self, name, filename=None, path=None):
  642. super(BaseModule, self).__init__(name)
  643. self.filename = filename
  644. self.packagepath = path
  645. def infoTuple(self):
  646. return tuple(filter(None, (self.identifier, self.filename, self.packagepath)))
  647. class BuiltinModule(BaseModule):
  648. pass
  649. class SourceModule(BaseModule):
  650. pass
  651. class InvalidSourceModule(SourceModule):
  652. pass
  653. class CompiledModule(BaseModule):
  654. pass
  655. class InvalidCompiledModule(BaseModule):
  656. pass
  657. class Extension(BaseModule):
  658. pass
  659. class Package(BaseModule):
  660. """
  661. Graph node representing a non-namespace package.
  662. """
  663. pass
  664. class ExtensionPackage(Extension, Package):
  665. """
  666. Graph node representing a package where the __init__ module is an extension
  667. module.
  668. """
  669. pass
  670. class NamespacePackage(Package):
  671. """
  672. Graph node representing a namespace package.
  673. """
  674. pass
  675. class RuntimeModule(BaseModule):
  676. """
  677. Graph node representing a non-package Python module dynamically defined at
  678. runtime.
  679. Most modules are statically defined on-disk as standard Python files.
  680. Some modules, however, are dynamically defined in-memory at runtime
  681. (e.g., `gi.repository.Gst`, dynamically defined by the statically
  682. defined `gi.repository.__init__` module).
  683. This node represents such a runtime module. Since this is _not_ a package,
  684. all attempts to import submodules from this module in `from`-style import
  685. statements (e.g., the `queue` submodule in `from six.moves import queue`)
  686. will be silently ignored.
  687. To ensure that the parent package of this module if any is also imported
  688. and added to the graph, this node is typically added to the graph by
  689. calling the `ModuleGraph.add_module()` method.
  690. """
  691. pass
  692. class RuntimePackage(Package):
  693. """
  694. Graph node representing a non-namespace Python package dynamically defined
  695. at runtime.
  696. Most packages are statically defined on-disk as standard subdirectories
  697. containing `__init__.py` files. Some packages, however, are dynamically
  698. defined in-memory at runtime (e.g., `six.moves`, dynamically defined by
  699. the statically defined `six` module).
  700. This node represents such a runtime package. All attributes imported from
  701. this package in `from`-style import statements that are submodules of this
  702. package (e.g., the `queue` submodule in `from six.moves import queue`) will
  703. be imported rather than ignored.
  704. To ensure that the parent package of this package if any is also imported
  705. and added to the graph, this node is typically added to the graph by
  706. calling the `ModuleGraph.add_module()` method.
  707. """
  708. pass
  709. #FIXME: Safely removable. We don't actually use this anywhere. After removing
  710. #this class, remove the corresponding entry from "compat".
  711. class FlatPackage(BaseModule):
  712. def __init__(self, *args, **kwds):
  713. warnings.warn(
  714. "This class will be removed in a future version of modulegraph",
  715. DeprecationWarning)
  716. super(FlatPackage, *args, **kwds)
  717. #FIXME: Safely removable. We don't actually use this anywhere. After removing
  718. #this class, remove the corresponding entry from "compat".
  719. class ArchiveModule(BaseModule):
  720. def __init__(self, *args, **kwds):
  721. warnings.warn(
  722. "This class will be removed in a future version of modulegraph",
  723. DeprecationWarning)
  724. super(FlatPackage, *args, **kwds)
  725. # HTML templates for ModuleGraph generator
  726. header = """\
  727. <!DOCTYPE html>
  728. <html>
  729. <head>
  730. <meta charset="UTF-8">
  731. <title>%(TITLE)s</title>
  732. <style>
  733. .node { padding: 0.5em 0 0.5em; border-top: thin grey dotted; }
  734. .moduletype { font: smaller italic }
  735. .node a { text-decoration: none; color: #006699; }
  736. .node a:visited { text-decoration: none; color: #2f0099; }
  737. </style>
  738. </head>
  739. <body>
  740. <h1>%(TITLE)s</h1>"""
  741. entry = """
  742. <div class="node">
  743. <a name="%(NAME)s"></a>
  744. %(CONTENT)s
  745. </div>"""
  746. contpl = """<tt>%(NAME)s</tt> <span class="moduletype">%(TYPE)s</span>"""
  747. contpl_linked = """\
  748. <a target="code" href="%(URL)s" type="text/plain"><tt>%(NAME)s</tt></a>
  749. <span class="moduletype">%(TYPE)s</span>"""
  750. imports = """\
  751. <div class="import">
  752. %(HEAD)s:
  753. %(LINKS)s
  754. </div>
  755. """
  756. footer = """
  757. </body>
  758. </html>"""
  759. def _ast_names(names):
  760. result = []
  761. for nm in names:
  762. if isinstance(nm, ast.alias):
  763. result.append(nm.name)
  764. else:
  765. result.append(nm)
  766. result = [r for r in result if r != '__main__']
  767. return result
  768. def uniq(seq):
  769. """Remove duplicates from a list, preserving order"""
  770. # Taken from https://stackoverflow.com/questions/480214
  771. seen = set()
  772. seen_add = seen.add
  773. return [x for x in seq if not (x in seen or seen_add(x))]
  774. if sys.version_info[0] == 2:
  775. DEFAULT_IMPORT_LEVEL = -1
  776. else:
  777. DEFAULT_IMPORT_LEVEL = 0
  778. class _Visitor(ast.NodeVisitor):
  779. def __init__(self, graph, module):
  780. self._graph = graph
  781. self._module = module
  782. self._level = DEFAULT_IMPORT_LEVEL
  783. self._in_if = [False]
  784. self._in_def = [False]
  785. self._in_tryexcept = [False]
  786. @property
  787. def in_if(self):
  788. return self._in_if[-1]
  789. @property
  790. def in_def(self):
  791. return self._in_def[-1]
  792. @property
  793. def in_tryexcept(self):
  794. return self._in_tryexcept[-1]
  795. def _collect_import(self, name, fromlist, level):
  796. if sys.version_info[0] == 2:
  797. if name == '__future__' and 'absolute_import' in (fromlist or ()):
  798. self._level = 0
  799. have_star = False
  800. if fromlist is not None:
  801. fromlist = uniq(fromlist)
  802. if '*' in fromlist:
  803. fromlist.remove('*')
  804. have_star = True
  805. # Record this import as originating from this module for subsequent
  806. # handling by the _process_imports() method.
  807. self._module._deferred_imports.append(
  808. (have_star,
  809. (name, self._module, fromlist, level),
  810. {'edge_attr': DependencyInfo(
  811. conditional=self.in_if,
  812. tryexcept=self.in_tryexcept,
  813. function=self.in_def,
  814. fromlist=False)}))
  815. def visit_Import(self, node):
  816. for nm in _ast_names(node.names):
  817. self._collect_import(nm, None, self._level)
  818. def visit_ImportFrom(self, node):
  819. level = node.level if node.level != 0 else self._level
  820. self._collect_import(node.module or '', _ast_names(node.names), level)
  821. def visit_If(self, node):
  822. self._in_if.append(True)
  823. self.generic_visit(node)
  824. self._in_if.pop()
  825. def visit_FunctionDef(self, node):
  826. self._in_def.append(True)
  827. self.generic_visit(node)
  828. self._in_def.pop()
  829. visit_AsyncFunctionDef = visit_FunctionDef
  830. def visit_Try(self, node):
  831. self._in_tryexcept.append(True)
  832. self.generic_visit(node)
  833. self._in_tryexcept.pop()
  834. def visit_TryExcept(self, node):
  835. self._in_tryexcept.append(True)
  836. self.generic_visit(node)
  837. self._in_tryexcept.pop()
  838. def visit_Expression(self, node):
  839. # Expression node's cannot contain import statements or
  840. # other nodes that are relevant for us.
  841. pass
  842. # Expression isn't actually used as such in AST trees,
  843. # therefore define visitors for all kinds of expression nodes.
  844. visit_BoolOp = visit_Expression
  845. visit_BinOp = visit_Expression
  846. visit_UnaryOp = visit_Expression
  847. visit_Lambda = visit_Expression
  848. visit_IfExp = visit_Expression
  849. visit_Dict = visit_Expression
  850. visit_Set = visit_Expression
  851. visit_ListComp = visit_Expression
  852. visit_SetComp = visit_Expression
  853. visit_ListComp = visit_Expression
  854. visit_GeneratorExp = visit_Expression
  855. visit_Compare = visit_Expression
  856. visit_Yield = visit_Expression
  857. visit_YieldFrom = visit_Expression
  858. visit_Await = visit_Expression
  859. visit_Call = visit_Expression
  860. visit_Await = visit_Expression
  861. class ModuleGraph(ObjectGraph):
  862. """
  863. Directed graph whose nodes represent modules and edges represent
  864. dependencies between these modules.
  865. """
  866. def createNode(self, cls, name, *args, **kw):
  867. m = self.find_node(name)
  868. if m is None:
  869. #assert m is None, m
  870. m = super(ModuleGraph, self).createNode(cls, name, *args, **kw)
  871. return m
  872. def __init__(self, path=None, excludes=(), replace_paths=(), implies=(), graph=None, debug=0):
  873. super(ModuleGraph, self).__init__(graph=graph, debug=debug)
  874. if path is None:
  875. path = sys.path
  876. self.path = path
  877. self.lazynodes = {}
  878. # excludes is stronger than implies
  879. self.lazynodes.update(dict(implies))
  880. for m in excludes:
  881. self.lazynodes[m] = None
  882. self.replace_paths = replace_paths
  883. self.set_setuptools_nspackages()
  884. # Maintain own list of package path mappings in the scope of Modulegraph
  885. # object.
  886. self._package_path_map = _packagePathMap
  887. def set_setuptools_nspackages(self):
  888. # This is used when running in the test-suite
  889. self.nspackages = self._calc_setuptools_nspackages()
  890. def _calc_setuptools_nspackages(self):
  891. # Setuptools has some magic handling for namespace
  892. # packages when using 'install --single-version-externally-managed'
  893. # (used by system packagers and also by pip)
  894. #
  895. # When this option is used namespace packages are writting to
  896. # disk *without* an __init__.py file, which means the regular
  897. # import machinery will not find them.
  898. #
  899. # We therefore explicitly look for the hack used by
  900. # setuptools to get this kind of namespace packages to work.
  901. pkgmap = {}
  902. try:
  903. from pkgutil import ImpImporter
  904. except ImportError:
  905. try:
  906. from _pkgutil import ImpImporter
  907. except ImportError:
  908. ImpImporter = pkg_resources.ImpWrapper
  909. if sys.version_info[:2] >= (3, 3):
  910. import importlib.machinery
  911. ImpImporter = importlib.machinery.FileFinder
  912. for entry in self.path:
  913. importer = pkg_resources.get_importer(entry)
  914. if isinstance(importer, ImpImporter):
  915. try:
  916. ldir = os.listdir(entry)
  917. except os.error:
  918. continue
  919. for fn in ldir:
  920. if fn.endswith('-nspkg.pth'):
  921. with open(os.path.join(entry, fn), _READ_MODE) as fp:
  922. for ln in fp:
  923. for pfx in _SETUPTOOLS_NAMESPACEPKG_PTHs:
  924. if ln.startswith(pfx):
  925. try:
  926. start = len(pfx)-2
  927. stop = ln.index(')', start)+1
  928. except ValueError:
  929. continue
  930. pkg = _eval_str_tuple(ln[start:stop])
  931. identifier = ".".join(pkg)
  932. subdir = os.path.join(entry, *pkg)
  933. if os.path.exists(os.path.join(subdir, '__init__.py')):
  934. # There is a real __init__.py,
  935. # ignore the setuptools hack
  936. continue
  937. if identifier in pkgmap:
  938. pkgmap[identifier].append(subdir)
  939. else:
  940. pkgmap[identifier] = [subdir]
  941. break
  942. return pkgmap
  943. def implyNodeReference(self, node, other, edge_data=None):
  944. """
  945. Create a reference from the passed source node to the passed other node,
  946. implying the former to depend upon the latter.
  947. While the source node _must_ be an existing graph node, the target node
  948. may be either an existing graph node _or_ a fully-qualified module name.
  949. In the latter case, the module with that name and all parent packages of
  950. that module will be imported _without_ raising exceptions and for each
  951. newly imported module or package:
  952. * A new graph node will be created for that module or package.
  953. * A reference from the passed source node to that module or package will
  954. be created.
  955. This method allows dependencies between Python objects _not_ importable
  956. with standard techniques (e.g., module aliases, C extensions).
  957. Parameters
  958. ----------
  959. node : str
  960. Graph node for this reference's source module or package.
  961. other : {Node, str}
  962. Either a graph node _or_ fully-qualified name for this reference's
  963. target module or package.
  964. """
  965. if isinstance(other, Node):
  966. self._updateReference(node, other, edge_data)
  967. else:
  968. if isinstance(other, tuple):
  969. raise ValueError(other)
  970. others = self._safe_import_hook(other, node, None)
  971. for other in others:
  972. self._updateReference(node, other, edge_data)
  973. def outgoing(self, fromnode):
  974. """
  975. Yield all nodes that `fromnode` dependes on (that is,
  976. all modules that `fromnode` imports.
  977. """
  978. node = self.find_node(fromnode)
  979. out_edges, _ = self.get_edges(node)
  980. return out_edges
  981. getReferences = outgoing
  982. def incoming(self, tonode, collapse_missing_modules=True):
  983. node = self.find_node(tonode)
  984. _, in_edges = self.get_edges(node)
  985. if collapse_missing_modules:
  986. for n in in_edges:
  987. if isinstance(n, MissingModule):
  988. for n in self.incoming(n, False):
  989. yield n
  990. else:
  991. yield n
  992. else:
  993. for n in in_edges:
  994. yield n
  995. getReferers = incoming
  996. def hasEdge(self, fromnode, tonode):
  997. """ Return True iff there is an edge from 'fromnode' to 'tonode' """
  998. fromnode = self.find_node(fromnode)
  999. tonode = self.find_node(tonode)
  1000. return self.graph.edge_by_node(fromnode, tonode) is not None
  1001. def foldReferences(self, packagenode):
  1002. """
  1003. Create edges to/from `packagenode` based on the edges to/from all
  1004. submodules of that package _and_ then hide the graph nodes
  1005. corresponding to those submodules.
  1006. """
  1007. pkg = self.find_node(packagenode)
  1008. for n in self.nodes():
  1009. if not n.identifier.startswith(pkg.identifier + '.'):
  1010. continue
  1011. iter_out, iter_inc = self.get_edges(n)
  1012. for other in iter_out:
  1013. if other.identifier.startswith(pkg.identifier + '.'):
  1014. continue
  1015. if not self.hasEdge(pkg, other):
  1016. # Ignore circular dependencies
  1017. self._updateReference(pkg, other, 'pkg-internal-import')
  1018. for other in iter_inc:
  1019. if other.identifier.startswith(pkg.identifier + '.'):
  1020. # Ignore circular dependencies
  1021. continue
  1022. if not self.hasEdge(other, pkg):
  1023. self._updateReference(other, pkg, 'pkg-import')
  1024. self.graph.hide_node(n)
  1025. # TODO: unfoldReferences(pkg) that restore the submodule nodes and
  1026. # removes 'pkg-import' and 'pkg-internal-import' edges. Care should
  1027. # be taken to ensure that references are correct if multiple packages
  1028. # are folded and then one of them in unfolded
  1029. def _updateReference(self, fromnode, tonode, edge_data):
  1030. try:
  1031. ed = self.edgeData(fromnode, tonode)
  1032. except (KeyError, GraphError): # XXX: Why 'GraphError'
  1033. return self.add_edge(fromnode, tonode, edge_data)
  1034. if not (isinstance(ed, DependencyInfo) and isinstance(edge_data, DependencyInfo)):
  1035. self.updateEdgeData(fromnode, tonode, edge_data)
  1036. else:
  1037. self.updateEdgeData(fromnode, tonode, ed._merged(edge_data))
  1038. def add_edge(self, fromnode, tonode, edge_data='direct'):
  1039. """
  1040. Create a reference from fromnode to tonode
  1041. """
  1042. return super(ModuleGraph, self).createReference(fromnode, tonode, edge_data=edge_data)
  1043. createReference = add_edge
  1044. def find_node(self, name, create_nspkg=True):
  1045. """
  1046. Graph node uniquely identified by the passed fully-qualified module
  1047. name if this module has been added to the graph _or_ `None` otherwise.
  1048. If (in order):
  1049. . A namespace package with this identifier exists _and_ the passed
  1050. `create_nspkg` parameter is `True`, this package will be
  1051. instantiated and returned.
  1052. . A lazy node with this identifier and:
  1053. * No dependencies exists, this node will be instantiated and
  1054. returned.
  1055. * Dependencies exists, this node and all transitive dependencies of
  1056. this node be instantiated and this node returned.
  1057. . A non-lazy node with this identifier exists, this node will be
  1058. returned as is.
  1059. Parameters
  1060. ----------
  1061. name : str
  1062. Fully-qualified name of the module whose graph node is to be found.
  1063. create_nspkg : bool
  1064. Whether or not to implicitly instantiate namespace packages. If
  1065. `True` _and_ this name is that of a previously registered namespace
  1066. package (i.e., in `self.nspackages`) not already added to the
  1067. graph, this package will be added to the graph. Defaults to `True`.
  1068. Returns
  1069. ----------
  1070. Node
  1071. Graph node of this module if added to the graph _or_ `None`
  1072. otherwise.
  1073. """
  1074. data = super(ModuleGraph, self).findNode(name)
  1075. if data is not None:
  1076. return data
  1077. if name in self.lazynodes:
  1078. deps = self.lazynodes.pop(name)
  1079. if deps is None:
  1080. # excluded module
  1081. m = self.createNode(ExcludedModule, name)
  1082. elif isinstance(deps, Alias):
  1083. other = self._safe_import_hook(deps, None, None).pop()
  1084. m = self.createNode(AliasNode, name, other)
  1085. self.implyNodeReference(m, other)
  1086. else:
  1087. m = self._safe_import_hook(name, None, None).pop()
  1088. for dep in deps:
  1089. self.implyNodeReference(m, dep)
  1090. return m
  1091. if name in self.nspackages and create_nspkg:
  1092. # name is a --single-version-externally-managed
  1093. # namespace package (setuptools/distribute)
  1094. pathnames = self.nspackages.pop(name)
  1095. m = self.createNode(NamespacePackage, name)
  1096. # FIXME: The filename must be set to a string to ensure that py2app
  1097. # works, it is not clear yet why that is. Setting to None would be
  1098. # cleaner.
  1099. m.filename = '-'
  1100. m.packagepath = _namespace_package_path(name, pathnames, self.path)
  1101. # As per comment at top of file, simulate runtime packagepath additions.
  1102. m.packagepath = m.packagepath + self._package_path_map.get(name, [])
  1103. return m
  1104. return None
  1105. findNode = find_node
  1106. iter_graph = ObjectGraph.flatten
  1107. def add_script(self, pathname, caller=None):
  1108. """
  1109. Create a node by path (not module name). It is expected to be a Python
  1110. source file, and will be scanned for dependencies.
  1111. """
  1112. self.msg(2, "run_script", pathname)
  1113. pathname = os.path.realpath(pathname)
  1114. m = self.find_node(pathname)
  1115. if m is not None:
  1116. return m
  1117. if sys.version_info[0] != 2:
  1118. with open(pathname, 'rb') as fp:
  1119. encoding = util.guess_encoding(fp)
  1120. with open(pathname, _READ_MODE, encoding=encoding) as fp:
  1121. contents = fp.read() + '\n'
  1122. if contents.startswith(BOM):
  1123. # Ignore BOM at start of input
  1124. contents = contents[1:]
  1125. else:
  1126. with open(pathname, _READ_MODE) as fp:
  1127. contents = fp.read() + '\n'
  1128. co_ast = compile(contents, pathname, 'exec', ast.PyCF_ONLY_AST, True)
  1129. co = compile(co_ast, pathname, 'exec', 0, True)
  1130. m = self.createNode(Script, pathname)
  1131. self._updateReference(caller, m, None)
  1132. n = self._scan_code(m, co, co_ast)
  1133. self._process_imports(n)
  1134. m.code = co
  1135. if self.replace_paths:
  1136. m.code = self._replace_paths_in_code(m.code)
  1137. return m
  1138. #FIXME: For safety, the "source_module" parameter should default to the
  1139. #root node of the current graph if unpassed. This parameter currently
  1140. #defaults to None, thus disconnected modules imported in this manner (e.g.,
  1141. #hidden imports imported by depend.analysis.initialize_modgraph()).
  1142. def import_hook(
  1143. self,
  1144. target_module_partname,
  1145. source_module=None,
  1146. target_attr_names=None,
  1147. level=DEFAULT_IMPORT_LEVEL,
  1148. edge_attr=None,
  1149. ):
  1150. """
  1151. Import the module with the passed name, all parent packages of this
  1152. module, _and_ all submodules and attributes in this module with the
  1153. passed names from the previously imported caller module signified by
  1154. the passed graph node.
  1155. Unlike most import methods (e.g., `_safe_import_hook()`), this method
  1156. is designed to be publicly called by both external and internal
  1157. callers and hence is public.
  1158. Parameters
  1159. ----------
  1160. target_module_partname : str
  1161. Partially-qualified name of the target module to be imported. See
  1162. `_safe_import_hook()` for further details.
  1163. source_module : Node
  1164. Graph node for the previously imported **source module** (i.e.,
  1165. module containing the `import` statement triggering the call to
  1166. this method) _or_ `None` if this module is to be imported in a
  1167. "disconnected" manner. **Passing `None` is _not_ recommended.**
  1168. Doing so produces a disconnected graph in which the graph node
  1169. created for the module to be imported will be disconnected and
  1170. hence unreachable from all other nodes -- which frequently causes
  1171. subtle issues in external callers (namely PyInstaller, which
  1172. silently ignores unreachable nodes).
  1173. target_attr_names : list
  1174. List of the unqualified names of all submodules and attributes to
  1175. be imported from the module to be imported if this is a "from"-
  1176. style import (e.g., `[encode_base64, encode_noop]` for the import
  1177. `from email.encoders import encode_base64, encode_noop`) _or_
  1178. `None` otherwise.
  1179. level : int
  1180. Whether to perform an absolute or relative import. See
  1181. `_safe_import_hook()` for further details.
  1182. Returns
  1183. ----------
  1184. list
  1185. List of the graph nodes created for all modules explicitly imported
  1186. by this call, including the passed module and all submodules listed
  1187. in `target_attr_names` _but_ excluding all parent packages
  1188. implicitly imported by this call. If `target_attr_names` is `None`
  1189. or the empty list, this is guaranteed to be a list of one element:
  1190. the graph node created for the passed module.
  1191. Raises
  1192. ----------
  1193. ImportError
  1194. If the target module to be imported is unimportable.
  1195. """
  1196. self.msg(3, "_import_hook", target_module_partname, source_module, source_module, level)
  1197. source_package = self._determine_parent(source_module)
  1198. target_package, target_module_partname = self._find_head_package(
  1199. source_package, target_module_partname, level)
  1200. self.msgin(4, "load_tail", target_package, target_module_partname)
  1201. submodule = target_package
  1202. while target_module_partname:
  1203. i = target_module_partname.find('.')
  1204. if i < 0:
  1205. i = len(target_module_partname)
  1206. head, target_module_partname = target_module_partname[
  1207. :i], target_module_partname[i+1:]
  1208. mname = "%s.%s" % (submodule.identifier, head)
  1209. submodule = self._safe_import_module(head, mname, submodule)
  1210. if submodule is None:
  1211. # FIXME: Why do we no longer return a MissingModule instance?
  1212. # result = self.createNode(MissingModule, mname)
  1213. self.msgout(4, "raise ImportError: No module named", mname)
  1214. raise ImportError("No module named " + repr(mname))
  1215. self.msgout(4, "load_tail ->", submodule)
  1216. target_module = submodule
  1217. target_modules = [target_module]
  1218. # If this is a "from"-style import *AND* this target module is
  1219. # actually a package, import all submodules of this package specified
  1220. # by the "import" half of this import (e.g., the submodules "bar" and
  1221. # "car" of the target package "foo" in "from foo import bar, car").
  1222. #
  1223. # If this target module is a non-package, it could still contain
  1224. # importable submodules (e.g., the non-package `os` module containing
  1225. # the `os.path` submodule). In this case, these submodules are already
  1226. # imported by this target module's pure-Python code. Since our import
  1227. # scanner already detects such imports, these submodules need *NOT* be
  1228. # reimported here.
  1229. if target_attr_names and isinstance(target_module,
  1230. (Package, AliasNode)):
  1231. for target_submodule in self._import_importable_package_submodules(
  1232. target_module, target_attr_names):
  1233. if target_submodule not in target_modules:
  1234. target_modules.append(target_submodule)
  1235. # Add an edge from this source module to each target module.
  1236. for target_module in target_modules:
  1237. self._updateReference(
  1238. source_module, target_module, edge_data=edge_attr)
  1239. return target_modules
  1240. def _determine_parent(self, caller):
  1241. """
  1242. Determine the package containing a node.
  1243. """
  1244. self.msgin(4, "determine_parent", caller)
  1245. parent = None
  1246. if caller:
  1247. pname = caller.identifier
  1248. if isinstance(caller, Package):
  1249. parent = caller
  1250. elif '.' in pname:
  1251. pname = pname[:pname.rfind('.')]
  1252. parent = self.find_node(pname)
  1253. elif caller.packagepath:
  1254. # XXX: I have no idea why this line
  1255. # is necessary.
  1256. parent = self.find_node(pname)
  1257. self.msgout(4, "determine_parent ->", parent)
  1258. return parent
  1259. def _find_head_package(
  1260. self,
  1261. source_package,
  1262. target_module_partname,
  1263. level=DEFAULT_IMPORT_LEVEL):
  1264. """
  1265. Import the target package providing the target module with the passed
  1266. name to be subsequently imported from the previously imported source
  1267. package corresponding to the passed graph node.
  1268. Parameters
  1269. ----------
  1270. source_package : Package
  1271. Graph node for the previously imported **source package** (i.e.,
  1272. package containing the module containing the `import` statement
  1273. triggering the call to this method) _or_ `None` if this module is
  1274. to be imported in a "disconnected" manner. **Passing `None` is
  1275. _not_ recommended.** See the `_import_hook()` method for further
  1276. details.
  1277. target_module_partname : str
  1278. Partially-qualified name of the target module to be imported. See
  1279. `_safe_import_hook()` for further details.
  1280. level : int
  1281. Whether to perform absolute or relative imports. See the
  1282. `_safe_import_hook()` method for further details.
  1283. Returns
  1284. ----------
  1285. (target_package, target_module_tailname)
  1286. 2-tuple describing the imported target package, where:
  1287. * `target_package` is the graph node created for this package.
  1288. * `target_module_tailname` is the unqualified name of the target
  1289. module to be subsequently imported (e.g., `text` when passed a
  1290. `target_module_partname` of `email.mime.text`).
  1291. Raises
  1292. ----------
  1293. ImportError
  1294. If the package to be imported is unimportable.
  1295. """
  1296. self.msgin(4, "find_head_package", source_package, target_module_partname, level)
  1297. #FIXME: Rename all local variable names to something sensible. No,
  1298. #"p_fqdn" is not a sensible name.
  1299. # If this target module is a submodule...
  1300. if '.' in target_module_partname:
  1301. target_module_headname, target_module_tailname = (
  1302. target_module_partname.split('.', 1))
  1303. # Else, this target module is a top-level module.
  1304. else:
  1305. target_module_headname = target_module_partname
  1306. target_module_tailname = ''
  1307. # If attempting both absolute and relative imports...
  1308. if level == ABSOLUTE_OR_RELATIVE_IMPORT_LEVEL:
  1309. if source_package:
  1310. target_package_name = source_package.identifier + '.' + target_module_headname
  1311. else:
  1312. target_package_name = target_module_headname
  1313. # Else if attempting only absolute imports...
  1314. elif level == ABSOLUTE_IMPORT_LEVEL:
  1315. target_package_name = target_module_headname
  1316. # Absolute import, ignore the parent
  1317. source_package = None
  1318. # Else if attempting only relative imports...
  1319. else:
  1320. if source_package is None:
  1321. self.msg(2, "Relative import outside of package")
  1322. raise InvalidRelativeImportError(
  1323. "Relative import outside of package (name=%r, parent=%r, level=%r)" % (
  1324. target_module_partname, source_package, level))
  1325. for i in range(level - 1):
  1326. if '.' not in source_package.identifier:
  1327. self.msg(2, "Relative import outside of package")
  1328. raise InvalidRelativeImportError(
  1329. "Relative import outside of package (name=%r, parent=%r, level=%r)" % (
  1330. target_module_partname, source_package, level))
  1331. p_fqdn = source_package.identifier.rsplit('.', 1)[0]
  1332. new_parent = self.find_node(p_fqdn)
  1333. if new_parent is None:
  1334. #FIXME: Repetition detected. Exterminate. Exterminate.
  1335. self.msg(2, "Relative import outside of package")
  1336. raise InvalidRelativeImportError(
  1337. "Relative import outside of package (name=%r, parent=%r, level=%r)" % (
  1338. target_module_partname, source_package, level))
  1339. assert new_parent is not source_package, (
  1340. new_parent, source_package)
  1341. source_package = new_parent
  1342. if target_module_headname:
  1343. target_package_name = (
  1344. source_package.identifier + '.' + target_module_headname)
  1345. else:
  1346. target_package_name = source_package.identifier
  1347. # Graph node of this target package.
  1348. target_package = self._safe_import_module(
  1349. target_module_headname, target_package_name, source_package)
  1350. #FIXME: Why exactly is this necessary again? This doesn't quite seem
  1351. #right but maybe it is. Shouldn't absolute imports only be performed if
  1352. #the passed "level" is either "ABSOLUTE_IMPORT_LEVEL" or
  1353. #"ABSOLUTE_OR_RELATIVE_IMPORT_LEVEL" -- or, more succinctly:
  1354. #
  1355. # if level < 1:
  1356. # If this target package is *NOT* importable and a source package was
  1357. # passed, attempt to import this target package as an absolute import.
  1358. if target_package is None and source_package is not None:
  1359. target_package_name = target_module_headname
  1360. source_package = None
  1361. # Graph node for the target package, again.
  1362. target_package = self._safe_import_module(
  1363. target_module_headname, target_package_name, source_package)
  1364. # If this target package is importable, return this package.
  1365. if target_package is not None:
  1366. self.msgout(4, "find_head_package ->", (target_package, target_module_tailname))
  1367. return target_package, target_module_tailname
  1368. # Else, raise an exception.
  1369. self.msgout(4, "raise ImportError: No module named", target_package_name)
  1370. raise ImportError("No module named " + target_package_name)
  1371. #FIXME: Refactor from a generator yielding graph nodes into a non-generator
  1372. #returning a list or tuple of all yielded graph nodes. This method is only
  1373. #called once above and the return value of that call is only iterated over
  1374. #as a list or tuple. There's no demonstrable reason for this to be a
  1375. #generator. Generators are great for their intended purposes (e.g., as
  1376. #continuations). This isn't one of those purposes.
  1377. def _import_importable_package_submodules(self, package, attr_names):
  1378. """
  1379. Generator importing and yielding each importable submodule (of the
  1380. previously imported package corresponding to the passed graph node)
  1381. whose unqualified name is in the passed list.
  1382. Elements of this list that are _not_ importable submodules of this
  1383. package are either:
  1384. * Ignorable attributes (e.g., classes, globals) defined at the top
  1385. level of this package's `__init__` submodule, which will be ignored.
  1386. * Else, unignorable unimportable submodules, in which case an
  1387. exception is raised.
  1388. Parameters
  1389. ----------
  1390. package : Package
  1391. Graph node of the previously imported package containing the
  1392. modules to be imported and yielded.
  1393. attr_names : list
  1394. List of the unqualified names of all attributes of this package to
  1395. attempt to import as submodules. This list will be internally
  1396. converted into a set, safely ignoring any duplicates in this list
  1397. (e.g., reducing the "from"-style import
  1398. `from foo import bar, car, far, bar, car, far` to merely
  1399. `from foo import bar, car, far`).
  1400. Yields
  1401. ----------
  1402. Node
  1403. Graph node created for the currently imported submodule.
  1404. Raises
  1405. ----------
  1406. ImportError
  1407. If any attribute whose name is in `attr_names` is neither:
  1408. * An importable submodule of this package.
  1409. * An ignorable global attribute (e.g., class, variable) defined at
  1410. the top level of this package's `__init__` submodule.
  1411. In this case, this attribute _must_ be an unimportable submodule of
  1412. this package.
  1413. """
  1414. # Ignore duplicate submodule names in the passed list.
  1415. attr_names = set(attr_names)
  1416. self.msgin(4, "_import_importable_package_submodules", package, attr_names)
  1417. #FIXME: This test *SHOULD* be superfluous and hence safely removable.
  1418. #The higher-level _scan_bytecode() and _collect_import() methods
  1419. #already guarantee "*" characters to be removed from fromlists.
  1420. if '*' in attr_names:
  1421. attr_names.update(self._find_all_submodules(package))
  1422. attr_names.remove('*')
  1423. # self.msg(4, '_import_importable_package_submodules (global attrs)', package.identifier, package._global_attr_names)
  1424. # For the name of each attribute to be imported from this package...
  1425. for attr_name in attr_names:
  1426. # self.msg(4, '_import_importable_package_submodules (fromlist attr)', package.identifier, attr_name)
  1427. # Graph node of this attribute if this attribute is a previously
  1428. # imported module or None otherwise.
  1429. submodule = package.get_submodule_or_none(attr_name)
  1430. # If this attribute is *NOT* a previously imported module, attempt
  1431. # to import this attribute as a submodule of this package.
  1432. if submodule is None:
  1433. # Fully-qualified name of this submodule.
  1434. submodule_name = package.identifier + '.' + attr_name
  1435. # Graph node of this submodule if importable or None otherwise.
  1436. submodule = self._safe_import_module(
  1437. attr_name, submodule_name, package)
  1438. # If this submodule is unimportable...
  1439. if submodule is None:
  1440. # If this attribute is a global (e.g., class, variable)
  1441. # defined at the top level of this package's "__init__"
  1442. # submodule, this importation is safely ignorable. Do so
  1443. # and skip to the next attribute.
  1444. #
  1445. # This behaviour is non-conformant with Python behaviour,
  1446. # which is bad, but is required to sanely handle all
  1447. # possible edge cases, which is good. In Python, a global
  1448. # attribute defined at the top level of a package's
  1449. # "__init__" submodule shadows a submodule of the same name
  1450. # in that package. Attempting to import that submodule
  1451. # instead imports that attribute; thus, that submodule is
  1452. # effectively unimportable. In this method and elsewhere,
  1453. # that submodule is tested for first and hence shadows that
  1454. # attribute -- the opposite logic. Attempts to import that
  1455. # attribute are mistakenly seen as attempts to import that
  1456. # submodule! Why?
  1457. #
  1458. # Edge cases. PyInstaller (and by extension ModuleGraph)
  1459. # only cares about module imports. Global attribute imports
  1460. # are parsed only as the means to this ends and are
  1461. # otherwise ignorable. The cost of erroneously shadowing:
  1462. #
  1463. # * Submodules by attributes is significant. Doing so
  1464. # prevents such submodules from being frozen and hence
  1465. # imported at application runtime.
  1466. # * Attributes by submodules is insignificant. Doing so
  1467. # could erroneously freeze such submodules despite their
  1468. # never being imported at application runtime. However,
  1469. # ModuleGraph is incapable of determining with certainty
  1470. # that Python logic in another module other than the
  1471. # "__init__" submodule containing these attributes does
  1472. # *NOT* delete these attributes and hence unshadow these
  1473. # submodules, which would then become importable at
  1474. # runtime and require freezing. Hence, ModuleGraph *MUST*
  1475. # permissively assume submodules of the same name as
  1476. # attributes to be unshadowed elsewhere and require
  1477. # freezing -- even if they do not.
  1478. #
  1479. # It is practically difficult (albeit technically feasible)
  1480. # for ModuleGraph to determine whether or not the target
  1481. # attribute names of "from"-style import statements (e.g.,
  1482. # "bar" and "car" in "from foo import bar, car") refer to
  1483. # non-ignorable submodules or ignorable non-module globals
  1484. # during opcode scanning. Distinguishing these two cases
  1485. # during opcode scanning would require a costly call to the
  1486. # _find_module() method, which would subsequently be
  1487. # repeated during import-graph construction. This could be
  1488. # ameliorated with caching, which itself would require
  1489. # costly space consumption and developer time.
  1490. #
  1491. # Since opcode scanning fails to distinguish these two
  1492. # cases, this and other methods subsequently called at
  1493. # import-graph construction time (e.g.,
  1494. # _safe_import_hook()) must do so. Since submodules of the
  1495. # same name as attributes must assume to be unshadowed
  1496. # elsewhere and require freezing, the only solution is to
  1497. # attempt to import an attribute as a non-ignorable module
  1498. # *BEFORE* assuming an attribute to be an ignorable
  1499. # non-module. Which is what this and other methods do.
  1500. #
  1501. # See Package.is_global_attr() for similar discussion.
  1502. if package.is_global_attr(attr_name):
  1503. self.msg(4, '_import_importable_package_submodules: ignoring from-imported global', package.identifier, attr_name)
  1504. continue
  1505. # Else, this attribute is an unimportable submodule. Since
  1506. # this is *NOT* safely ignorable, raise an exception.
  1507. else:
  1508. raise ImportError("No module named " + submodule_name)
  1509. # Yield this submodule's graph node to the caller.
  1510. yield submodule
  1511. self.msgin(4, "_import_importable_package_submodules ->")
  1512. def _find_all_submodules(self, m):
  1513. if not m.packagepath:
  1514. return
  1515. # 'suffixes' used to be a list hardcoded to [".py", ".pyc", ".pyo"].
  1516. # But we must also collect Python extension modules - although
  1517. # we cannot separate normal dlls from Python extensions.
  1518. for path in m.packagepath:
  1519. try:
  1520. names = zipio.listdir(path)
  1521. except (os.error, IOError):
  1522. self.msg(2, "can't list directory", path)
  1523. continue
  1524. for info in (moduleInfoForPath(p) for p in names):
  1525. if info is None:
  1526. continue
  1527. if info[0] != '__init__':
  1528. yield info[0]
  1529. def alias_module(self, src_module_name, trg_module_name):
  1530. """
  1531. Alias the source module to the target module with the passed names.
  1532. This method ensures that the next call to findNode() given the target
  1533. module name will resolve this alias. This includes importing and adding
  1534. a graph node for the source module if needed as well as adding a
  1535. reference from the target to source module.
  1536. Parameters
  1537. ----------
  1538. src_module_name : str
  1539. Fully-qualified name of the existing **source module** (i.e., the
  1540. module being aliased).
  1541. trg_module_name : str
  1542. Fully-qualified name of the non-existent **target module** (i.e.,
  1543. the alias to be created).
  1544. """
  1545. self.msg(3, 'alias_module "%s" -> "%s"' % (src_module_name, trg_module_name))
  1546. # print('alias_module "%s" -> "%s"' % (src_module_name, trg_module_name))
  1547. assert isinstance(src_module_name, str), '"%s" not a module name.' % str(src_module_name)
  1548. assert isinstance(trg_module_name, str), '"%s" not a module name.' % str(trg_module_name)
  1549. # If the target module has already been added to the graph as either a
  1550. # non-alias or as a different alias, raise an exception.
  1551. trg_module = self.find_node(trg_module_name)
  1552. if trg_module is not None and not (
  1553. isinstance(trg_module, AliasNode) and
  1554. trg_module.identifier == src_module_name):
  1555. raise ValueError(
  1556. 'Target module "%s" already imported as "%s".' % (
  1557. trg_module_name, trg_module))
  1558. # See findNode() for details.
  1559. self.lazynodes[trg_module_name] = Alias(src_module_name)
  1560. def add_module(self, module):
  1561. """
  1562. Add the passed module node to the graph if not already added.
  1563. If that module has a parent module or package with a previously added
  1564. node, this method also adds a reference from this module node to its
  1565. parent node and adds this module node to its parent node's namespace.
  1566. This high-level method wraps the low-level `addNode()` method, but is
  1567. typically _only_ called by graph hooks adding runtime module nodes. For
  1568. all other node types, the `import_module()` method should be called.
  1569. Parameters
  1570. ----------
  1571. module : BaseModule
  1572. Graph node of the module to be added.
  1573. """
  1574. self.msg(3, 'add_module', module)
  1575. # If no node exists for this module, add such a node.
  1576. module_added = self.find_node(module.identifier)
  1577. if module_added is None:
  1578. self.addNode(module)
  1579. else:
  1580. assert module == module_added, 'New module %r != previous %r.' % (module, module_added)
  1581. # If this module has a previously added parent, reference this module to
  1582. # its parent and add this module to its parent's namespace.
  1583. parent_name, _, module_basename = module.identifier.rpartition('.')
  1584. if parent_name:
  1585. parent = self.find_node(parent_name)
  1586. if parent is None:
  1587. self.msg(4, 'add_module parent not found:', parent_name)
  1588. else:
  1589. self.add_edge(module, parent)
  1590. parent.add_submodule(module_basename, module)
  1591. def append_package_path(self, package_name, directory):
  1592. """
  1593. Modulegraph does a good job at simulating Python's, but it can not
  1594. handle packagepath '__path__' modifications packages make at runtime.
  1595. Therefore there is a mechanism whereby you can register extra paths
  1596. in this map for a package, and it will be honored.
  1597. NOTE: This method has to be called before a package is resolved by
  1598. modulegraph.
  1599. Parameters
  1600. ----------
  1601. module : str
  1602. Fully-qualified module name.
  1603. directory : str
  1604. Absolute or relative path of the directory to append to the
  1605. '__path__' attribute.
  1606. """
  1607. paths = self._package_path_map.setdefault(package_name, [])
  1608. paths.append(directory)
  1609. def _safe_import_module(
  1610. self, module_partname, module_name, parent_module):
  1611. """
  1612. Create a new graph node for the module with the passed name under the
  1613. parent package signified by the passed graph node _without_ raising
  1614. `ImportError` exceptions.
  1615. If this module has already been imported, this module's existing graph
  1616. node will be returned; else if this module is importable, a new graph
  1617. node will be added for this module and returned; else this module is
  1618. unimportable, in which case `None` will be returned. Like the
  1619. `_safe_import_hook()` method, this method does _not_ raise
  1620. `ImportError` exceptions when this module is unimportable.
  1621. Parameters
  1622. ----------
  1623. module_partname : str
  1624. Unqualified name of the module to be imported (e.g., `text`).
  1625. module_name : str
  1626. Fully-qualified name of this module (e.g., `email.mime.text`).
  1627. parent_module : Package
  1628. Graph node of the previously imported parent module containing this
  1629. submodule _or_ `None` if this is a top-level module (i.e.,
  1630. `module_name` contains no `.` delimiters). This parent module is
  1631. typically but _not_ always a package (e.g., the `os.path` submodule
  1632. contained by the `os` module).
  1633. Returns
  1634. ----------
  1635. Node
  1636. Graph node created for this module _or_ `None` if this module is
  1637. unimportable.
  1638. """
  1639. self.msgin(3, "safe_import_module", module_partname, module_name, parent_module)
  1640. # If this module has *NOT* already been imported, do so.
  1641. module = self.find_node(module_name)
  1642. if module is None:
  1643. # List of the absolute paths of all directories to be searched for
  1644. # this module. This effectively defaults to "sys.path".
  1645. search_dirs = None
  1646. # If this module has a parent package...
  1647. if parent_module is not None:
  1648. # ...with a list of the absolute paths of all directories
  1649. # comprising this package, prefer that to "sys.path".
  1650. if parent_module.packagepath is not None:
  1651. search_dirs = parent_module.packagepath
  1652. # Else, something is horribly wrong. Return emptiness.
  1653. else:
  1654. self.msgout(3, "safe_import_module -> None (parent_parent.packagepath is None)")
  1655. return None
  1656. try:
  1657. pathname, loader = self._find_module(
  1658. module_partname, search_dirs, parent_module)
  1659. except ImportError as exc:
  1660. self.msgout(3, "safe_import_module -> None (%r)" % exc)
  1661. return None
  1662. (module, co) = self._load_module(module_name, pathname, loader)
  1663. if co is not None:
  1664. try:
  1665. if isinstance(co, ast.AST):
  1666. co_ast = co
  1667. co = compile(co_ast, pathname, 'exec', 0, True)
  1668. else:
  1669. co_ast = None
  1670. n = self._scan_code(module, co, co_ast)
  1671. self._process_imports(n)
  1672. if self.replace_paths:
  1673. co = self._replace_paths_in_code(co)
  1674. module.code = co
  1675. except SyntaxError:
  1676. self.msg(
  1677. 1, "safe_import_module: SyntaxError in ", pathname,
  1678. )
  1679. cls = InvalidSourceModule
  1680. module = self.createNode(cls, module_name)
  1681. # If this is a submodule rather than top-level module...
  1682. if parent_module is not None:
  1683. self.msg(4, "safe_import_module create reference", module, "->", parent_module)
  1684. # Add an edge from this submodule to its parent module.
  1685. self._updateReference(
  1686. module, parent_module, edge_data=DependencyInfo(
  1687. conditional=False,
  1688. fromlist=False,
  1689. function=False,
  1690. tryexcept=False,
  1691. ))
  1692. # Add this submodule to its parent module.
  1693. parent_module.add_submodule(module_partname, module)
  1694. # Return this module.
  1695. self.msgout(3, "safe_import_module ->", module)
  1696. return module
  1697. def _load_module(self, fqname, pathname, loader):
  1698. from importlib._bootstrap_external import ExtensionFileLoader
  1699. self.msgin(2, "load_module", fqname, pathname,
  1700. loader.__class__.__name__)
  1701. partname = fqname.rpartition(".")[-1]
  1702. if loader.is_package(partname):
  1703. is_nspkg = isinstance(loader, NAMESPACE_PACKAGE)
  1704. if is_nspkg:
  1705. pkgpath = loader.namespace_dirs[:] # copy for safety
  1706. else:
  1707. pkgpath = []
  1708. newname = _replacePackageMap.get(fqname)
  1709. if newname:
  1710. fqname = newname
  1711. ns_pkgpath = _namespace_package_path(
  1712. fqname, pkgpath or [], self.path)
  1713. if (ns_pkgpath or pkgpath) and is_nspkg:
  1714. # this is a PEP-420 namespace package
  1715. m = self.createNode(NamespacePackage, fqname)
  1716. m.filename = '-'
  1717. m.packagepath = ns_pkgpath
  1718. else:
  1719. if isinstance(loader, ExtensionFileLoader):
  1720. m = self.createNode(ExtensionPackage, fqname)
  1721. else:
  1722. m = self.createNode(Package, fqname)
  1723. m.filename = pathname
  1724. # PEP-302-compliant loaders return the pathname of the
  1725. # `__init__`-file, not the packge directory.
  1726. assert os.path.basename(pathname).startswith('__init__.')
  1727. m.packagepath = [os.path.dirname(pathname)] + ns_pkgpath
  1728. # As per comment at top of file, simulate runtime packagepath
  1729. # additions
  1730. m.packagepath = m.packagepath + self._package_path_map.get(
  1731. fqname, [])
  1732. if isinstance(m, NamespacePackage):
  1733. return (m, None)
  1734. co = None
  1735. if loader is BUILTIN_MODULE:
  1736. cls = BuiltinModule
  1737. elif isinstance(loader, ExtensionFileLoader):
  1738. cls = Extension
  1739. else:
  1740. src = loader.get_source(partname)
  1741. if src is not None:
  1742. try:
  1743. co = compile(src, pathname, 'exec', ast.PyCF_ONLY_AST,
  1744. True)
  1745. cls = SourceModule
  1746. if sys.version_info[:2] == (3, 5):
  1747. # In Python 3.5 some syntax problems with async
  1748. # functions are only reported when compiling to
  1749. # bytecode
  1750. compile(co, '-', 'exec', 0, True)
  1751. except SyntaxError:
  1752. co = None
  1753. cls = InvalidSourceModule
  1754. except Exception as exc: # FIXME: more specific?
  1755. cls = InvalidSourceModule
  1756. self.msg(2, "load_module: InvalidSourceModule", pathname,
  1757. exc)
  1758. else:
  1759. # no src available
  1760. try:
  1761. co = loader.get_code(partname)
  1762. cls = (CompiledModule if co is not None
  1763. else InvalidCompiledModule)
  1764. except Exception as exc: # FIXME: more specific?
  1765. self.msg(2, "load_module: InvalidCompiledModule, "
  1766. "Cannot load code", pathname, exc)
  1767. cls = InvalidCompiledModule
  1768. m = self.createNode(cls, fqname)
  1769. m.filename = pathname
  1770. self.msgout(2, "load_module ->", m)
  1771. return (m, co)
  1772. def _safe_import_hook(
  1773. self, target_module_partname, source_module, target_attr_names,
  1774. level=DEFAULT_IMPORT_LEVEL, edge_attr=None):
  1775. """
  1776. Import the module with the passed name and all parent packages of this
  1777. module from the previously imported caller module signified by the
  1778. passed graph node _without_ raising `ImportError` exceptions.
  1779. This method wraps the lowel-level `_import_hook()` method. On catching
  1780. an `ImportError` exception raised by that method, this method creates
  1781. and adds a `MissingNode` instance describing the unimportable module to
  1782. the graph instead.
  1783. Parameters
  1784. ----------
  1785. target_module_partname : str
  1786. Partially-qualified name of the module to be imported. If `level`
  1787. is:
  1788. * `ABSOLUTE_OR_RELATIVE_IMPORT_LEVEL` (e.g., the Python 2 default)
  1789. or a positive integer (e.g., an explicit relative import), the
  1790. fully-qualified name of this module is the concatenation of the
  1791. fully-qualified name of the caller module's package and this
  1792. parameter.
  1793. * `ABSOLUTE_IMPORT_LEVEL` (e.g., the Python 3 default), this name
  1794. is already fully-qualified.
  1795. * A non-negative integer (e.g., `1`), this name is typically the
  1796. empty string. In this case, this is a "from"-style relative
  1797. import (e.g., "from . import bar") and the fully-qualified name
  1798. of this module is dynamically resolved by import machinery.
  1799. source_module : Node
  1800. Graph node for the previously imported **caller module** (i.e.,
  1801. module containing the `import` statement triggering the call to
  1802. this method) _or_ `None` if this module is to be imported in a
  1803. "disconnected" manner. **Passing `None` is _not_ recommended.**
  1804. Doing so produces a disconnected graph in which the graph node
  1805. created for the module to be imported will be disconnected and
  1806. hence unreachable from all other nodes -- which frequently causes
  1807. subtle issues in external callers (e.g., PyInstaller, which
  1808. silently ignores unreachable nodes).
  1809. target_attr_names : list
  1810. List of the unqualified names of all submodules and attributes to
  1811. be imported via a `from`-style import statement from this target
  1812. module if any (e.g., the list `[encode_base64, encode_noop]` for
  1813. the import `from email.encoders import encode_base64, encode_noop`)
  1814. _or_ `None` otherwise. Ignored unless `source_module` is the graph
  1815. node of a package (i.e., is an instance of the `Package` class).
  1816. Why? Because:
  1817. * Consistency. The `_import_importable_package_submodules()`
  1818. method accepts a similar list applicable only to packages.
  1819. * Efficiency. Unlike packages, modules cannot physically contain
  1820. submodules. Hence, any target module imported via a `from`-style
  1821. import statement as an attribute from another target parent
  1822. module must itself have been imported in that target parent
  1823. module. The import statement responsible for that import must
  1824. already have been previously parsed by `ModuleGraph`, in which
  1825. case that target module will already be frozen by PyInstaller.
  1826. These imports are safely ignorable here.
  1827. level : int
  1828. Whether to perform an absolute or relative import. This parameter
  1829. corresponds exactly to the parameter of the same name accepted by
  1830. the `__import__()` built-in: "The default is -1 which indicates
  1831. both absolute and relative imports will be attempted. 0 means only
  1832. perform absolute imports. Positive values for level indicate the
  1833. number of parent directories to search relative to the directory of
  1834. the module calling `__import__()`." Defaults to -1 under Python 2
  1835. and 0 under Python 3. Since this default depends on the major
  1836. version of the current Python interpreter, depending on this
  1837. default can result in unpredictable and non-portable behaviour.
  1838. Callers are strongly recommended to explicitly pass this parameter
  1839. rather than implicitly accept this default.
  1840. Returns
  1841. ----------
  1842. list
  1843. List of the graph nodes created for all modules explicitly imported
  1844. by this call, including the passed module and all submodules listed
  1845. in `target_attr_names` _but_ excluding all parent packages
  1846. implicitly imported by this call. If `target_attr_names` is either
  1847. `None` or the empty list, this is guaranteed to be a list of one
  1848. element: the graph node created for the passed module. As above,
  1849. `MissingNode` instances are created for all unimportable modules.
  1850. """
  1851. self.msg(3, "_safe_import_hook", target_module_partname, source_module, target_attr_names, level)
  1852. def is_swig_candidate():
  1853. return (source_module is not None and
  1854. target_attr_names is None and
  1855. level == ABSOLUTE_IMPORT_LEVEL and
  1856. type(source_module) is SourceModule and
  1857. target_module_partname ==
  1858. '_' + source_module.identifier.rpartition('.')[2] and
  1859. sys.version_info[0] == 3)
  1860. def is_swig_wrapper(source_module):
  1861. # TODO Define a new function util.open_text_file() performing
  1862. # this logic, which is repeated numerous times in this module.
  1863. # FIXME: Actually, can't we just use the new compat.open()
  1864. # function to reliably open text files in a portable manner?
  1865. with open(source_module.filename, 'rb') as source_module_file:
  1866. encoding = util.guess_encoding(source_module_file)
  1867. with open(source_module.filename, _READ_MODE, encoding=encoding) \
  1868. as source_module_file:
  1869. first_line = source_module_file.readline()
  1870. self.msg(5, 'SWIG wrapper candidate first line: %r' % (first_line))
  1871. return "automatically generated by SWIG" in first_line
  1872. # List of the graph nodes created for all target modules both
  1873. # imported by and returned from this call, whose:
  1874. #
  1875. # * First element is the graph node for the core target module
  1876. # specified by the "target_module_partname" parameter.
  1877. # * Remaining elements are the graph nodes for all target submodules
  1878. # specified by the "target_attr_names" parameter.
  1879. target_modules = None
  1880. # True if this is a Python 2-style implicit relative import of a
  1881. # SWIG-generated C extension. False if we checked and it is not SWIG.
  1882. # None if we haven't checked yet.
  1883. is_swig_import = None
  1884. # Attempt to import this target module in the customary way.
  1885. try:
  1886. target_modules = self.import_hook(
  1887. target_module_partname, source_module,
  1888. target_attr_names=None, level=level, edge_attr=edge_attr)
  1889. # Failing that, defer to custom module importers handling non-standard
  1890. # import schemes (namely, SWIG).
  1891. except InvalidRelativeImportError:
  1892. self.msgout(2, "Invalid relative import", level,
  1893. target_module_partname, target_attr_names)
  1894. result = []
  1895. for sub in target_attr_names or '*':
  1896. m = self.createNode(InvalidRelativeImport,
  1897. '.' * level + target_module_partname, sub)
  1898. self._updateReference(source_module, m, edge_data=edge_attr)
  1899. result.append(m)
  1900. return result
  1901. except ImportError as msg:
  1902. # If this is an absolute top-level import under Python 3 and if the
  1903. # name to be imported is the caller's name prefixed by "_", this
  1904. # could be a SWIG-generated Python 2-style implicit relative import.
  1905. # SWIG-generated files contain functions named swig_import_helper()
  1906. # importing dynamic libraries residing in the same directory. For
  1907. # example, a SWIG-generated caller module "csr.py" might resemble:
  1908. #
  1909. # # This file was automatically generated by SWIG (http://www.swig.org).
  1910. # ...
  1911. # def swig_import_helper():
  1912. # ...
  1913. # try:
  1914. # fp, pathname, description = imp.find_module('_csr',
  1915. # [dirname(__file__)])
  1916. # except ImportError:
  1917. # import _csr
  1918. # return _csr
  1919. #
  1920. # While there exists no reasonable means for modulegraph to parse
  1921. # the call to imp.find_module(), the subsequent implicit relative
  1922. # import is trivially parsable. This import is prohibited under
  1923. # Python 3, however, and thus parsed only if the caller's file is
  1924. # parsable plaintext (as indicated by a filetype of ".py") and the
  1925. # first line of this file is the above SWIG header comment.
  1926. #
  1927. # The constraint that this library's name be the caller's name
  1928. # prefixed by '_' is explicitly mandated by SWIG and thus a
  1929. # reliable indicator of "SWIG-ness". The SWIG documentation states:
  1930. # "When linking the module, the name of the output file has to match
  1931. # the name of the module prefixed by an underscore."
  1932. #
  1933. # Only source modules (e.g., ".py"-suffixed files) are SWIG import
  1934. # candidates. All other node types are safely ignorable.
  1935. if is_swig_candidate():
  1936. self.msg(
  1937. 4,
  1938. 'SWIG import candidate (name=%r, caller=%r, level=%r)' % (
  1939. target_module_partname, source_module, level))
  1940. is_swig_import = is_swig_wrapper(source_module)
  1941. if is_swig_import:
  1942. # Convert this Python 2-compliant implicit relative
  1943. # import prohibited by Python 3 into a Python
  1944. # 3-compliant explicit relative "from"-style import for
  1945. # the duration of this function call by overwriting the
  1946. # original parameters passed to this call.
  1947. target_attr_names = [target_module_partname]
  1948. target_module_partname = ''
  1949. level = 1
  1950. self.msg(2,
  1951. 'SWIG import (caller=%r, fromlist=%r, level=%r)'
  1952. % (source_module, target_attr_names, level))
  1953. # Import this target SWIG C extension's package.
  1954. try:
  1955. target_modules = self.import_hook(
  1956. target_module_partname, source_module,
  1957. target_attr_names=None,
  1958. level=level,
  1959. edge_attr=edge_attr)
  1960. except ImportError as msg:
  1961. self.msg(2, "SWIG ImportError:", str(msg))
  1962. # If this module remains unimportable...
  1963. if target_modules is None:
  1964. self.msg(2, "ImportError:", str(msg))
  1965. # Add this module as a MissingModule node.
  1966. target_module = self.createNode(
  1967. MissingModule,
  1968. _path_from_importerror(msg, target_module_partname))
  1969. self._updateReference(
  1970. source_module, target_module, edge_data=edge_attr)
  1971. # Initialize this list to this node.
  1972. target_modules = [target_module]
  1973. # Ensure that the above logic imported exactly one target module.
  1974. assert len(target_modules) == 1, (
  1975. 'Expected import_hook() to'
  1976. 'return only one module but received: {}'.format(target_modules))
  1977. # Target module imported above.
  1978. target_module = target_modules[0]
  1979. if isinstance(target_module, MissingModule) \
  1980. and is_swig_import is None and is_swig_candidate() \
  1981. and is_swig_wrapper(source_module):
  1982. # if this possible swig C module was previously imported from
  1983. # a python module other than its corresponding swig python
  1984. # module, then it may have been considered a MissingModule.
  1985. # Try to reimport it now. For details see pull-request #2578
  1986. # and issue #1522.
  1987. #
  1988. # If this module was takes as a SWIG candidate above, but failed
  1989. # to import, this would be a MissingModule, too. Thus check if
  1990. # this was the case (is_swig_import would be not None) to avoid
  1991. # recursion error. If `is_swig_import` is None and we are still a
  1992. # swig candidate then that means we haven't properly imported this
  1993. # swig module yet so do that below.
  1994. #
  1995. # Remove the MissingModule node from the graph so that we can
  1996. # attempt a reimport and avoid collisions. This node should be
  1997. # fine to remove because the proper module will be imported and
  1998. # added to the graph in the next line (call to _safe_import_hook).
  1999. self.removeNode(target_module)
  2000. # Reimport the SWIG C module relative to the wrapper
  2001. target_modules = self._safe_import_hook(
  2002. target_module_partname, source_module,
  2003. target_attr_names=None, level=1, edge_attr=edge_attr)
  2004. # return the output regardless because it would just be
  2005. # duplicating the processing below
  2006. return target_modules
  2007. if isinstance(edge_attr, DependencyInfo):
  2008. edge_attr = edge_attr._replace(fromlist=True)
  2009. # If this is a "from"-style import *AND* this target module is a
  2010. # package, import all attributes listed by the "import" clause of this
  2011. # import that are submodules of this package. If this target module is
  2012. # *NOT* a package, these attributes are always ignorable globals (e.g.,
  2013. # classes, variables) defined at the top level of this module.
  2014. #
  2015. # If this target module is a non-package, it could still contain
  2016. # importable submodules (e.g., the non-package `os` module containing
  2017. # the `os.path` submodule). In this case, these submodules are already
  2018. # imported by this target module's pure-Python code. Since our import
  2019. # scanner already detects these imports, these submodules need *NOT* be
  2020. # reimported here. (Doing so would be harmless but inefficient.)
  2021. if target_attr_names and isinstance(target_module,
  2022. (Package, AliasNode)):
  2023. # For the name of each attribute imported from this target package
  2024. # into this source module...
  2025. for target_submodule_partname in target_attr_names:
  2026. #FIXME: Is this optimization *REALLY* an optimization or at all
  2027. #necessary? The findNode() method called below should already
  2028. #be heavily optimized, in which case this optimization here is
  2029. #premature, senseless, and should be eliminated.
  2030. # If this attribute is a previously imported submodule of this
  2031. # target module, optimize this edge case.
  2032. if target_module.is_submodule(target_submodule_partname):
  2033. # Graph node for this submodule.
  2034. target_submodule = target_module.get_submodule(
  2035. target_submodule_partname)
  2036. #FIXME: What? Shouldn't "target_submodule" *ALWAYS* be
  2037. #non-None here? Assert this to be non-None instead.
  2038. if target_submodule is not None:
  2039. #FIXME: Why does duplication matter? List searches are
  2040. #mildly expensive.
  2041. # If this submodule has not already been added to the
  2042. # list of submodules to be returned, do so.
  2043. if target_submodule not in target_modules:
  2044. self._updateReference(
  2045. source_module,
  2046. target_submodule,
  2047. edge_data=edge_attr)
  2048. target_modules.append(target_submodule)
  2049. continue
  2050. # Fully-qualified name of this submodule.
  2051. target_submodule_name = (
  2052. target_module.identifier + '.' + target_submodule_partname)
  2053. # Graph node of this submodule if previously imported or None.
  2054. target_submodule = self.find_node(target_submodule_name)
  2055. # If this submodule has not been imported, do so as if this
  2056. # submodule were the only attribute listed by the "import"
  2057. # clause of this import (e.g., as "from foo import bar" rather
  2058. # than "from foo import car, far, bar").
  2059. if target_submodule is None:
  2060. # Attempt to import this submodule.
  2061. try:
  2062. # Ignore the list of graph nodes returned by this
  2063. # method. If both this submodule's package and this
  2064. # submodule are importable, this method returns a
  2065. # 2-element list whose second element is this
  2066. # submodule's graph node. However, if this submodule's
  2067. # package is importable but this submodule is not,
  2068. # this submodule is either:
  2069. #
  2070. # * An ignorable global attribute defined at the top
  2071. # level of this package's "__init__" submodule. In
  2072. # this case, this method returns a 1-element list
  2073. # without raising an exception.
  2074. # * A non-ignorable unimportable submodule. In this
  2075. # case, this method raises an "ImportError".
  2076. #
  2077. # While the first two cases are disambiguatable by the
  2078. # length of this list, doing so would render this code
  2079. # dependent on import_hook() details subject to change.
  2080. # Instead, call findNode() to decide the truthiness.
  2081. self.import_hook(
  2082. target_module_partname, source_module,
  2083. target_attr_names=[target_submodule_partname],
  2084. level=level,
  2085. edge_attr=edge_attr)
  2086. # Graph node of this submodule imported by the prior
  2087. # call if importable or None otherwise.
  2088. target_submodule = self.find_node(target_submodule_name)
  2089. # If this submodule does not exist, this *MUST* be an
  2090. # ignorable global attribute defined at the top level
  2091. # of this package's "__init__" submodule.
  2092. if target_submodule is None:
  2093. # Assert this to actually be the case.
  2094. assert target_module.is_global_attr(
  2095. target_submodule_partname), (
  2096. 'No global named {} in {}.__init__'.format(
  2097. target_submodule_partname,
  2098. target_module.identifier))
  2099. # Skip this safely ignorable importation to the
  2100. # next attribute. See similar logic in the body of
  2101. # _import_importable_package_submodules().
  2102. self.msg(4, '_safe_import_hook', 'ignoring imported non-module global', target_module.identifier, target_submodule_partname)
  2103. continue
  2104. # If this is a SWIG C extension, instruct PyInstaller
  2105. # to freeze this extension under its unqualified rather
  2106. # than qualified name (e.g., as "_csr" rather than
  2107. # "scipy.sparse.sparsetools._csr"), permitting the
  2108. # implicit relative import in its parent SWIG module to
  2109. # successfully find this extension.
  2110. if is_swig_import:
  2111. # If a graph node with this name already exists,
  2112. # avoid collisions by emitting an error instead.
  2113. if self.find_node(target_submodule_partname):
  2114. self.msg(
  2115. 2,
  2116. 'SWIG import error: %r basename %r '
  2117. 'already exists' % (
  2118. target_submodule_name,
  2119. target_submodule_partname))
  2120. else:
  2121. self.msg(
  2122. 4,
  2123. 'SWIG import renamed from %r to %r' % (
  2124. target_submodule_name,
  2125. target_submodule_partname))
  2126. target_submodule.identifier = (
  2127. target_submodule_partname)
  2128. # If this submodule is unimportable, add a MissingModule.
  2129. except ImportError as msg:
  2130. self.msg(2, "ImportError:", str(msg))
  2131. target_submodule = self.createNode(
  2132. MissingModule, target_submodule_name)
  2133. # Add this submodule to its package.
  2134. target_module.add_submodule(
  2135. target_submodule_partname, target_submodule)
  2136. if target_submodule is not None:
  2137. self._updateReference(
  2138. target_module, target_submodule, edge_data=edge_attr)
  2139. self._updateReference(
  2140. source_module, target_submodule, edge_data=edge_attr)
  2141. if target_submodule not in target_modules:
  2142. target_modules.append(target_submodule)
  2143. # Return the list of all target modules imported by this call.
  2144. return target_modules
  2145. def _scan_code(
  2146. self,
  2147. module,
  2148. module_code_object,
  2149. module_code_object_ast=None):
  2150. """
  2151. Parse and add all import statements from the passed code object of the
  2152. passed source module to this graph, recursively.
  2153. **This method is at the root of all `ModuleGraph` recursion.**
  2154. Recursion begins here and ends when all import statements in all code
  2155. objects of all modules transitively imported by the source module
  2156. passed to the first call to this method have been added to the graph.
  2157. Specifically, this method:
  2158. 1. If the passed `module_code_object_ast` parameter is non-`None`,
  2159. parses all import statements from this object.
  2160. 2. Else, parses all import statements from the passed
  2161. `module_code_object` parameter.
  2162. 1. For each such import statement:
  2163. 1. Adds to this `ModuleGraph` instance:
  2164. 1. Nodes for all target modules of these imports.
  2165. 1. Directed edges from this source module to these target
  2166. modules.
  2167. 2. Recursively calls this method with these target modules.
  2168. Parameters
  2169. ----------
  2170. module : Node
  2171. Graph node of the module to be parsed.
  2172. module_code_object : PyCodeObject
  2173. Code object providing this module's disassembled Python bytecode.
  2174. Ignored unless `module_code_object_ast` is `None`.
  2175. module_code_object_ast : optional[ast.AST]
  2176. Optional abstract syntax tree (AST) of this module if any or `None`
  2177. otherwise. Defaults to `None`, in which case the passed
  2178. `module_code_object` is parsed instead.
  2179. Returns
  2180. ----------
  2181. module : Node
  2182. Graph node of the module to be parsed.
  2183. """
  2184. # For safety, guard against multiple scans of the same module by
  2185. # resetting this module's list of deferred target imports. While
  2186. # uncommon, this edge case can occur due to:
  2187. #
  2188. # * Dynamic package replacement via the replacePackage() function. For
  2189. # example, the real "_xmlplus" package dynamically replaces itself
  2190. # with the fake "xml" package into the "sys.modules" cache of all
  2191. # currently loaded modules at runtime.
  2192. module._deferred_imports = []
  2193. # Parse all imports from this module *BEFORE* adding these imports to
  2194. # the graph. If an AST is provided, parse that rather than this
  2195. # module's code object.
  2196. if module_code_object_ast is not None:
  2197. # Parse this module's AST for imports.
  2198. self._scan_ast(module, module_code_object_ast)
  2199. # Parse this module's code object for all relevant non-imports
  2200. # (e.g., global variable declarations and undeclarations).
  2201. self._scan_bytecode(
  2202. module, module_code_object, is_scanning_imports=False)
  2203. # Else, parse this module's code object for imports.
  2204. else:
  2205. self._scan_bytecode(
  2206. module, module_code_object, is_scanning_imports=True)
  2207. return module
  2208. def _scan_ast(self, module, module_code_object_ast):
  2209. """
  2210. Parse and add all import statements from the passed abstract syntax
  2211. tree (AST) of the passed source module to this graph, non-recursively.
  2212. Parameters
  2213. ----------
  2214. module : Node
  2215. Graph node of the module to be parsed.
  2216. module_code_object_ast : ast.AST
  2217. Abstract syntax tree (AST) of this module to be parsed.
  2218. """
  2219. visitor = _Visitor(self, module)
  2220. visitor.visit(module_code_object_ast)
  2221. #FIXME: Optimize. Global attributes added by this method are tested by
  2222. #other methods *ONLY* for packages, implying this method should scan and
  2223. #handle opcodes pertaining to global attributes (e.g.,
  2224. #"STORE_NAME", "DELETE_GLOBAL") only if the passed "module"
  2225. #object is an instance of the "Package" class. For all other module types,
  2226. #these opcodes should simply be ignored.
  2227. #
  2228. #After doing so, the "Node._global_attr_names" attribute and all methods
  2229. #using this attribute (e.g., Node.is_global()) should be moved from the
  2230. #"Node" superclass to the "Package" subclass.
  2231. def _scan_bytecode(
  2232. self, module, module_code_object, is_scanning_imports):
  2233. """
  2234. Parse and add all import statements from the passed code object of the
  2235. passed source module to this graph, non-recursively.
  2236. This method parses all reasonably parsable operations (i.e., operations
  2237. that are both syntactically and semantically parsable _without_
  2238. requiring Turing-complete interpretation) directly or indirectly
  2239. involving module importation from this code object. This includes:
  2240. * `IMPORT_NAME`, denoting an import statement. Ignored unless
  2241. the passed `is_scanning_imports` parameter is `True`.
  2242. * `STORE_NAME` and `STORE_GLOBAL`, denoting the
  2243. declaration of a global attribute (e.g., class, variable) in this
  2244. module. This method stores each such declaration for subsequent
  2245. lookup. While global attributes are usually irrelevant to import
  2246. parsing, they remain the only means of distinguishing erroneous
  2247. non-ignorable attempts to import non-existent submodules of a package
  2248. from successful ignorable attempts to import existing global
  2249. attributes of a package's `__init__` submodule (e.g., the `bar` in
  2250. `from foo import bar`, which is either a non-ignorable submodule of
  2251. `foo` or an ignorable global attribute of `foo.__init__`).
  2252. * `DELETE_NAME` and `DELETE_GLOBAL`, denoting the
  2253. undeclaration of a previously declared global attribute in this
  2254. module.
  2255. Since `ModuleGraph` is _not_ intended to replicate the behaviour of a
  2256. full-featured Turing-complete Python interpreter, this method ignores
  2257. operations that are _not_ reasonably parsable from this code object --
  2258. even those directly or indirectly involving module importation. This
  2259. includes:
  2260. * `STORE_ATTR(namei)`, implementing `TOS.name = TOS1`. If `TOS` is the
  2261. name of a target module currently imported into the namespace of the
  2262. passed source module, this opcode would ideally be parsed to add that
  2263. global attribute to that target module. Since this addition only
  2264. conditionally occurs on the importation of this source module and
  2265. execution of the code branch in this module performing this addition,
  2266. however, that global _cannot_ be unconditionally added to that target
  2267. module. In short, only Turing-complete behaviour suffices.
  2268. * `DELETE_ATTR(namei)`, implementing `del TOS.name`. If `TOS` is the
  2269. name of a target module currently imported into the namespace of the
  2270. passed source module, this opcode would ideally be parsed to remove
  2271. that global attribute from that target module. Again, however, only
  2272. Turing-complete behaviour suffices.
  2273. Parameters
  2274. ----------
  2275. module : Node
  2276. Graph node of the module to be parsed.
  2277. module_code_object : PyCodeObject
  2278. Code object of the module to be parsed.
  2279. is_scanning_imports : bool
  2280. `True` only if this method is parsing import statements from
  2281. `IMPORT_NAME` opcodes. If `False`, no import statements will be
  2282. parsed. This parameter is typically:
  2283. * `True` when parsing this module's code object for such imports.
  2284. * `False` when parsing this module's abstract syntax tree (AST)
  2285. (rather than code object) for such imports. In this case, that
  2286. parsing will have already parsed import statements, which this
  2287. parsing must avoid repeating.
  2288. """
  2289. level = None
  2290. fromlist = None
  2291. # 'deque' is a list-like container with fast appends, pops on
  2292. # either end, and automatically discarding elements too much.
  2293. prev_insts = deque(maxlen=2)
  2294. for inst in util.iterate_instructions(module_code_object):
  2295. if not inst:
  2296. continue
  2297. # If this is an import statement originating from this module,
  2298. # parse this import.
  2299. #
  2300. # Note that the related "IMPORT_FROM" opcode need *NOT* be parsed.
  2301. # "IMPORT_NAME" suffices. For further details, see
  2302. # http://probablyprogramming.com/2008/04/14/python-import_name
  2303. if inst.opname == 'IMPORT_NAME':
  2304. # If this method is ignoring import statements, skip to the
  2305. # next opcode.
  2306. if not is_scanning_imports:
  2307. continue
  2308. assert prev_insts[-2].opname == 'LOAD_CONST'
  2309. assert prev_insts[-1].opname == 'LOAD_CONST'
  2310. # Python >=2.5: LOAD_CONST flags, LOAD_CONST names, IMPORT_NAME name
  2311. level = prev_insts[-2].argval
  2312. fromlist = prev_insts[-1].argval
  2313. assert fromlist is None or type(fromlist) is tuple
  2314. target_module_partname = inst.argval
  2315. #FIXME: The exact same logic appears in _collect_import(),
  2316. #which isn't particularly helpful. Instead, defer this logic
  2317. #until later by:
  2318. #
  2319. #* Refactor the "_deferred_imports" list to contain 2-tuples
  2320. # "(_safe_import_hook_args, _safe_import_hook_kwargs)" rather
  2321. # than 3-tuples "(have_star, _safe_import_hook_args,
  2322. # _safe_import_hook_kwargs)".
  2323. #* Stop prepending these tuples by a "have_star" boolean both
  2324. # here, in _collect_import(), and in _process_imports().
  2325. #* Shift the logic below to _process_imports().
  2326. #* Remove the same logic from _collect_import().
  2327. have_star = False
  2328. if fromlist is not None:
  2329. fromlist = uniq(fromlist)
  2330. if '*' in fromlist:
  2331. fromlist.remove('*')
  2332. have_star = True
  2333. # Record this import as originating from this module for
  2334. # subsequent handling by the _process_imports() method.
  2335. module._deferred_imports.append((
  2336. have_star,
  2337. (target_module_partname, module, fromlist, level),
  2338. {}
  2339. ))
  2340. elif inst.opname in ('STORE_NAME', 'STORE_GLOBAL'):
  2341. # If this is the declaration of a global attribute (e.g.,
  2342. # class, variable) in this module, store this declaration for
  2343. # subsequent lookup. See method docstring for further details.
  2344. #
  2345. # Global attributes are usually irrelevant to import parsing, but
  2346. # remain the only means of distinguishing erroneous non-ignorable
  2347. # attempts to import non-existent submodules of a package from
  2348. # successful ignorable attempts to import existing global
  2349. # attributes of a package's "__init__" submodule (e.g., the "bar"
  2350. # in "from foo import bar", which is either a non-ignorable
  2351. # submodule of "foo" or an ignorable global attribute of
  2352. # "foo.__init__").
  2353. name = inst.argval
  2354. module.add_global_attr(name)
  2355. elif inst.opname in ('DELETE_NAME', 'DELETE_GLOBAL'):
  2356. # If this is the undeclaration of a previously declared global
  2357. # attribute (e.g., class, variable) in this module, remove that
  2358. # declaration to prevent subsequent lookup. See method docstring
  2359. # for further details.
  2360. name = inst.argval
  2361. module.remove_global_attr_if_found(name)
  2362. prev_insts.append(inst)
  2363. def _process_imports(self, source_module):
  2364. """
  2365. Graph all target modules whose importations were previously parsed from
  2366. the passed source module by a prior call to the `_scan_code()` method
  2367. and methods call by that method (e.g., `_scan_ast()`,
  2368. `_scan_bytecode()`, `_scan_bytecode_stores()`).
  2369. Parameters
  2370. ----------
  2371. source_module : Node
  2372. Graph node of the source module to graph target imports for.
  2373. """
  2374. # If this source module imported no target modules, noop.
  2375. if not source_module._deferred_imports:
  2376. return
  2377. # For each target module imported by this source module...
  2378. for have_star, import_info, kwargs in source_module._deferred_imports:
  2379. # Graph node of the target module specified by the "from" portion
  2380. # of this "from"-style star import (e.g., an import resembling
  2381. # "from {target_module_name} import *") or ignored otherwise.
  2382. target_module = self._safe_import_hook(*import_info, **kwargs)[0]
  2383. # If this is a "from"-style star import, process this import.
  2384. if have_star:
  2385. #FIXME: Sadly, the current approach to importing attributes
  2386. #from "from"-style star imports is... simplistic. This should
  2387. #be revised as follows. If this target module is:
  2388. #
  2389. #* A package:
  2390. # * Whose "__init__" submodule defines the "__all__" global
  2391. # attribute, only attributes listed by this attribute should
  2392. # be imported.
  2393. # * Else, *NO* attributes should be imported.
  2394. #* A non-package:
  2395. # * Defining the "__all__" global attribute, only attributes
  2396. # listed by this attribute should be imported.
  2397. # * Else, only public attributes whose names are *NOT*
  2398. # prefixed by "_" should be imported.
  2399. source_module.add_global_attrs_from_module(target_module)
  2400. source_module._starimported_ignored_module_names.update(
  2401. target_module._starimported_ignored_module_names)
  2402. # If this target module has no code object and hence is
  2403. # unparsable, record its name for posterity.
  2404. if target_module.code is None:
  2405. target_module_name = import_info[0]
  2406. source_module._starimported_ignored_module_names.add(
  2407. target_module_name)
  2408. # For safety, prevent these imports from being reprocessed.
  2409. source_module._deferred_imports = None
  2410. def _find_module(self, name, path, parent=None):
  2411. """
  2412. 3-tuple describing the physical location of the module with the passed
  2413. name if this module is physically findable _or_ raise `ImportError`.
  2414. This high-level method wraps the low-level `modulegraph.find_module()`
  2415. function with additional support for graph-based module caching.
  2416. Parameters
  2417. ----------
  2418. name : str
  2419. Fully-qualified name of the Python module to be found.
  2420. path : list
  2421. List of the absolute paths of all directories to search for this
  2422. module _or_ `None` if the default path list `self.path` is to be
  2423. searched.
  2424. parent : Node
  2425. Package containing this module if this module is a submodule of a
  2426. package _or_ `None` if this is a top-level module.
  2427. Returns
  2428. ----------
  2429. (filename, loader)
  2430. See `modulegraph._find_module()` for details.
  2431. Raises
  2432. ----------
  2433. ImportError
  2434. If this module is _not_ found.
  2435. """
  2436. if parent is not None:
  2437. # assert path is not None
  2438. fullname = parent.identifier + '.' + name
  2439. else:
  2440. fullname = name
  2441. node = self.find_node(fullname)
  2442. if node is not None:
  2443. self.msg(3, "find_module: already included?", node)
  2444. raise ImportError(name)
  2445. if path is None:
  2446. if name in sys.builtin_module_names:
  2447. return (None, BUILTIN_MODULE)
  2448. path = self.path
  2449. return self._find_module_path(fullname, name, path)
  2450. def _find_module_path(self, fullname, module_name, search_dirs):
  2451. """
  2452. 3-tuple describing the physical location of the module with the passed
  2453. name if this module is physically findable _or_ raise `ImportError`.
  2454. This low-level function is a variant on the standard `imp.find_module()`
  2455. function with additional support for:
  2456. * Multiple search paths. The passed list of absolute paths will be
  2457. iteratively searched for the first directory containing a file
  2458. corresponding to this module.
  2459. * Compressed (e.g., zipped) packages.
  2460. For efficiency, the higher level `ModuleGraph._find_module()` method
  2461. wraps this function with support for module caching.
  2462. Parameters
  2463. ----------
  2464. module_name : str
  2465. Fully-qualified name of the module to be found.
  2466. search_dirs : list
  2467. List of the absolute paths of all directories to search for this
  2468. module (in order). Searching will halt at the first directory
  2469. containing this module.
  2470. Returns
  2471. ----------
  2472. (filename, loader)
  2473. 2-tuple describing the physical location of this module, where:
  2474. * `filename` is the absolute path of this file.
  2475. * `loader` is the import loader.
  2476. In case of a namespace package, this is a NAMESPACE_PACKAGE
  2477. instance
  2478. Raises
  2479. ----------
  2480. ImportError
  2481. If this module is _not_ found.
  2482. """
  2483. self.msgin(4, "_find_module_path <-", fullname, search_dirs)
  2484. # Top-level 2-tuple to be returned.
  2485. path_data = None
  2486. # List of the absolute paths of all directories comprising the
  2487. # namespace package to which this module belongs if any.
  2488. namespace_dirs = []
  2489. try:
  2490. for search_dir in search_dirs:
  2491. # PEP 302-compliant importer making loaders for this directory.
  2492. importer = pkgutil.get_importer(search_dir)
  2493. # If this directory is not importable, continue.
  2494. if importer is None:
  2495. # self.msg(4, "_find_module_path importer not found", search_dir)
  2496. continue
  2497. # Get the PEP 302-compliant loader object loading this module.
  2498. #
  2499. # If this importer defines the PEP 302-compliant find_loader()
  2500. # method, prefer that.
  2501. if hasattr(importer, 'find_loader'):
  2502. loader, loader_namespace_dirs = importer.find_loader(
  2503. module_name)
  2504. namespace_dirs.extend(loader_namespace_dirs)
  2505. # Else if this importer defines the Python 2-specific
  2506. # find_module() method, fall back to that. Despite the method
  2507. # name, this method returns a loader rather than a module.
  2508. elif hasattr(importer, 'find_module'):
  2509. loader = importer.find_module(module_name)
  2510. # Else, raise an exception.
  2511. else:
  2512. raise ImportError(
  2513. "Module %r importer %r loader unobtainable" % (module_name, importer))
  2514. # If this module is not loadable from this directory, continue.
  2515. if loader is None:
  2516. # self.msg(4, "_find_module_path loader not found", search_dir)
  2517. continue
  2518. # Absolute path of this module. If this module resides in a
  2519. # compressed archive, this is the absolute path of this module
  2520. # after extracting this module from that archive and hence
  2521. # should not exist; else, this path should typically exist.
  2522. pathname = None
  2523. # If this loader defines the PEP 302-compliant get_filename()
  2524. # method, preferably call that method first. Most if not all
  2525. # loaders (including zipimporter objects) define this method.
  2526. if hasattr(loader, 'get_filename'):
  2527. pathname = loader.get_filename(module_name)
  2528. # Else if this loader provides a "path" attribute, defer to that.
  2529. elif hasattr(loader, 'path'):
  2530. pathname = loader.path
  2531. # Else, raise an exception.
  2532. else:
  2533. raise ImportError(
  2534. "Module %r loader %r path unobtainable" % (module_name, loader))
  2535. # If no path was found, this is probably a namespace package. In
  2536. # such case, continue collecting namespace directories.
  2537. if pathname is None:
  2538. self.msg(4, "_find_module_path path not found", pathname)
  2539. continue
  2540. # Return such metadata.
  2541. path_data = (pathname, loader)
  2542. break
  2543. # Else if this is a namespace package, return such metadata.
  2544. else:
  2545. if namespace_dirs:
  2546. path_data = (namespace_dirs[0],
  2547. NAMESPACE_PACKAGE(namespace_dirs))
  2548. except UnicodeDecodeError as exc:
  2549. self.msgout(1, "_find_module_path -> unicode error", exc)
  2550. # Ensure that exceptions are logged, as this function is typically
  2551. # called by the import_module() method which squelches ImportErrors.
  2552. except Exception as exc:
  2553. self.msgout(4, "_find_module_path -> exception", exc)
  2554. raise
  2555. # If this module was not found, raise an exception.
  2556. self.msgout(4, "_find_module_path ->", path_data)
  2557. if path_data is None:
  2558. raise ImportError("No module named " + repr(module_name))
  2559. return path_data
  2560. def create_xref(self, out=None):
  2561. global header, footer, entry, contpl, contpl_linked, imports
  2562. if out is None:
  2563. out = sys.stdout
  2564. scripts = []
  2565. mods = []
  2566. for mod in self.iter_graph():
  2567. name = os.path.basename(mod.identifier)
  2568. if isinstance(mod, Script):
  2569. scripts.append((name, mod))
  2570. else:
  2571. mods.append((name, mod))
  2572. scripts.sort()
  2573. mods.sort()
  2574. scriptnames = [sn for sn, m in scripts]
  2575. scripts.extend(mods)
  2576. mods = scripts
  2577. title = "modulegraph cross reference for " + ', '.join(scriptnames)
  2578. print(header % {"TITLE": title}, file=out)
  2579. def sorted_namelist(mods):
  2580. lst = [os.path.basename(mod.identifier) for mod in mods if mod]
  2581. lst.sort()
  2582. return lst
  2583. for name, m in mods:
  2584. content = ""
  2585. if isinstance(m, BuiltinModule):
  2586. content = contpl % {"NAME": name,
  2587. "TYPE": "<i>(builtin module)</i>"}
  2588. elif isinstance(m, Extension):
  2589. content = contpl % {"NAME": name,
  2590. "TYPE": "<tt>%s</tt>" % m.filename}
  2591. else:
  2592. url = pathname2url(m.filename or "")
  2593. content = contpl_linked % {"NAME": name, "URL": url,
  2594. 'TYPE': m.__class__.__name__}
  2595. oute, ince = map(sorted_namelist, self.get_edges(m))
  2596. if oute:
  2597. links = []
  2598. for n in oute:
  2599. links.append(""" <a href="#%s">%s</a>\n""" % (n, n))
  2600. # #8226 = bullet-point; can't use html-entities since the
  2601. # test-suite uses xml.etree.ElementTree.XMLParser, which
  2602. # does't supprot them.
  2603. links = " &#8226; ".join(links)
  2604. content += imports % {"HEAD": "imports", "LINKS": links}
  2605. if ince:
  2606. links = []
  2607. for n in ince:
  2608. links.append(""" <a href="#%s">%s</a>\n""" % (n, n))
  2609. # #8226 = bullet-point; can't use html-entities since the
  2610. # test-suite uses xml.etree.ElementTree.XMLParser, which
  2611. # does't supprot them.
  2612. links = " &#8226; ".join(links)
  2613. content += imports % {"HEAD": "imported by", "LINKS": links}
  2614. print(entry % {"NAME": name, "CONTENT": content}, file=out)
  2615. print(footer, file=out)
  2616. def itergraphreport(self, name='G', flatpackages=()):
  2617. # XXX: Can this be implemented using Dot()?
  2618. nodes = list(map(self.graph.describe_node, self.graph.iterdfs(self)))
  2619. describe_edge = self.graph.describe_edge
  2620. edges = deque()
  2621. packagenodes = set()
  2622. packageidents = {}
  2623. nodetoident = {}
  2624. inpackages = {}
  2625. mainedges = set()
  2626. # XXX - implement
  2627. flatpackages = dict(flatpackages)
  2628. def nodevisitor(node, data, outgoing, incoming):
  2629. if not isinstance(data, Node):
  2630. return {'label': str(node)}
  2631. #if isinstance(d, (ExcludedModule, MissingModule, BadModule)):
  2632. # return None
  2633. s = '<f0> ' + type(data).__name__
  2634. for i, v in enumerate(data.infoTuple()[:1], 1):
  2635. s += '| <f%d> %s' % (i, v)
  2636. return {'label': s, 'shape': 'record'}
  2637. def edgevisitor(edge, data, head, tail):
  2638. # XXX: This method nonsense, the edge
  2639. # data is never initialized.
  2640. if data == 'orphan':
  2641. return {'style': 'dashed'}
  2642. elif data == 'pkgref':
  2643. return {'style': 'dotted'}
  2644. return {}
  2645. yield 'digraph %s {\ncharset="UTF-8";\n' % (name,)
  2646. attr = dict(rankdir='LR', concentrate='true')
  2647. cpatt = '%s="%s"'
  2648. for item in attr.items():
  2649. yield '\t%s;\n' % (cpatt % item,)
  2650. # find all packages (subgraphs)
  2651. for (node, data, outgoing, incoming) in nodes:
  2652. nodetoident[node] = getattr(data, 'identifier', None)
  2653. if isinstance(data, Package):
  2654. packageidents[data.identifier] = node
  2655. inpackages[node] = set([node])
  2656. packagenodes.add(node)
  2657. # create sets for subgraph, write out descriptions
  2658. for (node, data, outgoing, incoming) in nodes:
  2659. # update edges
  2660. for edge in (describe_edge(e) for e in outgoing):
  2661. edges.append(edge)
  2662. # describe node
  2663. yield '\t"%s" [%s];\n' % (
  2664. node,
  2665. ','.join([
  2666. (cpatt % item) for item in
  2667. nodevisitor(node, data, outgoing, incoming).items()
  2668. ]),
  2669. )
  2670. inside = inpackages.get(node)
  2671. if inside is None:
  2672. inside = inpackages[node] = set()
  2673. ident = nodetoident[node]
  2674. if ident is None:
  2675. continue
  2676. pkgnode = packageidents.get(ident[:ident.rfind('.')])
  2677. if pkgnode is not None:
  2678. inside.add(pkgnode)
  2679. graph = []
  2680. subgraphs = {}
  2681. for key in packagenodes:
  2682. subgraphs[key] = []
  2683. while edges:
  2684. edge, data, head, tail = edges.popleft()
  2685. if ((head, tail)) in mainedges:
  2686. continue
  2687. mainedges.add((head, tail))
  2688. tailpkgs = inpackages[tail]
  2689. common = inpackages[head] & tailpkgs
  2690. if not common and tailpkgs:
  2691. usepkgs = sorted(tailpkgs)
  2692. if len(usepkgs) != 1 or usepkgs[0] != tail:
  2693. edges.append((edge, data, head, usepkgs[0]))
  2694. edges.append((edge, 'pkgref', usepkgs[-1], tail))
  2695. continue
  2696. if common:
  2697. common = common.pop()
  2698. if tail == common:
  2699. edges.append((edge, data, tail, head))
  2700. elif head == common:
  2701. subgraphs[common].append((edge, 'pkgref', head, tail))
  2702. else:
  2703. edges.append((edge, data, common, head))
  2704. edges.append((edge, data, common, tail))
  2705. else:
  2706. graph.append((edge, data, head, tail))
  2707. def do_graph(edges, tabs):
  2708. edgestr = tabs + '"%s" -> "%s" [%s];\n'
  2709. # describe edge
  2710. for (edge, data, head, tail) in edges:
  2711. attribs = edgevisitor(edge, data, head, tail)
  2712. yield edgestr % (
  2713. head,
  2714. tail,
  2715. ','.join([(cpatt % item) for item in attribs.items()]),
  2716. )
  2717. for g, edges in subgraphs.items():
  2718. yield '\tsubgraph "cluster_%s" {\n' % (g,)
  2719. yield '\t\tlabel="%s";\n' % (nodetoident[g],)
  2720. for s in do_graph(edges, '\t\t'):
  2721. yield s
  2722. yield '\t}\n'
  2723. for s in do_graph(graph, '\t'):
  2724. yield s
  2725. yield '}\n'
  2726. def graphreport(self, fileobj=None, flatpackages=()):
  2727. if fileobj is None:
  2728. fileobj = sys.stdout
  2729. fileobj.writelines(self.itergraphreport(flatpackages=flatpackages))
  2730. def report(self):
  2731. """Print a report to stdout, listing the found modules with their
  2732. paths, as well as modules that are missing, or seem to be missing.
  2733. """
  2734. print()
  2735. print("%-15s %-25s %s" % ("Class", "Name", "File"))
  2736. print("%-15s %-25s %s" % ("-----", "----", "----"))
  2737. for m in sorted(self.iter_graph(), key=lambda n: n.identifier):
  2738. print("%-15s %-25s %s" % (type(m).__name__, m.identifier, m.filename or ""))
  2739. def _replace_paths_in_code(self, co):
  2740. new_filename = original_filename = os.path.normpath(co.co_filename)
  2741. for f, r in self.replace_paths:
  2742. f = os.path.join(f, '')
  2743. r = os.path.join(r, '')
  2744. if original_filename.startswith(f):
  2745. new_filename = r + original_filename[len(f):]
  2746. break
  2747. else:
  2748. return co
  2749. consts = list(co.co_consts)
  2750. for i in range(len(consts)):
  2751. if isinstance(consts[i], type(co)):
  2752. consts[i] = self._replace_paths_in_code(consts[i])
  2753. code_func = type(co)
  2754. if hasattr(co, 'replace'): # is_py38
  2755. return co.replace(co_consts=tuple(consts),
  2756. co_filename=new_filename)
  2757. elif hasattr(co, 'co_kwonlyargcount'):
  2758. return code_func(
  2759. co.co_argcount, co.co_kwonlyargcount, co.co_nlocals,
  2760. co.co_stacksize, co.co_flags, co.co_code,
  2761. tuple(consts), co.co_names, co.co_varnames,
  2762. new_filename, co.co_name, co.co_firstlineno,
  2763. co.co_lnotab, co.co_freevars, co.co_cellvars)
  2764. else:
  2765. return code_func(
  2766. co.co_argcount, co.co_nlocals, co.co_stacksize,
  2767. co.co_flags, co.co_code, tuple(consts), co.co_names,
  2768. co.co_varnames, new_filename, co.co_name,
  2769. co.co_firstlineno, co.co_lnotab,
  2770. co.co_freevars, co.co_cellvars)