[Tutor] Scope Problem with Files
bob
bgailer at alum.rpi.edu
Wed Dec 14 23:00:03 CET 2005
At 11:13 AM 12/14/2005, Carroll, Barry wrote:
>Greetings:
>
>I am implementing a (crude but useful) debug facility in my test
>system client software. Basically, I test the value of a global
>Boolean. It True, I write pertinent data to a text file. I want to
>do this in multiple functions in a module. Rather than open and
>close the file each time I write, I want to open the file once at
>the start of process and close it at the end. Here are excerpts
>from the module.
>
>##########################
>import socket
>import struct
>import time
>
># initialize the debug flag
>DEBUG = True
>. . .
>
>dbgf = None # File object and path for saving debug output
>dbgfname = "debug.txt"
>
>def snd_cmd(sock,cmd):
>
> . . .
>
> while remainlen > 0:
> if remainlen > MTU:
> pktstp = pktstrt + MTU
> else:
> pktstp = pktlen
> pktflags |= EOD
>
> pkthdr = struct.pack('@2BH',pktflags,seq,pktlen)
> sndpkt = pkthdr+cmd[pktstrt:pktstp]
>
> if DEBUG:
> dbgf.write("command: " + cmd + "\n")
> dbgf.write("flags: 0x%X, seq: %u, len: %u\n" %
> (pktflags, seq, pktlen))
>
> sock.sendto(sndpkt,addr)
>
> pktstrt += MTU
> remainlen -= MTU
> pktflags = pktflags & ~SOD
> seq = (seq + 1) % 256
>
> . . .
>
>def recv_resp(sock):
> response = ''
> try:
> response = sock.recv(MTU)
> except socket.timeout:
> errtupl = ("ERROR", 'Server did not respond')
> return (errtupl, response)
>
> . . .
>
> if DEBUG:
> dbgf.write("response: " + response + "\n")
> dbgf.write("flags: 0x%X, seq: %u, len: %u\n" % (flags, retseq, dlen))
>
> . . .
>
> return (errtupl, response)
>
>def do_cmd(cmd):
>
> sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> sock.settimeout(timetowait)
> retriesleft = retries
> if DEBUG:
> dbgf = open(dbgfname,mode="a")
dbgf is a local variable. If you want to reassign to the global you must add
global dbgf
to the function
> dbgf.write("\n"+str(time.localtime())+"\n")
>
> while retriesleft > 0:
> snd_cmd(sock, cmd)
> recvtupl = recv_resp(sock)
> if recvtupl[0][0] != "ERROR":
> break
> retriesleft -= 1
>
> if DEBUG:
> dbgf.close()
>
> sock.close( )
> return recvtupl
>##########################
>
>When I run this code, I get the following error message:
>
><<<<<<<<<<<<<<<<<<<<<<<
>A problem occurred in a Python script. Here is the sequence of
>function calls leading up to the error, in the order they occurred.
>/var/www/cgi-bin/pagen.py
> 76 # function. Check the returned error code for success.
> 77 cmdtosnd = state['s']['getcmd']
> *****78 (errorcode, errorstr),
> platformstate['itype']['curstate'] = do_cmd(cmdtosnd)
> 79 if errorcode == 0:
> 80 cmdtosnd = state['t']['getcmd']
>
> . . .
>
>/var/www/cgi-bin/Client.py in do_cmd(cmd='cmd')
> 160
> 161 while retriesleft > 0:
> *****162 snd_cmd(sock, cmd)
> 163 recvtupl = recv_resp(sock)
> 164 if recvtupl[0][0] != IIPSRVERROR:
>
>global snd_cmd = <function snd_cmd>, sock = <socket._socketobject
>object>, cmd = 'cmd'
>
>
> /var/www/cgi-bin/Client.py in snd_cmd(sock=<socket._socketobject
> object>, cmd='cmd')
> 65
> 66 if DEBUG:
> *****67 dbgf.write("command: " + cmd + "\n")
> 69
>
>global dbgf = None, dbgf.write undefined, cmd = 'cmd'
>
>
>AttributeError: 'NoneType' object has no attribute 'write'
> args = ("'NoneType' object has no attribute 'write'",)
> >>>>>>>>>>>>>>>>>>>>>>>
>
>dbgf is declared at the top of the module. It is opened and closed
>in do_cmd. I attempt to write to it in snd_cmd and recv_resp, both
>of which are called by do_cmd. Since dbgf is global to all of these
>functions, I expected its value (the open file object) to
>persist. I don't understand why it didn't. I expect I have
>misunderstood Python's scoping rules. Can someone enlighten me?
>
>Thanks and enjoy the holidays.
>
>BGC
>________________________
>"Never trust anything that can think for itself
>if you can't see where it keeps its brain"
>JK Rowling
>
>
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list