[1.5.x] Stopped using non-standard __globals__ and __code__ attributes.

Some alternative implementations don't have them.

Closes #19944.

Backport of 9d4a5b00f1 from master.
This commit is contained in:
Aymeric Augustin 2013-03-17 10:19:30 +01:00
parent 2757d492bb
commit 1c9bd69ff0
2 changed files with 11 additions and 11 deletions

View File

@ -11,6 +11,7 @@ from django.utils import feedgenerator, tzinfo
from django.utils.encoding import force_text, iri_to_uri, smart_text from django.utils.encoding import force_text, iri_to_uri, smart_text
from django.utils.html import escape from django.utils.html import escape
from django.utils.http import http_date from django.utils.http import http_date
from django.utils import six
from django.utils.timezone import is_naive from django.utils.timezone import is_naive
@ -69,15 +70,14 @@ class Feed(object):
except AttributeError: except AttributeError:
return default return default
if callable(attr): if callable(attr):
# Check __code__.co_argcount rather than try/excepting the # Check co_argcount rather than try/excepting the function and
# function and catching the TypeError, because something inside # catching the TypeError, because something inside the function
# the function may raise the TypeError. This technique is more # may raise the TypeError. This technique is more accurate.
# accurate. try:
if hasattr(attr, '__code__'): code = six.get_function_code(attr)
argcount = attr.__code__.co_argcount except AttributeError:
else: code = six.get_function_code(attr.__call__)
argcount = attr.__call__.__code__.co_argcount if code.co_argcount == 2: # one argument is 'self'
if argcount == 2: # one argument is 'self'
return attr(obj) return attr(obj)
else: else:
return attr() return attr()

View File

@ -883,7 +883,7 @@ class DocTestFinder:
if module is None: if module is None:
return True return True
elif inspect.isfunction(object): elif inspect.isfunction(object):
return module.__dict__ is object.__globals__ return module.__dict__ is six.get_function_globals(object)
elif inspect.isclass(object): elif inspect.isclass(object):
return module.__name__ == object.__module__ return module.__name__ == object.__module__
elif inspect.getmodule(object) is not None: elif inspect.getmodule(object) is not None:
@ -1021,7 +1021,7 @@ class DocTestFinder:
# Find the line number for functions & methods. # Find the line number for functions & methods.
if inspect.ismethod(obj): obj = obj.__func__ if inspect.ismethod(obj): obj = obj.__func__
if inspect.isfunction(obj): obj = obj.__code__ if inspect.isfunction(obj): obj = six.get_function_code(obj)
if inspect.istraceback(obj): obj = obj.tb_frame if inspect.istraceback(obj): obj = obj.tb_frame
if inspect.isframe(obj): obj = obj.f_code if inspect.isframe(obj): obj = obj.f_code
if inspect.iscode(obj): if inspect.iscode(obj):