field.c, mp.c: Implement the `nb_index' conversion.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 18 Nov 2019 14:52:54 +0000 (14:52 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 11:44:21 +0000 (12:44 +0100)
This is Yet Another Integer Conversion added in 2.5, for the purpose of
/implicit/ conversions (since strings and other strange things will
convert explicitly to integers).

It's significant because the `bin' builtin (conversion to base-2 string,
added in 2.6) works by applying the `index' conversion rather than
having a special-purpose method like `hex' and `oct'.  (Indeed, Python 3
abolishes these methods, and `hex' and `oct' convert via `index' too.)

So add conversions somewhat judiciously.

field.c
mp.c
t/t-mp.py

diff --git a/field.c b/field.c
index 5f3b58c..88793b0 100644 (file)
--- a/field.c
+++ b/field.c
@@ -391,6 +391,8 @@ static const PyNumberMethods fe_pynumber = {
   fe_pydiv,                            /* @nb_true_divide@ */
   0,                                   /* @nb_inplace_floor_divide@ */
   0,                                   /* @nb_inplace_true_divide@ */
+
+  fe_pyint,                            /* @nb_index@ */
 };
 
 static const PyTypeObject fe_pytype_skel = {
diff --git a/mp.c b/mp.c
index e14b720..daa91ce 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -966,6 +966,8 @@ static const PyNumberMethods mp_pynumber = {
   0,                                   /* @nb_true_divide@ */
   0,                                   /* @nb_inplace_floor_divide@ */
   0,                                   /* @nb_inplace_true_divide@ */
+
+  mp_pyint,                            /* @nb_index@ */
 };
 
 static const PyTypeObject mp_pytype_skel = {
@@ -2149,6 +2151,8 @@ static const PyNumberMethods gf_pynumber = {
   0,                                   /* @nb_true_divide@ */
   0,                                   /* @nb_inplace_floor_divide@ */
   0,                                   /* @nb_inplace_true_divide@ */
+
+  mp_pyint,                            /* @nb_index@ */
 };
 
 static const PyTypeObject gf_pytype_skel = {
index ec84d74..93a1e2f 100644 (file)
--- a/t/t-mp.py
+++ b/t/t-mp.py
@@ -72,6 +72,10 @@ class TestMP (U.TestCase):
     me.assertEqual(repr(y), 'MP(6556380541834372447694561492436749633)')
     me.assertEqual(hex(y), '0x4eeb684a0954ec4ceb255e3e9778d41')
     me.assertEqual(oct(y), '047353320450112516611472622536175135706501')
+    try: bin
+    except NameError: pass
+    else: me.assertEqual(bin(C.MP(661438603)),
+                         '0b100111011011001100000010001011')
 
   def test_number(me):
     x, y, m, zero = C.MP(169), C.MP(24), C.MP(205), C.MP(0)
@@ -414,6 +418,10 @@ class TestGF (U.TestCase):
     me.assertEqual(repr(y), 'GF(0x4eeb684a0954ec4ceb255e3e9778d41)')
     me.assertEqual(hex(y), '0x4eeb684a0954ec4ceb255e3e9778d41')
     me.assertEqual(oct(y), '047353320450112516611472622536175135706501')
+    try: bin
+    except NameError: pass
+    else: me.assertEqual(bin(C.GF(661438603)),
+                         '0b100111011011001100000010001011')
 
   def test_number(me):
     x, y, m, zero = C.GF(0xa9), C.GF(0x18), C.GF(0x11b), C.GF(0)