Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reflect.py 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. # -*- test-case-name: twisted.test.test_reflect -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Standardized versions of various cool and/or strange things that you can do
  6. with Python's reflection capabilities.
  7. """
  8. import os
  9. import pickle
  10. import re
  11. import sys
  12. import traceback
  13. import types
  14. import weakref
  15. from collections import deque
  16. from io import IOBase, StringIO
  17. from typing import Type, Union
  18. from twisted.python.compat import nativeString
  19. from twisted.python.deprecate import _fullyQualifiedName as fullyQualifiedName
  20. RegexType = type(re.compile(""))
  21. def prefixedMethodNames(classObj, prefix):
  22. """
  23. Given a class object C{classObj}, returns a list of method names that match
  24. the string C{prefix}.
  25. @param classObj: A class object from which to collect method names.
  26. @param prefix: A native string giving a prefix. Each method with a name
  27. which begins with this prefix will be returned.
  28. @type prefix: L{str}
  29. @return: A list of the names of matching methods of C{classObj} (and base
  30. classes of C{classObj}).
  31. @rtype: L{list} of L{str}
  32. """
  33. dct = {}
  34. addMethodNamesToDict(classObj, dct, prefix)
  35. return list(dct.keys())
  36. def addMethodNamesToDict(classObj, dict, prefix, baseClass=None):
  37. """
  38. This goes through C{classObj} (and its bases) and puts method names
  39. starting with 'prefix' in 'dict' with a value of 1. if baseClass isn't
  40. None, methods will only be added if classObj is-a baseClass
  41. If the class in question has the methods 'prefix_methodname' and
  42. 'prefix_methodname2', the resulting dict should look something like:
  43. {"methodname": 1, "methodname2": 1}.
  44. @param classObj: A class object from which to collect method names.
  45. @param dict: A L{dict} which will be updated with the results of the
  46. accumulation. Items are added to this dictionary, with method names as
  47. keys and C{1} as values.
  48. @type dict: L{dict}
  49. @param prefix: A native string giving a prefix. Each method of C{classObj}
  50. (and base classes of C{classObj}) with a name which begins with this
  51. prefix will be returned.
  52. @type prefix: L{str}
  53. @param baseClass: A class object at which to stop searching upwards for new
  54. methods. To collect all method names, do not pass a value for this
  55. parameter.
  56. @return: L{None}
  57. """
  58. for base in classObj.__bases__:
  59. addMethodNamesToDict(base, dict, prefix, baseClass)
  60. if baseClass is None or baseClass in classObj.__bases__:
  61. for name, method in classObj.__dict__.items():
  62. optName = name[len(prefix) :]
  63. if (
  64. (type(method) is types.FunctionType)
  65. and (name[: len(prefix)] == prefix)
  66. and (len(optName))
  67. ):
  68. dict[optName] = 1
  69. def prefixedMethods(obj, prefix=""):
  70. """
  71. Given an object C{obj}, returns a list of method objects that match the
  72. string C{prefix}.
  73. @param obj: An arbitrary object from which to collect methods.
  74. @param prefix: A native string giving a prefix. Each method of C{obj} with
  75. a name which begins with this prefix will be returned.
  76. @type prefix: L{str}
  77. @return: A list of the matching method objects.
  78. @rtype: L{list}
  79. """
  80. dct = {}
  81. accumulateMethods(obj, dct, prefix)
  82. return list(dct.values())
  83. def accumulateMethods(obj, dict, prefix="", curClass=None):
  84. """
  85. Given an object C{obj}, add all methods that begin with C{prefix}.
  86. @param obj: An arbitrary object to collect methods from.
  87. @param dict: A L{dict} which will be updated with the results of the
  88. accumulation. Items are added to this dictionary, with method names as
  89. keys and corresponding instance method objects as values.
  90. @type dict: L{dict}
  91. @param prefix: A native string giving a prefix. Each method of C{obj} with
  92. a name which begins with this prefix will be returned.
  93. @type prefix: L{str}
  94. @param curClass: The class in the inheritance hierarchy at which to start
  95. collecting methods. Collection proceeds up. To collect all methods
  96. from C{obj}, do not pass a value for this parameter.
  97. @return: L{None}
  98. """
  99. if not curClass:
  100. curClass = obj.__class__
  101. for base in curClass.__bases__:
  102. # The implementation of the object class is different on PyPy vs.
  103. # CPython. This has the side effect of making accumulateMethods()
  104. # pick up object methods from all new-style classes -
  105. # such as __getattribute__, etc.
  106. # If we ignore 'object' when accumulating methods, we can get
  107. # consistent behavior on Pypy and CPython.
  108. if base is not object:
  109. accumulateMethods(obj, dict, prefix, base)
  110. for name, method in curClass.__dict__.items():
  111. optName = name[len(prefix) :]
  112. if (
  113. (type(method) is types.FunctionType)
  114. and (name[: len(prefix)] == prefix)
  115. and (len(optName))
  116. ):
  117. dict[optName] = getattr(obj, name)
  118. def namedModule(name):
  119. """
  120. Return a module given its name.
  121. """
  122. topLevel = __import__(name)
  123. packages = name.split(".")[1:]
  124. m = topLevel
  125. for p in packages:
  126. m = getattr(m, p)
  127. return m
  128. def namedObject(name):
  129. """
  130. Get a fully named module-global object.
  131. """
  132. classSplit = name.split(".")
  133. module = namedModule(".".join(classSplit[:-1]))
  134. return getattr(module, classSplit[-1])
  135. namedClass = namedObject # backwards compat
  136. def requireModule(name, default=None):
  137. """
  138. Try to import a module given its name, returning C{default} value if
  139. C{ImportError} is raised during import.
  140. @param name: Module name as it would have been passed to C{import}.
  141. @type name: C{str}.
  142. @param default: Value returned in case C{ImportError} is raised while
  143. importing the module.
  144. @return: Module or default value.
  145. """
  146. try:
  147. return namedModule(name)
  148. except ImportError:
  149. return default
  150. class _NoModuleFound(Exception):
  151. """
  152. No module was found because none exists.
  153. """
  154. class InvalidName(ValueError):
  155. """
  156. The given name is not a dot-separated list of Python objects.
  157. """
  158. class ModuleNotFound(InvalidName):
  159. """
  160. The module associated with the given name doesn't exist and it can't be
  161. imported.
  162. """
  163. class ObjectNotFound(InvalidName):
  164. """
  165. The object associated with the given name doesn't exist and it can't be
  166. imported.
  167. """
  168. def _importAndCheckStack(importName):
  169. """
  170. Import the given name as a module, then walk the stack to determine whether
  171. the failure was the module not existing, or some code in the module (for
  172. example a dependent import) failing. This can be helpful to determine
  173. whether any actual application code was run. For example, to distiguish
  174. administrative error (entering the wrong module name), from programmer
  175. error (writing buggy code in a module that fails to import).
  176. @param importName: The name of the module to import.
  177. @type importName: C{str}
  178. @raise Exception: if something bad happens. This can be any type of
  179. exception, since nobody knows what loading some arbitrary code might
  180. do.
  181. @raise _NoModuleFound: if no module was found.
  182. """
  183. try:
  184. return __import__(importName)
  185. except ImportError:
  186. excType, excValue, excTraceback = sys.exc_info()
  187. while excTraceback:
  188. execName = excTraceback.tb_frame.f_globals["__name__"]
  189. if execName == importName:
  190. raise excValue.with_traceback(excTraceback)
  191. excTraceback = excTraceback.tb_next
  192. raise _NoModuleFound()
  193. def namedAny(name):
  194. """
  195. Retrieve a Python object by its fully qualified name from the global Python
  196. module namespace. The first part of the name, that describes a module,
  197. will be discovered and imported. Each subsequent part of the name is
  198. treated as the name of an attribute of the object specified by all of the
  199. name which came before it. For example, the fully-qualified name of this
  200. object is 'twisted.python.reflect.namedAny'.
  201. @type name: L{str}
  202. @param name: The name of the object to return.
  203. @raise InvalidName: If the name is an empty string, starts or ends with
  204. a '.', or is otherwise syntactically incorrect.
  205. @raise ModuleNotFound: If the name is syntactically correct but the
  206. module it specifies cannot be imported because it does not appear to
  207. exist.
  208. @raise ObjectNotFound: If the name is syntactically correct, includes at
  209. least one '.', but the module it specifies cannot be imported because
  210. it does not appear to exist.
  211. @raise AttributeError: If an attribute of an object along the way cannot be
  212. accessed, or a module along the way is not found.
  213. @return: the Python object identified by 'name'.
  214. """
  215. if not name:
  216. raise InvalidName("Empty module name")
  217. names = name.split(".")
  218. # if the name starts or ends with a '.' or contains '..', the __import__
  219. # will raise an 'Empty module name' error. This will provide a better error
  220. # message.
  221. if "" in names:
  222. raise InvalidName(
  223. "name must be a string giving a '.'-separated list of Python "
  224. "identifiers, not %r" % (name,)
  225. )
  226. topLevelPackage = None
  227. moduleNames = names[:]
  228. while not topLevelPackage:
  229. if moduleNames:
  230. trialname = ".".join(moduleNames)
  231. try:
  232. topLevelPackage = _importAndCheckStack(trialname)
  233. except _NoModuleFound:
  234. moduleNames.pop()
  235. else:
  236. if len(names) == 1:
  237. raise ModuleNotFound(f"No module named {name!r}")
  238. else:
  239. raise ObjectNotFound(f"{name!r} does not name an object")
  240. obj = topLevelPackage
  241. for n in names[1:]:
  242. obj = getattr(obj, n)
  243. return obj
  244. def filenameToModuleName(fn):
  245. """
  246. Convert a name in the filesystem to the name of the Python module it is.
  247. This is aggressive about getting a module name back from a file; it will
  248. always return a string. Aggressive means 'sometimes wrong'; it won't look
  249. at the Python path or try to do any error checking: don't use this method
  250. unless you already know that the filename you're talking about is a Python
  251. module.
  252. @param fn: A filesystem path to a module or package; C{bytes} on Python 2,
  253. C{bytes} or C{unicode} on Python 3.
  254. @return: A hopefully importable module name.
  255. @rtype: C{str}
  256. """
  257. if isinstance(fn, bytes):
  258. initPy = b"__init__.py"
  259. else:
  260. initPy = "__init__.py"
  261. fullName = os.path.abspath(fn)
  262. base = os.path.basename(fn)
  263. if not base:
  264. # this happens when fn ends with a path separator, just skit it
  265. base = os.path.basename(fn[:-1])
  266. modName = nativeString(os.path.splitext(base)[0])
  267. while 1:
  268. fullName = os.path.dirname(fullName)
  269. if os.path.exists(os.path.join(fullName, initPy)):
  270. modName = "{}.{}".format(
  271. nativeString(os.path.basename(fullName)),
  272. nativeString(modName),
  273. )
  274. else:
  275. break
  276. return modName
  277. def qual(clazz: Type[object]) -> str:
  278. """
  279. Return full import path of a class.
  280. """
  281. return clazz.__module__ + "." + clazz.__name__
  282. def _determineClass(x):
  283. try:
  284. return x.__class__
  285. except BaseException:
  286. return type(x)
  287. def _determineClassName(x):
  288. c = _determineClass(x)
  289. try:
  290. return c.__name__
  291. except BaseException:
  292. try:
  293. return str(c)
  294. except BaseException:
  295. return "<BROKEN CLASS AT 0x%x>" % id(c)
  296. def _safeFormat(formatter: Union[types.FunctionType, Type[str]], o: object) -> str:
  297. """
  298. Helper function for L{safe_repr} and L{safe_str}.
  299. Called when C{repr} or C{str} fail. Returns a string containing info about
  300. C{o} and the latest exception.
  301. @param formatter: C{str} or C{repr}.
  302. @type formatter: C{type}
  303. @param o: Any object.
  304. @rtype: C{str}
  305. @return: A string containing information about C{o} and the raised
  306. exception.
  307. """
  308. io = StringIO()
  309. traceback.print_exc(file=io)
  310. className = _determineClassName(o)
  311. tbValue = io.getvalue()
  312. return "<{} instance at 0x{:x} with {} error:\n {}>".format(
  313. className,
  314. id(o),
  315. formatter.__name__,
  316. tbValue,
  317. )
  318. def safe_repr(o):
  319. """
  320. Returns a string representation of an object, or a string containing a
  321. traceback, if that object's __repr__ raised an exception.
  322. @param o: Any object.
  323. @rtype: C{str}
  324. """
  325. try:
  326. return repr(o)
  327. except BaseException:
  328. return _safeFormat(repr, o)
  329. def safe_str(o: object) -> str:
  330. """
  331. Returns a string representation of an object, or a string containing a
  332. traceback, if that object's __str__ raised an exception.
  333. @param o: Any object.
  334. """
  335. if isinstance(o, bytes):
  336. # If o is bytes and seems to holds a utf-8 encoded string,
  337. # convert it to str.
  338. try:
  339. return o.decode("utf-8")
  340. except BaseException:
  341. pass
  342. try:
  343. return str(o)
  344. except BaseException:
  345. return _safeFormat(str, o)
  346. class QueueMethod:
  347. """
  348. I represent a method that doesn't exist yet.
  349. """
  350. def __init__(self, name, calls):
  351. self.name = name
  352. self.calls = calls
  353. def __call__(self, *args):
  354. self.calls.append((self.name, args))
  355. def fullFuncName(func):
  356. qualName = str(pickle.whichmodule(func, func.__name__)) + "." + func.__name__
  357. if namedObject(qualName) is not func:
  358. raise Exception(f"Couldn't find {func} as {qualName}.")
  359. return qualName
  360. def getClass(obj):
  361. """
  362. Return the class or type of object 'obj'.
  363. """
  364. return type(obj)
  365. def accumulateClassDict(classObj, attr, adict, baseClass=None):
  366. """
  367. Accumulate all attributes of a given name in a class hierarchy into a single dictionary.
  368. Assuming all class attributes of this name are dictionaries.
  369. If any of the dictionaries being accumulated have the same key, the
  370. one highest in the class hierarchy wins.
  371. (XXX: If \"highest\" means \"closest to the starting class\".)
  372. Ex::
  373. class Soy:
  374. properties = {\"taste\": \"bland\"}
  375. class Plant:
  376. properties = {\"colour\": \"green\"}
  377. class Seaweed(Plant):
  378. pass
  379. class Lunch(Soy, Seaweed):
  380. properties = {\"vegan\": 1 }
  381. dct = {}
  382. accumulateClassDict(Lunch, \"properties\", dct)
  383. print(dct)
  384. {\"taste\": \"bland\", \"colour\": \"green\", \"vegan\": 1}
  385. """
  386. for base in classObj.__bases__:
  387. accumulateClassDict(base, attr, adict)
  388. if baseClass is None or baseClass in classObj.__bases__:
  389. adict.update(classObj.__dict__.get(attr, {}))
  390. def accumulateClassList(classObj, attr, listObj, baseClass=None):
  391. """
  392. Accumulate all attributes of a given name in a class hierarchy into a single list.
  393. Assuming all class attributes of this name are lists.
  394. """
  395. for base in classObj.__bases__:
  396. accumulateClassList(base, attr, listObj)
  397. if baseClass is None or baseClass in classObj.__bases__:
  398. listObj.extend(classObj.__dict__.get(attr, []))
  399. def isSame(a, b):
  400. return a is b
  401. def isLike(a, b):
  402. return a == b
  403. def modgrep(goal):
  404. return objgrep(sys.modules, goal, isLike, "sys.modules")
  405. def isOfType(start, goal):
  406. return type(start) is goal
  407. def findInstances(start, t):
  408. return objgrep(start, t, isOfType)
  409. def objgrep(
  410. start,
  411. goal,
  412. eq=isLike,
  413. path="",
  414. paths=None,
  415. seen=None,
  416. showUnknowns=0,
  417. maxDepth=None,
  418. ):
  419. """
  420. L{objgrep} finds paths between C{start} and C{goal}.
  421. Starting at the python object C{start}, we will loop over every reachable
  422. reference, tring to find the python object C{goal} (i.e. every object
  423. C{candidate} for whom C{eq(candidate, goal)} is truthy), and return a
  424. L{list} of L{str}, where each L{str} is Python syntax for a path between
  425. C{start} and C{goal}.
  426. Since this can be slightly difficult to visualize, here's an example::
  427. >>> class Holder:
  428. ... def __init__(self, x):
  429. ... self.x = x
  430. ...
  431. >>> start = Holder({"irrelevant": "ignore",
  432. ... "relevant": [7, 1, 3, 5, 7]})
  433. >>> for path in objgrep(start, 7):
  434. ... print("start" + path)
  435. start.x['relevant'][0]
  436. start.x['relevant'][4]
  437. This can be useful, for example, when debugging stateful graphs of objects
  438. attached to a socket, trying to figure out where a particular connection is
  439. attached.
  440. @param start: The object to start looking at.
  441. @param goal: The object to search for.
  442. @param eq: A 2-argument predicate which takes an object found by traversing
  443. references starting at C{start}, as well as C{goal}, and returns a
  444. boolean.
  445. @param path: The prefix of the path to include in every return value; empty
  446. by default.
  447. @param paths: The result object to append values to; a list of strings.
  448. @param seen: A dictionary mapping ints (object IDs) to objects already
  449. seen.
  450. @param showUnknowns: if true, print a message to C{stdout} when
  451. encountering objects that C{objgrep} does not know how to traverse.
  452. @param maxDepth: The maximum number of object references to attempt
  453. traversing before giving up. If an integer, limit to that many links,
  454. if C{None}, unlimited.
  455. @return: A list of strings representing python object paths starting at
  456. C{start} and terminating at C{goal}.
  457. """
  458. if paths is None:
  459. paths = []
  460. if seen is None:
  461. seen = {}
  462. if eq(start, goal):
  463. paths.append(path)
  464. if id(start) in seen:
  465. if seen[id(start)] is start:
  466. return
  467. if maxDepth is not None:
  468. if maxDepth == 0:
  469. return
  470. maxDepth -= 1
  471. seen[id(start)] = start
  472. # Make an alias for those arguments which are passed recursively to
  473. # objgrep for container objects.
  474. args = (paths, seen, showUnknowns, maxDepth)
  475. if isinstance(start, dict):
  476. for k, v in start.items():
  477. objgrep(k, goal, eq, path + "{" + repr(v) + "}", *args)
  478. objgrep(v, goal, eq, path + "[" + repr(k) + "]", *args)
  479. elif isinstance(start, (list, tuple, deque)):
  480. for idx, _elem in enumerate(start):
  481. objgrep(start[idx], goal, eq, path + "[" + str(idx) + "]", *args)
  482. elif isinstance(start, types.MethodType):
  483. objgrep(start.__self__, goal, eq, path + ".__self__", *args)
  484. objgrep(start.__func__, goal, eq, path + ".__func__", *args)
  485. objgrep(start.__self__.__class__, goal, eq, path + ".__self__.__class__", *args)
  486. elif hasattr(start, "__dict__"):
  487. for k, v in start.__dict__.items():
  488. objgrep(v, goal, eq, path + "." + k, *args)
  489. elif isinstance(start, weakref.ReferenceType):
  490. objgrep(start(), goal, eq, path + "()", *args)
  491. elif isinstance(
  492. start,
  493. (
  494. str,
  495. int,
  496. types.FunctionType,
  497. types.BuiltinMethodType,
  498. RegexType,
  499. float,
  500. type(None),
  501. IOBase,
  502. ),
  503. ) or type(start).__name__ in (
  504. "wrapper_descriptor",
  505. "method_descriptor",
  506. "member_descriptor",
  507. "getset_descriptor",
  508. ):
  509. pass
  510. elif showUnknowns:
  511. print("unknown type", type(start), start)
  512. return paths
  513. __all__ = [
  514. "InvalidName",
  515. "ModuleNotFound",
  516. "ObjectNotFound",
  517. "QueueMethod",
  518. "namedModule",
  519. "namedObject",
  520. "namedClass",
  521. "namedAny",
  522. "requireModule",
  523. "safe_repr",
  524. "safe_str",
  525. "prefixedMethodNames",
  526. "addMethodNamesToDict",
  527. "prefixedMethods",
  528. "accumulateMethods",
  529. "fullFuncName",
  530. "qual",
  531. "getClass",
  532. "accumulateClassDict",
  533. "accumulateClassList",
  534. "isSame",
  535. "isLike",
  536. "modgrep",
  537. "isOfType",
  538. "findInstances",
  539. "objgrep",
  540. "filenameToModuleName",
  541. "fullyQualifiedName",
  542. ]
  543. # This is to be removed when fixing #6986
  544. __all__.remove("objgrep")