Problems with telnetlib

Bengt Richter bokr at oz.net
Sat Feb 16 21:07:28 CET 2002


On 16 Feb 2002 18:08:10 +0100, Jan-Fredrik Braseth <janfbr at sex.ifi.uio.no> wrote:

>Sheila King <usenet at thinkspot.net> writes:
>
>> On 16 Feb 2002 13:56:35 +0100, Jan-Fredrik Braseth
>> <janfbr at sex.ifi.uio.no> wrote in comp.lang.python in article
>> <xyuvgcxlh4s.fsf at sex.ifi.uio.no>:
>> 
>> >  but after some time I get this error message:
>> > 
>> > Traceback (most recent call last):
>> >   File "./quizbot.py", line 366, in ?
>> >     foo = QuizBot(name,password,channel)
>> >   File "./quizbot.py", line 28, in __init__
>> >     line = self.tn.read_until("\n").split()
>> >   File "/usr/lib/python2.2/telnetlib.py", line 297, in read_until
>> >     self.process_rawq()
>> >   File "/usr/lib/python2.2/telnetlib.py", line 424, in process_rawq
>> >     self.msg('IAC %d not recognized' % ord(opt))
>> > UnboundLocalError: local variable 'opt' referenced before assignment
>> > > 
>> You have an error in your code. Show us the code. I'm guessing, that you
>> have a variable 'opt' that under some circumstances is not defined
>> before entering a particular loop or something.
>
>If you look closely at the traceback you see that the error with opt is in the
>telnetlib.py, that's a module for python. But it's unlikely that there is a bug
>in python itself, isn't it?
>
>BTW: it's python 2.2
>
I am not familiar with the code, but from a cursory look ISTM
there is a bug, and UnBoundLocalError nails it well.
I suspect line 424
    self.msg('IAC %d not recognized' % ord(opt))
should be
    self.msg('IAC %d not recognized' % ord(c))
since lacking a recognized value for c for the character gotten
after IAC, opt does not seem to be be set:
(snip from Python22\lib\telnetlib.py)
---------
                if c != IAC:
                    buf = buf + c
                    continue
                c = self.rawq_getchar()
                if c == IAC:
                    buf = buf + c
                elif c in (DO, DONT):
                    opt = self.rawq_getchar()
# ok here
                    self.msg('IAC %s %d', c == DO and 'DO' or 'DONT', ord(opt))
                    if self.option_callback:
                        self.option_callback(self.sock, c, opt)
                    else:
                        self.sock.send(IAC + WONT + opt)
                elif c in (WILL, WONT):
                    opt = self.rawq_getchar()
# ok here
                    self.msg('IAC %s %d',
                             c == WILL and 'WILL' or 'WONT', ord(opt))
                    if self.option_callback:
                        self.option_callback(self.sock, c, opt)
                    else:
                        self.sock.send(IAC + DONT + opt)
                else:
# not ok here: c seems to be the problem. opt would come from the *next* char,
# stored by  opt = self.rawq_getchar(), but that isn't happening here,
# and printing opt is not appropriate.
# I would change opt to c in the following line
                    self.msg('IAC %d not recognized' % ord(opt))
---------

HTH get to the real problem ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list