Ver Fonte

New C_IMPDEBUG envvar traces imports, from Armin's talk

Ask Solem há 12 anos atrás
pai
commit
1b97f567e8
1 ficheiros alterados com 16 adições e 0 exclusões
  1. 16 0
      celery/__init__.py

+ 16 - 0
celery/__init__.py

@@ -24,6 +24,21 @@ VERSION_BANNER = '{0} ({1})'.format(__version__, SERIES)
 
 # -eof meta-
 
+import os
+if os.environ.get('C_IMPDEBUG'):
+    import sys
+    import __builtin__
+    real_import = __builtin__.__import__
+
+    def debug_import(name, locals=None, globals=None, fromlist=None,
+            level=-1):
+        glob = globals or getattr(sys, 'emarfteg_'[::-1])(1).f_globals
+        importer_name = glob and glob.get('__name__') or 'unknown'
+        print('-- {0} imports {1}'.format(importer_name, name))
+        return real_import(name, locals, globals, fromlist, level)
+    __builtin__.__import__ = debug_import
+
+
 # This is for static analyzers
 Celery = object
 bugreport = lambda *a, **kw: None
@@ -59,3 +74,4 @@ old_module, new_module = recreate_module(__name__,  # pragma: no cover
     __homepage__=__homepage__, __docformat__=__docformat__,
     VERSION=VERSION, SERIES=SERIES, VERSION_BANNER=VERSION_BANNER,
 )
+