This commit is contained in:
Barry Warsaw 1996-12-09 22:32:36 +00:00
parent 5817f8f717
commit 8b43b19ec9
2 changed files with 87 additions and 90 deletions

View File

@ -31,21 +31,19 @@ PERFORMANCE OF THIS SOFTWARE.
/* Math module -- standard C math library functions, pi and e */ /* Math module -- standard C math library functions, pi and e */
#include "allobjects.h" #include "Python.h"
#include <errno.h> #define getdoublearg(v, a) PyArg_Parse(v, "d", a)
#define get2doublearg(v, a, b) PyArg_Parse(v, "(dd)", a, b)
#define getdoublearg(v, a) getargs(v, "d", a)
#define get2doublearg(v, a, b) getargs(v, "(dd)", a, b)
#include "mymath.h" #include "mymath.h"
#ifndef _MSC_VER #ifndef _MSC_VER
#ifndef __STDC__ #ifndef __STDC__
extern double fmod PROTO((double, double)); extern double fmod Py_PROTO((double, double));
extern double frexp PROTO((double, int *)); extern double frexp Py_PROTO((double, int *));
extern double ldexp PROTO((double, int)); extern double ldexp Py_PROTO((double, int));
extern double modf PROTO((double, double *)); extern double modf Py_PROTO((double, double *));
#endif /* __STDC__ */ #endif /* __STDC__ */
#endif /* _MSC_VER */ #endif /* _MSC_VER */
@ -63,22 +61,23 @@ extern double modf PROTO((double, double *));
#define CHECK(x) /* Don't know how to check */ #define CHECK(x) /* Don't know how to check */
#endif #endif
static object * static PyObject *
math_error() math_error()
{ {
if (errno == EDOM) if (errno == EDOM)
err_setstr(ValueError, "math domain error"); PyErr_SetString(PyExc_ValueError, "math domain error");
else if (errno == ERANGE) else if (errno == ERANGE)
err_setstr(OverflowError, "math range error"); PyErr_SetString(PyExc_OverflowError, "math range error");
else else
err_errno(ValueError); /* Unexpected math error */ /* Unexpected math error */
PyErr_SetFromErrno(PyExc_ValueError);
return NULL; return NULL;
} }
static object * static PyObject *
math_1(args, func) math_1(args, func)
object *args; PyObject *args;
double (*func) FPROTO((double)); double (*func) Py_FPROTO((double));
{ {
double x; double x;
if (!getdoublearg(args, &x)) if (!getdoublearg(args, &x))
@ -89,13 +88,13 @@ math_1(args, func)
if (errno != 0) if (errno != 0)
return math_error(); return math_error();
else else
return newfloatobject(x); return PyFloat_FromDouble(x);
} }
static object * static PyObject *
math_2(args, func) math_2(args, func)
object *args; PyObject *args;
double (*func) FPROTO((double, double)); double (*func) Py_FPROTO((double, double));
{ {
double x, y; double x, y;
if (!get2doublearg(args, &x, &y)) if (!get2doublearg(args, &x, &y))
@ -106,16 +105,16 @@ math_2(args, func)
if (errno != 0) if (errno != 0)
return math_error(); return math_error();
else else
return newfloatobject(x); return PyFloat_FromDouble(x);
} }
#define FUNC1(stubname, func) \ #define FUNC1(stubname, func) \
static object * stubname(self, args) object *self, *args; { \ static PyObject * stubname(self, args) PyObject *self, *args; { \
return math_1(args, func); \ return math_1(args, func); \
} }
#define FUNC2(stubname, func) \ #define FUNC2(stubname, func) \
static object * stubname(self, args) object *self, *args; { \ static PyObject * stubname(self, args) PyObject *self, *args; { \
return math_2(args, func); \ return math_2(args, func); \
} }
@ -150,10 +149,10 @@ FUNC1(math_tan, tan)
FUNC1(math_tanh, tanh) FUNC1(math_tanh, tanh)
static object * static PyObject *
math_frexp(self, args) math_frexp(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
double x; double x;
int i; int i;
@ -164,13 +163,13 @@ math_frexp(self, args)
CHECK(x); CHECK(x);
if (errno != 0) if (errno != 0)
return math_error(); return math_error();
return mkvalue("(di)", x, i); return Py_BuildValue("(di)", x, i);
} }
static object * static PyObject *
math_ldexp(self, args) math_ldexp(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
double x, y; double x, y;
/* Cheat -- allow float as second argument */ /* Cheat -- allow float as second argument */
@ -182,13 +181,13 @@ math_ldexp(self, args)
if (errno != 0) if (errno != 0)
return math_error(); return math_error();
else else
return newfloatobject(x); return PyFloat_FromDouble(x);
} }
static object * static PyObject *
math_modf(self, args) math_modf(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
double x, y; double x, y;
if (!getdoublearg(args, &x)) if (!getdoublearg(args, &x))
@ -206,10 +205,10 @@ math_modf(self, args)
CHECK(x); CHECK(x);
if (errno != 0) if (errno != 0)
return math_error(); return math_error();
return mkvalue("(dd)", x, y); return Py_BuildValue("(dd)", x, y);
} }
static struct methodlist math_methods[] = { static PyMethodDef math_methods[] = {
{"acos", math_acos}, {"acos", math_acos},
{"asin", math_asin}, {"asin", math_asin},
{"atan", math_atan}, {"atan", math_atan},
@ -239,12 +238,12 @@ static struct methodlist math_methods[] = {
void void
initmath() initmath()
{ {
object *m, *d, *v; PyObject *m, *d, *v;
m = initmodule("math", math_methods); m = Py_InitModule("math", math_methods);
d = getmoduledict(m); d = PyModule_GetDict(m);
dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0)); PyDict_SetItemString(d, "pi", v = PyFloat_FromDouble(atan(1.0) * 4.0));
DECREF(v); Py_DECREF(v);
dictinsert(d, "e", v = newfloatobject(exp(1.0))); PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0)));
DECREF(v); Py_DECREF(v);
} }

View File

@ -39,17 +39,15 @@ PERFORMANCE OF THIS SOFTWARE.
/* MD5 objects */ /* MD5 objects */
#include "allobjects.h" #include "Python.h"
#include "modsupport.h"
#include "md5.h" #include "md5.h"
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
MD5_CTX md5; /* the context holder */ MD5_CTX md5; /* the context holder */
} md5object; } md5object;
staticforward typeobject MD5type; staticforward PyTypeObject MD5type;
#define is_md5object(v) ((v)->ob_type == &MD5type) #define is_md5object(v) ((v)->ob_type == &MD5type)
@ -58,7 +56,7 @@ newmd5object()
{ {
md5object *md5p; md5object *md5p;
md5p = NEWOBJ(md5object, &MD5type); md5p = PyObject_NEW(md5object, &MD5type);
if (md5p == NULL) if (md5p == NULL)
return NULL; return NULL;
@ -73,56 +71,56 @@ static void
md5_dealloc(md5p) md5_dealloc(md5p)
md5object *md5p; md5object *md5p;
{ {
DEL(md5p); PyMem_DEL(md5p);
} }
/* MD5 methods-as-attributes */ /* MD5 methods-as-attributes */
static object * static PyObject *
md5_update(self, args) md5_update(self, args)
md5object *self; md5object *self;
object *args; PyObject *args;
{ {
unsigned char *cp; unsigned char *cp;
int len; int len;
if (!getargs(args, "s#", &cp, &len)) if (!PyArg_Parse(args, "s#", &cp, &len))
return NULL; return NULL;
MD5Update(&self->md5, cp, len); MD5Update(&self->md5, cp, len);
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
md5_digest(self, args) md5_digest(self, args)
md5object *self; md5object *self;
object *args; PyObject *args;
{ {
MD5_CTX mdContext; MD5_CTX mdContext;
unsigned char aDigest[16]; unsigned char aDigest[16];
if (!getnoarg(args)) if (!PyArg_NoArgs(args))
return NULL; return NULL;
/* make a temporary copy, and perform the final */ /* make a temporary copy, and perform the final */
mdContext = self->md5; mdContext = self->md5;
MD5Final(aDigest, &mdContext); MD5Final(aDigest, &mdContext);
return newsizedstringobject((char *)aDigest, 16); return PyString_FromStringAndSize((char *)aDigest, 16);
} }
static object * static PyObject *
md5_copy(self, args) md5_copy(self, args)
md5object *self; md5object *self;
object *args; PyObject *args;
{ {
md5object *md5p; md5object *md5p;
if (!getnoarg(args)) if (!PyArg_NoArgs(args))
return NULL; return NULL;
if ((md5p = newmd5object()) == NULL) if ((md5p = newmd5object()) == NULL)
@ -130,53 +128,53 @@ md5_copy(self, args)
md5p->md5 = self->md5; md5p->md5 = self->md5;
return (object *)md5p; return (PyObject *)md5p;
} }
static struct methodlist md5_methods[] = { static PyMethodDef md5_methods[] = {
{"update", (method)md5_update}, {"update", (PyCFunction)md5_update},
{"digest", (method)md5_digest}, {"digest", (PyCFunction)md5_digest},
{"copy", (method)md5_copy}, {"copy", (PyCFunction)md5_copy},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static object * static PyObject *
md5_getattr(self, name) md5_getattr(self, name)
md5object *self; md5object *self;
char *name; char *name;
{ {
return findmethod(md5_methods, (object *)self, name); return Py_FindMethod(md5_methods, (PyObject *)self, name);
} }
statichere typeobject MD5type = { statichere PyTypeObject MD5type = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"md5", /*tp_name*/ "md5", /*tp_name*/
sizeof(md5object), /*tp_size*/ sizeof(md5object), /*tp_size*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
/* methods */ /* methods */
(destructor)md5_dealloc, /*tp_dealloc*/ (destructor)md5_dealloc, /*tp_dealloc*/
0, /*tp_print*/ 0, /*tp_print*/
(getattrfunc)md5_getattr, /*tp_getattr*/ (getattrfunc)md5_getattr, /*tp_getattr*/
0, /*tp_setattr*/ 0, /*tp_setattr*/
0, /*tp_compare*/ 0, /*tp_compare*/
0, /*tp_repr*/ 0, /*tp_repr*/
0, /*tp_as_number*/ 0, /*tp_as_number*/
}; };
/* MD5 functions */ /* MD5 functions */
static object * static PyObject *
MD5_new(self, args) MD5_new(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
md5object *md5p; md5object *md5p;
unsigned char *cp = NULL; unsigned char *cp = NULL;
int len = 0; int len = 0;
if (!newgetargs(args, "|s#", &cp, &len)) if (!PyArg_ParseTuple(args, "|s#", &cp, &len))
return NULL; return NULL;
if ((md5p = newmd5object()) == NULL) if ((md5p = newmd5object()) == NULL)
@ -185,15 +183,15 @@ MD5_new(self, args)
if (cp) if (cp)
MD5Update(&md5p->md5, cp, len); MD5Update(&md5p->md5, cp, len);
return (object *)md5p; return (PyObject *)md5p;
} }
/* List of functions exported by this module */ /* List of functions exported by this module */
static struct methodlist md5_functions[] = { static PyMethodDef md5_functions[] = {
{"new", (method)MD5_new, 1}, {"new", (PyCFunction)MD5_new, 1},
{"md5", (method)MD5_new, 1}, /* Backward compatibility */ {"md5", (PyCFunction)MD5_new, 1}, /* Backward compatibility */
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
@ -203,5 +201,5 @@ static struct methodlist md5_functions[] = {
void void
initmd5() initmd5()
{ {
(void)initmodule("md5", md5_functions); (void)Py_InitModule("md5", md5_functions);
} }