Skip to content
Permalink
Browse files

Handle cases when inspect.stack() fails

  • Loading branch information
kmike authored and dangra committed Feb 8, 2014
1 parent 46d98d6 commit 8cb44f90ab709a4a586d70f73fb26be03a5d0716
Showing with 12 additions and 4 deletions.
  1. +12 −4 scrapy/utils/deprecate.py
@@ -101,10 +101,18 @@ def __call__(cls, *args, **kwargs):
return super(DeprecatedClass, cls).__call__(*args, **kwargs)

deprecated_cls = DeprecatedClass(name, (new_class,), clsdict or {})
frm = inspect.stack()[1]
parent_module = inspect.getmodule(frm[0])
if parent_module is not None:
deprecated_cls.__module__ = parent_module.__name__

try:
frm = inspect.stack()[1]
parent_module = inspect.getmodule(frm[0])
if parent_module is not None:
deprecated_cls.__module__ = parent_module.__name__
except Exception as e:
# Sometimes inspect.stack() fails (e.g. when the first import of
# deprecated class is in jinja2 template). __module__ attribute is not
# important enough to raise an exception as users may be unable
# to fix inspect.stack() errors.
warnings.warn("Error detecting parent module: %r" % e)

return deprecated_cls

0 comments on commit 8cb44f9

Please sign in to comment.
You can’t perform that action at this time.