Opened 6 weeks ago

Closed 5 weeks ago

#10259 defect closed invalid (invalid)

IRC protocol not connecting in Python 3.9

Reported by: darnti Owned by:
Priority: normal Milestone:
Component: words Keywords:
Cc: Branch:
Author:

Description

The following code:

from twisted.internet import reactor, protocol, defer
from twisted.words.protocols import irc

class Bot(irc.IRCClient):
    def _get_nickname(self):
        return self.factory.nickname
    def _get_username(self):
        return self.factory.username
    def _get_realname(self):
        return self.factory.realname
    def _get_versionname(self):
        return self.factory.versionName

    nickname = property(_get_nickname)
    username = property(_get_username)
    realname = property(_get_realname)
    versionName = property(_get_versionname)


    def signedOn(self):
        print("Signed on as %s." % self.nickname)
        print("Logged in.")
        print("Joining channels...")
        reactor.callLater(2, self.join, self.factory.channel)


class BotFactory(protocol.ClientFactory):
    global bot_nickname, bot_channel, server_ipaddress

    protocol = Bot

    def __init__(self, channel, nickname='EnterNickHere'):
        self.channel = "#bot-test"
        self.nickname = "EnterNickHere"
        self.realname = 'Bot Nick'
        self.username = 'botnick'
        self.versionName = "Twisted IRC"
        
if __name__ == "__main__":

    reactor.connectTCP("server address here", 6667, BotFactory("#bot-test"))

reactor.run()

Produces the following error:

Unhandled Error
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\twisted\internet\tcp.py", line 252, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "C:\Python39\lib\site-packages\twisted\words\protocols\irc.py", line 2616, in dataReceived
    basic.LineReceiver.dataReceived(self, data)
  File "C:\Python39\lib\site-packages\twisted\protocols\basic.py", line 548, in dataReceived
    why = self.lineReceived(line)
  File "C:\Python39\lib\site-packages\twisted\words\protocols\irc.py", line 2628, in lineReceived
    self.handleCommand(command, prefix, params)
--- <exception caught here> ---
  File "C:\Python39\lib\site-packages\twisted\words\protocols\irc.py", line 2680, in handleCommand
    method(prefix, params)
  File "C:\Python39\lib\site-packages\twisted\words\protocols\irc.py", line 1958, in irc_RPL_WELCOME
    self.nickname = self._attemptedNick
builtins.AttributeError: can't set attribute

Note that this code works with the lastest twisted release for Python 2.7.

Attachments (1)

bot-connect.py (1.3 KB) - added by darnti 6 weeks ago.

Download all attachments as: .zip

Change History (2)

Changed 6 weeks ago by darnti

Attachment: bot-connect.py added

comment:1 Changed 5 weeks ago by Tom Most

Resolution: invalid
Status: newclosed

It looks like you're defining a read-only property here:

    nickname = property(_get_nickname)

The exception indicates that the superclass is using an attribute of the same name.

I'd guess, but haven't checked, that this worked in Python 2.7 because IRCClient was an old-style class and descriptors like property were weird. It won't work as written in Python 3. If you run the Python 2.7 version with the TWISTED_NEWSTYLE=1 environment variable set, which forces all Twisted classes to be new-style, I expect you'll get the same result.

Since this is a Python 3 porting issue in user code rather than a bug in Twisted itself I'm closing it as "invalid". You'll have to make some changes for it to run under current versions of Python. Feel free to reach out on the mailing list for porting advice, and thanks for using Twisted!

Note: See TracTickets for help on using tickets.