You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2015/03/06 23:39:28 UTC

svn commit: r1664737 - in /qpid/dispatch/trunk: python/qpid_dispatch_internal/dispatch.py src/python_embedded.c tests/mock/__init__.py

Author: aconway
Date: Fri Mar  6 22:39:27 2015
New Revision: 1664737

URL: http://svn.apache.org/r1664737
Log:
NO-JIRA: Detect and reject attempts to import proton python module into a qdrouterd process.

The qdrouterd process should not depend on the proton python binding. Since
other areas of dispatch do (e.g. tools) this code is to detect programmer errors
that create such a dependency.

Modified:
    qpid/dispatch/trunk/python/qpid_dispatch_internal/dispatch.py
    qpid/dispatch/trunk/src/python_embedded.c
    qpid/dispatch/trunk/tests/mock/__init__.py

Modified: qpid/dispatch/trunk/python/qpid_dispatch_internal/dispatch.py
URL: http://svn.apache.org/viewvc/qpid/dispatch/trunk/python/qpid_dispatch_internal/dispatch.py?rev=1664737&r1=1664736&r2=1664737&view=diff
==============================================================================
--- qpid/dispatch/trunk/python/qpid_dispatch_internal/dispatch.py (original)
+++ qpid/dispatch/trunk/python/qpid_dispatch_internal/dispatch.py Fri Mar  6 22:39:27 2015
@@ -26,9 +26,11 @@ The C library also adds the following C
 
 - LogAdapter: Logs to the C logging system.
 - IoAdapter: Receives messages from the router into python.
+
+This module also prevents the proton python module from being accidentally loaded.
 """
 
-import ctypes
+import sys, ctypes
 from ctypes import c_char_p, c_long, py_object
 import qpid_dispatch_site
 
@@ -87,3 +89,23 @@ class QdDll(ctypes.PyDLL):
 
     def function(self, fname, restype, argtypes, check=True):
         return self._prototype(getattr(self, fname), restype, argtypes, check)
+
+
+# Prevent accidental loading of the proton module.
+
+FORBIDDEN = ["proton"]
+
+def check_forbidden():
+    bad = set(FORBIDDEN) & set(sys.modules)
+    if bad:
+        raise ImportError("Forbidden modules loaded: '%s'." % "', '".join(bad))
+
+def import_check(name, *args, **kw):
+    if name in FORBIDDEN:
+        raise ImportError("Attempted to load forbidden module '%s'." % name)
+    return builtin_import(name, *args, **kw)
+
+check_forbidden()
+import __builtin__
+builtin_import = __builtin__.__import__
+__builtin__.__import__ = import_check

Modified: qpid/dispatch/trunk/src/python_embedded.c
URL: http://svn.apache.org/viewvc/qpid/dispatch/trunk/src/python_embedded.c?rev=1664737&r1=1664736&r2=1664737&view=diff
==============================================================================
--- qpid/dispatch/trunk/src/python_embedded.c (original)
+++ qpid/dispatch/trunk/src/python_embedded.c Fri Mar  6 22:39:27 2015
@@ -691,7 +691,7 @@ static void qd_python_setup(void)
     IoAdapterType.tp_new  = PyType_GenericNew;
     if ((PyType_Ready(&LogAdapterType) < 0) || (PyType_Ready(&IoAdapterType) < 0)) {
         qd_error_py();
-        qd_log(log_source, QD_LOG_ERROR, "Unable to initialize Adapters");
+        qd_log(log_source, QD_LOG_CRITICAL, "Unable to initialize Adapters");
         abort();
     } else {
         //
@@ -704,6 +704,11 @@ static void qd_python_setup(void)
 
         // Import the initial dispatch module (we will add C extensions to it)
         PyObject *m = PyImport_ImportModule(DISPATCH_MODULE);
+        if (!m) {
+            qd_error_py();
+            qd_log(log_source, QD_LOG_CRITICAL, "Cannot load dispatch extension module '%s'", DISPATCH_MODULE);
+            abort();
+        }
 
         //
         // Add LogAdapter

Modified: qpid/dispatch/trunk/tests/mock/__init__.py
URL: http://svn.apache.org/viewvc/qpid/dispatch/trunk/tests/mock/__init__.py?rev=1664737&r1=1664736&r2=1664737&view=diff
==============================================================================
--- qpid/dispatch/trunk/tests/mock/__init__.py (original)
+++ qpid/dispatch/trunk/tests/mock/__init__.py Fri Mar  6 22:39:27 2015
@@ -24,6 +24,9 @@ Mock implementation of the dispatch C ex
 from qpid_dispatch_internal import dispatch as real_dispatch
 from . import dispatch as mock_dispatch
 
+# For tests we want to allow loading the proton module.
+real_dispatch.FORBIDDEN = []
+
 for name in dir(mock_dispatch):
     if not name.startswith("_"):
         setattr(real_dispatch, name, getattr(mock_dispatch, name))



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