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
9a7e7740
Commit
9a7e7740
authored
Feb 25, 2007
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for static and class methods.
parent
88470ec3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
3 deletions
+36
-3
Lib/xreload.py
Lib/xreload.py
+36
-3
No files found.
Lib/xreload.py
View file @
9a7e7740
...
...
@@ -65,6 +65,7 @@ def xreload(mod):
for
name
in
newnames
-
oldnames
:
modns
[
name
]
=
tmpns
[
name
]
# Delete names that are no longer current
# XXX What to do about renamed objects?
for
name
in
oldnames
-
newnames
-
{
"__name__"
}:
del
modns
[
name
]
# Now update the rest in place
...
...
@@ -86,6 +87,9 @@ def _update(oldobj, newobj):
Returns:
either oldobj, updated in place, or newobj.
"""
if
oldobj
is
newobj
:
# Probably something imported
return
newobj
if
type
(
oldobj
)
is
not
type
(
newobj
):
# Cop-out: if the type changed, give up
return
newobj
...
...
@@ -98,11 +102,18 @@ def _update(oldobj, newobj):
return
_update_function
(
oldobj
,
newobj
)
if
isinstance
(
newobj
,
types
.
MethodType
):
return
_update_method
(
oldobj
,
newobj
)
# XXX Support class methods, static methods, other decorators
if
isinstance
(
newobj
,
classmethod
):
return
_update_classmethod
(
oldobj
,
newobj
)
if
isinstance
(
newobj
,
staticmethod
):
return
_update_staticmethod
(
oldobj
,
newobj
)
# XXX How to support decorators?
# Not something we recognize, just give up
return
newobj
# All of the following functions have the same signature as _update()
def
_update_function
(
oldfunc
,
newfunc
):
"""Update a function object."""
oldfunc
.
__doc__
=
newfunc
.
__doc__
...
...
@@ -116,7 +127,7 @@ def _update_function(oldfunc, newfunc):
def
_update_method
(
oldmeth
,
newmeth
):
"""Update a method object."""
# XXX What if im_func is not a function?
_update
_function
(
oldmeth
.
im_func
,
newmeth
.
im_func
)
_update
(
oldmeth
.
im_func
,
newmeth
.
im_func
)
return
oldmeth
...
...
@@ -132,5 +143,27 @@ def _update_class(oldclass, newclass):
for
name
in
oldnames
-
newnames
:
delattr
(
oldclass
,
name
)
for
name
in
oldnames
&
newnames
-
{
"__dict__"
,
"__doc__"
}:
setattr
(
oldclass
,
name
,
newdict
[
name
]
)
setattr
(
oldclass
,
name
,
_update
(
olddict
[
name
],
newdict
[
name
])
)
return
oldclass
def
_update_classmethod
(
oldcm
,
newcm
):
"""Update a classmethod update."""
# While we can't modify the classmethod object itself (it has no
# mutable attributes), we *can* extract the underlying function
# (by calling __get__(), which returns a method object) and update
# it in-place. We don't have the class available to pass to
# __get__() but any object except None will do.
_update
(
oldcm
.
__get__
(
0
),
newcm
.
__get__
(
0
))
return
newcm
def
_update_staticmethod
(
oldsm
,
newsm
):
"""Update a staticmethod update."""
# While we can't modify the staticmethod object itself (it has no
# mutable attributes), we *can* extract the underlying function
# (by calling __get__(), which returns it) and update it in-place.
# We don't have the class available to pass to __get__() but any
# object except None will do.
_update
(
oldsm
.
__get__
(
0
),
newsm
.
__get__
(
0
))
return
newsm
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