[Python-checkins] python/dist/src/Modules cPickle.c,2.131,2.132

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Thu, 13 Feb 2003 10:24:19 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv9175/Modules

Modified Files:
	cPickle.c 
Log Message:
Taught cPickle how to read pickles containing NEWOBJ.  This won't get
exercised by the test suite before cPickle knows how to create NEWOBJ
too.  For now, it was just tried once by hand (via loading a NEWOBJ
pickle created by pickle.py).


Index: cPickle.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/cPickle.c,v
retrieving revision 2.131
retrieving revision 2.132
diff -C2 -d -r2.131 -r2.132
*** cPickle.c	13 Feb 2003 15:44:41 -0000	2.131
--- cPickle.c	13 Feb 2003 18:24:14 -0000	2.132
***************
*** 3699,3702 ****
--- 3699,3749 ----
  }
  
+ static int
+ load_newobj(Unpicklerobject *self)
+ {
+ 	PyObject *args = NULL;
+ 	PyObject *clsraw = NULL;
+ 	PyTypeObject *cls;	/* clsraw cast to its true type */
+ 	PyObject *obj;
+ 
+ 	/* Stack is ... cls argtuple, and we want to call
+ 	 * cls.__new__(cls, *argtuple).
+ 	 */
+ 	PDATA_POP(self->stack, args);
+ 	if (args == NULL) goto Fail;
+ 	if (! PyTuple_Check(args)) {
+ 		PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
+ 						 "tuple.");
+ 		goto Fail;
+ 	}
+ 
+ 	PDATA_POP(self->stack, clsraw);
+ 	cls = (PyTypeObject *)clsraw;
+ 	if (cls == NULL) goto Fail;
+ 	if (! PyType_Check(cls)) {
+ 		PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
+ 					 	 "isn't a type object");
+ 		goto Fail;
+ 	}
+ 	if (cls->tp_new == NULL) {
+ 		PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
+ 						 "has NULL tp_new");
+ 		goto Fail;
+ 	}
+ 
+ 	/* Call __new__. */
+ 	obj = cls->tp_new(cls, args, NULL);
+ 	if (obj == NULL) goto Fail;
+ 
+  	Py_DECREF(args);
+  	Py_DECREF(clsraw);
+ 	PDATA_PUSH(self->stack, obj, -1);
+  	return 0;
+ 
+  Fail:
+  	Py_XDECREF(args);
+  	Py_XDECREF(clsraw);
+  	return -1;
+ }
  
  static int
***************
*** 4462,4465 ****
--- 4509,4517 ----
  			continue;
  
+ 		case NEWOBJ:
+ 			if (load_newobj(self) < 0)
+ 				break;
+ 			continue;
+ 
  		case GLOBAL:
  			if (load_global(self) < 0)
***************
*** 4639,4647 ****
  	if (self->readline_func(self, &s) < 0) return -1;
  	if (self->readline_func(self, &s) < 0) return -1;
! 	PDATA_APPEND(self->stack, Py_None,-1);
  	return 0;
  }
  
  static int
  noload_global(Unpicklerobject *self)
  {
--- 4691,4716 ----
  	if (self->readline_func(self, &s) < 0) return -1;
  	if (self->readline_func(self, &s) < 0) return -1;
! 	PDATA_APPEND(self->stack, Py_None, -1);
  	return 0;
  }
  
  static int
+ noload_newobj(Unpicklerobject *self)
+ {
+ 	PyObject *obj;
+ 
+ 	PDATA_POP(self->stack, obj);	/* pop argtuple */
+ 	if (obj == NULL) return -1;
+ 	Py_DECREF(obj);
+ 
+ 	PDATA_POP(self->stack, obj);	/* pop cls */
+ 	if (obj == NULL) return -1;
+ 	Py_DECREF(obj);
+ 
+ 	PDATA_APPEND(self->stack, Py_None, -1);
+  	return 0;
+ }
+ 
+ static int
  noload_global(Unpicklerobject *self)
  {
***************
*** 4827,4830 ****
--- 4896,4904 ----
  		case INST:
  			if (noload_inst(self) < 0)
+ 				break;
+ 			continue;
+ 
+ 		case NEWOBJ:
+ 			if (noload_newobj(self) < 0)
  				break;
  			continue;