Merge pull request #582 from kmike/inspect-workaround

[WIP] Handle cases when inspect.stack() fails
This commit is contained in:
Daniel Graña 2014-02-06 19:41:32 -08:00
commit 45c9dab15b
2 changed files with 25 additions and 4 deletions

View File

@ -3,14 +3,18 @@ from __future__ import absolute_import
import inspect
import unittest
import warnings
import mock
from scrapy.utils.deprecate import create_deprecated_class
class MyWarning(UserWarning):
pass
class SomeBaseClass(object):
pass
class NewName(SomeBaseClass):
pass
@ -234,3 +238,12 @@ class WarnWhenSubclassedTest(unittest.TestCase):
self.assertIn('foo.Bar', str(w[0].message))
self.assertIn('AlsoDeprecated', str(w[1].message))
self.assertIn('foo.Bar', str(w[1].message))
def test_inspect_stack(self):
with mock.patch('inspect.stack', side_effect=IndexError):
with warnings.catch_warnings(record=True) as w:
DeprecatedName = create_deprecated_class('DeprecatedName', NewName)
class SubClass(DeprecatedName):
pass
self.assertIn("Error detecting parent module", str(w[0].message))

View File

@ -101,10 +101,18 @@ def create_deprecated_class(name, new_class, clsdict=None,
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