
"""Run a simple trace function on a file of Python code."""

import os, sys

nest = 0

def trace(frame, event, arg):
    global nest

    if nest is None:
        # This can happen when Python is shutting down.
        return None

    print("%s%s %s %d @%d" % (
        "    " * nest,
        event,
        os.path.basename(frame.f_code.co_filename),
        frame.f_lineno,
        frame.f_lasti,
        ))

    if event == 'call':
        nest += 1
    if event == 'return':
        nest -= 1

    return trace

the_program = sys.argv[1]

sys.settrace(trace)
execfile(the_program)


"""Run a simple trace function on a file of Python code."""

import os, sys

nest = 0

def trace(frame, event, arg):
    global nest

    if nest is None:
        # This can happen when Python is shutting down.
        return None

    print("%s%s %s %d @%d" % (
        "    " * nest,
        event,
        os.path.basename(frame.f_code.co_filename),
        frame.f_lineno,
        frame.f_lasti,
        ))

    if event == 'call':
        nest += 1
    if event == 'return':
        nest -= 1

    return trace

the_program = sys.argv[1]

code = open(the_program).read()
sys.settrace(trace)
exec(code)

Make this useful for py3 also
Make this useful for py3 also

  
  """Run a simple trace function on a file of Python code."""
  
  import os, sys
  
  nest = 0
  
  def trace(frame, event, arg):
      global nest
  
      if nest is None:
          # This can happen when Python is shutting down.
          return None
  
      print("%s%s %s %d @%d" % (
          "    " * nest,
          event,
          os.path.basename(frame.f_code.co_filename),
          frame.f_lineno,
          frame.f_lasti,
          ))
  
      if event == 'call':
          nest += 1
      if event == 'return':
          nest -= 1
  
      return trace
  
  the_program = sys.argv[1]
  
+ code = open(the_program).read()
  sys.settrace(trace)
- execfile(the_program)
+ exec(code)
  
Make this useful for py3 also
## Code Before:

"""Run a simple trace function on a file of Python code."""

import os, sys

nest = 0

def trace(frame, event, arg):
    global nest

    if nest is None:
        # This can happen when Python is shutting down.
        return None

    print("%s%s %s %d @%d" % (
        "    " * nest,
        event,
        os.path.basename(frame.f_code.co_filename),
        frame.f_lineno,
        frame.f_lasti,
        ))

    if event == 'call':
        nest += 1
    if event == 'return':
        nest -= 1

    return trace

the_program = sys.argv[1]

sys.settrace(trace)
execfile(the_program)

## Instruction:
Make this useful for py3 also
## Code After:

"""Run a simple trace function on a file of Python code."""

import os, sys

nest = 0

def trace(frame, event, arg):
    global nest

    if nest is None:
        # This can happen when Python is shutting down.
        return None

    print("%s%s %s %d @%d" % (
        "    " * nest,
        event,
        os.path.basename(frame.f_code.co_filename),
        frame.f_lineno,
        frame.f_lasti,
        ))

    if event == 'call':
        nest += 1
    if event == 'return':
        nest -= 1

    return trace

the_program = sys.argv[1]

code = open(the_program).read()
sys.settrace(trace)
exec(code)
