Debian Codegraph @

src/venusian/advice.py

raw
1 ##############################################################################
2 #
3 # Copyright (c) 2003 Zope Corporation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 """Class advice.
15
16 This module was adapted from 'protocols.advice', part of the Python
17 Enterprise Application Kit (PEAK). Please notify the PEAK authors
18 ([email protected] and [email protected]) if bugs are found or
19 Zope-specific changes are required, so that the PEAK version of this module
20 can be kept in sync.
21
22 PEAK is a Python application framework that interoperates with (but does
23 not require) Zope 3 and Twisted. It provides tools for manipulating UML
24 models, object-relational persistence, aspect-oriented programming, and more.
25 Visit the PEAK home page at http://peak.telecommunity.com for more information.
26
27 $Id: advice.py 25177 2004-06-02 13:17:31Z jim $
28 """
29
30 import inspect
31 import sys
32
33
34 def getFrameInfo(frame):
35 """Return (kind,module,locals,globals) for a frame
36
37 'kind' is one of "exec", "module", "class", "function call", or "unknown".
38 """
39
40 f_locals = frame.f_locals
41 f_globals = frame.f_globals
42
43 sameNamespace = f_locals is f_globals
44 hasModule = "__module__" in f_locals
45 hasName = "__name__" in f_globals
46
47 sameName = hasModule and hasName
48 sameName = sameName and f_globals["__name__"] == f_locals["__module__"]
49
50 module = hasName and sys.modules.get(f_globals["__name__"]) or None
51
52 namespaceIsModule = module and module.__dict__ is f_globals
53
54 frameinfo = inspect.getframeinfo(frame)
55 try:
56 sourceline = frameinfo[3][0].strip()
57 except: # pragma NO COVER
58 # dont understand circumstance here, 3rdparty code without comment
59 sourceline = frameinfo[3]
60
61 codeinfo = frameinfo[0], frameinfo[1], frameinfo[2], sourceline
62
63 if not namespaceIsModule: # pragma no COVER
64 # some kind of funky exec
65 kind = "exec" # don't know how to repeat this scenario
66 elif sameNamespace and not hasModule:
67 kind = "module"
68 elif sameName and not sameNamespace:
69 kind = "class"
70 elif not sameNamespace:
71 kind = "function call"
72 else: # pragma NO COVER
73 # How can you have f_locals is f_globals, and have '__module__' set?
74 # This is probably module-level code, but with a '__module__' variable.
75 kind = "unknown"
76 return kind, module, f_locals, f_globals, codeinfo