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