[Patches] Re: import floatdivision # 1/2==0.5
David Scherer
dscherer@cmu.edu
Mon, 12 Jun 2000 16:12:01 -0400
This is a multi-part message in MIME format.
------=_NextPart_000_0015_01BFD488.F0E76300
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
In case anyone's mailer had as much difficulty with the MIME attachment in
my original post as the pipermail archives did, the patch is also available
from
http://visualpython.sourceforge.net/floatdivision.patch
Dave
------=_NextPart_000_0015_01BFD488.F0E76300
Content-Type: application/octet-stream;
name="floatdivision.patch"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="floatdivision.patch"
diff -c -r -x Entries* old\python/dist/src/Include/opcode.h =
python/dist/src/Include/opcode.h
*** old\python/dist/src/Include/opcode.h Tue Mar 28 23:10:02 2000
--- python/dist/src/Include/opcode.h Mon Jun 12 14:41:06 2000
***************
*** 76,81 ****
--- 76,82 ----
#define BINARY_AND 64
#define BINARY_XOR 65
#define BINARY_OR 66
+ #define BINARY_FRACTION 67 /* 3/4 =3D=3D 0.75 */
=20
=20
#define PRINT_EXPR 70
diff -c -r -x Entries* old\python/dist/src/Lib/dis.py =
python/dist/src/Lib/dis.py
*** old\python/dist/src/Lib/dis.py Thu Mar 30 14:02:10 2000
--- python/dist/src/Lib/dis.py Mon Jun 12 15:52:20 2000
***************
*** 186,191 ****
--- 186,192 ----
def_op('BINARY_AND', 64)
def_op('BINARY_XOR', 65)
def_op('BINARY_OR', 66)
+ def_op('BINARY_FRACTION', 67)
=20
def_op('PRINT_EXPR', 70)
def_op('PRINT_ITEM', 71)
diff -c -r -x Entries* old\python/dist/src/Python/ceval.c =
python/dist/src/Python/ceval.c
*** old\python/dist/src/Python/ceval.c Mon May 08 14:06:50 2000
--- python/dist/src/Python/ceval.c Mon Jun 12 14:52:02 2000
***************
*** 787,792 ****
--- 787,816 ----
PUSH(x);
if (x !=3D NULL) continue;
break;
+=20
+ case BINARY_FRACTION:
+ w =3D POP();
+ v =3D POP();
+ if (PyInt_Check(v) && PyInt_Check(w)) {
+ /* 3/4 =3D=3D 0.75 */
+ long vi, wi;
+ vi =3D PyInt_AS_LONG(v);
+ wi =3D PyInt_AS_LONG(w);
+ if (wi =3D=3D 0) {
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "integer division");
+ x=3DNULL;
+ }
+ else
+ x=3DPyFloat_FromDouble( (double)vi / (double)wi );
+ }
+ else
+ x =3D PyNumber_Divide(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ PUSH(x);
+ if (x !=3D NULL) continue;
+ break;
=09
case BINARY_MODULO:
w =3D POP();
diff -c -r -x Entries* old\python/dist/src/Python/compile.c =
python/dist/src/Python/compile.c
*** old\python/dist/src/Python/compile.c Wed May 03 23:44:38 2000
--- python/dist/src/Python/compile.c Mon Jun 12 15:25:26 2000
***************
*** 329,334 ****
--- 329,335 ----
#ifdef PRIVATE_NAME_MANGLING
char *c_private; /* for private name mangling */
#endif
+ int c_floatdivision; /* generate BINARY_FRACTION; 3/4=3D=3D0.75 */
};
=20
=20
***************
*** 463,468 ****
--- 464,470 ----
c->c_last_addr =3D 0;
c->c_last_line =3D 0;
c-> c_lnotab_next =3D 0;
+ c->c_floatdivision =3D 0;
return 1;
=09
fail:
***************
*** 1482,1488 ****
op =3D BINARY_MULTIPLY;
break;
case SLASH:
! op =3D BINARY_DIVIDE;
break;
case PERCENT:
op =3D BINARY_MODULO;
--- 1484,1493 ----
op =3D BINARY_MULTIPLY;
break;
case SLASH:
! if (c->c_floatdivision)
! op =3D BINARY_FRACTION;
! else
! op =3D BINARY_DIVIDE;
break;
case PERCENT:
op =3D BINARY_MODULO;
***************
*** 2179,2188 ****
/* 'import' ... */
for (i =3D 1; i < NCH(n); i +=3D 2) {
REQ(CHILD(n, i), dotted_name);
! com_addopname(c, IMPORT_NAME, CHILD(n, i));
! com_push(c, 1);
! com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
! com_pop(c, 1);
}
}
}
--- 2184,2200 ----
/* 'import' ... */
for (i =3D 1; i < NCH(n); i +=3D 2) {
REQ(CHILD(n, i), dotted_name);
!=20
! if (NCH( CHILD(n,i) )=3D=3D1 &&=20
! !strcmp("floatdivision", STR(CHILD(CHILD(n,i),0))))
! {
! c->c_floatdivision++;
! } else {
! com_addopname(c, IMPORT_NAME, CHILD(n, i));
! com_push(c, 1);
! com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
! com_pop(c, 1);
! }
}
}
}
***************
*** 3306,3311 ****
--- 3318,3324 ----
case single_input: /* One interactive command */
/* NEWLINE | simple_stmt | compound_stmt NEWLINE */
c->c_interactive++;
+ /* c->c_floatdivision++; */
n =3D CHILD(n, 0);
if (TYPE(n) !=3D NEWLINE)
com_node(c, n);
***************
*** 3313,3318 ****
--- 3326,3332 ----
com_push(c, 1);
com_addbyte(c, RETURN_VALUE);
com_pop(c, 1);
+ /* c->c_floatdivision--; */
c->c_interactive--;
break;
=09
***************
*** 3482,3487 ****
--- 3496,3503 ----
else
sc.c_private =3D NULL;
#endif
+ if (base)
+ sc.c_floatdivision =3D base->c_floatdivision;
compile_node(&sc, n);
com_done(&sc);
if ((TYPE(n) =3D=3D funcdef || TYPE(n) =3D=3D lambdef) && sc.c_errors =
=3D=3D 0) {
------=_NextPart_000_0015_01BFD488.F0E76300--