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

View File

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