Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add superm() method

  • Loading branch information...
commit 57ce20eef3902224bb683ad26964165fcd1d344d 1 parent 94e3b93
@rfk authored
Showing with 27 additions and 4 deletions.
  1. +7 −0 ChangeLog.txt
  2. +10 −4 magicsuper/__init__.py
  3. +10 −0 magicsuper/tests.py
View
7 ChangeLog.txt
@@ -1,4 +1,11 @@
+v0.3.0:
+
+ * made it so you can do "from magicsuper import super".
+ * added "superm" function, which directly calls the superclass version
+ of the "current method".
+
+
v0.2.0:
* renamed to "magicsuper"; the "autosuper" name is already used by e.g.
View
14 magicsuper/__init__.py
@@ -46,7 +46,7 @@ def hello(self):
_SENTINEL = object()
-def _auto_super(typ=_SENTINEL,type_or_obj=_SENTINEL):
+def super(typ=_SENTINEL,type_or_obj=_SENTINEL,framedepth=1):
"""Like buildin super(), but capable of magic.
This acts just like the builtin super() function, but if you don't give
@@ -55,7 +55,7 @@ def _auto_super(typ=_SENTINEL,type_or_obj=_SENTINEL):
# Infer the correct call if used without arguments.
if typ is _SENTINEL:
# We'll need to do some frame hacking.
- f = sys._getframe(1)
+ f = sys._getframe(framedepth)
# Get the first positional argument of the function.
try:
type_or_obj = f.f_locals[f.f_code.co_varnames[0]]
@@ -72,7 +72,7 @@ def _auto_super(typ=_SENTINEL,type_or_obj=_SENTINEL):
# Now, find the class owning the currently-executing method.
for typ in mro:
for meth in typ.__dict__.itervalues():
- if not isinstance(meth,type(_auto_super)):
+ if not isinstance(meth,type(super)):
continue
if meth.func_code is f.f_code:
# Aha! Found you.
@@ -88,8 +88,14 @@ def _auto_super(typ=_SENTINEL,type_or_obj=_SENTINEL):
if type_or_obj is not _SENTINEL:
return _builtin_super(typ,type_or_obj)
return _builtin_super(typ)
+
+
+def superm(*args,**kwds):
+ f = sys._getframe(1)
+ nm = f.f_code.co_name
+ return getattr(super(framedepth=2),nm)(*args,**kwds)
-__builtin__.super = _auto_super
+__builtin__.super = super
View
10 magicsuper/tests.py
@@ -110,6 +110,16 @@ def calc(self,value):
self.assertSuperEquals(magicsuper._builtin_super(cls),super(cls))
self.assertSuperEquals(magicsuper._builtin_super(cls,obj),
super(cls,obj))
+
+ def test_superm(self):
+ class Base(object):
+ def getit(self):
+ return 2
+ class Sub(Base):
+ def getit(self):
+ return 10 * magicsuper.superm()
+ s = Sub()
+ self.assertEquals(s.getit(),20)
Please sign in to comment.
Something went wrong with that request. Please try again.