Exception when using kwargs for a topic listener
Publish - subscribe API for message/event-based python applications
Brought to you by:
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') (<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
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:
Related
Bugs:
#15Last edit: 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?
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:
So pypubsub does not allow sending arbitrary data. The sender and listener must agree on what can be sent. Is it clear that
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).
Fixed the example and added explaination that
**kwargs
does not get used towards topic specification so need a subscribe or def topic (via provider).