cinap_lenrekadd hg and python4bb9e2215 years agoHistory
2139 lines · 49.8 KB · C
RawDownload
	 * Use frexp to get at the bits in the double.	 * Since the VAX D double format has 56 mantissa bits, which is the	 * most of any double format in use, each of these parts may have as	 * many as (but no more than) 56 significant bits.	 * So, assuming sizeof(long) >= 4, each part can be broken into two	 * longs; frexp and multiplication are used to do that.	 * Also, since the Cray double format has 15 exponent bits, which is	 * the most of any double format in use, shifting the exponent field	 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).	 */	v = frexp(v, &expo);	v *= 2147483648.0;	/* 2**31 */	hipart = (long)v;	/* take the top 32 bits */	v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */	x = hipart + (long)v + (expo << 15);	if (x == -1)		x = -2;	return x;} long_Py_HashPointer(void *p){#if SIZEOF_LONG >= SIZEOF_VOID_P	return (long)p;#else	/* convert to a Python long and hash that */	PyObject* longobj;	long x; 	if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {		x = -1;		goto finally;	}	x = PyObject_Hash(longobj); finally:	Py_XDECREF(longobj);	return x;#endif}  longPyObject_Hash(PyObject *v){	PyTypeObject *tp = v->ob_type;	if (tp->tp_hash != NULL)		return (*tp->tp_hash)(v);	if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {		return _Py_HashPointer(v); /* Use address as hash value */	}	/* If there's a cmp but no hash defined, the object can't be hashed */	PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",		     v->ob_type->tp_name);	return -1;} PyObject *PyObject_GetAttrString(PyObject *v, const char *name){	PyObject *w, *res; 	if (v->ob_type->tp_getattr != NULL)		return (*v->ob_type->tp_getattr)(v, (char*)name);	w = PyString_InternFromString(name);	if (w == NULL)		return NULL;	res = PyObject_GetAttr(v, w);	Py_XDECREF(w);	return res;} intPyObject_HasAttrString(PyObject *v, const char *name){	PyObject *res = PyObject_GetAttrString(v, name);	if (res != NULL) {		Py_DECREF(res);		return 1;	}	PyErr_Clear();	return 0;} intPyObject_SetAttrString(PyObject *v, const char *name, PyObject *w){	PyObject *s;	int res; 	if (v->ob_type->tp_setattr != NULL)		return (*v->ob_type->tp_setattr)(v, (char*)name, w);	s = PyString_InternFromString(name);	if (s == NULL)		return -1;	res = PyObject_SetAttr(v, s, w);	Py_XDECREF(s);	return res;} PyObject *PyObject_GetAttr(PyObject *v, PyObject *name){	PyTypeObject *tp = v->ob_type; 	if (!PyString_Check(name)) {#ifdef Py_USING_UNICODE		/* The Unicode to string conversion is done here because the		   existing tp_getattro slots expect a string object as name		   and we wouldn't want to break those. */		if (PyUnicode_Check(name)) {			name = _PyUnicode_AsDefaultEncodedString(name, NULL);			if (name == NULL)				return NULL;		}		else#endif		{			PyErr_Format(PyExc_TypeError,				     "attribute name must be string, not '%.200s'",				     name->ob_type->tp_name);			return NULL;		}	}	if (tp->tp_getattro != NULL)		return (*tp->tp_getattro)(v, name);	if (tp->tp_getattr != NULL)		return (*tp->tp_getattr)(v, PyString_AS_STRING(name));	PyErr_Format(PyExc_AttributeError,		     "'%.50s' object has no attribute '%.400s'",		     tp->tp_name, PyString_AS_STRING(name));	return NULL;} intPyObject_HasAttr(PyObject *v, PyObject *name){	PyObject *res = PyObject_GetAttr(v, name);	if (res != NULL) {		Py_DECREF(res);		return 1;	}	PyErr_Clear();	return 0;} intPyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value){	PyTypeObject *tp = v->ob_type;	int err; 	if (!PyString_Check(name)){#ifdef Py_USING_UNICODE		/* The Unicode to string conversion is done here because the		   existing tp_setattro slots expect a string object as name		   and we wouldn't want to break those. */		if (PyUnicode_Check(name)) {			name = PyUnicode_AsEncodedString(name, NULL, NULL);			if (name == NULL)				return -1;		}		else#endif		{			PyErr_Format(PyExc_TypeError,				     "attribute name must be string, not '%.200s'",				     name->ob_type->tp_name);			return -1;		}	}	else		Py_INCREF(name); 	PyString_InternInPlace(&name);	if (tp->tp_setattro != NULL) {		err = (*tp->tp_setattro)(v, name, value);		Py_DECREF(name);		return err;	}	if (tp->tp_setattr != NULL) {		err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);		Py_DECREF(name);		return err;	}	Py_DECREF(name);	if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)		PyErr_Format(PyExc_TypeError,			     "'%.100s' object has no attributes "			     "(%s .%.100s)",			     tp->tp_name,			     value==NULL ? "del" : "assign to",			     PyString_AS_STRING(name));	else		PyErr_Format(PyExc_TypeError,			     "'%.100s' object has only read-only attributes "			     "(%s .%.100s)",			     tp->tp_name,			     value==NULL ? "del" : "assign to",			     PyString_AS_STRING(name));	return -1;} /* Helper to get a pointer to an object's __dict__ slot, if any */ PyObject **_PyObject_GetDictPtr(PyObject *obj){	Py_ssize_t dictoffset;	PyTypeObject *tp = obj->ob_type; 	if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))		return NULL;	dictoffset = tp->tp_dictoffset;	if (dictoffset == 0)		return NULL;	if (dictoffset < 0) {		Py_ssize_t tsize;		size_t size; 		tsize = ((PyVarObject *)obj)->ob_size;		if (tsize < 0)			tsize = -tsize;		size = _PyObject_VAR_SIZE(tp, tsize); 		dictoffset += (long)size;		assert(dictoffset > 0);		assert(dictoffset % SIZEOF_VOID_P == 0);	}	return (PyObject **) ((char *)obj + dictoffset);} PyObject *PyObject_SelfIter(PyObject *obj){	Py_INCREF(obj);	return obj;} /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ PyObject *PyObject_GenericGetAttr(PyObject *obj, PyObject *name){	PyTypeObject *tp = obj->ob_type;	PyObject *descr = NULL;	PyObject *res = NULL;	descrgetfunc f;	Py_ssize_t dictoffset;	PyObject **dictptr; 	if (!PyString_Check(name)){#ifdef Py_USING_UNICODE		/* The Unicode to string conversion is done here because the		   existing tp_setattro slots expect a string object as name		   and we wouldn't want to break those. */		if (PyUnicode_Check(name)) {			name = PyUnicode_AsEncodedString(name, NULL, NULL);			if (name == NULL)				return NULL;		}		else#endif		{			PyErr_Format(PyExc_TypeError,				     "attribute name must be string, not '%.200s'",				     name->ob_type->tp_name);			return NULL;		}	}	else		Py_INCREF(name); 	if (tp->tp_dict == NULL) {		if (PyType_Ready(tp) < 0)			goto done;	} 	/* Inline _PyType_Lookup */	{		Py_ssize_t i, n;		PyObject *mro, *base, *dict; 		/* Look in tp_dict of types in MRO */		mro = tp->tp_mro;		assert(mro != NULL);		assert(PyTuple_Check(mro));		n = PyTuple_GET_SIZE(mro);		for (i = 0; i < n; i++) {			base = PyTuple_GET_ITEM(mro, i);			if (PyClass_Check(base))				dict = ((PyClassObject *)base)->cl_dict;			else {				assert(PyType_Check(base));				dict = ((PyTypeObject *)base)->tp_dict;			}			assert(dict && PyDict_Check(dict));			descr = PyDict_GetItem(dict, name);			if (descr != NULL)				break;		}	} 	Py_XINCREF(descr); 	f = NULL;	if (descr != NULL &&	    PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {		f = descr->ob_type->tp_descr_get;		if (f != NULL && PyDescr_IsData(descr)) {			res = f(descr, obj, (PyObject *)obj->ob_type);			Py_DECREF(descr);			goto done;		}	} 	/* Inline _PyObject_GetDictPtr */	dictoffset = tp->tp_dictoffset;	if (dictoffset != 0) {		PyObject *dict;		if (dictoffset < 0) {			Py_ssize_t tsize;			size_t size; 			tsize = ((PyVarObject *)obj)->ob_size;			if (tsize < 0)				tsize = -tsize;			size = _PyObject_VAR_SIZE(tp, tsize); 			dictoffset += (long)size;			assert(dictoffset > 0);			assert(dictoffset % SIZEOF_VOID_P == 0);		}		dictptr = (PyObject **) ((char *)obj + dictoffset);		dict = *dictptr;		if (dict != NULL) {			res = PyDict_GetItem(dict, name);			if (res != NULL) {				Py_INCREF(res);				Py_XDECREF(descr);				goto done;			}		}	} 	if (f != NULL) {		res = f(descr, obj, (PyObject *)obj->ob_type);		Py_DECREF(descr);		goto done;	} 	if (descr != NULL) {		res = descr;		/* descr was already increfed above */		goto done;	} 	PyErr_Format(PyExc_AttributeError,		     "'%.50s' object has no attribute '%.400s'",		     tp->tp_name, PyString_AS_STRING(name));  done:	Py_DECREF(name);	return res;} intPyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value){	PyTypeObject *tp = obj->ob_type;	PyObject *descr;	descrsetfunc f;	PyObject **dictptr;	int res = -1; 	if (!PyString_Check(name)){#ifdef Py_USING_UNICODE		/* The Unicode to string conversion is done here because the		   existing tp_setattro slots expect a string object as name		   and we wouldn't want to break those. */		if (PyUnicode_Check(name)) {			name = PyUnicode_AsEncodedString(name, NULL, NULL);			if (name == NULL)				return -1;		}		else#endif		{			PyErr_Format(PyExc_TypeError,				     "attribute name must be string, not '%.200s'",				     name->ob_type->tp_name);			return -1;		}	}	else		Py_INCREF(name); 	if (tp->tp_dict == NULL) {		if (PyType_Ready(tp) < 0)			goto done;	} 	descr = _PyType_Lookup(tp, name);	f = NULL;	if (descr != NULL &&	    PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {		f = descr->ob_type->tp_descr_set;		if (f != NULL && PyDescr_IsData(descr)) {			res = f(descr, obj, value);			goto done;		}	} 	dictptr = _PyObject_GetDictPtr(obj);	if (dictptr != NULL) {		PyObject *dict = *dictptr;		if (dict == NULL && value != NULL) {			dict = PyDict_New();			if (dict == NULL)				goto done;			*dictptr = dict;		}		if (dict != NULL) {			if (value == NULL)				res = PyDict_DelItem(dict, name);			else				res = PyDict_SetItem(dict, name, value);			if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))				PyErr_SetObject(PyExc_AttributeError, name);			goto done;		}	} 	if (f != NULL) {		res = f(descr, obj, value);		goto done;	} 	if (descr == NULL) {		PyErr_Format(PyExc_AttributeError,			     "'%.100s' object has no attribute '%.200s'",			     tp->tp_name, PyString_AS_STRING(name));		goto done;	} 	PyErr_Format(PyExc_AttributeError,		     "'%.50s' object attribute '%.400s' is read-only",		     tp->tp_name, PyString_AS_STRING(name));  done:	Py_DECREF(name);	return res;} /* Test a value used as condition, e.g., in a for or if statement.   Return -1 if an error occurred */ intPyObject_IsTrue(PyObject *v){	Py_ssize_t res;	if (v == Py_True)		return 1;	if (v == Py_False)		return 0;	if (v == Py_None)		return 0;	else if (v->ob_type->tp_as_number != NULL &&		 v->ob_type->tp_as_number->nb_nonzero != NULL)		res = (*v->ob_type->tp_as_number->nb_nonzero)(v);	else if (v->ob_type->tp_as_mapping != NULL &&		 v->ob_type->tp_as_mapping->mp_length != NULL)		res = (*v->ob_type->tp_as_mapping->mp_length)(v);	else if (v->ob_type->tp_as_sequence != NULL &&		 v->ob_type->tp_as_sequence->sq_length != NULL)		res = (*v->ob_type->tp_as_sequence->sq_length)(v);	else		return 1;	/* if it is negative, it should be either -1 or -2 */	return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);} /* equivalent of 'not v'   Return -1 if an error occurred */ intPyObject_Not(PyObject *v){	int res;	res = PyObject_IsTrue(v);	if (res < 0)		return res;	return res == 0;} /* Coerce two numeric types to the "larger" one.   Increment the reference count on each argument.   Return value:   -1 if an error occurred;   0 if the coercion succeeded (and then the reference counts are increased);   1 if no coercion is possible (and no error is raised).

Lines 1001–1500 of 2139. Previous lines