You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by gs...@apache.org on 2013/09/05 22:38:15 UTC

svn commit: r1520416 - in /qpid/trunk/qpid: cpp/bindings/qpid/python/python.i cpp/include/qpid/messaging/Message.h cpp/include/qpid/swig_python_typemaps.i cpp/src/qpid/messaging/Message.cpp python/qpid/tests/messaging/message.py

Author: gsim
Date: Thu Sep  5 20:38:15 2013
New Revision: 1520416

URL: http://svn.apache.org/r1520416
Log:
QPID-5104: make handling of properties in swigged impl match the pure python impl more closely

Modified:
    qpid/trunk/qpid/cpp/bindings/qpid/python/python.i
    qpid/trunk/qpid/cpp/include/qpid/messaging/Message.h
    qpid/trunk/qpid/cpp/include/qpid/swig_python_typemaps.i
    qpid/trunk/qpid/cpp/src/qpid/messaging/Message.cpp
    qpid/trunk/qpid/python/qpid/tests/messaging/message.py

Modified: qpid/trunk/qpid/cpp/bindings/qpid/python/python.i
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/python/python.i?rev=1520416&r1=1520415&r2=1520416&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/python/python.i (original)
+++ qpid/trunk/qpid/cpp/bindings/qpid/python/python.i Thu Sep  5 20:38:15 2013
@@ -164,10 +164,6 @@ QPID_EXCEPTION(UnauthorizedAccess, Sessi
 
 %extend qpid::messaging::Connection {
     %pythoncode %{
-         # Handle the different options by converting underscores to hyphens.
-         # Also, the sasl_mechanisms option in Python has no direct
-         # equivalent in C++, so we will translate them to sasl_mechanism
-         # when possible.
          def __init__(self, url=None, **options):
              if url:
                  args = [str(url)]
@@ -312,6 +308,31 @@ QPID_EXCEPTION(UnauthorizedAccess, Sessi
 
 %extend qpid::messaging::Message {
     %pythoncode %{
+         class MessageProperties:
+             def __init__(self, msg):
+                 self.msg = msg
+                 self.properties = self.msg.getProperties()
+
+             def __len__(self):
+                 return self.properties.__len__()
+
+             def __getitem__(self, key):
+                 return self.properties[key];
+
+             def __setitem__(self, key, value):
+                 self.properties[key] = value
+                 self.msg.setProperty(key, value)
+
+             def __delitem__(self, key):
+                 del self.properties[key]
+                 self.msg.setProperties(self.properties)
+
+             def __iter__(self):
+                 return self.properties.iteritems()
+
+             def __repr__(self):
+                 return str(self.properties)
+
          # UNSPECIFIED was module level before, but I do not
          # know how to insert python code at the top of the module.
          # (A bare "%pythoncode" inserts at the end.
@@ -344,11 +365,14 @@ QPID_EXCEPTION(UnauthorizedAccess, Sessi
              if ttl is not None :
                  self.ttl = ttl
              if properties is not None :
-                 # Can't set properties via (inst).getProperties, because
-                 # the typemaps make a copy of the underlying properties.
-                 # Instead, set via setProperty for the time-being
-                 for k, v in properties.iteritems() :
-                     self.setProperty(k, v)
+                 self.setProperties(properties)
+
+         def _get_msg_props(self):
+             try:
+                 return self._msg_props
+             except AttributeError:
+                 self._msg_props = Message.MessageProperties(self)
+                 return self._msg_props
 
          def _get_content(self) :
              obj = self.getContentObject()
@@ -418,8 +442,8 @@ QPID_EXCEPTION(UnauthorizedAccess, Sessi
          __swig_setmethods__["durable"] = setDurable
          if _newclass: durable = property(getDurable, setDurable)
 
-         __swig_getmethods__["properties"] = getProperties
-         if _newclass: properties = property(getProperties)
+         __swig_getmethods__["properties"] = _get_msg_props
+         if _newclass: properties = property(_get_msg_props)
 
          def getReplyTo(self) :
              return self._getReplyTo().str()
@@ -428,7 +452,7 @@ QPID_EXCEPTION(UnauthorizedAccess, Sessi
          __swig_getmethods__["reply_to"] = getReplyTo
          __swig_setmethods__["reply_to"] = setReplyTo
          if _newclass: reply_to = property(getReplyTo, setReplyTo)
-         
+
          def __repr__(self):
              args = []
              for name in ["id", "subject", "user_id", "reply_to",

Modified: qpid/trunk/qpid/cpp/include/qpid/messaging/Message.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/include/qpid/messaging/Message.h?rev=1520416&r1=1520415&r2=1520416&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/include/qpid/messaging/Message.h (original)
+++ qpid/trunk/qpid/cpp/include/qpid/messaging/Message.h Thu Sep  5 20:38:15 2013
@@ -149,6 +149,7 @@ class QPID_MESSAGING_CLASS_EXTERN Messag
      */
     QPID_MESSAGING_EXTERN const qpid::types::Variant::Map& getProperties() const;
     QPID_MESSAGING_EXTERN qpid::types::Variant::Map& getProperties();
+    QPID_MESSAGING_EXTERN void setProperties(const qpid::types::Variant::Map&);
 
     /**
      * Set the content to the data held in the string parameter. Note:

Modified: qpid/trunk/qpid/cpp/include/qpid/swig_python_typemaps.i
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/include/qpid/swig_python_typemaps.i?rev=1520416&r1=1520415&r2=1520416&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/include/qpid/swig_python_typemaps.i (original)
+++ qpid/trunk/qpid/cpp/include/qpid/swig_python_typemaps.i Thu Sep  5 20:38:15 2013
@@ -61,6 +61,11 @@ typedef int Py_ssize_t;
         if (PyInt_Check(value))    return qpid::types::Variant(int64_t(PyInt_AS_LONG(value)));
         if (PyLong_Check(value))   return qpid::types::Variant(int64_t(PyLong_AsLongLong(value)));
         if (PyString_Check(value)) return qpid::types::Variant(std::string(PyString_AS_STRING(value)));
+        if (PyUnicode_Check(value)) {
+            qpid::types::Variant v(std::string(PyUnicode_AS_DATA(value)));
+            v.setEncoding("utf8");
+            return v;
+        }
         if (PyDict_Check(value)) {
             qpid::types::Variant::Map map;
             PyToMap(value, &map);
@@ -116,7 +121,10 @@ typedef int Py_ssize_t;
             }
             case qpid::types::VAR_STRING : {
                 const std::string val(v->asString());
-                result = PyString_FromStringAndSize(val.c_str(), val.size());
+                if (v->getEncoding() == "utf8")
+                    result = PyUnicode_FromStringAndSize(val.c_str(), val.size());
+                else
+                    result = PyString_FromStringAndSize(val.c_str(), val.size());
                 break;
             }
             case qpid::types::VAR_MAP : {

Modified: qpid/trunk/qpid/cpp/src/qpid/messaging/Message.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/messaging/Message.cpp?rev=1520416&r1=1520415&r2=1520416&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/messaging/Message.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/messaging/Message.cpp Thu Sep  5 20:38:15 2013
@@ -73,6 +73,7 @@ void Message::setRedelivered(bool redeli
 
 const Variant::Map& Message::getProperties() const { return impl->getHeaders(); }
 Variant::Map& Message::getProperties() { return impl->getHeaders(); }
+void Message::setProperties(const Variant::Map& p) { getProperties() = p; }
 void Message::setProperty(const std::string& k, const qpid::types::Variant& v) { impl->setHeader(k,v); }
 
 void Message::setContent(const std::string& c) { impl->setBytes(c); }

Modified: qpid/trunk/qpid/python/qpid/tests/messaging/message.py
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/python/qpid/tests/messaging/message.py?rev=1520416&r1=1520415&r2=1520416&view=diff
==============================================================================
--- qpid/trunk/qpid/python/qpid/tests/messaging/message.py (original)
+++ qpid/trunk/qpid/python/qpid/tests/messaging/message.py Thu Sep  5 20:38:15 2013
@@ -111,6 +111,17 @@ class MessageEchoTests(Base):
     msg.reply_to = "reply-address"
     self.check(msg)
 
+  def testApplicationProperties(self):
+    msg = Message()
+    msg.properties["a"] = u"A"
+    msg.properties["b"] = 1
+    msg.properties["c"] = ["x", 2]
+    msg.properties["d"] = "D"
+    #make sure deleting works as expected
+    msg.properties["foo"] = "bar"
+    del msg.properties["foo"]
+    self.check(msg)
+
   def testContentTypeUnknown(self):
     msg = Message(content_type = "this-content-type-does-not-exist")
     self.check(msg)



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org