mirror of https://github.com/python/cpython
Quickly renamed the last directory.
This commit is contained in:
parent
f4806c2a85
commit
c0b618a2cc
|
@ -979,7 +979,7 @@ PyObject_CallObject(o, a)
|
|||
PyObject *
|
||||
#ifdef HAVE_STDARG_PROTOTYPES
|
||||
/* VARARGS 2 */
|
||||
PyObject_CallFunction(PyObject *callable, char *format, ...)
|
||||
PyObject_CallFunction(PyObject *PyCallable_Check, char *format, ...)
|
||||
#else
|
||||
/* VARARGS */
|
||||
PyObject_CallFunction(va_alist) va_dcl
|
||||
|
@ -990,14 +990,14 @@ PyObject_CallFunction(va_alist) va_dcl
|
|||
#ifdef HAVE_STDARG_PROTOTYPES
|
||||
va_start(va, format);
|
||||
#else
|
||||
PyObject *callable;
|
||||
PyObject *PyCallable_Check;
|
||||
char *format;
|
||||
va_start(va);
|
||||
callable = va_arg(va, PyObject *);
|
||||
PyCallable_Check = va_arg(va, PyObject *);
|
||||
format = va_arg(va, char *);
|
||||
#endif
|
||||
|
||||
if( ! callable)
|
||||
if( ! PyCallable_Check)
|
||||
{
|
||||
va_end(va);
|
||||
return Py_ReturnNullError();
|
||||
|
@ -1019,7 +1019,7 @@ PyObject_CallFunction(va_alist) va_dcl
|
|||
Py_TRY(PyTuple_SetItem(a,0,args) != -1);
|
||||
args=a;
|
||||
}
|
||||
retval = PyObject_CallObject(callable,args);
|
||||
retval = PyObject_CallObject(PyCallable_Check,args);
|
||||
Py_DECREF(args);
|
||||
return retval;
|
||||
}
|
||||
|
@ -1034,7 +1034,7 @@ PyObject_CallMethod(va_alist) va_dcl
|
|||
#endif
|
||||
{
|
||||
va_list va;
|
||||
PyObject *args, *method=0, *retval;
|
||||
PyObject *args, *PyCFunction=0, *retval;
|
||||
#ifdef HAVE_STDARG_PROTOTYPES
|
||||
va_start(va, format);
|
||||
#else
|
||||
|
@ -1053,15 +1053,15 @@ PyObject_CallMethod(va_alist) va_dcl
|
|||
return Py_ReturnNullError();
|
||||
}
|
||||
|
||||
method=PyObject_GetAttrString(o,name);
|
||||
if(! method)
|
||||
PyCFunction=PyObject_GetAttrString(o,name);
|
||||
if(! PyCFunction)
|
||||
{
|
||||
va_end(va);
|
||||
PyErr_SetString(PyExc_AttributeError,name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(! (PyCallable_Check(method)))
|
||||
if(! (PyCallable_Check(PyCFunction)))
|
||||
{
|
||||
va_end(va);
|
||||
PyErr_SetString(PyExc_TypeError,"call of non-callable attribute");
|
||||
|
@ -1086,9 +1086,9 @@ PyObject_CallMethod(va_alist) va_dcl
|
|||
args=a;
|
||||
}
|
||||
|
||||
retval = PyObject_CallObject(method,args);
|
||||
retval = PyObject_CallObject(PyCFunction,args);
|
||||
Py_DECREF(args);
|
||||
Py_DECREF(method);
|
||||
Py_DECREF(PyCFunction);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,10 +4,7 @@
|
|||
|
||||
#ifndef WITHOUT_COMPLEX
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "modsupport.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include "Python.h"
|
||||
#include "mymath.h"
|
||||
|
||||
#ifdef i860
|
||||
|
@ -48,8 +45,8 @@
|
|||
#endif
|
||||
|
||||
#if !defined(__STDC__) && !defined(macintosh)
|
||||
extern double fmod PROTO((double, double));
|
||||
extern double pow PROTO((double, double));
|
||||
extern double fmod Py_PROTO((double, double));
|
||||
extern double pow Py_PROTO((double, double));
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -175,14 +172,14 @@ PyObject *
|
|||
PyComplex_FromCComplex(cval)
|
||||
Py_complex cval;
|
||||
{
|
||||
register complexobject *op =
|
||||
(complexobject *) malloc(sizeof(complexobject));
|
||||
register PyComplexObject *op =
|
||||
(PyComplexObject *) malloc(sizeof(PyComplexObject));
|
||||
if (op == NULL)
|
||||
return err_nomem();
|
||||
op->ob_type = &Complextype;
|
||||
return PyErr_NoMemory();
|
||||
op->ob_type = &PyComplex_Type;
|
||||
op->cval = cval;
|
||||
NEWREF(op);
|
||||
return (object *) op;
|
||||
_Py_NewReference(op);
|
||||
return (PyObject *) op;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
|
@ -233,16 +230,16 @@ PyComplex_AsCComplex(op)
|
|||
|
||||
static void
|
||||
complex_dealloc(op)
|
||||
object *op;
|
||||
PyObject *op;
|
||||
{
|
||||
DEL(op);
|
||||
PyMem_DEL(op);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
complex_buf_repr(buf, v)
|
||||
char *buf;
|
||||
complexobject *v;
|
||||
PyComplexObject *v;
|
||||
{
|
||||
if (v->cval.real == 0.)
|
||||
sprintf(buf, "%.12gj", v->cval.imag);
|
||||
|
@ -252,7 +249,7 @@ complex_buf_repr(buf, v)
|
|||
|
||||
static int
|
||||
complex_print(v, fp, flags)
|
||||
complexobject *v;
|
||||
PyComplexObject *v;
|
||||
FILE *fp;
|
||||
int flags; /* Not used but required by interface */
|
||||
{
|
||||
|
@ -262,18 +259,18 @@ complex_print(v, fp, flags)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_repr(v)
|
||||
complexobject *v;
|
||||
PyComplexObject *v;
|
||||
{
|
||||
char buf[100];
|
||||
complex_buf_repr(buf, v);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
static int
|
||||
complex_compare(v, w)
|
||||
complexobject *v, *w;
|
||||
PyComplexObject *v, *w;
|
||||
{
|
||||
/* Note: "greater" and "smaller" have no meaning for complex numbers,
|
||||
but Python requires that they be defined nevertheless. */
|
||||
|
@ -290,7 +287,7 @@ complex_compare(v, w)
|
|||
|
||||
static long
|
||||
complex_hash(v)
|
||||
complexobject *v;
|
||||
PyComplexObject *v;
|
||||
{
|
||||
double intpart, fractpart;
|
||||
int expo;
|
||||
|
@ -312,11 +309,11 @@ complex_hash(v)
|
|||
if (fractpart == 0.0 && v->cval.imag == 0.0) {
|
||||
if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
|
||||
/* Convert to long int and use its hash... */
|
||||
object *w = dnewlongobject(v->cval.real);
|
||||
PyObject *w = PyLong_FromDouble(v->cval.real);
|
||||
if (w == NULL)
|
||||
return -1;
|
||||
x = hashobject(w);
|
||||
DECREF(w);
|
||||
x = PyObject_Hash(w);
|
||||
Py_DECREF(w);
|
||||
return x;
|
||||
}
|
||||
x = (long)intpart;
|
||||
|
@ -359,46 +356,46 @@ complex_hash(v)
|
|||
return x;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_add(v, w)
|
||||
complexobject *v;
|
||||
complexobject *w;
|
||||
PyComplexObject *v;
|
||||
PyComplexObject *w;
|
||||
{
|
||||
Py_complex result;
|
||||
PyFPE_START_PROTECT("complex_add", return 0)
|
||||
result = c_sum(v->cval,w->cval);
|
||||
PyFPE_END_PROTECT(result)
|
||||
return newcomplexobject(result);
|
||||
return PyComplex_FromCComplex(result);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_sub(v, w)
|
||||
complexobject *v;
|
||||
complexobject *w;
|
||||
PyComplexObject *v;
|
||||
PyComplexObject *w;
|
||||
{
|
||||
Py_complex result;
|
||||
PyFPE_START_PROTECT("complex_sub", return 0)
|
||||
result = c_diff(v->cval,w->cval);
|
||||
PyFPE_END_PROTECT(result)
|
||||
return newcomplexobject(result);
|
||||
return PyComplex_FromCComplex(result);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_mul(v, w)
|
||||
complexobject *v;
|
||||
complexobject *w;
|
||||
PyComplexObject *v;
|
||||
PyComplexObject *w;
|
||||
{
|
||||
Py_complex result;
|
||||
PyFPE_START_PROTECT("complex_mul", return 0)
|
||||
result = c_prod(v->cval,w->cval);
|
||||
PyFPE_END_PROTECT(result)
|
||||
return newcomplexobject(result);
|
||||
return PyComplex_FromCComplex(result);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_div(v, w)
|
||||
complexobject *v;
|
||||
complexobject *w;
|
||||
PyComplexObject *v;
|
||||
PyComplexObject *w;
|
||||
{
|
||||
Py_complex quot;
|
||||
PyFPE_START_PROTECT("complex_div", return 0)
|
||||
|
@ -406,74 +403,74 @@ complex_div(v, w)
|
|||
quot = c_quot(v->cval,w->cval);
|
||||
PyFPE_END_PROTECT(quot)
|
||||
if (c_error == 1) {
|
||||
err_setstr(ZeroDivisionError, "complex division");
|
||||
PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
|
||||
return NULL;
|
||||
}
|
||||
return newcomplexobject(quot);
|
||||
return PyComplex_FromCComplex(quot);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_remainder(v, w)
|
||||
complexobject *v;
|
||||
complexobject *w;
|
||||
PyComplexObject *v;
|
||||
PyComplexObject *w;
|
||||
{
|
||||
Py_complex div, mod;
|
||||
c_error = 0;
|
||||
div = c_quot(v->cval,w->cval); /* The raw divisor value. */
|
||||
if (c_error == 1) {
|
||||
err_setstr(ZeroDivisionError, "complex remainder");
|
||||
PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder");
|
||||
return NULL;
|
||||
}
|
||||
div.real = floor(div.real); /* Use the floor of the real part. */
|
||||
div.imag = 0.0;
|
||||
mod = c_diff(v->cval, c_prod(w->cval, div));
|
||||
|
||||
return newcomplexobject(mod);
|
||||
return PyComplex_FromCComplex(mod);
|
||||
}
|
||||
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_divmod(v, w)
|
||||
complexobject *v;
|
||||
complexobject *w;
|
||||
PyComplexObject *v;
|
||||
PyComplexObject *w;
|
||||
{
|
||||
Py_complex div, mod;
|
||||
PyObject *d, *m, *z;
|
||||
c_error = 0;
|
||||
div = c_quot(v->cval,w->cval); /* The raw divisor value. */
|
||||
if (c_error == 1) {
|
||||
err_setstr(ZeroDivisionError, "complex divmod()");
|
||||
PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()");
|
||||
return NULL;
|
||||
}
|
||||
div.real = floor(div.real); /* Use the floor of the real part. */
|
||||
div.imag = 0.0;
|
||||
mod = c_diff(v->cval, c_prod(w->cval, div));
|
||||
d = newcomplexobject(div);
|
||||
m = newcomplexobject(mod);
|
||||
z = mkvalue("(OO)", d, m);
|
||||
d = PyComplex_FromCComplex(div);
|
||||
m = PyComplex_FromCComplex(mod);
|
||||
z = Py_BuildValue("(OO)", d, m);
|
||||
Py_XDECREF(d);
|
||||
Py_XDECREF(m);
|
||||
return z;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_pow(v, w, z)
|
||||
complexobject *v;
|
||||
object *w;
|
||||
complexobject *z;
|
||||
PyComplexObject *v;
|
||||
PyObject *w;
|
||||
PyComplexObject *z;
|
||||
{
|
||||
Py_complex p;
|
||||
Py_complex exponent;
|
||||
long int_exponent;
|
||||
|
||||
if ((object *)z!=None) {
|
||||
err_setstr(ValueError, "complex modulo");
|
||||
if ((PyObject *)z!=Py_None) {
|
||||
PyErr_SetString(PyExc_ValueError, "complex modulo");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyFPE_START_PROTECT("complex_pow", return 0)
|
||||
c_error = 0;
|
||||
exponent = ((complexobject*)w)->cval;
|
||||
exponent = ((PyComplexObject*)w)->cval;
|
||||
int_exponent = (long)exponent.real;
|
||||
if (exponent.imag == 0. && exponent.real == int_exponent)
|
||||
p = c_powi(v->cval,int_exponent);
|
||||
|
@ -482,112 +479,113 @@ complex_pow(v, w, z)
|
|||
|
||||
PyFPE_END_PROTECT(p)
|
||||
if (c_error == 2) {
|
||||
err_setstr(ValueError, "0.0 to a negative or complex power");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"0.0 to a negative or complex power");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return newcomplexobject(p);
|
||||
return PyComplex_FromCComplex(p);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_neg(v)
|
||||
complexobject *v;
|
||||
PyComplexObject *v;
|
||||
{
|
||||
Py_complex neg;
|
||||
neg.real = -v->cval.real;
|
||||
neg.imag = -v->cval.imag;
|
||||
return newcomplexobject(neg);
|
||||
return PyComplex_FromCComplex(neg);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_pos(v)
|
||||
complexobject *v;
|
||||
PyComplexObject *v;
|
||||
{
|
||||
INCREF(v);
|
||||
return (object *)v;
|
||||
Py_INCREF(v);
|
||||
return (PyObject *)v;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_abs(v)
|
||||
complexobject *v;
|
||||
PyComplexObject *v;
|
||||
{
|
||||
double result;
|
||||
PyFPE_START_PROTECT("complex_abs", return 0)
|
||||
result = hypot(v->cval.real,v->cval.imag);
|
||||
PyFPE_END_PROTECT(result)
|
||||
return newfloatobject(result);
|
||||
return PyFloat_FromDouble(result);
|
||||
}
|
||||
|
||||
static int
|
||||
complex_nonzero(v)
|
||||
complexobject *v;
|
||||
PyComplexObject *v;
|
||||
{
|
||||
return v->cval.real != 0.0 && v->cval.imag != 0.0;
|
||||
}
|
||||
|
||||
static int
|
||||
complex_coerce(pv, pw)
|
||||
object **pv;
|
||||
object **pw;
|
||||
PyObject **pv;
|
||||
PyObject **pw;
|
||||
{
|
||||
Py_complex cval;
|
||||
cval.imag = 0.;
|
||||
if (is_intobject(*pw)) {
|
||||
cval.real = (double)getintvalue(*pw);
|
||||
*pw = newcomplexobject(cval);
|
||||
INCREF(*pv);
|
||||
if (PyInt_Check(*pw)) {
|
||||
cval.real = (double)PyInt_AsLong(*pw);
|
||||
*pw = PyComplex_FromCComplex(cval);
|
||||
Py_INCREF(*pv);
|
||||
return 0;
|
||||
}
|
||||
else if (is_longobject(*pw)) {
|
||||
cval.real = dgetlongvalue(*pw);
|
||||
*pw = newcomplexobject(cval);
|
||||
INCREF(*pv);
|
||||
else if (PyLong_Check(*pw)) {
|
||||
cval.real = PyLong_AsDouble(*pw);
|
||||
*pw = PyComplex_FromCComplex(cval);
|
||||
Py_INCREF(*pv);
|
||||
return 0;
|
||||
}
|
||||
else if (is_floatobject(*pw)) {
|
||||
cval.real = getfloatvalue(*pw);
|
||||
*pw = newcomplexobject(cval);
|
||||
INCREF(*pv);
|
||||
else if (PyFloat_Check(*pw)) {
|
||||
cval.real = PyFloat_AsDouble(*pw);
|
||||
*pw = PyComplex_FromCComplex(cval);
|
||||
Py_INCREF(*pv);
|
||||
return 0;
|
||||
}
|
||||
return 1; /* Can't do it */
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_int(v)
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
err_setstr(TypeError,
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"can't convert complex to int; use e.g. int(abs(z))");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_long(v)
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
err_setstr(TypeError,
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"can't convert complex to long; use e.g. long(abs(z))");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_float(v)
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
err_setstr(TypeError,
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"can't convert complex to float; use e.g. abs(z)");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_conjugate(self)
|
||||
object *self;
|
||||
PyObject *self;
|
||||
{
|
||||
Py_complex c;
|
||||
c = ((complexobject *)self)->cval;
|
||||
c = ((PyComplexObject *)self)->cval;
|
||||
c.imag = -c.imag;
|
||||
return newcomplexobject(c);
|
||||
return PyComplex_FromCComplex(c);
|
||||
}
|
||||
|
||||
static PyMethodDef complex_methods[] = {
|
||||
|
@ -596,21 +594,21 @@ static PyMethodDef complex_methods[] = {
|
|||
};
|
||||
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
complex_getattr(self, name)
|
||||
complexobject *self;
|
||||
PyComplexObject *self;
|
||||
char *name;
|
||||
{
|
||||
if (strcmp(name, "real") == 0)
|
||||
return (object *)newfloatobject(self->cval.real);
|
||||
return (PyObject *)PyFloat_FromDouble(self->cval.real);
|
||||
else if (strcmp(name, "imag") == 0)
|
||||
return (object *)newfloatobject(self->cval.imag);
|
||||
return (PyObject *)PyFloat_FromDouble(self->cval.imag);
|
||||
else if (strcmp(name, "__members__") == 0)
|
||||
return mkvalue("[ss]", "imag", "real");
|
||||
return findmethod(complex_methods, (object *)self, name);
|
||||
return Py_BuildValue("[ss]", "imag", "real");
|
||||
return Py_FindMethod(complex_methods, (PyObject *)self, name);
|
||||
}
|
||||
|
||||
static number_methods complex_as_number = {
|
||||
static PyNumberMethods complex_as_number = {
|
||||
(binaryfunc)complex_add, /*nb_add*/
|
||||
(binaryfunc)complex_sub, /*nb_subtract*/
|
||||
(binaryfunc)complex_mul, /*nb_multiply*/
|
||||
|
@ -636,11 +634,11 @@ static number_methods complex_as_number = {
|
|||
0, /*nb_hex*/
|
||||
};
|
||||
|
||||
typeobject Complextype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyComplex_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
"complex",
|
||||
sizeof(complexobject),
|
||||
sizeof(PyComplexObject),
|
||||
0,
|
||||
(destructor)complex_dealloc, /*tp_dealloc*/
|
||||
(printfunc)complex_print, /*tp_print*/
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -34,10 +34,8 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
/* XXX There should be overflow checks here, but it's hard to check
|
||||
for any kind of float exception without losing portability. */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "modsupport.h"
|
||||
#include "Python.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
#include "mymath.h"
|
||||
|
||||
|
@ -81,62 +79,64 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
#endif
|
||||
|
||||
#if !defined(__STDC__) && !defined(macintosh)
|
||||
extern double fmod PROTO((double, double));
|
||||
extern double pow PROTO((double, double));
|
||||
extern double fmod Py_PROTO((double, double));
|
||||
extern double pow Py_PROTO((double, double));
|
||||
#endif
|
||||
|
||||
object *
|
||||
PyObject *
|
||||
#ifdef __SC__
|
||||
newfloatobject(double fval)
|
||||
PyFloat_FromDouble(double fval)
|
||||
#else
|
||||
newfloatobject(fval)
|
||||
PyFloat_FromDouble(fval)
|
||||
double fval;
|
||||
#endif
|
||||
{
|
||||
/* For efficiency, this code is copied from newobject() */
|
||||
register floatobject *op = (floatobject *) malloc(sizeof(floatobject));
|
||||
register PyFloatObject *op =
|
||||
(PyFloatObject *) malloc(sizeof(PyFloatObject));
|
||||
if (op == NULL)
|
||||
return err_nomem();
|
||||
op->ob_type = &Floattype;
|
||||
return PyErr_NoMemory();
|
||||
op->ob_type = &PyFloat_Type;
|
||||
op->ob_fval = fval;
|
||||
NEWREF(op);
|
||||
return (object *) op;
|
||||
_Py_NewReference(op);
|
||||
return (PyObject *) op;
|
||||
}
|
||||
|
||||
static void
|
||||
float_dealloc(op)
|
||||
object *op;
|
||||
PyObject *op;
|
||||
{
|
||||
DEL(op);
|
||||
PyMem_DEL(op);
|
||||
}
|
||||
|
||||
double
|
||||
getfloatvalue(op)
|
||||
object *op;
|
||||
PyFloat_AsDouble(op)
|
||||
PyObject *op;
|
||||
{
|
||||
number_methods *nb;
|
||||
floatobject *fo;
|
||||
PyNumberMethods *nb;
|
||||
PyFloatObject *fo;
|
||||
double val;
|
||||
|
||||
if (op && is_floatobject(op))
|
||||
return GETFLOATVALUE((floatobject*) op);
|
||||
if (op && PyFloat_Check(op))
|
||||
return PyFloat_AS_DOUBLE((PyFloatObject*) op);
|
||||
|
||||
if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
|
||||
nb->nb_float == NULL) {
|
||||
err_badarg();
|
||||
PyErr_BadArgument();
|
||||
return -1;
|
||||
}
|
||||
|
||||
fo = (floatobject*) (*nb->nb_float) (op);
|
||||
fo = (PyFloatObject*) (*nb->nb_float) (op);
|
||||
if (fo == NULL)
|
||||
return -1;
|
||||
if (!is_floatobject(fo)) {
|
||||
err_setstr(TypeError, "nb_float should return float object");
|
||||
if (!PyFloat_Check(fo)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"nb_float should return float object");
|
||||
return -1;
|
||||
}
|
||||
|
||||
val = GETFLOATVALUE(fo);
|
||||
DECREF(fo);
|
||||
val = PyFloat_AS_DOUBLE(fo);
|
||||
Py_DECREF(fo);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
@ -144,9 +144,9 @@ getfloatvalue(op)
|
|||
/* Methods */
|
||||
|
||||
void
|
||||
float_buf_repr(buf, v)
|
||||
PyFloat_AsString(buf, v)
|
||||
char *buf;
|
||||
floatobject *v;
|
||||
PyFloatObject *v;
|
||||
{
|
||||
register char *cp;
|
||||
/* Subroutine for float_repr and float_print.
|
||||
|
@ -174,28 +174,28 @@ float_buf_repr(buf, v)
|
|||
/* ARGSUSED */
|
||||
static int
|
||||
float_print(v, fp, flags)
|
||||
floatobject *v;
|
||||
PyFloatObject *v;
|
||||
FILE *fp;
|
||||
int flags; /* Not used but required by interface */
|
||||
{
|
||||
char buf[100];
|
||||
float_buf_repr(buf, v);
|
||||
PyFloat_AsString(buf, v);
|
||||
fputs(buf, fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_repr(v)
|
||||
floatobject *v;
|
||||
PyFloatObject *v;
|
||||
{
|
||||
char buf[100];
|
||||
float_buf_repr(buf, v);
|
||||
return newstringobject(buf);
|
||||
PyFloat_AsString(buf, v);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
static int
|
||||
float_compare(v, w)
|
||||
floatobject *v, *w;
|
||||
PyFloatObject *v, *w;
|
||||
{
|
||||
double i = v->ob_fval;
|
||||
double j = w->ob_fval;
|
||||
|
@ -204,7 +204,7 @@ float_compare(v, w)
|
|||
|
||||
static long
|
||||
float_hash(v)
|
||||
floatobject *v;
|
||||
PyFloatObject *v;
|
||||
{
|
||||
double intpart, fractpart;
|
||||
int expo;
|
||||
|
@ -226,11 +226,11 @@ float_hash(v)
|
|||
if (fractpart == 0.0) {
|
||||
if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
|
||||
/* Convert to long int and use its hash... */
|
||||
object *w = dnewlongobject(v->ob_fval);
|
||||
PyObject *w = PyLong_FromDouble(v->ob_fval);
|
||||
if (w == NULL)
|
||||
return -1;
|
||||
x = hashobject(w);
|
||||
DECREF(w);
|
||||
x = PyObject_Hash(w);
|
||||
Py_DECREF(w);
|
||||
return x;
|
||||
}
|
||||
x = (long)intpart;
|
||||
|
@ -252,69 +252,69 @@ float_hash(v)
|
|||
return x;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_add(v, w)
|
||||
floatobject *v;
|
||||
floatobject *w;
|
||||
PyFloatObject *v;
|
||||
PyFloatObject *w;
|
||||
{
|
||||
double result;
|
||||
PyFPE_START_PROTECT("add", return 0)
|
||||
result = v->ob_fval + w->ob_fval;
|
||||
PyFPE_END_PROTECT(result)
|
||||
return newfloatobject(result);
|
||||
return PyFloat_FromDouble(result);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_sub(v, w)
|
||||
floatobject *v;
|
||||
floatobject *w;
|
||||
PyFloatObject *v;
|
||||
PyFloatObject *w;
|
||||
{
|
||||
double result;
|
||||
PyFPE_START_PROTECT("subtract", return 0)
|
||||
result = v->ob_fval - w->ob_fval;
|
||||
PyFPE_END_PROTECT(result)
|
||||
return newfloatobject(result);
|
||||
return PyFloat_FromDouble(result);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_mul(v, w)
|
||||
floatobject *v;
|
||||
floatobject *w;
|
||||
PyFloatObject *v;
|
||||
PyFloatObject *w;
|
||||
{
|
||||
double result;
|
||||
|
||||
PyFPE_START_PROTECT("multiply", return 0)
|
||||
result = v->ob_fval * w->ob_fval;
|
||||
PyFPE_END_PROTECT(result)
|
||||
return newfloatobject(result);
|
||||
return PyFloat_FromDouble(result);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_div(v, w)
|
||||
floatobject *v;
|
||||
floatobject *w;
|
||||
PyFloatObject *v;
|
||||
PyFloatObject *w;
|
||||
{
|
||||
double result;
|
||||
if (w->ob_fval == 0) {
|
||||
err_setstr(ZeroDivisionError, "float division");
|
||||
PyErr_SetString(PyExc_ZeroDivisionError, "float division");
|
||||
return NULL;
|
||||
}
|
||||
PyFPE_START_PROTECT("divide", return 0)
|
||||
result = v->ob_fval / w->ob_fval;
|
||||
PyFPE_END_PROTECT(result)
|
||||
return newfloatobject(result);
|
||||
return PyFloat_FromDouble(result);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_rem(v, w)
|
||||
floatobject *v;
|
||||
floatobject *w;
|
||||
PyFloatObject *v;
|
||||
PyFloatObject *w;
|
||||
{
|
||||
double vx, wx;
|
||||
double /* div, */ mod;
|
||||
wx = w->ob_fval;
|
||||
if (wx == 0.0) {
|
||||
err_setstr(ZeroDivisionError, "float modulo");
|
||||
PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
|
||||
return NULL;
|
||||
}
|
||||
PyFPE_START_PROTECT("modulo", return 0)
|
||||
|
@ -326,19 +326,19 @@ float_rem(v, w)
|
|||
/* div -= 1.0; */
|
||||
}
|
||||
PyFPE_END_PROTECT(mod)
|
||||
return newfloatobject(mod);
|
||||
return PyFloat_FromDouble(mod);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_divmod(v, w)
|
||||
floatobject *v;
|
||||
floatobject *w;
|
||||
PyFloatObject *v;
|
||||
PyFloatObject *w;
|
||||
{
|
||||
double vx, wx;
|
||||
double div, mod;
|
||||
wx = w->ob_fval;
|
||||
if (wx == 0.0) {
|
||||
err_setstr(ZeroDivisionError, "float divmod()");
|
||||
PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
|
||||
return NULL;
|
||||
}
|
||||
PyFPE_START_PROTECT("divmod", return 0)
|
||||
|
@ -350,7 +350,7 @@ float_divmod(v, w)
|
|||
div -= 1.0;
|
||||
}
|
||||
PyFPE_END_PROTECT(div)
|
||||
return mkvalue("(dd)", div, mod);
|
||||
return Py_BuildValue("(dd)", div, mod);
|
||||
}
|
||||
|
||||
static double powu(x, n)
|
||||
|
@ -369,11 +369,11 @@ static double powu(x, n)
|
|||
return r;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_pow(v, w, z)
|
||||
floatobject *v;
|
||||
object *w;
|
||||
floatobject *z;
|
||||
PyFloatObject *v;
|
||||
PyObject *w;
|
||||
PyFloatObject *z;
|
||||
{
|
||||
double iv, iw, ix;
|
||||
long intw;
|
||||
|
@ -383,19 +383,19 @@ float_pow(v, w, z)
|
|||
* [AMK]
|
||||
*/
|
||||
iv = v->ob_fval;
|
||||
iw = ((floatobject *)w)->ob_fval;
|
||||
iw = ((PyFloatObject *)w)->ob_fval;
|
||||
intw = (long)iw;
|
||||
if (iw == intw && -10000 < intw && intw < 10000) {
|
||||
/* Sort out special cases here instead of relying on pow() */
|
||||
if (intw == 0) { /* x**0 is 1, even 0**0 */
|
||||
PyFPE_START_PROTECT("pow", return 0)
|
||||
if ((object *)z!=None) {
|
||||
if ((PyObject *)z!=Py_None) {
|
||||
ix=fmod(1.0, z->ob_fval);
|
||||
if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval;
|
||||
}
|
||||
else ix=1.0;
|
||||
PyFPE_END_PROTECT(ix)
|
||||
return newfloatobject(ix);
|
||||
return PyFloat_FromDouble(ix);
|
||||
}
|
||||
errno = 0;
|
||||
PyFPE_START_PROTECT("pow", return 0)
|
||||
|
@ -409,14 +409,14 @@ float_pow(v, w, z)
|
|||
/* Sort out special cases here instead of relying on pow() */
|
||||
if (iv == 0.0) {
|
||||
if (iw < 0.0) {
|
||||
err_setstr(ValueError,
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"0.0 to a negative power");
|
||||
return NULL;
|
||||
}
|
||||
return newfloatobject(0.0);
|
||||
return PyFloat_FromDouble(0.0);
|
||||
}
|
||||
if (iv < 0.0) {
|
||||
err_setstr(ValueError,
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"negative number to a float power");
|
||||
return NULL;
|
||||
}
|
||||
|
@ -428,10 +428,10 @@ float_pow(v, w, z)
|
|||
CHECK(ix);
|
||||
if (errno != 0) {
|
||||
/* XXX could it be another type of error? */
|
||||
err_errno(OverflowError);
|
||||
PyErr_SetFromErrno(PyExc_OverflowError);
|
||||
return NULL;
|
||||
}
|
||||
if ((object *)z!=None) {
|
||||
if ((PyObject *)z!=Py_None) {
|
||||
PyFPE_START_PROTECT("pow", return 0)
|
||||
ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */
|
||||
if ( ix!=0 &&
|
||||
|
@ -440,27 +440,27 @@ float_pow(v, w, z)
|
|||
}
|
||||
PyFPE_END_PROTECT(ix)
|
||||
}
|
||||
return newfloatobject(ix);
|
||||
return PyFloat_FromDouble(ix);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_neg(v)
|
||||
floatobject *v;
|
||||
PyFloatObject *v;
|
||||
{
|
||||
return newfloatobject(-v->ob_fval);
|
||||
return PyFloat_FromDouble(-v->ob_fval);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_pos(v)
|
||||
floatobject *v;
|
||||
PyFloatObject *v;
|
||||
{
|
||||
INCREF(v);
|
||||
return (object *)v;
|
||||
Py_INCREF(v);
|
||||
return (PyObject *)v;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_abs(v)
|
||||
floatobject *v;
|
||||
PyFloatObject *v;
|
||||
{
|
||||
if (v->ob_fval < 0)
|
||||
return float_neg(v);
|
||||
|
@ -470,61 +470,62 @@ float_abs(v)
|
|||
|
||||
static int
|
||||
float_nonzero(v)
|
||||
floatobject *v;
|
||||
PyFloatObject *v;
|
||||
{
|
||||
return v->ob_fval != 0.0;
|
||||
}
|
||||
|
||||
static int
|
||||
float_coerce(pv, pw)
|
||||
object **pv;
|
||||
object **pw;
|
||||
PyObject **pv;
|
||||
PyObject **pw;
|
||||
{
|
||||
if (is_intobject(*pw)) {
|
||||
long x = getintvalue(*pw);
|
||||
*pw = newfloatobject((double)x);
|
||||
INCREF(*pv);
|
||||
if (PyInt_Check(*pw)) {
|
||||
long x = PyInt_AsLong(*pw);
|
||||
*pw = PyFloat_FromDouble((double)x);
|
||||
Py_INCREF(*pv);
|
||||
return 0;
|
||||
}
|
||||
else if (is_longobject(*pw)) {
|
||||
*pw = newfloatobject(dgetlongvalue(*pw));
|
||||
INCREF(*pv);
|
||||
else if (PyLong_Check(*pw)) {
|
||||
*pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
|
||||
Py_INCREF(*pv);
|
||||
return 0;
|
||||
}
|
||||
return 1; /* Can't do it */
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_int(v)
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
double x = getfloatvalue(v);
|
||||
double x = PyFloat_AsDouble(v);
|
||||
if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN
|
||||
: (x = floor(x)) > (double)LONG_MAX) {
|
||||
err_setstr(OverflowError, "float too large to convert");
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"float too large to convert");
|
||||
return NULL;
|
||||
}
|
||||
return newintobject((long)x);
|
||||
return PyInt_FromLong((long)x);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_long(v)
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
double x = getfloatvalue(v);
|
||||
return dnewlongobject(x);
|
||||
double x = PyFloat_AsDouble(v);
|
||||
return PyLong_FromDouble(x);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
float_float(v)
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
INCREF(v);
|
||||
Py_INCREF(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
static number_methods float_as_number = {
|
||||
static PyNumberMethods float_as_number = {
|
||||
(binaryfunc)float_add, /*nb_add*/
|
||||
(binaryfunc)float_sub, /*nb_subtract*/
|
||||
(binaryfunc)float_mul, /*nb_multiply*/
|
||||
|
@ -550,11 +551,11 @@ static number_methods float_as_number = {
|
|||
0, /*nb_hex*/
|
||||
};
|
||||
|
||||
typeobject Floattype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyFloat_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
"float",
|
||||
sizeof(floatobject),
|
||||
sizeof(PyFloatObject),
|
||||
0,
|
||||
(destructor)float_dealloc, /*tp_dealloc*/
|
||||
(printfunc)float_print, /*tp_print*/
|
||||
|
|
|
@ -31,98 +31,99 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
/* Function object implementation */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "Python.h"
|
||||
#include "compile.h"
|
||||
#include "structmember.h"
|
||||
|
||||
object *
|
||||
newfuncobject(code, globals)
|
||||
object *code;
|
||||
object *globals;
|
||||
PyObject *
|
||||
PyFunction_New(code, globals)
|
||||
PyObject *code;
|
||||
PyObject *globals;
|
||||
{
|
||||
funcobject *op = NEWOBJ(funcobject, &Functype);
|
||||
PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
|
||||
&PyFunction_Type);
|
||||
if (op != NULL) {
|
||||
object *doc;
|
||||
object *consts;
|
||||
INCREF(code);
|
||||
PyObject *doc;
|
||||
PyObject *consts;
|
||||
Py_INCREF(code);
|
||||
op->func_code = code;
|
||||
INCREF(globals);
|
||||
Py_INCREF(globals);
|
||||
op->func_globals = globals;
|
||||
op->func_name = ((codeobject *)code)->co_name;
|
||||
INCREF(op->func_name);
|
||||
op->func_name = ((PyCodeObject *)code)->co_name;
|
||||
Py_INCREF(op->func_name);
|
||||
op->func_defaults = NULL; /* No default arguments */
|
||||
consts = ((codeobject *)code)->co_consts;
|
||||
if (gettuplesize(consts) >= 1) {
|
||||
doc = gettupleitem(consts, 0);
|
||||
if (!is_stringobject(doc))
|
||||
doc = None;
|
||||
consts = ((PyCodeObject *)code)->co_consts;
|
||||
if (PyTuple_Size(consts) >= 1) {
|
||||
doc = PyTuple_GetItem(consts, 0);
|
||||
if (!PyString_Check(doc))
|
||||
doc = Py_None;
|
||||
}
|
||||
else
|
||||
doc = None;
|
||||
INCREF(doc);
|
||||
doc = Py_None;
|
||||
Py_INCREF(doc);
|
||||
op->func_doc = doc;
|
||||
}
|
||||
return (object *)op;
|
||||
return (PyObject *)op;
|
||||
}
|
||||
|
||||
object *
|
||||
getfunccode(op)
|
||||
object *op;
|
||||
PyObject *
|
||||
PyFunction_GetCode(op)
|
||||
PyObject *op;
|
||||
{
|
||||
if (!is_funcobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyFunction_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
return ((funcobject *) op) -> func_code;
|
||||
return ((PyFunctionObject *) op) -> func_code;
|
||||
}
|
||||
|
||||
object *
|
||||
getfuncglobals(op)
|
||||
object *op;
|
||||
PyObject *
|
||||
PyFunction_GetGlobals(op)
|
||||
PyObject *op;
|
||||
{
|
||||
if (!is_funcobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyFunction_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
return ((funcobject *) op) -> func_globals;
|
||||
return ((PyFunctionObject *) op) -> func_globals;
|
||||
}
|
||||
|
||||
object *
|
||||
PyObject *
|
||||
PyFunction_GetDefaults(op)
|
||||
object *op;
|
||||
PyObject *op;
|
||||
{
|
||||
if (!is_funcobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyFunction_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
return ((funcobject *) op) -> func_defaults;
|
||||
return ((PyFunctionObject *) op) -> func_defaults;
|
||||
}
|
||||
|
||||
int
|
||||
PyFunction_SetDefaults(op, defaults)
|
||||
object *op;
|
||||
object *defaults;
|
||||
PyObject *op;
|
||||
PyObject *defaults;
|
||||
{
|
||||
if (!is_funcobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyFunction_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
if (defaults == None)
|
||||
if (defaults == Py_None)
|
||||
defaults = NULL;
|
||||
else if (is_tupleobject(defaults))
|
||||
XINCREF(defaults);
|
||||
else if (PyTuple_Check(defaults))
|
||||
Py_XINCREF(defaults);
|
||||
else {
|
||||
err_setstr(SystemError, "non-tuple default args");
|
||||
PyErr_SetString(PyExc_SystemError, "non-tuple default args");
|
||||
return -1;
|
||||
}
|
||||
XDECREF(((funcobject *) op) -> func_defaults);
|
||||
((funcobject *) op) -> func_defaults = defaults;
|
||||
Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
|
||||
((PyFunctionObject *) op) -> func_defaults = defaults;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Methods */
|
||||
|
||||
#define OFF(x) offsetof(funcobject, x)
|
||||
#define OFF(x) offsetof(PyFunctionObject, x)
|
||||
|
||||
static struct memberlist func_memberlist[] = {
|
||||
{"func_code", T_OBJECT, OFF(func_code), READONLY},
|
||||
|
@ -135,75 +136,75 @@ static struct memberlist func_memberlist[] = {
|
|||
{NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
func_getattr(op, name)
|
||||
funcobject *op;
|
||||
PyFunctionObject *op;
|
||||
char *name;
|
||||
{
|
||||
if (name[0] != '_' && getrestricted()) {
|
||||
err_setstr(RuntimeError,
|
||||
if (name[0] != '_' && PyEval_GetRestricted()) {
|
||||
PyErr_SetString(PyExc_RuntimeError,
|
||||
"function attributes not accessible in restricted mode");
|
||||
return NULL;
|
||||
}
|
||||
return getmember((char *)op, func_memberlist, name);
|
||||
return PyMember_Get((char *)op, func_memberlist, name);
|
||||
}
|
||||
|
||||
static void
|
||||
func_dealloc(op)
|
||||
funcobject *op;
|
||||
PyFunctionObject *op;
|
||||
{
|
||||
DECREF(op->func_code);
|
||||
DECREF(op->func_globals);
|
||||
DECREF(op->func_name);
|
||||
XDECREF(op->func_defaults);
|
||||
XDECREF(op->func_doc);
|
||||
DEL(op);
|
||||
Py_DECREF(op->func_code);
|
||||
Py_DECREF(op->func_globals);
|
||||
Py_DECREF(op->func_name);
|
||||
Py_XDECREF(op->func_defaults);
|
||||
Py_XDECREF(op->func_doc);
|
||||
PyMem_DEL(op);
|
||||
}
|
||||
|
||||
static object*
|
||||
static PyObject*
|
||||
func_repr(op)
|
||||
funcobject *op;
|
||||
PyFunctionObject *op;
|
||||
{
|
||||
char buf[140];
|
||||
if (op->func_name == None)
|
||||
if (op->func_name == Py_None)
|
||||
sprintf(buf, "<anonymous function at %lx>", (long)op);
|
||||
else
|
||||
sprintf(buf, "<function %.100s at %lx>",
|
||||
getstringvalue(op->func_name),
|
||||
PyString_AsString(op->func_name),
|
||||
(long)op);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
static int
|
||||
func_compare(f, g)
|
||||
funcobject *f, *g;
|
||||
PyFunctionObject *f, *g;
|
||||
{
|
||||
int c;
|
||||
if (f->func_globals != g->func_globals)
|
||||
return (f->func_globals < g->func_globals) ? -1 : 1;
|
||||
c = cmpobject(f->func_defaults, g->func_defaults);
|
||||
c = PyObject_Compare(f->func_defaults, g->func_defaults);
|
||||
if (c != 0)
|
||||
return c;
|
||||
return cmpobject(f->func_code, g->func_code);
|
||||
return PyObject_Compare(f->func_code, g->func_code);
|
||||
}
|
||||
|
||||
static long
|
||||
func_hash(f)
|
||||
funcobject *f;
|
||||
PyFunctionObject *f;
|
||||
{
|
||||
long h;
|
||||
h = hashobject(f->func_code);
|
||||
h = PyObject_Hash(f->func_code);
|
||||
if (h == -1) return h;
|
||||
h = h ^ (long)f->func_globals;
|
||||
if (h == -1) h = -2;
|
||||
return h;
|
||||
}
|
||||
|
||||
typeobject Functype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyFunction_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
"function",
|
||||
sizeof(funcobject),
|
||||
sizeof(PyFunctionObject),
|
||||
0,
|
||||
(destructor)func_dealloc, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
|
|
|
@ -31,8 +31,7 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
/* Integer object implementation */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "modsupport.h"
|
||||
#include "Python.h"
|
||||
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
|
@ -55,28 +54,28 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
#endif
|
||||
|
||||
long
|
||||
getmaxint()
|
||||
PyInt_GetMax()
|
||||
{
|
||||
return LONG_MAX; /* To initialize sys.maxint */
|
||||
}
|
||||
|
||||
/* Standard Booleans */
|
||||
|
||||
intobject FalseObject = {
|
||||
OB_HEAD_INIT(&Inttype)
|
||||
PyIntObject _Py_ZeroStruct = {
|
||||
PyObject_HEAD_INIT(&PyInt_Type)
|
||||
0
|
||||
};
|
||||
|
||||
intobject TrueObject = {
|
||||
OB_HEAD_INIT(&Inttype)
|
||||
PyIntObject _Py_TrueStruct = {
|
||||
PyObject_HEAD_INIT(&PyInt_Type)
|
||||
1
|
||||
};
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
err_ovf(msg)
|
||||
char *msg;
|
||||
{
|
||||
err_setstr(OverflowError, msg);
|
||||
PyErr_SetString(PyExc_OverflowError, msg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -91,23 +90,23 @@ err_ovf(msg)
|
|||
*/
|
||||
|
||||
#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
|
||||
#define N_INTOBJECTS (BLOCK_SIZE / sizeof(intobject))
|
||||
#define N_INTOBJECTS (BLOCK_SIZE / sizeof(PyIntObject))
|
||||
|
||||
static intobject *
|
||||
static PyIntObject *
|
||||
fill_free_list()
|
||||
{
|
||||
intobject *p, *q;
|
||||
p = NEW(intobject, N_INTOBJECTS);
|
||||
PyIntObject *p, *q;
|
||||
p = PyMem_NEW(PyIntObject, N_INTOBJECTS);
|
||||
if (p == NULL)
|
||||
return (intobject *)err_nomem();
|
||||
return (PyIntObject *)PyErr_NoMemory();
|
||||
q = p + N_INTOBJECTS;
|
||||
while (--q > p)
|
||||
*(intobject **)q = q-1;
|
||||
*(intobject **)q = NULL;
|
||||
*(PyIntObject **)q = q-1;
|
||||
*(PyIntObject **)q = NULL;
|
||||
return p + N_INTOBJECTS - 1;
|
||||
}
|
||||
|
||||
static intobject *free_list = NULL;
|
||||
static PyIntObject *free_list = NULL;
|
||||
#ifndef NSMALLPOSINTS
|
||||
#define NSMALLPOSINTS 100
|
||||
#endif
|
||||
|
@ -120,28 +119,28 @@ static intobject *free_list = NULL;
|
|||
The integers that are saved are those in the range
|
||||
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
|
||||
*/
|
||||
static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
|
||||
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
|
||||
#endif
|
||||
#ifdef COUNT_ALLOCS
|
||||
int quick_int_allocs, quick_neg_int_allocs;
|
||||
#endif
|
||||
|
||||
object *
|
||||
newintobject(ival)
|
||||
PyObject *
|
||||
PyInt_FromLong(ival)
|
||||
long ival;
|
||||
{
|
||||
register intobject *v;
|
||||
register PyIntObject *v;
|
||||
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
||||
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
|
||||
(v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
|
||||
INCREF(v);
|
||||
Py_INCREF(v);
|
||||
#ifdef COUNT_ALLOCS
|
||||
if (ival >= 0)
|
||||
quick_int_allocs++;
|
||||
else
|
||||
quick_neg_int_allocs++;
|
||||
#endif
|
||||
return (object *) v;
|
||||
return (PyObject *) v;
|
||||
}
|
||||
#endif
|
||||
if (free_list == NULL) {
|
||||
|
@ -149,55 +148,56 @@ newintobject(ival)
|
|||
return NULL;
|
||||
}
|
||||
v = free_list;
|
||||
free_list = *(intobject **)free_list;
|
||||
v->ob_type = &Inttype;
|
||||
free_list = *(PyIntObject **)free_list;
|
||||
v->ob_type = &PyInt_Type;
|
||||
v->ob_ival = ival;
|
||||
NEWREF(v);
|
||||
_Py_NewReference(v);
|
||||
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
||||
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
|
||||
/* save this one for a following allocation */
|
||||
INCREF(v);
|
||||
Py_INCREF(v);
|
||||
small_ints[ival + NSMALLNEGINTS] = v;
|
||||
}
|
||||
#endif
|
||||
return (object *) v;
|
||||
return (PyObject *) v;
|
||||
}
|
||||
|
||||
static void
|
||||
int_dealloc(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
*(intobject **)v = free_list;
|
||||
*(PyIntObject **)v = free_list;
|
||||
free_list = v;
|
||||
}
|
||||
|
||||
long
|
||||
getintvalue(op)
|
||||
register object *op;
|
||||
PyInt_AsLong(op)
|
||||
register PyObject *op;
|
||||
{
|
||||
number_methods *nb;
|
||||
intobject *io;
|
||||
PyNumberMethods *nb;
|
||||
PyIntObject *io;
|
||||
long val;
|
||||
|
||||
if (op && is_intobject(op))
|
||||
return GETINTVALUE((intobject*) op);
|
||||
if (op && PyInt_Check(op))
|
||||
return PyInt_AS_LONG((PyIntObject*) op);
|
||||
|
||||
if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
|
||||
nb->nb_int == NULL) {
|
||||
err_badarg();
|
||||
PyErr_BadArgument();
|
||||
return -1;
|
||||
}
|
||||
|
||||
io = (intobject*) (*nb->nb_int) (op);
|
||||
io = (PyIntObject*) (*nb->nb_int) (op);
|
||||
if (io == NULL)
|
||||
return -1;
|
||||
if (!is_intobject(io)) {
|
||||
err_setstr(TypeError, "nb_int should return int object");
|
||||
if (!PyInt_Check(io)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"nb_int should return int object");
|
||||
return -1;
|
||||
}
|
||||
|
||||
val = GETINTVALUE(io);
|
||||
DECREF(io);
|
||||
val = PyInt_AS_LONG(io);
|
||||
Py_DECREF(io);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
@ -207,7 +207,7 @@ getintvalue(op)
|
|||
/* ARGSUSED */
|
||||
static int
|
||||
int_print(v, fp, flags)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
FILE *fp;
|
||||
int flags; /* Not used but required by interface */
|
||||
{
|
||||
|
@ -215,18 +215,18 @@ int_print(v, fp, flags)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_repr(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
char buf[20];
|
||||
sprintf(buf, "%ld", v->ob_ival);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
static int
|
||||
int_compare(v, w)
|
||||
intobject *v, *w;
|
||||
PyIntObject *v, *w;
|
||||
{
|
||||
register long i = v->ob_ival;
|
||||
register long j = w->ob_ival;
|
||||
|
@ -235,7 +235,7 @@ int_compare(v, w)
|
|||
|
||||
static long
|
||||
int_hash(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
/* XXX If this is changed, you also need to change the way
|
||||
Python's long, float and complex types are hashed. */
|
||||
|
@ -245,10 +245,10 @@ int_hash(v)
|
|||
return x;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_add(v, w)
|
||||
intobject *v;
|
||||
intobject *w;
|
||||
PyIntObject *v;
|
||||
PyIntObject *w;
|
||||
{
|
||||
register long a, b, x;
|
||||
a = v->ob_ival;
|
||||
|
@ -256,13 +256,13 @@ int_add(v, w)
|
|||
x = a + b;
|
||||
if ((x^a) < 0 && (x^b) < 0)
|
||||
return err_ovf("integer addition");
|
||||
return newintobject(x);
|
||||
return PyInt_FromLong(x);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_sub(v, w)
|
||||
intobject *v;
|
||||
intobject *w;
|
||||
PyIntObject *v;
|
||||
PyIntObject *w;
|
||||
{
|
||||
register long a, b, x;
|
||||
a = v->ob_ival;
|
||||
|
@ -270,7 +270,7 @@ int_sub(v, w)
|
|||
x = a - b;
|
||||
if ((x^a) < 0 && (x^~b) < 0)
|
||||
return err_ovf("integer subtraction");
|
||||
return newintobject(x);
|
||||
return PyInt_FromLong(x);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -302,10 +302,10 @@ guess the above is the preferred solution.
|
|||
|
||||
*/
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_mul(v, w)
|
||||
intobject *v;
|
||||
intobject *w;
|
||||
PyIntObject *v;
|
||||
PyIntObject *w;
|
||||
{
|
||||
long a, b, ah, bh, x, y;
|
||||
int s = 1;
|
||||
|
@ -321,7 +321,7 @@ int_mul(v, w)
|
|||
x = a*b;
|
||||
if (x < 0)
|
||||
goto bad;
|
||||
return newintobject(x);
|
||||
return PyInt_FromLong(x);
|
||||
}
|
||||
|
||||
/* Arrange that a >= b >= 0 */
|
||||
|
@ -367,7 +367,7 @@ int_mul(v, w)
|
|||
x = a*b;
|
||||
if (x < 0)
|
||||
goto bad;
|
||||
return newintobject(x*s);
|
||||
return PyInt_FromLong(x*s);
|
||||
}
|
||||
|
||||
if (a < b) {
|
||||
|
@ -397,7 +397,7 @@ int_mul(v, w)
|
|||
if (x < 0)
|
||||
goto bad;
|
||||
ok:
|
||||
return newintobject(x * s);
|
||||
return PyInt_FromLong(x * s);
|
||||
|
||||
bad:
|
||||
return err_ovf("integer multiplication");
|
||||
|
@ -405,7 +405,7 @@ int_mul(v, w)
|
|||
|
||||
static int
|
||||
i_divmod(x, y, p_xdivy, p_xmody)
|
||||
register intobject *x, *y;
|
||||
register PyIntObject *x, *y;
|
||||
long *p_xdivy, *p_xmody;
|
||||
{
|
||||
long xi = x->ob_ival;
|
||||
|
@ -413,7 +413,8 @@ i_divmod(x, y, p_xdivy, p_xmody)
|
|||
long xdivy, xmody;
|
||||
|
||||
if (yi == 0) {
|
||||
err_setstr(ZeroDivisionError, "integer division or modulo");
|
||||
PyErr_SetString(PyExc_ZeroDivisionError,
|
||||
"integer division or modulo");
|
||||
return -1;
|
||||
}
|
||||
if (yi < 0) {
|
||||
|
@ -438,57 +439,59 @@ i_divmod(x, y, p_xdivy, p_xmody)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_div(x, y)
|
||||
intobject *x;
|
||||
intobject *y;
|
||||
PyIntObject *x;
|
||||
PyIntObject *y;
|
||||
{
|
||||
long d, m;
|
||||
if (i_divmod(x, y, &d, &m) < 0)
|
||||
return NULL;
|
||||
return newintobject(d);
|
||||
return PyInt_FromLong(d);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_mod(x, y)
|
||||
intobject *x;
|
||||
intobject *y;
|
||||
PyIntObject *x;
|
||||
PyIntObject *y;
|
||||
{
|
||||
long d, m;
|
||||
if (i_divmod(x, y, &d, &m) < 0)
|
||||
return NULL;
|
||||
return newintobject(m);
|
||||
return PyInt_FromLong(m);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_divmod(x, y)
|
||||
intobject *x;
|
||||
intobject *y;
|
||||
PyIntObject *x;
|
||||
PyIntObject *y;
|
||||
{
|
||||
long d, m;
|
||||
if (i_divmod(x, y, &d, &m) < 0)
|
||||
return NULL;
|
||||
return mkvalue("(ll)", d, m);
|
||||
return Py_BuildValue("(ll)", d, m);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_pow(v, w, z)
|
||||
intobject *v;
|
||||
intobject *w;
|
||||
intobject *z;
|
||||
PyIntObject *v;
|
||||
PyIntObject *w;
|
||||
PyIntObject *z;
|
||||
{
|
||||
#if 1
|
||||
register long iv, iw, iz=0, ix, temp, prev;
|
||||
iv = v->ob_ival;
|
||||
iw = w->ob_ival;
|
||||
if (iw < 0) {
|
||||
err_setstr(ValueError, "integer to the negative power");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"integer to the negative power");
|
||||
return NULL;
|
||||
}
|
||||
if ((object *)z != None) {
|
||||
if ((PyObject *)z != Py_None) {
|
||||
iz = z->ob_ival;
|
||||
if (iz == 0) {
|
||||
err_setstr(ValueError, "pow(x, y, z) with z==0");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"pow(x, y, z) with z==0");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@ -524,31 +527,35 @@ int_pow(v, w, z)
|
|||
}
|
||||
}
|
||||
if (iz) {
|
||||
object *t1, *t2;
|
||||
PyObject *t1, *t2;
|
||||
long int div, mod;
|
||||
t1=newintobject(ix);
|
||||
t2=newintobject(iz);
|
||||
t1=PyInt_FromLong(ix);
|
||||
t2=PyInt_FromLong(iz);
|
||||
if (t1==NULL || t2==NULL ||
|
||||
i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) {
|
||||
XDECREF(t1);
|
||||
XDECREF(t2);
|
||||
i_divmod((PyIntObject *)t1,
|
||||
(PyIntObject *)t2, &div, &mod)<0)
|
||||
{
|
||||
Py_XDECREF(t1);
|
||||
Py_XDECREF(t2);
|
||||
return(NULL);
|
||||
}
|
||||
DECREF(t1);
|
||||
DECREF(t2);
|
||||
Py_DECREF(t1);
|
||||
Py_DECREF(t2);
|
||||
ix=mod;
|
||||
}
|
||||
return newintobject(ix);
|
||||
return PyInt_FromLong(ix);
|
||||
#else
|
||||
register long iv, iw, ix;
|
||||
iv = v->ob_ival;
|
||||
iw = w->ob_ival;
|
||||
if (iw < 0) {
|
||||
err_setstr(ValueError, "integer to the negative power");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"integer to the negative power");
|
||||
return NULL;
|
||||
}
|
||||
if ((object *)z != None) {
|
||||
err_setstr(TypeError, "pow(int, int, int) not yet supported");
|
||||
if ((PyObject *)z != Py_None) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"pow(int, int, int) not yet supported");
|
||||
return NULL;
|
||||
}
|
||||
ix = 1;
|
||||
|
@ -560,33 +567,33 @@ int_pow(v, w, z)
|
|||
if (ix / iv != prev)
|
||||
return err_ovf("integer pow()");
|
||||
}
|
||||
return newintobject(ix);
|
||||
return PyInt_FromLong(ix);
|
||||
#endif
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_neg(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
register long a, x;
|
||||
a = v->ob_ival;
|
||||
x = -a;
|
||||
if (a < 0 && x < 0)
|
||||
return err_ovf("integer negation");
|
||||
return newintobject(x);
|
||||
return PyInt_FromLong(x);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_pos(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
INCREF(v);
|
||||
return (object *)v;
|
||||
Py_INCREF(v);
|
||||
return (PyObject *)v;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_abs(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
if (v->ob_ival >= 0)
|
||||
return int_pos(v);
|
||||
|
@ -596,56 +603,56 @@ int_abs(v)
|
|||
|
||||
static int
|
||||
int_nonzero(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
return v->ob_ival != 0;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_invert(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
return newintobject(~v->ob_ival);
|
||||
return PyInt_FromLong(~v->ob_ival);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_lshift(v, w)
|
||||
intobject *v;
|
||||
intobject *w;
|
||||
PyIntObject *v;
|
||||
PyIntObject *w;
|
||||
{
|
||||
register long a, b;
|
||||
a = v->ob_ival;
|
||||
b = w->ob_ival;
|
||||
if (b < 0) {
|
||||
err_setstr(ValueError, "negative shift count");
|
||||
PyErr_SetString(PyExc_ValueError, "negative shift count");
|
||||
return NULL;
|
||||
}
|
||||
if (a == 0 || b == 0) {
|
||||
INCREF(v);
|
||||
return (object *) v;
|
||||
Py_INCREF(v);
|
||||
return (PyObject *) v;
|
||||
}
|
||||
if (b >= LONG_BIT) {
|
||||
return newintobject(0L);
|
||||
return PyInt_FromLong(0L);
|
||||
}
|
||||
a = (unsigned long)a << b;
|
||||
return newintobject(a);
|
||||
return PyInt_FromLong(a);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_rshift(v, w)
|
||||
intobject *v;
|
||||
intobject *w;
|
||||
PyIntObject *v;
|
||||
PyIntObject *w;
|
||||
{
|
||||
register long a, b;
|
||||
a = v->ob_ival;
|
||||
b = w->ob_ival;
|
||||
if (b < 0) {
|
||||
err_setstr(ValueError, "negative shift count");
|
||||
PyErr_SetString(PyExc_ValueError, "negative shift count");
|
||||
return NULL;
|
||||
}
|
||||
if (a == 0 || b == 0) {
|
||||
INCREF(v);
|
||||
return (object *) v;
|
||||
Py_INCREF(v);
|
||||
return (PyObject *) v;
|
||||
}
|
||||
if (b >= LONG_BIT) {
|
||||
if (a < 0)
|
||||
|
@ -659,67 +666,67 @@ int_rshift(v, w)
|
|||
else
|
||||
a = (unsigned long)a >> b;
|
||||
}
|
||||
return newintobject(a);
|
||||
return PyInt_FromLong(a);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_and(v, w)
|
||||
intobject *v;
|
||||
intobject *w;
|
||||
PyIntObject *v;
|
||||
PyIntObject *w;
|
||||
{
|
||||
register long a, b;
|
||||
a = v->ob_ival;
|
||||
b = w->ob_ival;
|
||||
return newintobject(a & b);
|
||||
return PyInt_FromLong(a & b);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_xor(v, w)
|
||||
intobject *v;
|
||||
intobject *w;
|
||||
PyIntObject *v;
|
||||
PyIntObject *w;
|
||||
{
|
||||
register long a, b;
|
||||
a = v->ob_ival;
|
||||
b = w->ob_ival;
|
||||
return newintobject(a ^ b);
|
||||
return PyInt_FromLong(a ^ b);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_or(v, w)
|
||||
intobject *v;
|
||||
intobject *w;
|
||||
PyIntObject *v;
|
||||
PyIntObject *w;
|
||||
{
|
||||
register long a, b;
|
||||
a = v->ob_ival;
|
||||
b = w->ob_ival;
|
||||
return newintobject(a | b);
|
||||
return PyInt_FromLong(a | b);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_int(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
INCREF(v);
|
||||
return (object *)v;
|
||||
Py_INCREF(v);
|
||||
return (PyObject *)v;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_long(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
return newlongobject((v -> ob_ival));
|
||||
return PyLong_FromLong((v -> ob_ival));
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_float(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
return newfloatobject((double)(v -> ob_ival));
|
||||
return PyFloat_FromDouble((double)(v -> ob_ival));
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_oct(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
char buf[100];
|
||||
long x = v -> ob_ival;
|
||||
|
@ -727,20 +734,20 @@ int_oct(v)
|
|||
strcpy(buf, "0");
|
||||
else
|
||||
sprintf(buf, "0%lo", x);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
int_hex(v)
|
||||
intobject *v;
|
||||
PyIntObject *v;
|
||||
{
|
||||
char buf[100];
|
||||
long x = v -> ob_ival;
|
||||
sprintf(buf, "0x%lx", x);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
static number_methods int_as_number = {
|
||||
static PyNumberMethods int_as_number = {
|
||||
(binaryfunc)int_add, /*nb_add*/
|
||||
(binaryfunc)int_sub, /*nb_subtract*/
|
||||
(binaryfunc)int_mul, /*nb_multiply*/
|
||||
|
@ -766,11 +773,11 @@ static number_methods int_as_number = {
|
|||
(unaryfunc)int_hex, /*nb_hex*/
|
||||
};
|
||||
|
||||
typeobject Inttype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyInt_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
"int",
|
||||
sizeof(intobject),
|
||||
sizeof(PyIntObject),
|
||||
0,
|
||||
(destructor)int_dealloc, /*tp_dealloc*/
|
||||
(printfunc)int_print, /*tp_print*/
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -31,111 +31,113 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
/* Method object implementation */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "Python.h"
|
||||
|
||||
#include "token.h"
|
||||
|
||||
typedef struct {
|
||||
OB_HEAD
|
||||
struct methodlist *m_ml;
|
||||
object *m_self;
|
||||
} methodobject;
|
||||
PyObject_HEAD
|
||||
PyMethodDef *m_ml;
|
||||
PyObject *m_self;
|
||||
} PyCFunctionObject;
|
||||
|
||||
object *
|
||||
newmethodobject(ml, self)
|
||||
struct methodlist *ml;
|
||||
object *self;
|
||||
PyObject *
|
||||
PyCFunction_New(ml, self)
|
||||
PyMethodDef *ml;
|
||||
PyObject *self;
|
||||
{
|
||||
methodobject *op = NEWOBJ(methodobject, &Methodtype);
|
||||
PyCFunctionObject *op = PyObject_NEW(PyCFunctionObject,
|
||||
&PyCFunction_Type);
|
||||
if (op != NULL) {
|
||||
op->m_ml = ml;
|
||||
XINCREF(self);
|
||||
Py_XINCREF(self);
|
||||
op->m_self = self;
|
||||
}
|
||||
return (object *)op;
|
||||
return (PyObject *)op;
|
||||
}
|
||||
|
||||
method
|
||||
getmethod(op)
|
||||
object *op;
|
||||
PyCFunction
|
||||
PyCFunction_GetFunction(op)
|
||||
PyObject *op;
|
||||
{
|
||||
if (!is_methodobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyCFunction_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
return ((methodobject *)op) -> m_ml -> ml_meth;
|
||||
return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
|
||||
}
|
||||
|
||||
object *
|
||||
getself(op)
|
||||
object *op;
|
||||
PyObject *
|
||||
PyCFunction_GetSelf(op)
|
||||
PyObject *op;
|
||||
{
|
||||
if (!is_methodobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyCFunction_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
return ((methodobject *)op) -> m_self;
|
||||
return ((PyCFunctionObject *)op) -> m_self;
|
||||
}
|
||||
|
||||
int
|
||||
getflags(op)
|
||||
object *op;
|
||||
PyCFunction_GetFlags(op)
|
||||
PyObject *op;
|
||||
{
|
||||
if (!is_methodobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyCFunction_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
return ((methodobject *)op) -> m_ml -> ml_flags;
|
||||
return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
|
||||
}
|
||||
|
||||
/* Methods (the standard built-in methods, that is) */
|
||||
|
||||
static void
|
||||
meth_dealloc(m)
|
||||
methodobject *m;
|
||||
PyCFunctionObject *m;
|
||||
{
|
||||
XDECREF(m->m_self);
|
||||
Py_XDECREF(m->m_self);
|
||||
free((char *)m);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
meth_getattr(m, name)
|
||||
methodobject *m;
|
||||
PyCFunctionObject *m;
|
||||
char *name;
|
||||
{
|
||||
if (strcmp(name, "__name__") == 0) {
|
||||
return newstringobject(m->m_ml->ml_name);
|
||||
return PyString_FromString(m->m_ml->ml_name);
|
||||
}
|
||||
if (strcmp(name, "__doc__") == 0) {
|
||||
char *doc = m->m_ml->ml_doc;
|
||||
if (doc != NULL)
|
||||
return newstringobject(doc);
|
||||
INCREF(None);
|
||||
return None;
|
||||
return PyString_FromString(doc);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
if (strcmp(name, "__self__") == 0) {
|
||||
object *self;
|
||||
if (getrestricted()) {
|
||||
err_setstr(RuntimeError,
|
||||
PyObject *self;
|
||||
if (PyEval_GetRestricted()) {
|
||||
PyErr_SetString(PyExc_RuntimeError,
|
||||
"method.__self__ not accessible in restricted mode");
|
||||
return NULL;
|
||||
}
|
||||
self = m->m_self;
|
||||
if (self == NULL)
|
||||
self = None;
|
||||
INCREF(self);
|
||||
self = Py_None;
|
||||
Py_INCREF(self);
|
||||
return self;
|
||||
}
|
||||
if (strcmp(name, "__members__") == 0) {
|
||||
return mkvalue("[sss]", "__doc__", "__name__", "__self__");
|
||||
return Py_BuildValue("[sss]",
|
||||
"__doc__", "__name__", "__self__");
|
||||
}
|
||||
err_setstr(AttributeError, name);
|
||||
PyErr_SetString(PyExc_AttributeError, name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
meth_repr(m)
|
||||
methodobject *m;
|
||||
PyCFunctionObject *m;
|
||||
{
|
||||
char buf[200];
|
||||
if (m->m_self == NULL)
|
||||
|
@ -145,15 +147,15 @@ meth_repr(m)
|
|||
"<built-in method %.80s of %.80s object at %lx>",
|
||||
m->m_ml->ml_name, m->m_self->ob_type->tp_name,
|
||||
(long)m->m_self);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
static int
|
||||
meth_compare(a, b)
|
||||
methodobject *a, *b;
|
||||
PyCFunctionObject *a, *b;
|
||||
{
|
||||
if (a->m_self != b->m_self)
|
||||
return cmpobject(a->m_self, b->m_self);
|
||||
return PyObject_Compare(a->m_self, b->m_self);
|
||||
if (a->m_ml->ml_meth == b->m_ml->ml_meth)
|
||||
return 0;
|
||||
if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0)
|
||||
|
@ -164,24 +166,24 @@ meth_compare(a, b)
|
|||
|
||||
static long
|
||||
meth_hash(a)
|
||||
methodobject *a;
|
||||
PyCFunctionObject *a;
|
||||
{
|
||||
long x;
|
||||
if (a->m_self == NULL)
|
||||
x = 0;
|
||||
else {
|
||||
x = hashobject(a->m_self);
|
||||
x = PyObject_Hash(a->m_self);
|
||||
if (x == -1)
|
||||
return -1;
|
||||
}
|
||||
return x ^ (long) a->m_ml->ml_meth;
|
||||
}
|
||||
|
||||
typeobject Methodtype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyCFunction_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
"builtin_function_or_method",
|
||||
sizeof(methodobject),
|
||||
sizeof(PyCFunctionObject),
|
||||
0,
|
||||
(destructor)meth_dealloc, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
|
@ -197,71 +199,71 @@ typeobject Methodtype = {
|
|||
|
||||
/* List all methods in a chain -- helper for findmethodinchain */
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
listmethodchain(chain)
|
||||
struct methodchain *chain;
|
||||
PyMethodChain *chain;
|
||||
{
|
||||
struct methodchain *c;
|
||||
struct methodlist *ml;
|
||||
PyMethodChain *c;
|
||||
PyMethodDef *ml;
|
||||
int i, n;
|
||||
object *v;
|
||||
PyObject *v;
|
||||
|
||||
n = 0;
|
||||
for (c = chain; c != NULL; c = c->link) {
|
||||
for (ml = c->methods; ml->ml_name != NULL; ml++)
|
||||
n++;
|
||||
}
|
||||
v = newlistobject(n);
|
||||
v = PyList_New(n);
|
||||
if (v == NULL)
|
||||
return NULL;
|
||||
i = 0;
|
||||
for (c = chain; c != NULL; c = c->link) {
|
||||
for (ml = c->methods; ml->ml_name != NULL; ml++) {
|
||||
setlistitem(v, i, newstringobject(ml->ml_name));
|
||||
PyList_SetItem(v, i, PyString_FromString(ml->ml_name));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
if (err_occurred()) {
|
||||
DECREF(v);
|
||||
if (PyErr_Occurred()) {
|
||||
Py_DECREF(v);
|
||||
return NULL;
|
||||
}
|
||||
sortlist(v);
|
||||
PyList_Sort(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
/* Find a method in a method chain */
|
||||
|
||||
object *
|
||||
findmethodinchain(chain, self, name)
|
||||
struct methodchain *chain;
|
||||
object *self;
|
||||
PyObject *
|
||||
Py_FindMethodInChain(chain, self, name)
|
||||
PyMethodChain *chain;
|
||||
PyObject *self;
|
||||
char *name;
|
||||
{
|
||||
if (strcmp(name, "__methods__") == 0)
|
||||
return listmethodchain(chain);
|
||||
while (chain != NULL) {
|
||||
struct methodlist *ml = chain->methods;
|
||||
PyMethodDef *ml = chain->methods;
|
||||
for (; ml->ml_name != NULL; ml++) {
|
||||
if (name[0] == ml->ml_name[0] &&
|
||||
strcmp(name+1, ml->ml_name+1) == 0)
|
||||
return newmethodobject(ml, self);
|
||||
return PyCFunction_New(ml, self);
|
||||
}
|
||||
chain = chain->link;
|
||||
}
|
||||
err_setstr(AttributeError, name);
|
||||
PyErr_SetString(PyExc_AttributeError, name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Find a method in a single method list */
|
||||
|
||||
object *
|
||||
findmethod(methods, self, name)
|
||||
struct methodlist *methods;
|
||||
object *self;
|
||||
PyObject *
|
||||
Py_FindMethod(methods, self, name)
|
||||
PyMethodDef *methods;
|
||||
PyObject *self;
|
||||
char *name;
|
||||
{
|
||||
struct methodchain chain;
|
||||
PyMethodChain chain;
|
||||
chain.methods = methods;
|
||||
chain.link = NULL;
|
||||
return findmethodinchain(&chain, self, name);
|
||||
return Py_FindMethodInChain(&chain, self, name);
|
||||
}
|
||||
|
|
|
@ -31,153 +31,154 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
/* Module object implementation */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "ceval.h"
|
||||
#include "Python.h"
|
||||
|
||||
typedef struct {
|
||||
OB_HEAD
|
||||
object *md_dict;
|
||||
} moduleobject;
|
||||
PyObject_HEAD
|
||||
PyObject *md_dict;
|
||||
} PyModuleObject;
|
||||
|
||||
object *
|
||||
newmoduleobject(name)
|
||||
PyObject *
|
||||
PyModule_New(name)
|
||||
char *name;
|
||||
{
|
||||
moduleobject *m;
|
||||
object *nameobj;
|
||||
m = NEWOBJ(moduleobject, &Moduletype);
|
||||
PyModuleObject *m;
|
||||
PyObject *nameobj;
|
||||
m = PyObject_NEW(PyModuleObject, &PyModule_Type);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
nameobj = newstringobject(name);
|
||||
m->md_dict = newdictobject();
|
||||
nameobj = PyString_FromString(name);
|
||||
m->md_dict = PyDict_New();
|
||||
if (m->md_dict == NULL || nameobj == NULL)
|
||||
goto fail;
|
||||
if (dictinsert(m->md_dict, "__name__", nameobj) != 0)
|
||||
if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
|
||||
goto fail;
|
||||
if (dictinsert(m->md_dict, "__doc__", None) != 0)
|
||||
if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
|
||||
goto fail;
|
||||
DECREF(nameobj);
|
||||
return (object *)m;
|
||||
Py_DECREF(nameobj);
|
||||
return (PyObject *)m;
|
||||
|
||||
fail:
|
||||
XDECREF(nameobj);
|
||||
DECREF(m);
|
||||
Py_XDECREF(nameobj);
|
||||
Py_DECREF(m);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
object *
|
||||
getmoduledict(m)
|
||||
object *m;
|
||||
PyObject *
|
||||
PyModule_GetDict(m)
|
||||
PyObject *m;
|
||||
{
|
||||
if (!is_moduleobject(m)) {
|
||||
err_badcall();
|
||||
if (!PyModule_Check(m)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
return ((moduleobject *)m) -> md_dict;
|
||||
return ((PyModuleObject *)m) -> md_dict;
|
||||
}
|
||||
|
||||
char *
|
||||
getmodulename(m)
|
||||
object *m;
|
||||
PyModule_GetName(m)
|
||||
PyObject *m;
|
||||
{
|
||||
object *nameobj;
|
||||
if (!is_moduleobject(m)) {
|
||||
err_badarg();
|
||||
PyObject *nameobj;
|
||||
if (!PyModule_Check(m)) {
|
||||
PyErr_BadArgument();
|
||||
return NULL;
|
||||
}
|
||||
nameobj = dictlookup(((moduleobject *)m)->md_dict, "__name__");
|
||||
if (nameobj == NULL || !is_stringobject(nameobj)) {
|
||||
err_setstr(SystemError, "nameless module");
|
||||
nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
|
||||
"__name__");
|
||||
if (nameobj == NULL || !PyString_Check(nameobj)) {
|
||||
PyErr_SetString(PyExc_SystemError, "nameless module");
|
||||
return NULL;
|
||||
}
|
||||
return getstringvalue(nameobj);
|
||||
return PyString_AsString(nameobj);
|
||||
}
|
||||
|
||||
/* Methods */
|
||||
|
||||
static void
|
||||
module_dealloc(m)
|
||||
moduleobject *m;
|
||||
PyModuleObject *m;
|
||||
{
|
||||
if (m->md_dict != NULL) {
|
||||
mappingclear(m->md_dict);
|
||||
DECREF(m->md_dict);
|
||||
PyDict_Clear(m->md_dict);
|
||||
Py_DECREF(m->md_dict);
|
||||
}
|
||||
free((char *)m);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
module_repr(m)
|
||||
moduleobject *m;
|
||||
PyModuleObject *m;
|
||||
{
|
||||
char buf[100];
|
||||
char *name = getmodulename((object *)m);
|
||||
char *name = PyModule_GetName((PyObject *)m);
|
||||
if (name == NULL) {
|
||||
err_clear();
|
||||
PyErr_Clear();
|
||||
name = "?";
|
||||
}
|
||||
sprintf(buf, "<module '%.80s'>", name);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
module_getattr(m, name)
|
||||
moduleobject *m;
|
||||
PyModuleObject *m;
|
||||
char *name;
|
||||
{
|
||||
object *res;
|
||||
PyObject *res;
|
||||
if (strcmp(name, "__dict__") == 0) {
|
||||
INCREF(m->md_dict);
|
||||
Py_INCREF(m->md_dict);
|
||||
return m->md_dict;
|
||||
}
|
||||
res = dictlookup(m->md_dict, name);
|
||||
res = PyDict_GetItemString(m->md_dict, name);
|
||||
if (res == NULL)
|
||||
err_setstr(AttributeError, name);
|
||||
PyErr_SetString(PyExc_AttributeError, name);
|
||||
else {
|
||||
#ifdef SUPPORT_OBSOLETE_ACCESS
|
||||
if (is_accessobject(res))
|
||||
res = getaccessvalue(res, getglobals());
|
||||
if (PyAccess_Check(res))
|
||||
res = PyAccess_AsValue(res, PyEval_GetGlobals());
|
||||
else
|
||||
#endif
|
||||
INCREF(res);
|
||||
Py_INCREF(res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int
|
||||
module_setattr(m, name, v)
|
||||
moduleobject *m;
|
||||
PyModuleObject *m;
|
||||
char *name;
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
#ifdef SUPPORT_OBSOLETE_ACCESS
|
||||
object *ac;
|
||||
PyObject *ac;
|
||||
#endif
|
||||
if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
|
||||
err_setstr(TypeError, "read-only special attribute");
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"read-only special attribute");
|
||||
return -1;
|
||||
}
|
||||
#ifdef SUPPORT_OBSOLETE_ACCESS
|
||||
ac = dictlookup(m->md_dict, name);
|
||||
if (ac != NULL && is_accessobject(ac))
|
||||
return setaccessvalue(ac, getglobals(), v);
|
||||
ac = PyDict_GetItemString(m->md_dict, name);
|
||||
if (ac != NULL && PyAccess_Check(ac))
|
||||
return PyAccess_SetValue(ac, PyEval_GetGlobals(), v);
|
||||
#endif
|
||||
if (v == NULL) {
|
||||
int rv = dictremove(m->md_dict, name);
|
||||
int rv = PyDict_DelItemString(m->md_dict, name);
|
||||
if (rv < 0)
|
||||
err_setstr(AttributeError,
|
||||
PyErr_SetString(PyExc_AttributeError,
|
||||
"delete non-existing module attribute");
|
||||
return rv;
|
||||
}
|
||||
else
|
||||
return dictinsert(m->md_dict, name, v);
|
||||
return PyDict_SetItemString(m->md_dict, name, v);
|
||||
}
|
||||
|
||||
typeobject Moduletype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyModule_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0, /*ob_size*/
|
||||
"module", /*tp_name*/
|
||||
sizeof(moduleobject), /*tp_size*/
|
||||
sizeof(PyModuleObject), /*tp_size*/
|
||||
0, /*tp_itemsize*/
|
||||
(destructor)module_dealloc, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
|
|
289
Objects/object.c
289
Objects/object.c
|
@ -31,10 +31,10 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
/* Generic object operations; and implementation of None (NoObject) */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "Python.h"
|
||||
|
||||
#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
|
||||
long ref_total;
|
||||
long _Py_RefTotal;
|
||||
#endif
|
||||
|
||||
/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
|
||||
|
@ -42,14 +42,14 @@ long ref_total;
|
|||
Do not call them otherwise, they do not initialize the object! */
|
||||
|
||||
#ifdef COUNT_ALLOCS
|
||||
static typeobject *type_list;
|
||||
static PyTypeObject *type_list;
|
||||
extern int tuple_zero_allocs, fast_tuple_allocs;
|
||||
extern int quick_int_allocs, quick_neg_int_allocs;
|
||||
extern int null_strings, one_strings;
|
||||
void
|
||||
dump_counts()
|
||||
{
|
||||
typeobject *tp;
|
||||
PyTypeObject *tp;
|
||||
|
||||
for (tp = type_list; tp; tp = tp->tp_next)
|
||||
fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
|
||||
|
@ -92,12 +92,12 @@ get_counts()
|
|||
|
||||
void
|
||||
inc_count(tp)
|
||||
typeobject *tp;
|
||||
PyTypeObject *tp;
|
||||
{
|
||||
if (tp->tp_alloc == 0) {
|
||||
/* first time; insert in linked list */
|
||||
if (tp->tp_next != NULL) /* sanity check */
|
||||
fatal("XXX inc_count sanity check");
|
||||
Py_FatalError("XXX inc_count sanity check");
|
||||
tp->tp_next = type_list;
|
||||
type_list = tp;
|
||||
}
|
||||
|
@ -108,35 +108,35 @@ inc_count(tp)
|
|||
#endif
|
||||
|
||||
#ifndef MS_COREDLL
|
||||
object *
|
||||
newobject(tp)
|
||||
typeobject *tp;
|
||||
PyObject *
|
||||
_PyObject_New(tp)
|
||||
PyTypeObject *tp;
|
||||
#else
|
||||
object *
|
||||
newobject(tp,op)
|
||||
typeobject *tp;
|
||||
PyObject *
|
||||
_PyObject_New(tp,op)
|
||||
PyTypeObject *tp;
|
||||
PyObject *op;
|
||||
#endif
|
||||
{
|
||||
#ifndef MS_COREDLL
|
||||
object *op = (object *) malloc(tp->tp_basicsize);
|
||||
PyObject *op = (PyObject *) malloc(tp->tp_basicsize);
|
||||
#endif
|
||||
if (op == NULL)
|
||||
return err_nomem();
|
||||
return PyErr_NoMemory();
|
||||
op->ob_type = tp;
|
||||
NEWREF(op);
|
||||
_Py_NewReference(op);
|
||||
return op;
|
||||
}
|
||||
|
||||
#ifndef MS_COREDLL
|
||||
varobject *
|
||||
newvarobject(tp, size)
|
||||
typeobject *tp;
|
||||
_PyObject_NewVar(tp, size)
|
||||
PyTypeObject *tp;
|
||||
int size;
|
||||
#else
|
||||
varobject *
|
||||
newvarobject(tp, size, op)
|
||||
typeobject *tp;
|
||||
_PyObject_NewVar(tp, size, op)
|
||||
PyTypeObject *tp;
|
||||
int size;
|
||||
varobject *op;
|
||||
#endif
|
||||
|
@ -146,21 +146,21 @@ newvarobject(tp, size, op)
|
|||
malloc(tp->tp_basicsize + size * tp->tp_itemsize);
|
||||
#endif
|
||||
if (op == NULL)
|
||||
return (varobject *)err_nomem();
|
||||
return (varobject *)PyErr_NoMemory();
|
||||
op->ob_type = tp;
|
||||
op->ob_size = size;
|
||||
NEWREF(op);
|
||||
_Py_NewReference(op);
|
||||
return op;
|
||||
}
|
||||
|
||||
int
|
||||
printobject(op, fp, flags)
|
||||
object *op;
|
||||
PyObject_Print(op, fp, flags)
|
||||
PyObject *op;
|
||||
FILE *fp;
|
||||
int flags;
|
||||
{
|
||||
int ret = 0;
|
||||
if (sigcheck())
|
||||
if (PyErr_CheckSignals())
|
||||
return -1;
|
||||
if (op == NULL) {
|
||||
fprintf(fp, "<nil>");
|
||||
|
@ -175,22 +175,23 @@ printobject(op, fp, flags)
|
|||
op->ob_type->tp_name, (long)op);
|
||||
}
|
||||
else {
|
||||
object *s;
|
||||
if (flags & PRINT_RAW)
|
||||
s = strobject(op);
|
||||
PyObject *s;
|
||||
if (flags & Py_PRINT_RAW)
|
||||
s = PyObject_Str(op);
|
||||
else
|
||||
s = reprobject(op);
|
||||
s = PyObject_Repr(op);
|
||||
if (s == NULL)
|
||||
ret = -1;
|
||||
else if (!is_stringobject(s)) {
|
||||
err_setstr(TypeError,
|
||||
else if (!PyString_Check(s)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"repr not string");
|
||||
ret = -1;
|
||||
}
|
||||
else {
|
||||
fprintf(fp, "%s", getstringvalue(s));
|
||||
fprintf(fp, "%s",
|
||||
PyString_AsString(s));
|
||||
}
|
||||
XDECREF(s);
|
||||
Py_XDECREF(s);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -198,7 +199,7 @@ printobject(op, fp, flags)
|
|||
}
|
||||
if (ret == 0) {
|
||||
if (ferror(fp)) {
|
||||
err_errno(IOError);
|
||||
PyErr_SetFromErrno(PyExc_IOError);
|
||||
clearerr(fp);
|
||||
ret = -1;
|
||||
}
|
||||
|
@ -206,104 +207,104 @@ printobject(op, fp, flags)
|
|||
return ret;
|
||||
}
|
||||
|
||||
object *
|
||||
reprobject(v)
|
||||
object *v;
|
||||
PyObject *
|
||||
PyObject_Repr(v)
|
||||
PyObject *v;
|
||||
{
|
||||
if (sigcheck())
|
||||
if (PyErr_CheckSignals())
|
||||
return NULL;
|
||||
if (v == NULL)
|
||||
return newstringobject("<NULL>");
|
||||
return PyString_FromString("<NULL>");
|
||||
else if (v->ob_type->tp_repr == NULL) {
|
||||
char buf[120];
|
||||
sprintf(buf, "<%.80s object at %lx>",
|
||||
v->ob_type->tp_name, (long)v);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
else
|
||||
return (*v->ob_type->tp_repr)(v);
|
||||
}
|
||||
|
||||
object *
|
||||
strobject(v)
|
||||
object *v;
|
||||
PyObject *
|
||||
PyObject_Str(v)
|
||||
PyObject *v;
|
||||
{
|
||||
if (v == NULL)
|
||||
return newstringobject("<NULL>");
|
||||
else if (is_stringobject(v)) {
|
||||
INCREF(v);
|
||||
return PyString_FromString("<NULL>");
|
||||
else if (PyString_Check(v)) {
|
||||
Py_INCREF(v);
|
||||
return v;
|
||||
}
|
||||
else if (v->ob_type->tp_str != NULL)
|
||||
return (*v->ob_type->tp_str)(v);
|
||||
else {
|
||||
object *func;
|
||||
object *res;
|
||||
if (!is_instanceobject(v) ||
|
||||
(func = getattr(v, "__str__")) == NULL) {
|
||||
err_clear();
|
||||
return reprobject(v);
|
||||
PyObject *func;
|
||||
PyObject *res;
|
||||
if (!PyInstance_Check(v) ||
|
||||
(func = PyObject_GetAttrString(v, "__str__")) == NULL) {
|
||||
PyErr_Clear();
|
||||
return PyObject_Repr(v);
|
||||
}
|
||||
res = call_object(func, (object *)NULL);
|
||||
DECREF(func);
|
||||
res = PyEval_CallObject(func, (PyObject *)NULL);
|
||||
Py_DECREF(func);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
do_cmp(v, w)
|
||||
object *v, *w;
|
||||
PyObject *v, *w;
|
||||
{
|
||||
/* __rcmp__ actually won't be called unless __cmp__ isn't defined,
|
||||
because the check in cmpobject() reverses the objects first.
|
||||
This is intentional -- it makes no sense to define cmp(x,y) different
|
||||
than -cmp(y,x). */
|
||||
if (is_instanceobject(v) || is_instanceobject(w))
|
||||
return instancebinop(v, w, "__cmp__", "__rcmp__", do_cmp);
|
||||
return newintobject((long)cmpobject(v, w));
|
||||
This is intentional -- it makes no sense to define cmp(x,y)
|
||||
different than -cmp(y,x). */
|
||||
if (PyInstance_Check(v) || PyInstance_Check(w))
|
||||
return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
|
||||
return PyInt_FromLong((long)PyObject_Compare(v, w));
|
||||
}
|
||||
|
||||
int
|
||||
cmpobject(v, w)
|
||||
object *v, *w;
|
||||
PyObject_Compare(v, w)
|
||||
PyObject *v, *w;
|
||||
{
|
||||
typeobject *tp;
|
||||
PyTypeObject *tp;
|
||||
if (v == w)
|
||||
return 0;
|
||||
if (v == NULL)
|
||||
return -1;
|
||||
if (w == NULL)
|
||||
return 1;
|
||||
if (is_instanceobject(v) || is_instanceobject(w)) {
|
||||
object *res;
|
||||
if (PyInstance_Check(v) || PyInstance_Check(w)) {
|
||||
PyObject *res;
|
||||
int c;
|
||||
if (!is_instanceobject(v))
|
||||
return -cmpobject(w, v);
|
||||
if (!PyInstance_Check(v))
|
||||
return -PyObject_Compare(w, v);
|
||||
res = do_cmp(v, w);
|
||||
if (res == NULL) {
|
||||
err_clear();
|
||||
PyErr_Clear();
|
||||
return (v < w) ? -1 : 1;
|
||||
}
|
||||
if (!is_intobject(res)) {
|
||||
DECREF(res);
|
||||
if (!PyInt_Check(res)) {
|
||||
Py_DECREF(res);
|
||||
return (v < w) ? -1 : 1;
|
||||
}
|
||||
c = getintvalue(res);
|
||||
DECREF(res);
|
||||
c = PyInt_AsLong(res);
|
||||
Py_DECREF(res);
|
||||
return (c < 0) ? -1 : (c > 0) ? 1 : 0;
|
||||
}
|
||||
if ((tp = v->ob_type) != w->ob_type) {
|
||||
if (tp->tp_as_number != NULL &&
|
||||
w->ob_type->tp_as_number != NULL) {
|
||||
if (coerce(&v, &w) != 0) {
|
||||
err_clear();
|
||||
if (PyNumber_Coerce(&v, &w) != 0) {
|
||||
PyErr_Clear();
|
||||
/* XXX Should report the error,
|
||||
XXX but the interface isn't there... */
|
||||
}
|
||||
else {
|
||||
int cmp = (*v->ob_type->tp_compare)(v, w);
|
||||
DECREF(v);
|
||||
DECREF(w);
|
||||
Py_DECREF(v);
|
||||
Py_DECREF(w);
|
||||
return cmp;
|
||||
}
|
||||
}
|
||||
|
@ -315,36 +316,36 @@ cmpobject(v, w)
|
|||
}
|
||||
|
||||
long
|
||||
hashobject(v)
|
||||
object *v;
|
||||
PyObject_Hash(v)
|
||||
PyObject *v;
|
||||
{
|
||||
typeobject *tp = v->ob_type;
|
||||
PyTypeObject *tp = v->ob_type;
|
||||
if (tp->tp_hash != NULL)
|
||||
return (*tp->tp_hash)(v);
|
||||
if (tp->tp_compare == NULL)
|
||||
return (long) v; /* Use address as hash value */
|
||||
/* If there's a cmp but no hash defined, the object can't be hashed */
|
||||
err_setstr(TypeError, "unhashable type");
|
||||
PyErr_SetString(PyExc_TypeError, "unhashable type");
|
||||
return -1;
|
||||
}
|
||||
|
||||
object *
|
||||
getattr(v, name)
|
||||
object *v;
|
||||
PyObject *
|
||||
PyObject_GetAttrString(v, name)
|
||||
PyObject *v;
|
||||
char *name;
|
||||
{
|
||||
if (v->ob_type->tp_getattro != NULL) {
|
||||
object *w, *res;
|
||||
PyObject *w, *res;
|
||||
w = PyString_InternFromString(name);
|
||||
if (w == NULL)
|
||||
return NULL;
|
||||
res = (*v->ob_type->tp_getattro)(v, w);
|
||||
XDECREF(w);
|
||||
Py_XDECREF(w);
|
||||
return res;
|
||||
}
|
||||
|
||||
if (v->ob_type->tp_getattr == NULL) {
|
||||
err_setstr(AttributeError, "attribute-less object");
|
||||
PyErr_SetString(PyExc_AttributeError, "attribute-less object");
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
|
@ -353,42 +354,42 @@ getattr(v, name)
|
|||
}
|
||||
|
||||
int
|
||||
hasattr(v, name)
|
||||
object *v;
|
||||
PyObject_HasAttrString(v, name)
|
||||
PyObject *v;
|
||||
char *name;
|
||||
{
|
||||
object *res = getattr(v, name);
|
||||
PyObject *res = PyObject_GetAttrString(v, name);
|
||||
if (res != NULL) {
|
||||
DECREF(res);
|
||||
Py_DECREF(res);
|
||||
return 1;
|
||||
}
|
||||
err_clear();
|
||||
PyErr_Clear();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
setattr(v, name, w)
|
||||
object *v;
|
||||
PyObject_SetAttrString(v, name, w)
|
||||
PyObject *v;
|
||||
char *name;
|
||||
object *w;
|
||||
PyObject *w;
|
||||
{
|
||||
if (v->ob_type->tp_setattro != NULL) {
|
||||
object *s;
|
||||
PyObject *s;
|
||||
int res;
|
||||
s = PyString_InternFromString(name);
|
||||
if (s == NULL)
|
||||
return -1;
|
||||
res = (*v->ob_type->tp_setattro)(v, s, w);
|
||||
XDECREF(s);
|
||||
Py_XDECREF(s);
|
||||
return res;
|
||||
}
|
||||
|
||||
if (v->ob_type->tp_setattr == NULL) {
|
||||
if (v->ob_type->tp_getattr == NULL)
|
||||
err_setstr(TypeError,
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"attribute-less object (assign or del)");
|
||||
else
|
||||
err_setstr(TypeError,
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"object has read-only attributes");
|
||||
return -1;
|
||||
}
|
||||
|
@ -401,11 +402,11 @@ setattr(v, name, w)
|
|||
Return -1 if an error occurred */
|
||||
|
||||
int
|
||||
testbool(v)
|
||||
object *v;
|
||||
PyObject_IsTrue(v)
|
||||
PyObject *v;
|
||||
{
|
||||
int res;
|
||||
if (v == None)
|
||||
if (v == Py_None)
|
||||
res = 0;
|
||||
else if (v->ob_type->tp_as_number != NULL)
|
||||
res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
|
||||
|
@ -427,16 +428,16 @@ testbool(v)
|
|||
*/
|
||||
|
||||
int
|
||||
coerce(pv, pw)
|
||||
object **pv, **pw;
|
||||
PyNumber_Coerce(pv, pw)
|
||||
PyObject **pv, **pw;
|
||||
{
|
||||
register object *v = *pv;
|
||||
register object *w = *pw;
|
||||
register PyObject *v = *pv;
|
||||
register PyObject *w = *pw;
|
||||
int res;
|
||||
|
||||
if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
|
||||
INCREF(v);
|
||||
INCREF(w);
|
||||
if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
|
||||
Py_INCREF(v);
|
||||
Py_INCREF(w);
|
||||
return 0;
|
||||
}
|
||||
if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
|
||||
|
@ -449,7 +450,7 @@ coerce(pv, pw)
|
|||
if (res <= 0)
|
||||
return res;
|
||||
}
|
||||
err_setstr(TypeError, "number coercion failed");
|
||||
PyErr_SetString(PyExc_TypeError, "number coercion failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -457,26 +458,26 @@ coerce(pv, pw)
|
|||
/* Test whether an object can be called */
|
||||
|
||||
int
|
||||
callable(x)
|
||||
object *x;
|
||||
PyCallable_Check(x)
|
||||
PyObject *x;
|
||||
{
|
||||
if (x == NULL)
|
||||
return 0;
|
||||
if (x->ob_type->tp_call != NULL ||
|
||||
is_funcobject(x) ||
|
||||
is_instancemethodobject(x) ||
|
||||
is_methodobject(x) ||
|
||||
is_classobject(x))
|
||||
PyFunction_Check(x) ||
|
||||
PyMethod_Check(x) ||
|
||||
PyCFunction_Check(x) ||
|
||||
PyClass_Check(x))
|
||||
return 1;
|
||||
if (is_instanceobject(x)) {
|
||||
object *call = getattr(x, "__call__");
|
||||
if (PyInstance_Check(x)) {
|
||||
PyObject *call = PyObject_GetAttrString(x, "__call__");
|
||||
if (call == NULL) {
|
||||
err_clear();
|
||||
PyErr_Clear();
|
||||
return 0;
|
||||
}
|
||||
/* Could test recursively but don't, for fear of endless
|
||||
recursion if some joker sets self.__call__ = self */
|
||||
DECREF(call);
|
||||
Py_DECREF(call);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
@ -490,15 +491,15 @@ so there is exactly one (which is indestructible, by the way).
|
|||
*/
|
||||
|
||||
/* ARGSUSED */
|
||||
static object *
|
||||
static PyObject *
|
||||
none_repr(op)
|
||||
object *op;
|
||||
PyObject *op;
|
||||
{
|
||||
return newstringobject("None");
|
||||
return PyString_FromString("None");
|
||||
}
|
||||
|
||||
static typeobject Notype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
static PyTypeObject PyNothing_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
"None",
|
||||
0,
|
||||
|
@ -515,20 +516,20 @@ static typeobject Notype = {
|
|||
0, /*tp_hash */
|
||||
};
|
||||
|
||||
object NoObject = {
|
||||
OB_HEAD_INIT(&Notype)
|
||||
PyObject _Py_NoneStruct = {
|
||||
PyObject_HEAD_INIT(&PyNothing_Type)
|
||||
};
|
||||
|
||||
|
||||
#ifdef Py_TRACE_REFS
|
||||
|
||||
static object refchain = {&refchain, &refchain};
|
||||
static PyObject refchain = {&refchain, &refchain};
|
||||
|
||||
void
|
||||
NEWREF(op)
|
||||
object *op;
|
||||
_Py_NewReference(op)
|
||||
PyObject *op;
|
||||
{
|
||||
ref_total++;
|
||||
_Py_RefTotal++;
|
||||
op->ob_refcnt = 1;
|
||||
op->_ob_next = refchain._ob_next;
|
||||
op->_ob_prev = &refchain;
|
||||
|
@ -540,22 +541,22 @@ NEWREF(op)
|
|||
}
|
||||
|
||||
void
|
||||
UNREF(op)
|
||||
register object *op;
|
||||
_Py_ForgetReference(op)
|
||||
register PyObject *op;
|
||||
{
|
||||
register object *p;
|
||||
register PyObject *p;
|
||||
if (op->ob_refcnt < 0)
|
||||
fatal("UNREF negative refcnt");
|
||||
Py_FatalError("UNREF negative refcnt");
|
||||
if (op == &refchain ||
|
||||
op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
|
||||
fatal("UNREF invalid object");
|
||||
Py_FatalError("UNREF invalid object");
|
||||
#ifdef SLOW_UNREF_CHECK
|
||||
for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
|
||||
if (p == op)
|
||||
break;
|
||||
}
|
||||
if (p == &refchain) /* Not found */
|
||||
fatal("UNREF unknown object");
|
||||
Py_FatalError("UNREF unknown object");
|
||||
#endif
|
||||
op->_ob_next->_ob_prev = op->_ob_prev;
|
||||
op->_ob_prev->_ob_next = op->_ob_next;
|
||||
|
@ -566,11 +567,11 @@ UNREF(op)
|
|||
}
|
||||
|
||||
void
|
||||
DELREF(op)
|
||||
object *op;
|
||||
_Py_Dealloc(op)
|
||||
PyObject *op;
|
||||
{
|
||||
destructor dealloc = op->ob_type->tp_dealloc;
|
||||
UNREF(op);
|
||||
_Py_ForgetReference(op);
|
||||
op->ob_type = NULL;
|
||||
(*dealloc)(op);
|
||||
}
|
||||
|
@ -579,14 +580,14 @@ void
|
|||
_Py_PrintReferences(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
object *op;
|
||||
PyObject *op;
|
||||
fprintf(fp, "Remaining objects (except strings referenced once):\n");
|
||||
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
|
||||
if (op->ob_refcnt == 1 && is_stringobject(op))
|
||||
if (op->ob_refcnt == 1 && PyString_Check(op))
|
||||
continue; /* Will be printed elsewhere */
|
||||
fprintf(fp, "[%d] ", op->ob_refcnt);
|
||||
if (printobject(op, fp, 0) != 0)
|
||||
err_clear();
|
||||
if (PyObject_Print(op, fp, 0) != 0)
|
||||
PyErr_Clear();
|
||||
putc('\n', fp);
|
||||
}
|
||||
}
|
||||
|
@ -630,4 +631,4 @@ PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
|
|||
|
||||
|
||||
/* Hack to force loading of abstract.o */
|
||||
int (*_Py_abstract_hack) FPROTO((PyObject *)) = &PyObject_Length;
|
||||
int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
|
||||
|
|
|
@ -31,10 +31,10 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
/* Range object implementation */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "Python.h"
|
||||
|
||||
typedef struct {
|
||||
OB_HEAD
|
||||
PyObject_HEAD
|
||||
long start;
|
||||
long step;
|
||||
long len;
|
||||
|
@ -42,39 +42,40 @@ typedef struct {
|
|||
} rangeobject;
|
||||
|
||||
|
||||
object *
|
||||
newrangeobject(start, len, step, reps)
|
||||
PyObject *
|
||||
PyRange_New(start, len, step, reps)
|
||||
long start, len, step;
|
||||
int reps;
|
||||
{
|
||||
rangeobject *obj = NEWOBJ(rangeobject, &Rangetype);
|
||||
rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
|
||||
|
||||
obj->start = start;
|
||||
obj->len = len;
|
||||
obj->step = step;
|
||||
obj->reps = reps;
|
||||
|
||||
return (object *) obj;
|
||||
return (PyObject *) obj;
|
||||
}
|
||||
|
||||
static void
|
||||
range_dealloc(r)
|
||||
rangeobject *r;
|
||||
{
|
||||
DEL(r);
|
||||
PyMem_DEL(r);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
range_item(r, i)
|
||||
rangeobject *r;
|
||||
int i;
|
||||
{
|
||||
if (i < 0 || i >= r->len * r->reps) {
|
||||
err_setstr(IndexError, "range object index out of range");
|
||||
PyErr_SetString(PyExc_IndexError,
|
||||
"range object index out of range");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return newintobject(r->start + (i % r->len) * r->step);
|
||||
return PyInt_FromLong(r->start + (i % r->len) * r->step);
|
||||
}
|
||||
|
||||
static int
|
||||
|
@ -107,7 +108,7 @@ range_print(r, fp, flags)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
range_repr(r)
|
||||
rangeobject *r;
|
||||
{
|
||||
|
@ -117,33 +118,33 @@ range_repr(r)
|
|||
r->start + r->len * r->step,
|
||||
r->step,
|
||||
r->reps);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
range_concat(r, obj)
|
||||
rangeobject *r;
|
||||
object *obj;
|
||||
PyObject *obj;
|
||||
{
|
||||
err_setstr(TypeError, "cannot concatenate range objects");
|
||||
PyErr_SetString(PyExc_TypeError, "cannot concatenate range objects");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
range_repeat(r, n)
|
||||
rangeobject *r;
|
||||
int n;
|
||||
{
|
||||
if (n < 0)
|
||||
return (object *) newrangeobject(0, 0, 1, 1);
|
||||
return (PyObject *) PyRange_New(0, 0, 1, 1);
|
||||
|
||||
else if (n == 1) {
|
||||
INCREF(r);
|
||||
return (object *) r;
|
||||
Py_INCREF(r);
|
||||
return (PyObject *) r;
|
||||
}
|
||||
|
||||
else
|
||||
return (object *) newrangeobject(
|
||||
return (PyObject *) PyRange_New(
|
||||
r->start,
|
||||
r->len,
|
||||
r->step,
|
||||
|
@ -167,13 +168,14 @@ range_compare(r1, r2)
|
|||
return r1->reps - r2->reps;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
range_slice(r, low, high)
|
||||
rangeobject *r;
|
||||
int low, high;
|
||||
{
|
||||
if (r->reps != 1) {
|
||||
err_setstr(TypeError, "cannot slice a replicated range");
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"cannot slice a replicated range");
|
||||
return NULL;
|
||||
}
|
||||
if (low < 0)
|
||||
|
@ -188,55 +190,54 @@ range_slice(r, low, high)
|
|||
high = r->len;
|
||||
|
||||
if (low == 0 && high == r->len) {
|
||||
INCREF(r);
|
||||
return (object *) r;
|
||||
Py_INCREF(r);
|
||||
return (PyObject *) r;
|
||||
}
|
||||
|
||||
return (object *) newrangeobject(
|
||||
return (PyObject *) PyRange_New(
|
||||
low * r->step + r->start,
|
||||
high - low,
|
||||
r->step,
|
||||
1);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
range_tolist(self, args)
|
||||
rangeobject *self;
|
||||
object *args;
|
||||
PyObject *args;
|
||||
{
|
||||
object *thelist;
|
||||
PyObject *thelist;
|
||||
int j;
|
||||
int len = self->len * self->reps;
|
||||
|
||||
if (! getargs(args, ""))
|
||||
if (! PyArg_Parse(args, ""))
|
||||
return NULL;
|
||||
|
||||
if ((thelist = newlistobject(len)) == NULL)
|
||||
if ((thelist = PyList_New(len)) == NULL)
|
||||
return NULL;
|
||||
|
||||
for (j = 0; j < len; ++j)
|
||||
if ((setlistitem(thelist, j,
|
||||
(object *) newintobject(
|
||||
self->start + (j % self->len) * self->step))) < 0)
|
||||
if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
|
||||
self->start + (j % self->len) * self->step))) < 0)
|
||||
return NULL;
|
||||
|
||||
return thelist;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
range_getattr(r, name)
|
||||
rangeobject *r;
|
||||
char *name;
|
||||
{
|
||||
static struct methodlist range_methods[] = {
|
||||
{"tolist", (method)range_tolist},
|
||||
static PyMethodDef range_methods[] = {
|
||||
{"tolist", (PyCFunction)range_tolist},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
return findmethod(range_methods, (object *) r, name);
|
||||
return Py_FindMethod(range_methods, (PyObject *) r, name);
|
||||
}
|
||||
|
||||
static sequence_methods range_as_sequence = {
|
||||
static PySequenceMethods range_as_sequence = {
|
||||
(inquiry)range_length, /*sq_length*/
|
||||
(binaryfunc)range_concat, /*sq_concat*/
|
||||
(intargfunc)range_repeat, /*sq_repeat*/
|
||||
|
@ -246,8 +247,8 @@ static sequence_methods range_as_sequence = {
|
|||
0, /*sq_ass_slice*/
|
||||
};
|
||||
|
||||
typeobject Rangetype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyRange_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0, /* Number of items for varobject */
|
||||
"xrange", /* Name of this type */
|
||||
sizeof(rangeobject), /* Basic object size */
|
||||
|
|
|
@ -31,7 +31,7 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
/* String object implementation */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "Python.h"
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
|
@ -47,9 +47,9 @@ int null_strings, one_strings;
|
|||
#endif
|
||||
#endif
|
||||
|
||||
static stringobject *characters[UCHAR_MAX + 1];
|
||||
static PyStringObject *characters[UCHAR_MAX + 1];
|
||||
#ifndef DONT_SHARE_SHORT_STRINGS
|
||||
static stringobject *nullstring;
|
||||
static PyStringObject *nullstring;
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
@ -67,33 +67,35 @@ static stringobject *nullstring;
|
|||
newsizedstringobject() with a NULL first argument, because in the
|
||||
future these routines may try to do even more sharing of objects.
|
||||
*/
|
||||
object *
|
||||
newsizedstringobject(str, size)
|
||||
PyObject *
|
||||
PyString_FromStringAndSize(str, size)
|
||||
const char *str;
|
||||
int size;
|
||||
{
|
||||
register stringobject *op;
|
||||
register PyStringObject *op;
|
||||
#ifndef DONT_SHARE_SHORT_STRINGS
|
||||
if (size == 0 && (op = nullstring) != NULL) {
|
||||
#ifdef COUNT_ALLOCS
|
||||
null_strings++;
|
||||
#endif
|
||||
INCREF(op);
|
||||
return (object *)op;
|
||||
Py_INCREF(op);
|
||||
return (PyObject *)op;
|
||||
}
|
||||
if (size == 1 && str != NULL && (op = characters[*str & UCHAR_MAX]) != NULL) {
|
||||
if (size == 1 && str != NULL &&
|
||||
(op = characters[*str & UCHAR_MAX]) != NULL)
|
||||
{
|
||||
#ifdef COUNT_ALLOCS
|
||||
one_strings++;
|
||||
#endif
|
||||
INCREF(op);
|
||||
return (object *)op;
|
||||
Py_INCREF(op);
|
||||
return (PyObject *)op;
|
||||
}
|
||||
#endif /* DONT_SHARE_SHORT_STRINGS */
|
||||
op = (stringobject *)
|
||||
malloc(sizeof(stringobject) + size * sizeof(char));
|
||||
op = (PyStringObject *)
|
||||
malloc(sizeof(PyStringObject) + size * sizeof(char));
|
||||
if (op == NULL)
|
||||
return err_nomem();
|
||||
op->ob_type = &Stringtype;
|
||||
return PyErr_NoMemory();
|
||||
op->ob_type = &PyString_Type;
|
||||
op->ob_size = size;
|
||||
#ifdef CACHE_HASH
|
||||
op->ob_shash = -1;
|
||||
|
@ -101,49 +103,49 @@ newsizedstringobject(str, size)
|
|||
#ifdef INTERN_STRINGS
|
||||
op->ob_sinterned = NULL;
|
||||
#endif
|
||||
NEWREF(op);
|
||||
_Py_NewReference(op);
|
||||
if (str != NULL)
|
||||
memcpy(op->ob_sval, str, size);
|
||||
op->ob_sval[size] = '\0';
|
||||
#ifndef DONT_SHARE_SHORT_STRINGS
|
||||
if (size == 0) {
|
||||
nullstring = op;
|
||||
INCREF(op);
|
||||
Py_INCREF(op);
|
||||
} else if (size == 1 && str != NULL) {
|
||||
characters[*str & UCHAR_MAX] = op;
|
||||
INCREF(op);
|
||||
Py_INCREF(op);
|
||||
}
|
||||
#endif
|
||||
return (object *) op;
|
||||
return (PyObject *) op;
|
||||
}
|
||||
|
||||
object *
|
||||
newstringobject(str)
|
||||
PyObject *
|
||||
PyString_FromString(str)
|
||||
const char *str;
|
||||
{
|
||||
register unsigned int size = strlen(str);
|
||||
register stringobject *op;
|
||||
register PyStringObject *op;
|
||||
#ifndef DONT_SHARE_SHORT_STRINGS
|
||||
if (size == 0 && (op = nullstring) != NULL) {
|
||||
#ifdef COUNT_ALLOCS
|
||||
null_strings++;
|
||||
#endif
|
||||
INCREF(op);
|
||||
return (object *)op;
|
||||
Py_INCREF(op);
|
||||
return (PyObject *)op;
|
||||
}
|
||||
if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
|
||||
#ifdef COUNT_ALLOCS
|
||||
one_strings++;
|
||||
#endif
|
||||
INCREF(op);
|
||||
return (object *)op;
|
||||
Py_INCREF(op);
|
||||
return (PyObject *)op;
|
||||
}
|
||||
#endif /* DONT_SHARE_SHORT_STRINGS */
|
||||
op = (stringobject *)
|
||||
malloc(sizeof(stringobject) + size * sizeof(char));
|
||||
op = (PyStringObject *)
|
||||
malloc(sizeof(PyStringObject) + size * sizeof(char));
|
||||
if (op == NULL)
|
||||
return err_nomem();
|
||||
op->ob_type = &Stringtype;
|
||||
return PyErr_NoMemory();
|
||||
op->ob_type = &PyString_Type;
|
||||
op->ob_size = size;
|
||||
#ifdef CACHE_HASH
|
||||
op->ob_shash = -1;
|
||||
|
@ -151,54 +153,54 @@ newstringobject(str)
|
|||
#ifdef INTERN_STRINGS
|
||||
op->ob_sinterned = NULL;
|
||||
#endif
|
||||
NEWREF(op);
|
||||
_Py_NewReference(op);
|
||||
strcpy(op->ob_sval, str);
|
||||
#ifndef DONT_SHARE_SHORT_STRINGS
|
||||
if (size == 0) {
|
||||
nullstring = op;
|
||||
INCREF(op);
|
||||
Py_INCREF(op);
|
||||
} else if (size == 1) {
|
||||
characters[*str & UCHAR_MAX] = op;
|
||||
INCREF(op);
|
||||
Py_INCREF(op);
|
||||
}
|
||||
#endif
|
||||
return (object *) op;
|
||||
return (PyObject *) op;
|
||||
}
|
||||
|
||||
static void
|
||||
string_dealloc(op)
|
||||
object *op;
|
||||
PyObject *op;
|
||||
{
|
||||
DEL(op);
|
||||
PyMem_DEL(op);
|
||||
}
|
||||
|
||||
int
|
||||
getstringsize(op)
|
||||
register object *op;
|
||||
PyString_Size(op)
|
||||
register PyObject *op;
|
||||
{
|
||||
if (!is_stringobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyString_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
return ((stringobject *)op) -> ob_size;
|
||||
return ((PyStringObject *)op) -> ob_size;
|
||||
}
|
||||
|
||||
/*const*/ char *
|
||||
getstringvalue(op)
|
||||
register object *op;
|
||||
PyString_AsString(op)
|
||||
register PyObject *op;
|
||||
{
|
||||
if (!is_stringobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyString_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
return ((stringobject *)op) -> ob_sval;
|
||||
return ((PyStringObject *)op) -> ob_sval;
|
||||
}
|
||||
|
||||
/* Methods */
|
||||
|
||||
static int
|
||||
string_print(op, fp, flags)
|
||||
stringobject *op;
|
||||
PyStringObject *op;
|
||||
FILE *fp;
|
||||
int flags;
|
||||
{
|
||||
|
@ -206,7 +208,7 @@ string_print(op, fp, flags)
|
|||
char c;
|
||||
int quote;
|
||||
/* XXX Ought to check for interrupts when writing long strings */
|
||||
if (flags & PRINT_RAW) {
|
||||
if (flags & Py_PRINT_RAW) {
|
||||
fwrite(op->ob_sval, 1, (int) op->ob_size, fp);
|
||||
return 0;
|
||||
}
|
||||
|
@ -230,13 +232,13 @@ string_print(op, fp, flags)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
string_repr(op)
|
||||
register stringobject *op;
|
||||
register PyStringObject *op;
|
||||
{
|
||||
/* XXX overflow? */
|
||||
int newsize = 2 + 4 * op->ob_size * sizeof(char);
|
||||
object *v = newsizedstringobject((char *)NULL, newsize);
|
||||
PyObject *v = PyString_FromStringAndSize((char *)NULL, newsize);
|
||||
if (v == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -251,7 +253,7 @@ string_repr(op)
|
|||
if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
|
||||
quote = '"';
|
||||
|
||||
p = ((stringobject *)v)->ob_sval;
|
||||
p = ((PyStringObject *)v)->ob_sval;
|
||||
*p++ = quote;
|
||||
for (i = 0; i < op->ob_size; i++) {
|
||||
c = op->ob_sval[i];
|
||||
|
@ -267,45 +269,46 @@ string_repr(op)
|
|||
}
|
||||
*p++ = quote;
|
||||
*p = '\0';
|
||||
resizestring(&v, (int) (p - ((stringobject *)v)->ob_sval));
|
||||
_PyString_Resize(
|
||||
&v, (int) (p - ((PyStringObject *)v)->ob_sval));
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
string_length(a)
|
||||
stringobject *a;
|
||||
PyStringObject *a;
|
||||
{
|
||||
return a->ob_size;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
string_concat(a, bb)
|
||||
register stringobject *a;
|
||||
register object *bb;
|
||||
register PyStringObject *a;
|
||||
register PyObject *bb;
|
||||
{
|
||||
register unsigned int size;
|
||||
register stringobject *op;
|
||||
if (!is_stringobject(bb)) {
|
||||
err_badarg();
|
||||
register PyStringObject *op;
|
||||
if (!PyString_Check(bb)) {
|
||||
PyErr_BadArgument();
|
||||
return NULL;
|
||||
}
|
||||
#define b ((stringobject *)bb)
|
||||
#define b ((PyStringObject *)bb)
|
||||
/* Optimize cases with empty left or right operand */
|
||||
if (a->ob_size == 0) {
|
||||
INCREF(bb);
|
||||
Py_INCREF(bb);
|
||||
return bb;
|
||||
}
|
||||
if (b->ob_size == 0) {
|
||||
INCREF(a);
|
||||
return (object *)a;
|
||||
Py_INCREF(a);
|
||||
return (PyObject *)a;
|
||||
}
|
||||
size = a->ob_size + b->ob_size;
|
||||
op = (stringobject *)
|
||||
malloc(sizeof(stringobject) + size * sizeof(char));
|
||||
op = (PyStringObject *)
|
||||
malloc(sizeof(PyStringObject) + size * sizeof(char));
|
||||
if (op == NULL)
|
||||
return err_nomem();
|
||||
op->ob_type = &Stringtype;
|
||||
return PyErr_NoMemory();
|
||||
op->ob_type = &PyString_Type;
|
||||
op->ob_size = size;
|
||||
#ifdef CACHE_HASH
|
||||
op->ob_shash = -1;
|
||||
|
@ -313,34 +316,34 @@ string_concat(a, bb)
|
|||
#ifdef INTERN_STRINGS
|
||||
op->ob_sinterned = NULL;
|
||||
#endif
|
||||
NEWREF(op);
|
||||
_Py_NewReference(op);
|
||||
memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
|
||||
memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size);
|
||||
op->ob_sval[size] = '\0';
|
||||
return (object *) op;
|
||||
return (PyObject *) op;
|
||||
#undef b
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
string_repeat(a, n)
|
||||
register stringobject *a;
|
||||
register PyStringObject *a;
|
||||
register int n;
|
||||
{
|
||||
register int i;
|
||||
register int size;
|
||||
register stringobject *op;
|
||||
register PyStringObject *op;
|
||||
if (n < 0)
|
||||
n = 0;
|
||||
size = a->ob_size * n;
|
||||
if (size == a->ob_size) {
|
||||
INCREF(a);
|
||||
return (object *)a;
|
||||
Py_INCREF(a);
|
||||
return (PyObject *)a;
|
||||
}
|
||||
op = (stringobject *)
|
||||
malloc(sizeof(stringobject) + size * sizeof(char));
|
||||
op = (PyStringObject *)
|
||||
malloc(sizeof(PyStringObject) + size * sizeof(char));
|
||||
if (op == NULL)
|
||||
return err_nomem();
|
||||
op->ob_type = &Stringtype;
|
||||
return PyErr_NoMemory();
|
||||
op->ob_type = &PyString_Type;
|
||||
op->ob_size = size;
|
||||
#ifdef CACHE_HASH
|
||||
op->ob_shash = -1;
|
||||
|
@ -348,18 +351,18 @@ string_repeat(a, n)
|
|||
#ifdef INTERN_STRINGS
|
||||
op->ob_sinterned = NULL;
|
||||
#endif
|
||||
NEWREF(op);
|
||||
_Py_NewReference(op);
|
||||
for (i = 0; i < size; i += a->ob_size)
|
||||
memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size);
|
||||
op->ob_sval[size] = '\0';
|
||||
return (object *) op;
|
||||
return (PyObject *) op;
|
||||
}
|
||||
|
||||
/* String slice a[i:j] consists of characters a[i] ... a[j-1] */
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
string_slice(a, i, j)
|
||||
register stringobject *a;
|
||||
register PyStringObject *a;
|
||||
register int i, j; /* May be negative! */
|
||||
{
|
||||
if (i < 0)
|
||||
|
@ -369,45 +372,45 @@ string_slice(a, i, j)
|
|||
if (j > a->ob_size)
|
||||
j = a->ob_size;
|
||||
if (i == 0 && j == a->ob_size) { /* It's the same as a */
|
||||
INCREF(a);
|
||||
return (object *)a;
|
||||
Py_INCREF(a);
|
||||
return (PyObject *)a;
|
||||
}
|
||||
if (j < i)
|
||||
j = i;
|
||||
return newsizedstringobject(a->ob_sval + i, (int) (j-i));
|
||||
return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i));
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
string_item(a, i)
|
||||
stringobject *a;
|
||||
PyStringObject *a;
|
||||
register int i;
|
||||
{
|
||||
int c;
|
||||
object *v;
|
||||
PyObject *v;
|
||||
if (i < 0 || i >= a->ob_size) {
|
||||
err_setstr(IndexError, "string index out of range");
|
||||
PyErr_SetString(PyExc_IndexError, "string index out of range");
|
||||
return NULL;
|
||||
}
|
||||
c = a->ob_sval[i] & UCHAR_MAX;
|
||||
v = (object *) characters[c];
|
||||
v = (PyObject *) characters[c];
|
||||
#ifdef COUNT_ALLOCS
|
||||
if (v != NULL)
|
||||
one_strings++;
|
||||
#endif
|
||||
if (v == NULL) {
|
||||
v = newsizedstringobject((char *)NULL, 1);
|
||||
v = PyString_FromStringAndSize((char *)NULL, 1);
|
||||
if (v == NULL)
|
||||
return NULL;
|
||||
characters[c] = (stringobject *) v;
|
||||
((stringobject *)v)->ob_sval[0] = c;
|
||||
characters[c] = (PyStringObject *) v;
|
||||
((PyStringObject *)v)->ob_sval[0] = c;
|
||||
}
|
||||
INCREF(v);
|
||||
Py_INCREF(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
static int
|
||||
string_compare(a, b)
|
||||
stringobject *a, *b;
|
||||
PyStringObject *a, *b;
|
||||
{
|
||||
int len_a = a->ob_size, len_b = b->ob_size;
|
||||
int min_len = (len_a < len_b) ? len_a : len_b;
|
||||
|
@ -424,7 +427,7 @@ string_compare(a, b)
|
|||
|
||||
static long
|
||||
string_hash(a)
|
||||
stringobject *a;
|
||||
PyStringObject *a;
|
||||
{
|
||||
register int len;
|
||||
register unsigned char *p;
|
||||
|
@ -436,7 +439,7 @@ string_hash(a)
|
|||
#ifdef INTERN_STRINGS
|
||||
if (a->ob_sinterned != NULL)
|
||||
return (a->ob_shash =
|
||||
((stringobject *)(a->ob_sinterned))->ob_shash);
|
||||
((PyStringObject *)(a->ob_sinterned))->ob_shash);
|
||||
#endif
|
||||
#endif
|
||||
len = a->ob_size;
|
||||
|
@ -453,7 +456,7 @@ string_hash(a)
|
|||
return x;
|
||||
}
|
||||
|
||||
static sequence_methods string_as_sequence = {
|
||||
static PySequenceMethods string_as_sequence = {
|
||||
(inquiry)string_length, /*sq_length*/
|
||||
(binaryfunc)string_concat, /*sq_concat*/
|
||||
(intargfunc)string_repeat, /*sq_repeat*/
|
||||
|
@ -463,11 +466,11 @@ static sequence_methods string_as_sequence = {
|
|||
0, /*sq_ass_slice*/
|
||||
};
|
||||
|
||||
typeobject Stringtype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyString_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
"string",
|
||||
sizeof(stringobject),
|
||||
sizeof(PyStringObject),
|
||||
sizeof(char),
|
||||
(destructor)string_dealloc, /*tp_dealloc*/
|
||||
(printfunc)string_print, /*tp_print*/
|
||||
|
@ -489,30 +492,30 @@ typeobject Stringtype = {
|
|||
};
|
||||
|
||||
void
|
||||
joinstring(pv, w)
|
||||
register object **pv;
|
||||
register object *w;
|
||||
PyString_Concat(pv, w)
|
||||
register PyObject **pv;
|
||||
register PyObject *w;
|
||||
{
|
||||
register object *v;
|
||||
register PyObject *v;
|
||||
if (*pv == NULL)
|
||||
return;
|
||||
if (w == NULL || !is_stringobject(*pv)) {
|
||||
DECREF(*pv);
|
||||
if (w == NULL || !PyString_Check(*pv)) {
|
||||
Py_DECREF(*pv);
|
||||
*pv = NULL;
|
||||
return;
|
||||
}
|
||||
v = string_concat((stringobject *) *pv, w);
|
||||
DECREF(*pv);
|
||||
v = string_concat((PyStringObject *) *pv, w);
|
||||
Py_DECREF(*pv);
|
||||
*pv = v;
|
||||
}
|
||||
|
||||
void
|
||||
joinstring_decref(pv, w)
|
||||
register object **pv;
|
||||
register object *w;
|
||||
PyString_ConcatAndDel(pv, w)
|
||||
register PyObject **pv;
|
||||
register PyObject *w;
|
||||
{
|
||||
joinstring(pv, w);
|
||||
XDECREF(w);
|
||||
PyString_Concat(pv, w);
|
||||
Py_XDECREF(w);
|
||||
}
|
||||
|
||||
|
||||
|
@ -524,34 +527,34 @@ joinstring_decref(pv, w)
|
|||
already be known to some other part of the code... */
|
||||
|
||||
int
|
||||
resizestring(pv, newsize)
|
||||
object **pv;
|
||||
_PyString_Resize(pv, newsize)
|
||||
PyObject **pv;
|
||||
int newsize;
|
||||
{
|
||||
register object *v;
|
||||
register stringobject *sv;
|
||||
register PyObject *v;
|
||||
register PyStringObject *sv;
|
||||
v = *pv;
|
||||
if (!is_stringobject(v) || v->ob_refcnt != 1) {
|
||||
if (!PyString_Check(v) || v->ob_refcnt != 1) {
|
||||
*pv = 0;
|
||||
DECREF(v);
|
||||
err_badcall();
|
||||
Py_DECREF(v);
|
||||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
/* XXX UNREF/NEWREF interface should be more symmetrical */
|
||||
#ifdef Py_REF_DEBUG
|
||||
--_Py_RefTotal;
|
||||
#endif
|
||||
UNREF(v);
|
||||
*pv = (object *)
|
||||
_Py_ForgetReference(v);
|
||||
*pv = (PyObject *)
|
||||
realloc((char *)v,
|
||||
sizeof(stringobject) + newsize * sizeof(char));
|
||||
sizeof(PyStringObject) + newsize * sizeof(char));
|
||||
if (*pv == NULL) {
|
||||
DEL(v);
|
||||
err_nomem();
|
||||
PyMem_DEL(v);
|
||||
PyErr_NoMemory();
|
||||
return -1;
|
||||
}
|
||||
NEWREF(*pv);
|
||||
sv = (stringobject *) *pv;
|
||||
_Py_NewReference(*pv);
|
||||
sv = (PyStringObject *) *pv;
|
||||
sv->ob_size = newsize;
|
||||
sv->ob_sval[newsize] = '\0';
|
||||
return 0;
|
||||
|
@ -559,9 +562,9 @@ resizestring(pv, newsize)
|
|||
|
||||
/* Helpers for formatstring */
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
getnextarg(args, arglen, p_argidx)
|
||||
object *args;
|
||||
PyObject *args;
|
||||
int arglen;
|
||||
int *p_argidx;
|
||||
{
|
||||
|
@ -571,9 +574,10 @@ getnextarg(args, arglen, p_argidx)
|
|||
if (arglen < 0)
|
||||
return args;
|
||||
else
|
||||
return gettupleitem(args, argidx);
|
||||
return PyTuple_GetItem(args, argidx);
|
||||
}
|
||||
err_setstr(TypeError, "not enough arguments for format string");
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"not enough arguments for format string");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -583,7 +587,7 @@ getnextarg(args, arglen, p_argidx)
|
|||
#define F_ALT (1<<3)
|
||||
#define F_ZERO (1<<4)
|
||||
|
||||
extern double fabs PROTO((double));
|
||||
extern double fabs Py_PROTO((double));
|
||||
|
||||
static int
|
||||
formatfloat(buf, flags, prec, type, v)
|
||||
|
@ -591,11 +595,11 @@ formatfloat(buf, flags, prec, type, v)
|
|||
int flags;
|
||||
int prec;
|
||||
int type;
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
char fmt[20];
|
||||
double x;
|
||||
if (!getargs(v, "d;float argument required", &x))
|
||||
if (!PyArg_Parse(v, "d;float argument required", &x))
|
||||
return -1;
|
||||
if (prec < 0)
|
||||
prec = 6;
|
||||
|
@ -614,11 +618,11 @@ formatint(buf, flags, prec, type, v)
|
|||
int flags;
|
||||
int prec;
|
||||
int type;
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
char fmt[20];
|
||||
long x;
|
||||
if (!getargs(v, "l;int argument required", &x))
|
||||
if (!PyArg_Parse(v, "l;int argument required", &x))
|
||||
return -1;
|
||||
if (prec < 0)
|
||||
prec = 1;
|
||||
|
@ -630,14 +634,14 @@ formatint(buf, flags, prec, type, v)
|
|||
static int
|
||||
formatchar(buf, v)
|
||||
char *buf;
|
||||
object *v;
|
||||
PyObject *v;
|
||||
{
|
||||
if (is_stringobject(v)) {
|
||||
if (!getargs(v, "c;%c requires int or char", &buf[0]))
|
||||
if (PyString_Check(v)) {
|
||||
if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0]))
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
if (!getargs(v, "b;%c requires int or char", &buf[0]))
|
||||
if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0]))
|
||||
return -1;
|
||||
}
|
||||
buf[1] = '\0';
|
||||
|
@ -647,29 +651,29 @@ formatchar(buf, v)
|
|||
|
||||
/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */
|
||||
|
||||
object *
|
||||
formatstring(format, args)
|
||||
object *format;
|
||||
object *args;
|
||||
PyObject *
|
||||
PyString_Format(format, args)
|
||||
PyObject *format;
|
||||
PyObject *args;
|
||||
{
|
||||
char *fmt, *res;
|
||||
int fmtcnt, rescnt, reslen, arglen, argidx;
|
||||
int args_owned = 0;
|
||||
object *result;
|
||||
object *dict = NULL;
|
||||
if (format == NULL || !is_stringobject(format) || args == NULL) {
|
||||
err_badcall();
|
||||
PyObject *result;
|
||||
PyObject *dict = NULL;
|
||||
if (format == NULL || !PyString_Check(format) || args == NULL) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
fmt = getstringvalue(format);
|
||||
fmtcnt = getstringsize(format);
|
||||
fmt = PyString_AsString(format);
|
||||
fmtcnt = PyString_Size(format);
|
||||
reslen = rescnt = fmtcnt + 100;
|
||||
result = newsizedstringobject((char *)NULL, reslen);
|
||||
result = PyString_FromStringAndSize((char *)NULL, reslen);
|
||||
if (result == NULL)
|
||||
return NULL;
|
||||
res = getstringvalue(result);
|
||||
if (is_tupleobject(args)) {
|
||||
arglen = gettuplesize(args);
|
||||
res = PyString_AsString(result);
|
||||
if (PyTuple_Check(args)) {
|
||||
arglen = PyTuple_Size(args);
|
||||
argidx = 0;
|
||||
}
|
||||
else {
|
||||
|
@ -683,9 +687,10 @@ formatstring(format, args)
|
|||
if (--rescnt < 0) {
|
||||
rescnt = fmtcnt + 100;
|
||||
reslen += rescnt;
|
||||
if (resizestring(&result, reslen) < 0)
|
||||
if (_PyString_Resize(&result, reslen) < 0)
|
||||
return NULL;
|
||||
res = getstringvalue(result) + reslen - rescnt;
|
||||
res = PyString_AsString(result)
|
||||
+ reslen - rescnt;
|
||||
--rescnt;
|
||||
}
|
||||
*res++ = *fmt++;
|
||||
|
@ -698,8 +703,8 @@ formatstring(format, args)
|
|||
int size = 0;
|
||||
int c = '\0';
|
||||
int fill;
|
||||
object *v = NULL;
|
||||
object *temp = NULL;
|
||||
PyObject *v = NULL;
|
||||
PyObject *temp = NULL;
|
||||
char *buf;
|
||||
int sign;
|
||||
int len;
|
||||
|
@ -708,10 +713,10 @@ formatstring(format, args)
|
|||
if (*fmt == '(') {
|
||||
char *keystart;
|
||||
int keylen;
|
||||
object *key;
|
||||
PyObject *key;
|
||||
|
||||
if (dict == NULL) {
|
||||
err_setstr(TypeError,
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"format requires a mapping");
|
||||
goto error;
|
||||
}
|
||||
|
@ -723,19 +728,20 @@ formatstring(format, args)
|
|||
keylen = fmt - keystart;
|
||||
++fmt;
|
||||
if (fmtcnt < 0) {
|
||||
err_setstr(ValueError,
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"incomplete format key");
|
||||
goto error;
|
||||
}
|
||||
key = newsizedstringobject(keystart, keylen);
|
||||
key = PyString_FromStringAndSize(keystart,
|
||||
keylen);
|
||||
if (key == NULL)
|
||||
goto error;
|
||||
if (args_owned) {
|
||||
DECREF(args);
|
||||
Py_DECREF(args);
|
||||
args_owned = 0;
|
||||
}
|
||||
args = PyObject_GetItem(dict, key);
|
||||
DECREF(key);
|
||||
Py_DECREF(key);
|
||||
if (args == NULL) {
|
||||
goto error;
|
||||
}
|
||||
|
@ -757,11 +763,12 @@ formatstring(format, args)
|
|||
v = getnextarg(args, arglen, &argidx);
|
||||
if (v == NULL)
|
||||
goto error;
|
||||
if (!is_intobject(v)) {
|
||||
err_setstr(TypeError, "* wants int");
|
||||
if (!PyInt_Check(v)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"* wants int");
|
||||
goto error;
|
||||
}
|
||||
width = getintvalue(v);
|
||||
width = PyInt_AsLong(v);
|
||||
if (width < 0)
|
||||
width = 0;
|
||||
if (--fmtcnt >= 0)
|
||||
|
@ -774,8 +781,9 @@ formatstring(format, args)
|
|||
if (!isdigit(c))
|
||||
break;
|
||||
if ((width*10) / 10 != width) {
|
||||
err_setstr(ValueError,
|
||||
"width too big");
|
||||
PyErr_SetString(
|
||||
PyExc_ValueError,
|
||||
"width too big");
|
||||
goto error;
|
||||
}
|
||||
width = width*10 + (c - '0');
|
||||
|
@ -789,12 +797,13 @@ formatstring(format, args)
|
|||
v = getnextarg(args, arglen, &argidx);
|
||||
if (v == NULL)
|
||||
goto error;
|
||||
if (!is_intobject(v)) {
|
||||
err_setstr(TypeError,
|
||||
"* wants int");
|
||||
if (!PyInt_Check(v)) {
|
||||
PyErr_SetString(
|
||||
PyExc_TypeError,
|
||||
"* wants int");
|
||||
goto error;
|
||||
}
|
||||
prec = getintvalue(v);
|
||||
prec = PyInt_AsLong(v);
|
||||
if (prec < 0)
|
||||
prec = 0;
|
||||
if (--fmtcnt >= 0)
|
||||
|
@ -807,7 +816,8 @@ formatstring(format, args)
|
|||
if (!isdigit(c))
|
||||
break;
|
||||
if ((prec*10) / 10 != prec) {
|
||||
err_setstr(ValueError,
|
||||
PyErr_SetString(
|
||||
PyExc_ValueError,
|
||||
"prec too big");
|
||||
goto error;
|
||||
}
|
||||
|
@ -823,7 +833,8 @@ formatstring(format, args)
|
|||
}
|
||||
}
|
||||
if (fmtcnt < 0) {
|
||||
err_setstr(ValueError, "incomplete format");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"incomplete format");
|
||||
goto error;
|
||||
}
|
||||
if (c != '%') {
|
||||
|
@ -839,11 +850,11 @@ formatstring(format, args)
|
|||
len = 1;
|
||||
break;
|
||||
case 's':
|
||||
temp = strobject(v);
|
||||
temp = PyObject_Str(v);
|
||||
if (temp == NULL)
|
||||
goto error;
|
||||
buf = getstringvalue(temp);
|
||||
len = getstringsize(temp);
|
||||
buf = PyString_AsString(temp);
|
||||
len = PyString_Size(temp);
|
||||
if (prec >= 0 && len > prec)
|
||||
len = prec;
|
||||
break;
|
||||
|
@ -895,7 +906,7 @@ formatstring(format, args)
|
|||
goto error;
|
||||
break;
|
||||
default:
|
||||
err_setstr(ValueError,
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"unsupported format character");
|
||||
goto error;
|
||||
}
|
||||
|
@ -917,9 +928,10 @@ formatstring(format, args)
|
|||
reslen -= rescnt;
|
||||
rescnt = width + fmtcnt + 100;
|
||||
reslen += rescnt;
|
||||
if (resizestring(&result, reslen) < 0)
|
||||
if (_PyString_Resize(&result, reslen) < 0)
|
||||
return NULL;
|
||||
res = getstringvalue(result) + reslen - rescnt;
|
||||
res = PyString_AsString(result)
|
||||
+ reslen - rescnt;
|
||||
}
|
||||
if (sign) {
|
||||
if (fill != ' ')
|
||||
|
@ -944,25 +956,26 @@ formatstring(format, args)
|
|||
*res++ = ' ';
|
||||
}
|
||||
if (dict && (argidx < arglen) && c != '%') {
|
||||
err_setstr(TypeError,
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"not all arguments converted");
|
||||
goto error;
|
||||
}
|
||||
XDECREF(temp);
|
||||
Py_XDECREF(temp);
|
||||
} /* '%' */
|
||||
} /* until end */
|
||||
if (argidx < arglen && !dict) {
|
||||
err_setstr(TypeError, "not all arguments converted");
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"not all arguments converted");
|
||||
goto error;
|
||||
}
|
||||
if (args_owned)
|
||||
DECREF(args);
|
||||
resizestring(&result, reslen - rescnt);
|
||||
Py_DECREF(args);
|
||||
_PyString_Resize(&result, reslen - rescnt);
|
||||
return result;
|
||||
error:
|
||||
DECREF(result);
|
||||
Py_DECREF(result);
|
||||
if (args_owned)
|
||||
DECREF(args);
|
||||
Py_DECREF(args);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
/* Tuple object implementation */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "Python.h"
|
||||
|
||||
#ifndef MAXSAVESIZE
|
||||
#define MAXSAVESIZE 20
|
||||
|
@ -41,109 +41,112 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
/* Entries 1 upto MAXSAVESIZE are free lists, entry 0 is the empty
|
||||
tuple () of which at most one instance will be allocated.
|
||||
*/
|
||||
static tupleobject *free_tuples[MAXSAVESIZE];
|
||||
static PyTupleObject *free_tuples[MAXSAVESIZE];
|
||||
#endif
|
||||
#ifdef COUNT_ALLOCS
|
||||
int fast_tuple_allocs;
|
||||
int tuple_zero_allocs;
|
||||
#endif
|
||||
|
||||
object *
|
||||
newtupleobject(size)
|
||||
PyObject *
|
||||
PyTuple_New(size)
|
||||
register int size;
|
||||
{
|
||||
register int i;
|
||||
register tupleobject *op;
|
||||
register PyTupleObject *op;
|
||||
if (size < 0) {
|
||||
err_badcall();
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
#if MAXSAVESIZE > 0
|
||||
if (size == 0 && free_tuples[0]) {
|
||||
op = free_tuples[0];
|
||||
INCREF(op);
|
||||
Py_INCREF(op);
|
||||
#ifdef COUNT_ALLOCS
|
||||
tuple_zero_allocs++;
|
||||
#endif
|
||||
return (object *) op;
|
||||
return (PyObject *) op;
|
||||
}
|
||||
if (0 < size && size < MAXSAVESIZE && (op = free_tuples[size]) != NULL) {
|
||||
free_tuples[size] = (tupleobject *) op->ob_item[0];
|
||||
if (0 < size && size < MAXSAVESIZE &&
|
||||
(op = free_tuples[size]) != NULL)
|
||||
{
|
||||
free_tuples[size] = (PyTupleObject *) op->ob_item[0];
|
||||
#ifdef COUNT_ALLOCS
|
||||
fast_tuple_allocs++;
|
||||
#endif
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
op = (tupleobject *)
|
||||
malloc(sizeof(tupleobject) + size * sizeof(object *));
|
||||
op = (PyTupleObject *) malloc(
|
||||
sizeof(PyTupleObject) + size * sizeof(PyObject *));
|
||||
if (op == NULL)
|
||||
return err_nomem();
|
||||
return PyErr_NoMemory();
|
||||
}
|
||||
op->ob_type = &Tupletype;
|
||||
op->ob_type = &PyTuple_Type;
|
||||
op->ob_size = size;
|
||||
for (i = 0; i < size; i++)
|
||||
op->ob_item[i] = NULL;
|
||||
NEWREF(op);
|
||||
_Py_NewReference(op);
|
||||
#if MAXSAVESIZE > 0
|
||||
if (size == 0) {
|
||||
free_tuples[0] = op;
|
||||
INCREF(op); /* extra INCREF so that this is never freed */
|
||||
Py_INCREF(op); /* extra INCREF so that this is never freed */
|
||||
}
|
||||
#endif
|
||||
return (object *) op;
|
||||
return (PyObject *) op;
|
||||
}
|
||||
|
||||
int
|
||||
gettuplesize(op)
|
||||
register object *op;
|
||||
PyTuple_Size(op)
|
||||
register PyObject *op;
|
||||
{
|
||||
if (!is_tupleobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyTuple_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
return ((tupleobject *)op)->ob_size;
|
||||
return ((PyTupleObject *)op)->ob_size;
|
||||
}
|
||||
|
||||
object *
|
||||
gettupleitem(op, i)
|
||||
register object *op;
|
||||
PyObject *
|
||||
PyTuple_GetItem(op, i)
|
||||
register PyObject *op;
|
||||
register int i;
|
||||
{
|
||||
if (!is_tupleobject(op)) {
|
||||
err_badcall();
|
||||
if (!PyTuple_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
|
||||
err_setstr(IndexError, "tuple index out of range");
|
||||
if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
|
||||
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
|
||||
return NULL;
|
||||
}
|
||||
return ((tupleobject *)op) -> ob_item[i];
|
||||
return ((PyTupleObject *)op) -> ob_item[i];
|
||||
}
|
||||
|
||||
int
|
||||
settupleitem(op, i, newitem)
|
||||
register object *op;
|
||||
PyTuple_SetItem(op, i, newitem)
|
||||
register PyObject *op;
|
||||
register int i;
|
||||
object *newitem;
|
||||
PyObject *newitem;
|
||||
{
|
||||
register object *olditem;
|
||||
register object **p;
|
||||
if (!is_tupleobject(op)) {
|
||||
XDECREF(newitem);
|
||||
err_badcall();
|
||||
register PyObject *olditem;
|
||||
register PyObject **p;
|
||||
if (!PyTuple_Check(op)) {
|
||||
Py_XDECREF(newitem);
|
||||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
|
||||
XDECREF(newitem);
|
||||
err_setstr(IndexError, "tuple assignment index out of range");
|
||||
if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
|
||||
Py_XDECREF(newitem);
|
||||
PyErr_SetString(PyExc_IndexError,
|
||||
"tuple assignment index out of range");
|
||||
return -1;
|
||||
}
|
||||
p = ((tupleobject *)op) -> ob_item + i;
|
||||
p = ((PyTupleObject *)op) -> ob_item + i;
|
||||
olditem = *p;
|
||||
*p = newitem;
|
||||
XDECREF(olditem);
|
||||
Py_XDECREF(olditem);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -151,14 +154,14 @@ settupleitem(op, i, newitem)
|
|||
|
||||
static void
|
||||
tupledealloc(op)
|
||||
register tupleobject *op;
|
||||
register PyTupleObject *op;
|
||||
{
|
||||
register int i;
|
||||
for (i = 0; i < op->ob_size; i++)
|
||||
XDECREF(op->ob_item[i]);
|
||||
Py_XDECREF(op->ob_item[i]);
|
||||
#if MAXSAVESIZE > 0
|
||||
if (0 < op->ob_size && op->ob_size < MAXSAVESIZE) {
|
||||
op->ob_item[0] = (object *) free_tuples[op->ob_size];
|
||||
op->ob_item[0] = (PyObject *) free_tuples[op->ob_size];
|
||||
free_tuples[op->ob_size] = op;
|
||||
} else
|
||||
#endif
|
||||
|
@ -167,7 +170,7 @@ tupledealloc(op)
|
|||
|
||||
static int
|
||||
tupleprint(op, fp, flags)
|
||||
tupleobject *op;
|
||||
PyTupleObject *op;
|
||||
FILE *fp;
|
||||
int flags;
|
||||
{
|
||||
|
@ -176,7 +179,7 @@ tupleprint(op, fp, flags)
|
|||
for (i = 0; i < op->ob_size; i++) {
|
||||
if (i > 0)
|
||||
fprintf(fp, ", ");
|
||||
if (printobject(op->ob_item[i], fp, 0) != 0)
|
||||
if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
|
||||
return -1;
|
||||
}
|
||||
if (op->ob_size == 1)
|
||||
|
@ -185,35 +188,35 @@ tupleprint(op, fp, flags)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
tuplerepr(v)
|
||||
tupleobject *v;
|
||||
PyTupleObject *v;
|
||||
{
|
||||
object *s, *comma;
|
||||
PyObject *s, *comma;
|
||||
int i;
|
||||
s = newstringobject("(");
|
||||
comma = newstringobject(", ");
|
||||
s = PyString_FromString("(");
|
||||
comma = PyString_FromString(", ");
|
||||
for (i = 0; i < v->ob_size && s != NULL; i++) {
|
||||
if (i > 0)
|
||||
joinstring(&s, comma);
|
||||
joinstring_decref(&s, reprobject(v->ob_item[i]));
|
||||
PyString_Concat(&s, comma);
|
||||
PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
|
||||
}
|
||||
DECREF(comma);
|
||||
Py_DECREF(comma);
|
||||
if (v->ob_size == 1)
|
||||
joinstring_decref(&s, newstringobject(","));
|
||||
joinstring_decref(&s, newstringobject(")"));
|
||||
PyString_ConcatAndDel(&s, PyString_FromString(","));
|
||||
PyString_ConcatAndDel(&s, PyString_FromString(")"));
|
||||
return s;
|
||||
}
|
||||
|
||||
static int
|
||||
tuplecompare(v, w)
|
||||
register tupleobject *v, *w;
|
||||
register PyTupleObject *v, *w;
|
||||
{
|
||||
register int len =
|
||||
(v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
|
||||
register int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
int cmp = cmpobject(v->ob_item[i], w->ob_item[i]);
|
||||
int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
}
|
||||
|
@ -222,15 +225,15 @@ tuplecompare(v, w)
|
|||
|
||||
static long
|
||||
tuplehash(v)
|
||||
tupleobject *v;
|
||||
PyTupleObject *v;
|
||||
{
|
||||
register long x, y;
|
||||
register int len = v->ob_size;
|
||||
register object **p;
|
||||
register PyObject **p;
|
||||
x = 0x345678L;
|
||||
p = v->ob_item;
|
||||
while (--len >= 0) {
|
||||
y = hashobject(*p++);
|
||||
y = PyObject_Hash(*p++);
|
||||
if (y == -1)
|
||||
return -1;
|
||||
x = (1000003*x) ^ y;
|
||||
|
@ -243,30 +246,30 @@ tuplehash(v)
|
|||
|
||||
static int
|
||||
tuplelength(a)
|
||||
tupleobject *a;
|
||||
PyTupleObject *a;
|
||||
{
|
||||
return a->ob_size;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
tupleitem(a, i)
|
||||
register tupleobject *a;
|
||||
register PyTupleObject *a;
|
||||
register int i;
|
||||
{
|
||||
if (i < 0 || i >= a->ob_size) {
|
||||
err_setstr(IndexError, "tuple index out of range");
|
||||
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
|
||||
return NULL;
|
||||
}
|
||||
INCREF(a->ob_item[i]);
|
||||
Py_INCREF(a->ob_item[i]);
|
||||
return a->ob_item[i];
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
tupleslice(a, ilow, ihigh)
|
||||
register tupleobject *a;
|
||||
register PyTupleObject *a;
|
||||
register int ilow, ihigh;
|
||||
{
|
||||
register tupleobject *np;
|
||||
register PyTupleObject *np;
|
||||
register int i;
|
||||
if (ilow < 0)
|
||||
ilow = 0;
|
||||
|
@ -276,97 +279,97 @@ tupleslice(a, ilow, ihigh)
|
|||
ihigh = ilow;
|
||||
if (ilow == 0 && ihigh == a->ob_size) {
|
||||
/* XXX can only do this if tuples are immutable! */
|
||||
INCREF(a);
|
||||
return (object *)a;
|
||||
Py_INCREF(a);
|
||||
return (PyObject *)a;
|
||||
}
|
||||
np = (tupleobject *)newtupleobject(ihigh - ilow);
|
||||
np = (PyTupleObject *)PyTuple_New(ihigh - ilow);
|
||||
if (np == NULL)
|
||||
return NULL;
|
||||
for (i = ilow; i < ihigh; i++) {
|
||||
object *v = a->ob_item[i];
|
||||
INCREF(v);
|
||||
PyObject *v = a->ob_item[i];
|
||||
Py_INCREF(v);
|
||||
np->ob_item[i - ilow] = v;
|
||||
}
|
||||
return (object *)np;
|
||||
return (PyObject *)np;
|
||||
}
|
||||
|
||||
object *
|
||||
gettupleslice(op, i, j)
|
||||
object *op;
|
||||
PyObject *
|
||||
PyTuple_GetSlice(op, i, j)
|
||||
PyObject *op;
|
||||
int i, j;
|
||||
{
|
||||
if (op == NULL || !is_tupleobject(op)) {
|
||||
err_badcall();
|
||||
if (op == NULL || !PyTuple_Check(op)) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
return tupleslice((tupleobject *)op, i, j);
|
||||
return tupleslice((PyTupleObject *)op, i, j);
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
tupleconcat(a, bb)
|
||||
register tupleobject *a;
|
||||
register object *bb;
|
||||
register PyTupleObject *a;
|
||||
register PyObject *bb;
|
||||
{
|
||||
register int size;
|
||||
register int i;
|
||||
tupleobject *np;
|
||||
if (!is_tupleobject(bb)) {
|
||||
err_badarg();
|
||||
PyTupleObject *np;
|
||||
if (!PyTuple_Check(bb)) {
|
||||
PyErr_BadArgument();
|
||||
return NULL;
|
||||
}
|
||||
#define b ((tupleobject *)bb)
|
||||
#define b ((PyTupleObject *)bb)
|
||||
size = a->ob_size + b->ob_size;
|
||||
np = (tupleobject *) newtupleobject(size);
|
||||
np = (PyTupleObject *) PyTuple_New(size);
|
||||
if (np == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
for (i = 0; i < a->ob_size; i++) {
|
||||
object *v = a->ob_item[i];
|
||||
INCREF(v);
|
||||
PyObject *v = a->ob_item[i];
|
||||
Py_INCREF(v);
|
||||
np->ob_item[i] = v;
|
||||
}
|
||||
for (i = 0; i < b->ob_size; i++) {
|
||||
object *v = b->ob_item[i];
|
||||
INCREF(v);
|
||||
PyObject *v = b->ob_item[i];
|
||||
Py_INCREF(v);
|
||||
np->ob_item[i + a->ob_size] = v;
|
||||
}
|
||||
return (object *)np;
|
||||
return (PyObject *)np;
|
||||
#undef b
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
tuplerepeat(a, n)
|
||||
tupleobject *a;
|
||||
PyTupleObject *a;
|
||||
int n;
|
||||
{
|
||||
int i, j;
|
||||
int size;
|
||||
tupleobject *np;
|
||||
object **p;
|
||||
PyTupleObject *np;
|
||||
PyObject **p;
|
||||
if (n < 0)
|
||||
n = 0;
|
||||
if (a->ob_size*n == a->ob_size) {
|
||||
/* Since tuples are immutable, we can return a shared
|
||||
copy in this case */
|
||||
INCREF(a);
|
||||
return (object *)a;
|
||||
Py_INCREF(a);
|
||||
return (PyObject *)a;
|
||||
}
|
||||
size = a->ob_size * n;
|
||||
np = (tupleobject *) newtupleobject(size);
|
||||
np = (PyTupleObject *) PyTuple_New(size);
|
||||
if (np == NULL)
|
||||
return NULL;
|
||||
p = np->ob_item;
|
||||
for (i = 0; i < n; i++) {
|
||||
for (j = 0; j < a->ob_size; j++) {
|
||||
*p = a->ob_item[j];
|
||||
INCREF(*p);
|
||||
Py_INCREF(*p);
|
||||
p++;
|
||||
}
|
||||
}
|
||||
return (object *) np;
|
||||
return (PyObject *) np;
|
||||
}
|
||||
|
||||
static sequence_methods tuple_as_sequence = {
|
||||
static PySequenceMethods tuple_as_sequence = {
|
||||
(inquiry)tuplelength, /*sq_length*/
|
||||
(binaryfunc)tupleconcat, /*sq_concat*/
|
||||
(intargfunc)tuplerepeat, /*sq_repeat*/
|
||||
|
@ -376,12 +379,12 @@ static sequence_methods tuple_as_sequence = {
|
|||
0, /*sq_ass_slice*/
|
||||
};
|
||||
|
||||
typeobject Tupletype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyTuple_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
"tuple",
|
||||
sizeof(tupleobject) - sizeof(object *),
|
||||
sizeof(object *),
|
||||
sizeof(PyTupleObject) - sizeof(PyObject *),
|
||||
sizeof(PyObject *),
|
||||
(destructor)tupledealloc, /*tp_dealloc*/
|
||||
(printfunc)tupleprint, /*tp_print*/
|
||||
0, /*tp_getattr*/
|
||||
|
@ -404,21 +407,21 @@ typeobject Tupletype = {
|
|||
front, otherwise it will grow or shrink at the end. */
|
||||
|
||||
int
|
||||
resizetuple(pv, newsize, last_is_sticky)
|
||||
object **pv;
|
||||
_PyTuple_Resize(pv, newsize, last_is_sticky)
|
||||
PyObject **pv;
|
||||
int newsize;
|
||||
int last_is_sticky;
|
||||
{
|
||||
register tupleobject *v;
|
||||
register tupleobject *sv;
|
||||
register PyTupleObject *v;
|
||||
register PyTupleObject *sv;
|
||||
int i;
|
||||
int sizediff;
|
||||
|
||||
v = (tupleobject *) *pv;
|
||||
if (v == NULL || !is_tupleobject(v) || v->ob_refcnt != 1) {
|
||||
v = (PyTupleObject *) *pv;
|
||||
if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1) {
|
||||
*pv = 0;
|
||||
DECREF(v);
|
||||
err_badcall();
|
||||
Py_DECREF(v);
|
||||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
sizediff = newsize - v->ob_size;
|
||||
|
@ -428,29 +431,30 @@ resizetuple(pv, newsize, last_is_sticky)
|
|||
#ifdef Py_REF_DEBUG
|
||||
--_Py_RefTotal;
|
||||
#endif
|
||||
UNREF(v);
|
||||
_Py_ForgetReference(v);
|
||||
if (last_is_sticky && sizediff < 0) {
|
||||
/* shrinking: move entries to the front and zero moved entries */
|
||||
/* shrinking:
|
||||
move entries to the front and zero moved entries */
|
||||
for (i = 0; i < newsize; i++) {
|
||||
XDECREF(v->ob_item[i]);
|
||||
Py_XDECREF(v->ob_item[i]);
|
||||
v->ob_item[i] = v->ob_item[i - sizediff];
|
||||
v->ob_item[i - sizediff] = NULL;
|
||||
}
|
||||
}
|
||||
for (i = newsize; i < v->ob_size; i++) {
|
||||
XDECREF(v->ob_item[i]);
|
||||
Py_XDECREF(v->ob_item[i]);
|
||||
v->ob_item[i] = NULL;
|
||||
}
|
||||
sv = (tupleobject *)
|
||||
sv = (PyTupleObject *)
|
||||
realloc((char *)v,
|
||||
sizeof(tupleobject) + newsize * sizeof(object *));
|
||||
*pv = (object *) sv;
|
||||
sizeof(PyTupleObject) + newsize * sizeof(PyObject *));
|
||||
*pv = (PyObject *) sv;
|
||||
if (sv == NULL) {
|
||||
DEL(v);
|
||||
err_nomem();
|
||||
PyMem_DEL(v);
|
||||
PyErr_NoMemory();
|
||||
return -1;
|
||||
}
|
||||
NEWREF(sv);
|
||||
_Py_NewReference(sv);
|
||||
for (i = sv->ob_size; i < newsize; i++)
|
||||
sv->ob_item[i] = NULL;
|
||||
if (last_is_sticky && sizediff > 0) {
|
||||
|
|
|
@ -31,44 +31,44 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
/* Type object implementation */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "Python.h"
|
||||
|
||||
/* Type object implementation */
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
type_getattr(t, name)
|
||||
typeobject *t;
|
||||
PyTypeObject *t;
|
||||
char *name;
|
||||
{
|
||||
if (strcmp(name, "__name__") == 0)
|
||||
return newstringobject(t->tp_name);
|
||||
return PyString_FromString(t->tp_name);
|
||||
if (strcmp(name, "__doc__") == 0) {
|
||||
char *doc = t->tp_doc;
|
||||
if (doc != NULL)
|
||||
return newstringobject(doc);
|
||||
INCREF(None);
|
||||
return None;
|
||||
return PyString_FromString(doc);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
if (strcmp(name, "__members__") == 0)
|
||||
return mkvalue("[ss]", "__doc__", "__name__");
|
||||
err_setstr(AttributeError, name);
|
||||
return Py_BuildValue("[ss]", "__doc__", "__name__");
|
||||
PyErr_SetString(PyExc_AttributeError, name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static object *
|
||||
static PyObject *
|
||||
type_repr(v)
|
||||
typeobject *v;
|
||||
PyTypeObject *v;
|
||||
{
|
||||
char buf[100];
|
||||
sprintf(buf, "<type '%.80s'>", v->tp_name);
|
||||
return newstringobject(buf);
|
||||
return PyString_FromString(buf);
|
||||
}
|
||||
|
||||
typeobject Typetype = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
PyTypeObject PyType_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0, /* Number of items for varobject */
|
||||
"type", /* Name of this type */
|
||||
sizeof(typeobject), /* Basic object size */
|
||||
sizeof(PyTypeObject), /* Basic object size */
|
||||
0, /* Item size for varobject */
|
||||
0, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
|
|
Loading…
Reference in New Issue