cinap_lenrekadd hg and python4bb9e2215 years agoHistory
2139 lines · 49.8 KB · C
RawDownload
*/intPyNumber_CoerceEx(PyObject **pv, PyObject **pw){	register PyObject *v = *pv;	register PyObject *w = *pw;	int res; 	/* Shortcut only for old-style types */	if (v->ob_type == w->ob_type &&	    !PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES))	{		Py_INCREF(v);		Py_INCREF(w);		return 0;	}	if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {		res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);		if (res <= 0)			return res;	}	if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {		res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);		if (res <= 0)			return res;	}	return 1;} /* Coerce two numeric types to the "larger" one.   Increment the reference count on each argument.   Return -1 and raise an exception if no coercion is possible   (and then no reference count is incremented).*/intPyNumber_Coerce(PyObject **pv, PyObject **pw){	int err = PyNumber_CoerceEx(pv, pw);	if (err <= 0)		return err;	PyErr_SetString(PyExc_TypeError, "number coercion failed");	return -1;}  /* Test whether an object can be called */ intPyCallable_Check(PyObject *x){	if (x == NULL)		return 0;	if (PyInstance_Check(x)) {		PyObject *call = PyObject_GetAttrString(x, "__call__");		if (call == NULL) {			PyErr_Clear();			return 0;		}		/* Could test recursively but don't, for fear of endless		   recursion if some joker sets self.__call__ = self */		Py_DECREF(call);		return 1;	}	else {		return x->ob_type->tp_call != NULL;	}} /* Helper for PyObject_Dir.   Merge the __dict__ of aclass into dict, and recursively also all   the __dict__s of aclass's base classes.  The order of merging isn't   defined, as it's expected that only the final set of dict keys is   interesting.   Return 0 on success, -1 on error.*/ static intmerge_class_dict(PyObject* dict, PyObject* aclass){	PyObject *classdict;	PyObject *bases; 	assert(PyDict_Check(dict));	assert(aclass); 	/* Merge in the type's dict (if any). */	classdict = PyObject_GetAttrString(aclass, "__dict__");	if (classdict == NULL)		PyErr_Clear();	else {		int status = PyDict_Update(dict, classdict);		Py_DECREF(classdict);		if (status < 0)			return -1;	} 	/* Recursively merge in the base types' (if any) dicts. */	bases = PyObject_GetAttrString(aclass, "__bases__");	if (bases == NULL)		PyErr_Clear();	else {		/* We have no guarantee that bases is a real tuple */		Py_ssize_t i, n;		n = PySequence_Size(bases); /* This better be right */		if (n < 0)			PyErr_Clear();		else {			for (i = 0; i < n; i++) {				int status;				PyObject *base = PySequence_GetItem(bases, i);				if (base == NULL) {					Py_DECREF(bases);					return -1;				}				status = merge_class_dict(dict, base);				Py_DECREF(base);				if (status < 0) {					Py_DECREF(bases);					return -1;				}			}		}		Py_DECREF(bases);	}	return 0;} /* Helper for PyObject_Dir.   If obj has an attr named attrname that's a list, merge its string   elements into keys of dict.   Return 0 on success, -1 on error.  Errors due to not finding the attr,   or the attr not being a list, are suppressed.*/ static intmerge_list_attr(PyObject* dict, PyObject* obj, const char *attrname){	PyObject *list;	int result = 0; 	assert(PyDict_Check(dict));	assert(obj);	assert(attrname); 	list = PyObject_GetAttrString(obj, attrname);	if (list == NULL)		PyErr_Clear(); 	else if (PyList_Check(list)) {		int i;		for (i = 0; i < PyList_GET_SIZE(list); ++i) {			PyObject *item = PyList_GET_ITEM(list, i);			if (PyString_Check(item)) {				result = PyDict_SetItem(dict, item, Py_None);				if (result < 0)					break;			}		}	} 	Py_XDECREF(list);	return result;} /* Like __builtin__.dir(arg).  See bltinmodule.c's builtin_dir for the   docstring, which should be kept in synch with this implementation. */ PyObject *PyObject_Dir(PyObject *arg){	/* Set exactly one of these non-NULL before the end. */	PyObject *result = NULL;	/* result list */	PyObject *masterdict = NULL;	/* result is masterdict.keys() */ 	/* If NULL arg, return the locals. */	if (arg == NULL) {		PyObject *locals = PyEval_GetLocals();		if (locals == NULL)			goto error;		result = PyMapping_Keys(locals);		if (result == NULL)			goto error;	} 	/* Elif this is some form of module, we only want its dict. */	else if (PyModule_Check(arg)) {		masterdict = PyObject_GetAttrString(arg, "__dict__");		if (masterdict == NULL)			goto error;		if (!PyDict_Check(masterdict)) {			PyErr_SetString(PyExc_TypeError,					"module.__dict__ is not a dictionary");			goto error;		}	} 	/* Elif some form of type or class, grab its dict and its bases.	   We deliberately don't suck up its __class__, as methods belonging	   to the metaclass would probably be more confusing than helpful. */	else if (PyType_Check(arg) || PyClass_Check(arg)) {		masterdict = PyDict_New();		if (masterdict == NULL)			goto error;		if (merge_class_dict(masterdict, arg) < 0)			goto error;	} 	/* Else look at its dict, and the attrs reachable from its class. */	else {		PyObject *itsclass;		/* Create a dict to start with.  CAUTION:  Not everything		   responding to __dict__ returns a dict! */		masterdict = PyObject_GetAttrString(arg, "__dict__");		if (masterdict == NULL) {			PyErr_Clear();			masterdict = PyDict_New();		}		else if (!PyDict_Check(masterdict)) {			Py_DECREF(masterdict);			masterdict = PyDict_New();		}		else {			/* The object may have returned a reference to its			   dict, so copy it to avoid mutating it. */			PyObject *temp = PyDict_Copy(masterdict);			Py_DECREF(masterdict);			masterdict = temp;		}		if (masterdict == NULL)			goto error; 		/* Merge in __members__ and __methods__ (if any).		   XXX Would like this to go away someday; for now, it's		   XXX needed to get at im_self etc of method objects. */		if (merge_list_attr(masterdict, arg, "__members__") < 0)			goto error;		if (merge_list_attr(masterdict, arg, "__methods__") < 0)			goto error; 		/* Merge in attrs reachable from its class.		   CAUTION:  Not all objects have a __class__ attr. */		itsclass = PyObject_GetAttrString(arg, "__class__");		if (itsclass == NULL)			PyErr_Clear();		else {			int status = merge_class_dict(masterdict, itsclass);			Py_DECREF(itsclass);			if (status < 0)				goto error;		}	} 	assert((result == NULL) ^ (masterdict == NULL));	if (masterdict != NULL) {		/* The result comes from its keys. */		assert(result == NULL);		result = PyDict_Keys(masterdict);		if (result == NULL)			goto error;	} 	assert(result);	if (!PyList_Check(result)) {		PyErr_Format(PyExc_TypeError,			"Expected keys() to be a list, not '%.200s'",			result->ob_type->tp_name);		goto error;	}	if (PyList_Sort(result) != 0)		goto error;	else		goto normal_return;   error:	Py_XDECREF(result);	result = NULL;	/* fall through */  normal_return:  	Py_XDECREF(masterdict);	return result;} /*NoObject is usable as a non-NULL undefined value, used by the macro None.There is (and should be!) no way to create other objects of this type,so there is exactly one (which is indestructible, by the way).(XXX This type and the type of NotImplemented below should be unified.)*/ /* ARGSUSED */static PyObject *none_repr(PyObject *op){	return PyString_FromString("None");} /* ARGUSED */static voidnone_dealloc(PyObject* ignore){	/* This should never get called, but we also don't want to SEGV if	 * we accidently decref None out of existance.	 */	Py_FatalError("deallocating None");}  static PyTypeObject PyNone_Type = {	PyObject_HEAD_INIT(&PyType_Type)	0,	"NoneType",	0,	0,	none_dealloc,	/*tp_dealloc*/ /*never called*/	0,		/*tp_print*/	0,		/*tp_getattr*/	0,		/*tp_setattr*/	0,		/*tp_compare*/	none_repr,	/*tp_repr*/	0,		/*tp_as_number*/	0,		/*tp_as_sequence*/	0,		/*tp_as_mapping*/	0,		/*tp_hash */}; PyObject _Py_NoneStruct = {	PyObject_HEAD_INIT(&PyNone_Type)}; /* NotImplemented is an object that can be used to signal that an   operation is not implemented for the given type combination. */ static PyObject *NotImplemented_repr(PyObject *op){	return PyString_FromString("NotImplemented");} static PyTypeObject PyNotImplemented_Type = {	PyObject_HEAD_INIT(&PyType_Type)	0,	"NotImplementedType",	0,	0,	none_dealloc,	/*tp_dealloc*/ /*never called*/	0,		/*tp_print*/	0,		/*tp_getattr*/	0,		/*tp_setattr*/	0,		/*tp_compare*/	NotImplemented_repr, /*tp_repr*/	0,		/*tp_as_number*/	0,		/*tp_as_sequence*/	0,		/*tp_as_mapping*/	0,		/*tp_hash */}; PyObject _Py_NotImplementedStruct = {	PyObject_HEAD_INIT(&PyNotImplemented_Type)}; void_Py_ReadyTypes(void){	if (PyType_Ready(&PyType_Type) < 0)		Py_FatalError("Can't initialize 'type'"); 	if (PyType_Ready(&_PyWeakref_RefType) < 0)		Py_FatalError("Can't initialize 'weakref'"); 	if (PyType_Ready(&PyBool_Type) < 0)		Py_FatalError("Can't initialize 'bool'"); 	if (PyType_Ready(&PyString_Type) < 0)		Py_FatalError("Can't initialize 'str'"); 	if (PyType_Ready(&PyList_Type) < 0)		Py_FatalError("Can't initialize 'list'"); 	if (PyType_Ready(&PyNone_Type) < 0)		Py_FatalError("Can't initialize type(None)"); 	if (PyType_Ready(&PyNotImplemented_Type) < 0)		Py_FatalError("Can't initialize type(NotImplemented)");}  #ifdef Py_TRACE_REFS void_Py_NewReference(PyObject *op){	_Py_INC_REFTOTAL;	op->ob_refcnt = 1;	_Py_AddToAllObjects(op, 1);	_Py_INC_TPALLOCS(op);} void_Py_ForgetReference(register PyObject *op){#ifdef SLOW_UNREF_CHECK        register PyObject *p;#endif	if (op->ob_refcnt < 0)		Py_FatalError("UNREF negative refcnt");	if (op == &refchain ||	    op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)		Py_FatalError("UNREF invalid object");#ifdef SLOW_UNREF_CHECK	for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {		if (p == op)			break;	}	if (p == &refchain) /* Not found */		Py_FatalError("UNREF unknown object");#endif	op->_ob_next->_ob_prev = op->_ob_prev;	op->_ob_prev->_ob_next = op->_ob_next;	op->_ob_next = op->_ob_prev = NULL;	_Py_INC_TPFREES(op);} void_Py_Dealloc(PyObject *op){	destructor dealloc = op->ob_type->tp_dealloc;	_Py_ForgetReference(op);	(*dealloc)(op);} /* Print all live objects.  Because PyObject_Print is called, the * interpreter must be in a healthy state. */void_Py_PrintReferences(FILE *fp){	PyObject *op;	fprintf(fp, "Remaining objects:\n");	for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {		fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);		if (PyObject_Print(op, fp, 0) != 0)			PyErr_Clear();		putc('\n', fp);	}} /* Print the addresses of all live objects.  Unlike _Py_PrintReferences, this * doesn't make any calls to the Python C API, so is always safe to call. */void_Py_PrintReferenceAddresses(FILE *fp){	PyObject *op;	fprintf(fp, "Remaining object addresses:\n");	for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)		fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,			op->ob_refcnt, op->ob_type->tp_name);} PyObject *_Py_GetObjects(PyObject *self, PyObject *args){	int i, n;	PyObject *t = NULL;	PyObject *res, *op; 	if (!PyArg_ParseTuple(args, "i|O", &n, &t))		return NULL;	op = refchain._ob_next;	res = PyList_New(0);	if (res == NULL)		return NULL;	for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {		while (op == self || op == args || op == res || op == t ||		       (t != NULL && op->ob_type != (PyTypeObject *) t)) {			op = op->_ob_next;			if (op == &refchain)				return res;		}		if (PyList_Append(res, op) < 0) {			Py_DECREF(res);			return NULL;		}		op = op->_ob_next;	}	return res;} #endif  /* Hack to force loading of cobject.o */PyTypeObject *_Py_cobject_hack = &PyCObject_Type;  /* Hack to force loading of abstract.o */Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;  /* Python's malloc wrappers (see pymem.h) */

Lines 1501–2000 of 2139. Previous lines