Skip to content

Commit

Permalink
logHandler: Refactor code to get the fully qualified module name so t…
Browse files Browse the repository at this point in the history
…hat it doesn't fail when the module is on a drive different to NVDA itself.

This code works, but the check for external paths isn't optimised and will break in some cases.
Re #3370.
  • Loading branch information
jcsteh committed Aug 14, 2013
1 parent 351d042 commit cc034be
Showing 1 changed file with 9 additions and 31 deletions.
40 changes: 9 additions & 31 deletions source/logHandler.py
Expand Up @@ -24,44 +24,22 @@
RPC_E_CALL_REJECTED = -2147418111
RPC_E_DISCONNECTED = -2147417848

moduleCache={}

def makeModulePathFromFilePath(path):
"""calculates the pythonic dotted module path from a file path of a python module.
@param path: the relative or absolute path to the module
@type path: string
@returns: the Pythonic dotted module path
@rtype: string
"""
if path in moduleCache:
return moduleCache[path]
modPathList = []
# Work through the path components from right to left.
curPath = path
while curPath:
curPath, curPathCom = os.path.split(curPath)
if not curPathCom:
break
curPathCom = os.path.splitext(curPathCom)[0]
# __init__ is the root module of a package, so skip it.
if curPathCom != "__init__":
modPathList.insert(0, curPathCom)
if curPath in sys.path:
# curPath is in the Python search path, so the Pythonic module path is relative to curPath.
break
modulePath = ".".join(modPathList)
if modulePath:
moduleCache[path] = modulePath
return modulePath

def getCodePath(f):
"""Using a frame object, gets its module path (relative to the current directory).[className.[funcName]]
@param f: the frame object to use
@type f: frame
@returns: the dotted module.class.attribute path
@rtype: string
"""
path=makeModulePathFromFilePath(os.path.relpath(f.f_code.co_filename))
fn=f.f_code.co_filename
if fn[0] != "<" and (not fn.startswith(sys.path[0] + "\\") or fn.startswith(globalVars.appArgs.configPath + "\\")):
path="external:"
else:
path=""
try:
path+=f.f_globals["__name__"]
except KeyError:
path+=fn
funcName=f.f_code.co_name
if funcName.startswith('<'):
funcName=""
Expand Down

0 comments on commit cc034be

Please sign in to comment.