Skip to content

Commit 1de7a44

Browse files
Fix the fraction module so that __rpow__ works on arbitrary classes.
The fraction module implicitly casted float if __rpow__ was implemented in your class when raising a fraction to a power. Now it isn't done but will raise an exception if __rpow__ isn't implemented.
1 parent 1195c16 commit 1de7a44

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

Doc/library/functions.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,13 @@ are always available. They are listed here in alphabetical order.
608608
will be used for both the global and the local variables. If *globals* and
609609
*locals* are given, they are used for the global and local variables,
610610
respectively. If provided, *locals* can be any mapping object. Remember
611-
that at the module level, globals and locals are the same dictionary. If exec
612-
gets two separate objects as *globals* and *locals*, the code will be
613-
executed as if it were embedded in a class definition.
611+
that at the module level, globals and locals are the same dictionary.
612+
613+
.. note::
614+
615+
Most users should just pass a *globals* argument and never *locals*.
616+
If exec gets two separate objects as *globals* and *locals*, the code
617+
will be executed as if it were embedded in a class definition.
614618

615619
If the *globals* dictionary does not contain a value for the key
616620
``__builtins__``, a reference to the dictionary of the built-in module

Lib/fractions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,10 @@ def __pow__(a, b):
875875
# A fractional power will generally produce an
876876
# irrational number.
877877
return float(a) ** float(b)
878-
else:
878+
elif isinstance(b, (float, complex)):
879879
return float(a) ** b
880+
else:
881+
return NotImplemented
880882

881883
def __rpow__(b, a):
882884
"""a ** b"""

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ Kasun Herath
751751
Chris Herborth
752752
Ivan Herman
753753
Jürgen Hermann
754+
Joshua Jay Herman
754755
Gary Herron
755756
Ernie Hershey
756757
Thomas Herve
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This fixes a situation where raising a class with rpow in the class calling
2+
the exponent operator to raise it to the power of that class to not cast it
3+
as a float.

0 commit comments

Comments
 (0)