Finding the full path in a Python trace

When Python tracing is enabled, a file name is provided along with the module and source code.

Can I specify the path to the file, as well as the file name?

I use:

-m trace -t

In the example below, in different directories there are two different files account_bank_statement.py .

17  --- modulename: account_bank_statement, funcname: button_create_invoice
18 account_bank_statement.py(329):         if context is None:
19 account_bank_statement.py(333):         currency =  self.read(cr, uid, ids, ['currency'])[0]['currency']
20  --- modulename: account_bank_statement, funcname: _currency
21 account_bank_statement.py(107):         res = {}
22 account_bank_statement.py(108):         res_currency_obj = self.pool.get('res.currency')

This is a duplicate of this (unanswered) question: Tracing the path and line number

An answer that will require hacking the trace module will work for me.

EDIT

Solution based on Alpha answer below. is heobsessive, but does what I'm looking for. I left the module name and added the path. I work with OpenERP, and often the same module name is used in several places.

, Alfe, , , , .

(1) trace.py (2) , :

171 def modname(path):
172     """Return a plausible module name for the patch."""
173 
174     base = os.path.basename(path)
175     filename, ext = os.path.splitext(base)
176     return filename

593     def globaltrace_lt(self, frame, why, arg):
594         """Handler for call events.
595 
596         If the code block being entered is to be ignored, returns `None',
597         else returns self.localtrace.
598         """
599         if why == 'call':
600             code = frame.f_code
601             filename = frame.f_globals.get('__file__', None)
602             if filename:
603                 # XXX modname() doesn't work right for packages, so
604                 # the ignore support won't work right for packages
605                 #modulename = fullmodname(filename)
606                 modfile, ext = os.path.splitext(filename)
607                 modulename = fullmodname(modfile)
608                 if modulename is not None:
609                     ignore_it = self.ignore.names(modfile, modulename)
610                     if not ignore_it:
611                         if self.trace:
612                             print (" --- modulename: %s, funcname: %s, filename: %s"
613                                    % (modulename, code.co_name, filename))
614                         return self.localtrace
615             else:
616                 return None

, , , . * trace.py ** .

2  --- modulename: register_accounting, funcname: button_create_invoice, filename: /home/sean/unifield/utp729/unifield-wm/register_accounting/account_bank_statement.pyc
3 account_bank_statement.py(329):         if context is None:
4 account_bank_statement.py(333):         currency =  self.read(cr, uid, ids, ['currency'])[0]['currency']
5  --- modulename: account, funcname: _currency, filename: /home/sean/unifield/utp729/unifield-addons/account/account_bank_statement.pyc
6 account_bank_statement.py(107):         res = {}
7 account_bank_statement.py(108):         res_currency_obj = self.pool.get('res.currency')
+2
1

trace.py , .

trace.py ( /usr/lib/python2.7/ ) (, ), modname(path) . , .

filename, ext = os.path.splitext(base)

filename, ext = os.path.splitext(path)

.

./trace.py --trace t.py :

 --- modulename: t, funcname: <module>
t.py(3): import mypackage.mymodule
 --- modulename: mypackage/__init__, funcname: <module>
__init__.py(1):   --- modulename: mypackage/mymodule, funcname: <module>
mymodule.py(1): print 42
42
t.py(5): print 5
5
 --- modulename: ./trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

script t.py, mymodule.py, mypackage ( ./mypackage/mymodule.py). 42, script 5.

?

EDIT:

.

globaltrace_lt() modname(); , fullmodname():

            modulename = fullmodname(filename)

, .

+2

All Articles