cinap_lenrekadd hg and python4bb9e2215 years agoHistory
2580 lines · 60.9 KB · C
RawDownload
		return NULL;	}	if (dict == NULL) {		dict = PyDict_New();		if (dict == NULL)			return NULL;	}	else {		if (!PyDict_Check(dict)) {			PyErr_BadInternalCall();			return NULL;		}		Py_INCREF(dict);	}	inst = PyObject_GC_New(PyInstanceObject, &PyInstance_Type);	if (inst == NULL) {		Py_DECREF(dict);		return NULL;	}	inst->in_weakreflist = NULL;	Py_INCREF(klass);	inst->in_class = (PyClassObject *)klass;	inst->in_dict = dict;	_PyObject_GC_TRACK(inst);	return (PyObject *)inst;} PyObject *PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw){	register PyInstanceObject *inst;	PyObject *init;	static PyObject *initstr; 	if (initstr == NULL) {		initstr = PyString_InternFromString("__init__");		if (initstr == NULL)			return NULL;	}	inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL);	if (inst == NULL)		return NULL;	init = instance_getattr2(inst, initstr);	if (init == NULL) {		if (PyErr_Occurred()) {			Py_DECREF(inst);			return NULL;		}		if ((arg != NULL && (!PyTuple_Check(arg) ||				     PyTuple_Size(arg) != 0))		    || (kw != NULL && (!PyDict_Check(kw) ||				      PyDict_Size(kw) != 0))) {			PyErr_SetString(PyExc_TypeError,				   "this constructor takes no arguments");			Py_DECREF(inst);			inst = NULL;		}	}	else {		PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw);		Py_DECREF(init);		if (res == NULL) {			Py_DECREF(inst);			inst = NULL;		}		else {			if (res != Py_None) {				PyErr_SetString(PyExc_TypeError,					   "__init__() should return None");				Py_DECREF(inst);				inst = NULL;			}			Py_DECREF(res);		}	}	return (PyObject *)inst;} /* Instance methods */ PyDoc_STRVAR(instance_doc,"instance(class[, dict])\n\\n\Create an instance without calling its __init__() method.\n\The class must be a classic class.\n\If present, dict must be a dictionary or None."); static PyObject *instance_new(PyTypeObject* type, PyObject* args, PyObject *kw){	PyObject *klass;	PyObject *dict = Py_None; 	if (!PyArg_ParseTuple(args, "O!|O:instance",			      &PyClass_Type, &klass, &dict))		return NULL; 	if (dict == Py_None)		dict = NULL;	else if (!PyDict_Check(dict)) {		PyErr_SetString(PyExc_TypeError,		      "instance() second arg must be dictionary or None");		return NULL;	}	return PyInstance_NewRaw(klass, dict);}  static voidinstance_dealloc(register PyInstanceObject *inst){	PyObject *error_type, *error_value, *error_traceback;	PyObject *del;	static PyObject *delstr; 	_PyObject_GC_UNTRACK(inst);	if (inst->in_weakreflist != NULL)		PyObject_ClearWeakRefs((PyObject *) inst); 	/* Temporarily resurrect the object. */	assert(inst->ob_type == &PyInstance_Type);	assert(inst->ob_refcnt == 0);	inst->ob_refcnt = 1; 	/* Save the current exception, if any. */	PyErr_Fetch(&error_type, &error_value, &error_traceback);	/* Execute __del__ method, if any. */	if (delstr == NULL) {		delstr = PyString_InternFromString("__del__");		if (delstr == NULL)			PyErr_WriteUnraisable((PyObject*)inst);	}	if (delstr && (del = instance_getattr2(inst, delstr)) != NULL) {		PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);		if (res == NULL)			PyErr_WriteUnraisable(del);		else			Py_DECREF(res);		Py_DECREF(del);	}	/* Restore the saved exception. */	PyErr_Restore(error_type, error_value, error_traceback); 	/* Undo the temporary resurrection; can't use DECREF here, it would	 * cause a recursive call.	 */	assert(inst->ob_refcnt > 0);	if (--inst->ob_refcnt == 0) {		Py_DECREF(inst->in_class);		Py_XDECREF(inst->in_dict);		PyObject_GC_Del(inst);	}	else {		Py_ssize_t refcnt = inst->ob_refcnt;		/* __del__ resurrected it!  Make it look like the original		 * Py_DECREF never happened.		 */		_Py_NewReference((PyObject *)inst);		inst->ob_refcnt = refcnt;		_PyObject_GC_TRACK(inst);		/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so		 * we need to undo that. */		_Py_DEC_REFTOTAL;		/* If Py_TRACE_REFS, _Py_NewReference re-added self to the		 * object chain, so no more to do there.		 * If COUNT_ALLOCS, the original decref bumped tp_frees, and		 * _Py_NewReference bumped tp_allocs: both of those need to be		 * undone.		 */#ifdef COUNT_ALLOCS		--inst->ob_type->tp_frees;		--inst->ob_type->tp_allocs;#endif	}} static PyObject *instance_getattr1(register PyInstanceObject *inst, PyObject *name){	register PyObject *v;	register char *sname = PyString_AsString(name);	if (sname[0] == '_' && sname[1] == '_') {		if (strcmp(sname, "__dict__") == 0) {			if (PyEval_GetRestricted()) {				PyErr_SetString(PyExc_RuntimeError,			"instance.__dict__ not accessible in restricted mode");				return NULL;			}			Py_INCREF(inst->in_dict);			return inst->in_dict;		}		if (strcmp(sname, "__class__") == 0) {			Py_INCREF(inst->in_class);			return (PyObject *)inst->in_class;		}	}	v = instance_getattr2(inst, name);	if (v == NULL && !PyErr_Occurred()) {		PyErr_Format(PyExc_AttributeError,			     "%.50s instance has no attribute '%.400s'",			     PyString_AS_STRING(inst->in_class->cl_name), sname);	}	return v;} static PyObject *instance_getattr2(register PyInstanceObject *inst, PyObject *name){	register PyObject *v;	PyClassObject *klass;	descrgetfunc f; 	v = PyDict_GetItem(inst->in_dict, name);	if (v != NULL) {		Py_INCREF(v);		return v;	}	v = class_lookup(inst->in_class, name, &klass);	if (v != NULL) {		Py_INCREF(v);		f = TP_DESCR_GET(v->ob_type);		if (f != NULL) {			PyObject *w = f(v, (PyObject *)inst,					(PyObject *)(inst->in_class));			Py_DECREF(v);			v = w;		}	}	return v;} static PyObject *instance_getattr(register PyInstanceObject *inst, PyObject *name){	register PyObject *func, *res;	res = instance_getattr1(inst, name);	if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {		PyObject *args;		if (!PyErr_ExceptionMatches(PyExc_AttributeError))			return NULL;		PyErr_Clear();		args = PyTuple_Pack(2, inst, name);		if (args == NULL)			return NULL;		res = PyEval_CallObject(func, args);		Py_DECREF(args);	}	return res;} /* See classobject.h comments:  this only does dict lookups, and is always * safe to call. */PyObject *_PyInstance_Lookup(PyObject *pinst, PyObject *name){	PyObject *v;	PyClassObject *klass;	PyInstanceObject *inst;	/* pinst cast to the right type */ 	assert(PyInstance_Check(pinst));	inst = (PyInstanceObject *)pinst; 	assert(PyString_Check(name));  	v = PyDict_GetItem(inst->in_dict, name);	if (v == NULL)		v = class_lookup(inst->in_class, name, &klass);	return v;} static intinstance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v){	if (v == NULL) {		int rv = PyDict_DelItem(inst->in_dict, name);		if (rv < 0)			PyErr_Format(PyExc_AttributeError,				     "%.50s instance has no attribute '%.400s'",				     PyString_AS_STRING(inst->in_class->cl_name),				     PyString_AS_STRING(name));		return rv;	}	else		return PyDict_SetItem(inst->in_dict, name, v);} static intinstance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v){	PyObject *func, *args, *res, *tmp;	char *sname = PyString_AsString(name);	if (sname[0] == '_' && sname[1] == '_') {		Py_ssize_t n = PyString_Size(name);		if (sname[n-1] == '_' && sname[n-2] == '_') {			if (strcmp(sname, "__dict__") == 0) {				if (PyEval_GetRestricted()) {					PyErr_SetString(PyExc_RuntimeError,				 "__dict__ not accessible in restricted mode");					return -1;				}				if (v == NULL || !PyDict_Check(v)) {				    PyErr_SetString(PyExc_TypeError,				       "__dict__ must be set to a dictionary");				    return -1;				}				tmp = inst->in_dict;				Py_INCREF(v);				inst->in_dict = v;				Py_DECREF(tmp);				return 0;			}			if (strcmp(sname, "__class__") == 0) {				if (PyEval_GetRestricted()) {					PyErr_SetString(PyExc_RuntimeError,				"__class__ not accessible in restricted mode");					return -1;				}				if (v == NULL || !PyClass_Check(v)) {					PyErr_SetString(PyExc_TypeError,					   "__class__ must be set to a class");					return -1;				}				tmp = (PyObject *)(inst->in_class);				Py_INCREF(v);				inst->in_class = (PyClassObject *)v;				Py_DECREF(tmp);				return 0;			}		}	}	if (v == NULL)		func = inst->in_class->cl_delattr;	else		func = inst->in_class->cl_setattr;	if (func == NULL)		return instance_setattr1(inst, name, v);	if (v == NULL)		args = PyTuple_Pack(2, inst, name);	else		args = PyTuple_Pack(3, inst, name, v);	if (args == NULL)		return -1;	res = PyEval_CallObject(func, args);	Py_DECREF(args);	if (res == NULL)		return -1;	Py_DECREF(res);	return 0;} static PyObject *instance_repr(PyInstanceObject *inst){	PyObject *func;	PyObject *res;	static PyObject *reprstr; 	if (reprstr == NULL) {		reprstr = PyString_InternFromString("__repr__");		if (reprstr == NULL)			return NULL;	}	func = instance_getattr(inst, reprstr);	if (func == NULL) {		PyObject *classname, *mod;		char *cname;		if (!PyErr_ExceptionMatches(PyExc_AttributeError))			return NULL;		PyErr_Clear();		classname = inst->in_class->cl_name;		mod = PyDict_GetItemString(inst->in_class->cl_dict,					   "__module__");		if (classname != NULL && PyString_Check(classname))			cname = PyString_AsString(classname);		else			cname = "?";		if (mod == NULL || !PyString_Check(mod))			return PyString_FromFormat("<?.%s instance at %p>",						   cname, inst);		else			return PyString_FromFormat("<%s.%s instance at %p>",						   PyString_AsString(mod),						   cname, inst);	}	res = PyEval_CallObject(func, (PyObject *)NULL);	Py_DECREF(func);	return res;} static PyObject *instance_str(PyInstanceObject *inst){	PyObject *func;	PyObject *res;	static PyObject *strstr; 	if (strstr == NULL) {		strstr = PyString_InternFromString("__str__");		if (strstr == NULL)			return NULL;	}	func = instance_getattr(inst, strstr);	if (func == NULL) {		if (!PyErr_ExceptionMatches(PyExc_AttributeError))			return NULL;		PyErr_Clear();		return instance_repr(inst);	}	res = PyEval_CallObject(func, (PyObject *)NULL);	Py_DECREF(func);	return res;} static longinstance_hash(PyInstanceObject *inst){	PyObject *func;	PyObject *res;	long outcome;	static PyObject *hashstr, *eqstr, *cmpstr; 	if (hashstr == NULL) {		hashstr = PyString_InternFromString("__hash__");		if (hashstr == NULL)			return -1;	}	func = instance_getattr(inst, hashstr);	if (func == NULL) {		if (!PyErr_ExceptionMatches(PyExc_AttributeError))			return -1;		PyErr_Clear();		/* If there is no __eq__ and no __cmp__ method, we hash on the		   address.  If an __eq__ or __cmp__ method exists, there must		   be a __hash__. */		if (eqstr == NULL) {			eqstr = PyString_InternFromString("__eq__");			if (eqstr == NULL)				return -1;		}		func = instance_getattr(inst, eqstr);		if (func == NULL) {			if (!PyErr_ExceptionMatches(PyExc_AttributeError))				return -1;			PyErr_Clear();			if (cmpstr == NULL) {				cmpstr = PyString_InternFromString("__cmp__");				if (cmpstr == NULL)					return -1;			}			func = instance_getattr(inst, cmpstr);			if (func == NULL) {				if (!PyErr_ExceptionMatches(					PyExc_AttributeError))					return -1;				PyErr_Clear();				return _Py_HashPointer(inst);			}		}		Py_XDECREF(func);		PyErr_SetString(PyExc_TypeError, "unhashable instance");		return -1;	}	res = PyEval_CallObject(func, (PyObject *)NULL);	Py_DECREF(func);	if (res == NULL)		return -1;	if (PyInt_Check(res) || PyLong_Check(res))		/* This already converts a -1 result to -2. */		outcome = res->ob_type->tp_hash(res);	else {		PyErr_SetString(PyExc_TypeError,				"__hash__() should return an int");		outcome = -1;	}	Py_DECREF(res);	return outcome;} static intinstance_traverse(PyInstanceObject *o, visitproc visit, void *arg){	Py_VISIT(o->in_class);	Py_VISIT(o->in_dict);	return 0;} static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr;static PyObject *iterstr, *nextstr; static Py_ssize_tinstance_length(PyInstanceObject *inst){	PyObject *func;	PyObject *res;	Py_ssize_t outcome; 	if (lenstr == NULL) {		lenstr = PyString_InternFromString("__len__");		if (lenstr == NULL)

Lines 501–1000 of 2580. Previous lines