[Tutor] parsing pyc files

Steven D'Aprano steve at pearwood.info
Tue Mar 23 12:44:02 CET 2010


On Tue, 23 Mar 2010 12:16:03 pm Jojo Mwebaze wrote:
> Hello Tutor
>
> I have two problems, any help will be highly appreciated.
>
> How is possible to trace the all method calls, object instantiations,
> variables used in running an experiment dynamically,  without putting
> print - or log statements in my code?  - some sort of provenance!

Look at the profile module for starters.


>>> def test(x):
...     y = -42
...     for i in xrange(10000):
...             y += func(i)
...     return x + y
...
>>> def func(x):
...     return x
...
>>>
>>> import profile
>>> profile.run("test(3)")
         10004 function calls in 0.286 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.002    0.002    0.002    0.002 :0(setprofile)
    10000    0.145    0.000    0.145    0.000 <stdin>:1(func)
        1    0.139    0.139    0.284    0.284 <stdin>:1(test)
        1    0.000    0.000    0.284    0.284 <string>:1(<module>)
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.286    0.286 profile:0(test(3))




> I would like to create a tool that can look into pyc files to find
> classes/methods that was executed without looking the the source
> code. Is this possible in python.

Look at the dis module.


>>> import dis
>>> dis.dis(test)
  2           0 LOAD_CONST               1 (-42)
              3 STORE_FAST               1 (y)

  3           6 SETUP_LOOP              36 (to 45)
              9 LOAD_GLOBAL              0 (xrange)
             12 LOAD_CONST               2 (10000)
             15 CALL_FUNCTION            1
             18 GET_ITER
        >>   19 FOR_ITER                22 (to 44)
             22 STORE_FAST               2 (i)

  4          25 LOAD_FAST                1 (y)
             28 LOAD_GLOBAL              1 (func)
             31 LOAD_FAST                2 (i)
             34 CALL_FUNCTION            1
             37 INPLACE_ADD
             38 STORE_FAST               1 (y)
             41 JUMP_ABSOLUTE           19
        >>   44 POP_BLOCK

  5     >>   45 LOAD_FAST                0 (x)
             48 LOAD_FAST                1 (y)
             51 BINARY_ADD
             52 RETURN_VALUE






-- 
Steven D'Aprano


More information about the Tutor mailing list