Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e516265b
Commit
e516265b
authored
Nov 20, 2010
by
Michael Foord
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 9732: fetch the method resolution order from the type metaclass directly in getattr_static
parent
6bb9989a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
22 deletions
+29
-22
Doc/library/inspect.rst
Doc/library/inspect.rst
+8
-20
Lib/inspect.py
Lib/inspect.py
+5
-2
Lib/test/test_inspect.py
Lib/test/test_inspect.py
+16
-0
No files found.
Doc/library/inspect.rst
View file @
e516265b
...
@@ -587,26 +587,14 @@ but avoids executing code when it fetches attributes.
...
@@ -587,26 +587,14 @@ but avoids executing code when it fetches attributes.
that raise AttributeError). It can also return descriptors objects
that raise AttributeError). It can also return descriptors objects
instead of instance members.
instead of instance members.
There are several cases that will break `getattr_static` or be handled
The only known case that can cause `getattr_static` to trigger code execution,
incorrectly. These are pathological enough not to worry about (i.e. if you do
and cause it to return incorrect results (or even break), is where a class uses
any of these then you deserve to have everything break anyway):
:data:`~object.__slots__` and provides a `__dict__` member using a property or
descriptor. If you find other cases please report them so they can be fixed
* :data:`~object.__dict__` existing (e.g. as a property) but returning the
or documented.
wrong dictionary or even returning something other than a
dictionary
`getattr_static` does not resolve descriptors, for example slot descriptors or
* classes created with :data:`~object.__slots__` that have the `__slots__`
getset descriptors on objects implemented in C. The descriptor object
member deleted from the class, or a fake `__slots__` attribute
attached to the instance, or any other monkeying with
`__slots__`
* type objects that lie about their :term:`MRO`
.. note::
Classes that override :data:`~object.__mro__` as a property will have this
code executed by `getattr_static`.
Descriptors are not resolved (for example slot descriptors or
getset descriptors on objects implemented in C). The descriptor
is returned instead of the underlying attribute.
is returned instead of the underlying attribute.
You can handle these with code like the following. Note that
You can handle these with code like the following. Note that
...
...
Lib/inspect.py
View file @
e516265b
...
@@ -1060,6 +1060,9 @@ def trace(context=1):
...
@@ -1060,6 +1060,9 @@ def trace(context=1):
_sentinel
=
object
()
_sentinel
=
object
()
def
_static_getmro
(
klass
):
return
type
.
__dict__
[
'__mro__'
].
__get__
(
klass
)
def
_check_instance
(
obj
,
attr
):
def
_check_instance
(
obj
,
attr
):
instance_dict
=
{}
instance_dict
=
{}
try
:
try
:
...
@@ -1070,7 +1073,7 @@ def _check_instance(obj, attr):
...
@@ -1070,7 +1073,7 @@ def _check_instance(obj, attr):
def
_check_class
(
klass
,
attr
):
def
_check_class
(
klass
,
attr
):
for
entry
in
getmro
(
klass
):
for
entry
in
_static_
getmro
(
klass
):
try
:
try
:
return
entry
.
__dict__
[
attr
]
return
entry
.
__dict__
[
attr
]
except
KeyError
:
except
KeyError
:
...
@@ -1110,7 +1113,7 @@ def getattr_static(obj, attr, default=_sentinel):
...
@@ -1110,7 +1113,7 @@ def getattr_static(obj, attr, default=_sentinel):
if
obj
is
klass
:
if
obj
is
klass
:
# for types we check the metaclass too
# for types we check the metaclass too
for
entry
in
getmro
(
type
(
klass
)):
for
entry
in
_static_
getmro
(
type
(
klass
)):
try
:
try
:
return
entry
.
__dict__
[
attr
]
return
entry
.
__dict__
[
attr
]
except
KeyError
:
except
KeyError
:
...
...
Lib/test/test_inspect.py
View file @
e516265b
...
@@ -867,6 +867,22 @@ class TestGetattrStatic(unittest.TestCase):
...
@@ -867,6 +867,22 @@ class TestGetattrStatic(unittest.TestCase):
self
.
assertEqual
(
inspect
.
getattr_static
(
Something
(),
'foo'
),
3
)
self
.
assertEqual
(
inspect
.
getattr_static
(
Something
(),
'foo'
),
3
)
self
.
assertEqual
(
inspect
.
getattr_static
(
Something
,
'foo'
),
3
)
self
.
assertEqual
(
inspect
.
getattr_static
(
Something
,
'foo'
),
3
)
def
test_mro_as_property
(
self
):
class
Meta
(
type
):
@
property
def
__mro__
(
self
):
return
(
object
,)
class
Base
(
object
):
foo
=
3
class
Something
(
Base
,
metaclass
=
Meta
):
pass
self
.
assertEqual
(
inspect
.
getattr_static
(
Something
(),
'foo'
),
3
)
self
.
assertEqual
(
inspect
.
getattr_static
(
Something
,
'foo'
),
3
)
def
test_main
():
def
test_main
():
run_unittest
(
run_unittest
(
TestDecorators
,
TestRetrievingSourceCode
,
TestOneliners
,
TestBuggyCases
,
TestDecorators
,
TestRetrievingSourceCode
,
TestOneliners
,
TestBuggyCases
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment