[Twisted-Python] Passing arguments to protocol instances (was Re: Testing AMP-based code)

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Jul 17 05:14:55 MDT 2013


On 16 Jul, 07:13 pm, marchael at ya.ru wrote:
><exarkun <at> twistedmatrix.com> writes:
>>I think this is thinking in the right direction.  Twisted generally
>>tries to be responsible for testing its own code, and the 
>>serialization
>>from commands to bytes (and the reverse) that AMP does is part of
>>Twisted, so you should really be free from the burden of testing that
>>that stuff works.
>>
>>....
>>
>>Some pieces are probably still missing from the public API - for
>>example, you do want to test that your objects all get properly
>>serialized and deserialized through AMP, particularly if you're
>>implementing custom Argument types.  There are some private APIs,
>>_objectsToStrings and _stringsToObjects mostly, that really help with
>>testing this, and we should think about how to expose this 
>>functionality
>>publically.  Also, we should document this whole pile of stuff.  Maybe
>>you'd be interested in writing something up after you've had a chance 
>>to
>>play with these ideas?
>>
>>Jean-Paul
>
>
>Greetings!
>
>Perhaps I should create a new thread, but I think that my problem could 
>be
>related with this question.
>
>I want to implement master/slave protocol based on AMP, here is a slave 
>side
>code https://github.com/MichaelMayorov/buildbot/compare/fake-master- 
>slave-
>amp
>
>The problem is that I want to pass few arguments to Bot
>https://github.com/MichaelMayorov/buildbot/compare/fake-master-slave-
>amp#L0R38 from this place
>https://github.com/MichaelMayorov/buildbot/compare/fake-master-slave-
>amp#L0R124
>but current implementation not allow me to do this. I've read twisted 
>API
>docs and discussed my idea on #buildbot where finally take advice to 
>post my
>question here.

There are a lot of ways to go about this.  The smallest change required 
to pass an argument to the `Bot` protocol in your code is to use a 
lambda:

    factory = Factory()
    factory.protocol = lambda: Bot(arg1, arg2, ...)

This isn't necessarily the best solution.  Some other options include:

  - use the newly introduced Factory.forProtocol(Bot, arg1, arg2, ...)
  - use connectProtocol(endpoint, Bot(arg1, arg2, ...)) (only slightly 
less new)

Jean-Paul



More information about the Twisted-Python mailing list