Menu

#15 Exception when using kwargs for a topic listener

v3.2.0
closed
None
1
2016-10-21
2014-03-11
RediZ
No

Hi, I'm using PyPubSub v3.3 and the example declared here [1] gives me an error. (see below the whole trace)

http://pubsub.sourceforge.net/usage/usage_advanced_other.html#receiving-all-data-of-a-topic

===== Trace =====
$ python
Python 2.7.6 (default, Jan  6 2014, 12:07:35) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pubsub import pub
>>> def listener(**kwargs): print 'listener: ', kwargs
... 
>>> pub.subscribe(listener, 'topic')
(<pubsub.core.listenerimpl.Listener instance="" at="" 0x1069f3710="">, True)
>>> pub.sendMessage('topic', test='foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pubsub/core/kwargs/publisher.py", line 27, in sendMessage
    topicObj.publish(**kwargs)
  File "pubsub/core/kwargs/publishermixin.py", line 24, in publish
    self._publish(msgKwargs)
  File "pubsub/core/topicobj.py", line 375, in _publish
    iterState = self._mix_prePublish(data)
  File "pubsub/core/kwargs/publishermixin.py", line 49, in _mix_prePublish
    iterState.checkMsgArgs( self._getListenerSpec() )
  File "pubsub/core/kwargs/publishermixin.py", line 34, in checkMsgArgs
    spec.check(self.filteredArgs)
  File "pubsub/core/kwargs/topicargspecimpl.py", line 125, in check
    py2and3.keys(msgKwargs), optional - set(self.allOptional) )
pubsub.core.topicargspecimpl.SenderUnknownMsgDataError: Some optional args unknown in call to sendMessage('('topic',)', test): test


Related

Bugs: #15

Discussion

  • schoenborno

    schoenborno - 2014-03-22
    • status: open --> accepted
    • assigned_to: schoenborno
     
    • RediZ

      RediZ - 2014-03-22

      This was on osx Mavericks, but I imagine the behavior should be consistent
      across this version of python (?)
      On Mar 22, 2014 9:24 AM, "schoenborno" schoenborno@users.sf.net wrote:

      • status: open --> accepted
      • assigned_to: schoenborno
      • Comment:

      Interesting, because this functionality is tested in the
      root/tests/pubsub_kwargs/test1_listener.py and
      tests\pubsub_kwargs\test3c_pubsub3.py and thoses tests pass on Windows,
      OSXhttp://jeromelaheurte.net:8200/builders/osx/builds/55/steps/tests/logs/stdioand Linux. What is you platform, the python header indicates Darwin can you
      get specifically what it is?


      Status: accepted
      Group: v3.2.0
      Created: Tue Mar 11, 2014 11:22 PM UTC by RediZ
      Last Updated: Tue Mar 11, 2014 11:22 PM UTC
      Owner: schoenborno

      Hi, I'm using PyPubSub v3.3 and the example declared here [1] gives me an
      error. (see below the whole trace)

      http://pubsub.sourceforge.net/usage/usage_advanced_other.html#receiving-all-data-of-a-topic

      ===== Trace =====
      $ python
      Python 2.7.6 (default, Jan 6 2014, 12:07:35)
      [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.

      from pubsub import pub
      def listener(**kwargs): print 'listener: ', kwargs
      ...
      pub.subscribe(listener, 'topic')
      (, True)
      pub.sendMessage('topic', test='foo')
      Traceback (most recent call last):
      File "", line 1, in
      File "pubsub/core/kwargs/publisher.py", line 27, in sendMessage
      topicObj.publish(**kwargs)
      File "pubsub/core/kwargs/publishermixin.py", line 24, in publish
      self._publish(msgKwargs)
      File "pubsub/core/topicobj.py", line 375, in _publish
      iterState = self._mix_prePublish(data)
      File "pubsub/core/kwargs/publishermixin.py", line 49, in _mix_prePublish
      iterState.checkMsgArgs( self._getListenerSpec() )
      File "pubsub/core/kwargs/publishermixin.py", line 34, in checkMsgArgs
      spec.check(self.filteredArgs)
      File "pubsub/core/kwargs/topicargspecimpl.py", line 125, in check
      py2and3.keys(msgKwargs), optional - set(self.allOptional) )
      pubsub.core.topicargspecimpl.SenderUnknownMsgDataError: Some optional args unknown in call to sendMessage('('topic',)', test): test


      Sent from sourceforge.net because you indicated interest in

      To unsubscribe from further messages, please visit

       

      Related

      Bugs: #15


      Last edit: schoenborno 2014-03-22
  • schoenborno

    schoenborno - 2014-03-22

    Interesting, because this functionality is tested in the root/tests/pubsub_kwargs/test1_listener.py and tests\pubsub_kwargs\test3c_pubsub3.py and thoses tests pass on Windows, OSX and Linux. What is you platform, the python header indicates Darwin can you get specifically what it is?

     
  • schoenborno

    schoenborno - 2014-03-22

    Actually this is not a bug in the code, only in the documentation.

    The reason this happens is that pypubsub verifies that listeners and messages are "compatible" in their keyword arguments. Unless there is a topic message data specification that defines the given topic, pypubsub infers it based on the first sendMessage (via the keyword arguments provided) or listener subscription (via the listener's arguments), whichever comes first. But **kwargs is a catch-all, it defines a "generic listener" that can be used for any topic that has the arguments that come before it.

    So pypubsub infers from your lines of code, that all messages for topic "topic" have no data whatsoever (since there is nothing before the **kwargs). So when you sendMessage with a "test" data pypubsub says, rightfully, that the topic does not accept this data.

    The example implicitly assumes that the topic has already been subscribed to by a non-generic listener. I should update the docs with this info, and fix the example to show this:

    def listener1(arg1=1, arg2=2): print 'listener1'
    def listener2(**kwargs): print 'listener2'
    pub.subscribe(listener1, 'topic')
    pub.subscribe(listener2, 'topic')
    pub.sendmessage('topic', arg1=123, arg2=456)
    

    So pypubsub does not allow sending arbitrary data. The sender and listener must agree on what can be sent. Is it clear that

    pub.sendmessage('topic', notAnArg=1231)
    

    will not work if run after the above code? PyPubSub will not allow it because it is a breach of contract: listeners of "topic" can only handle messages that have arg1 and arg2 fields (or a subset thereof since the listener shows them as optional -- see the docs for this).

     
  • schoenborno

    schoenborno - 2016-10-21

    Fixed the example and added explaination that **kwargs does not get used towards topic specification so need a subscribe or def topic (via provider).

     
  • schoenborno

    schoenborno - 2016-10-21
    • status: accepted --> closed
     

Log in to post a comment.