chiark / gitweb /
field: Correct conversions from integers.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 30 Mar 2006 23:25:22 +0000 (00:25 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 30 Mar 2006 23:25:22 +0000 (00:25 +0100)
The function tofe refused to convert integers to field elements.  I seem
to remember deciding that this was too weird for binary fields, but in
retrospect this seems a mistake, since there's nothing stopping a binary
field element converting to an mp (or gf).

Previously, binary operations between a field element and a Python
integer failed, but a binop with a fe and an mp would force the fe to
downgrade to an mp, which seems really bizarre.

Also, I think there was a possibility of a memory leak in the old code.

field.c

diff --git a/field.c b/field.c
index bb99b77ffeb9e61325bc9f8a8d3b30529b0a9b1d..d0ab05d605f236e50a5c7186f40a080d4b131d70 100644 (file)
--- a/field.c
+++ b/field.c
@@ -115,16 +115,16 @@ static mp *tofe(field *f, PyObject *o)
 
   if (FE_PYCHECK(o)) {
     if (FE_F(o) != f && !field_samep(FE_F(o), f)) return (0);
-    y = FE_X(o);
-  }
-  if ((x = tomp(o)) != 0) {
+    y = MP_COPY(FE_X(o));
+  } else if ((x = tomp(o)) != 0) {
     if (MP_ZEROP(x))
-      y = f->zero;
+      y = MP_COPY(f->zero);
     else if (MP_EQ(x, MP_ONE))
-      y = f->one;
+      y = MP_COPY(f->one);
+    else
+      y = F_IN(f, MP_NEW, x);
+    MP_DROP(x);
   }
-  if (x) MP_DROP(x);
-  if (y) MP_COPY(y);
   return (y);
 }