profile.run won't run!

Nick Perkins nperkins7 at home.com
Fri Jun 15 17:28:45 EDT 2001


"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:EZpW6.288352$oc7.19519615 at news2.rdc2.tx.home.com...

> My mistake; this should be:
>
>   profile.run('solve_problem()')

Thanks again, i thought so, and tried that, too.

.. but it still won't run.
I get an Unbound Local error at the first imported function..

Here is the error, and the relevant code, if anyone can help...



Traceback (most recent call last):
  File "e:\komodo\mozilla\chrome\komodo\content\python\kdb.py", line 640, in
?
    requester.start(sys.argv[arg_num:])
  File "e:\komodo\mozilla\chrome\komodo\content\python\kdb.py", line 570, in
start
    return Requester.start(self, debug_args)
  File "e:\komodo\mozilla\chrome\komodo\content\python\kdb.py", line 256, in
start
    self._do_start(debug_args)
  File "e:\komodo\mozilla\chrome\komodo\content\python\kdb.py", line 286, in
_do_start
    self.kdb.run(code_ob, locals, locals)
  File "c:\python21\lib\bdb.py", line 351, in run
    exec cmd in globals, locals
  File "E:\Komodo\python\my\profile_soko.py", line 46, in ?
    profile.run('solve_problem()')
  File "c:\python21\lib\profile.py", line 71, in run
    prof = prof.run(statement)
  File "c:\python21\lib\profile.py", line 356, in run
    return self.runctx(cmd, dict, dict)
  File "c:\python21\lib\profile.py", line 362, in runctx
    exec cmd in globals, locals
  File "<string>", line 1, in ?
  File "E:\Komodo\python\my\profile_soko.py", line 21, in solve_problem
    solv = sokoban.solver_class(filename)
  File "E:\Komodo\python\my\sokoban.py", line 413, in __init__
    self.board, blocks, soko  = load_soko(filename)
  File "E:\Komodo\python\my\sokoban.py", line 358, in load_soko
    file = open(file_name,'r')
UnboundLocalError: local variable 'file_name' referenced before assignment
========== Python debug run finished. ==========


here is the part that chokes:


--------:file:profile_soko.py:-----------

import sokoban
import sys, time
import profile

filename = sys.argv[1]  <= not the problem, arg is passing ok..

def solve_problem():
    print 'you chose: ', filename     <= works during profile run...

    solv = sokoban.solver_class(filename)   <= chokes here, caused by
imported sokoban.py

    start_time = time.time()
    solution = solv.find_solution()
    elapsed_time = time.time()-start_time

    print filename
    print elapsed_time, 'seconds'
    print solution



print 'direct:'
solve_problem()

import __main__
__main__.solve_problem = solve_problem

print 'profiled:'
profile.run('solve_problem()')


--------------::file sokoban.py::-------------------------------
...

def load_soko(file_name):

    file = open(file_name,'r')    <== line 358: Unbound Local error:
'file_name'
    lines = file.readlines()                ( what's up with that???)
    file.close()

    ... more stuff..


class solver_class:
    def __init__(self,filename):

        self.board, blocks, soko  = load_soko(filename)

        ...more stuff...


It also still works if I call exec('solve_problem()'),
but not if call profile.run('solve_problem()').


...dazed and confuzed...  --Nick Perkins.






More information about the Python-list mailing list