[Python-checkins] CVS: python/dist/src/Python marshal.c,1.44,1.45

Guido van Rossum guido@cnri.reston.va.us
Fri, 10 Mar 2000 18:03:04 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Python
In directory eric:/home/guido/hp/mal/py-patched/Python

Modified Files:
	marshal.c 
Log Message:
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').


Index: marshal.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Python/marshal.c,v
retrieving revision 1.44
retrieving revision 1.45
diff -C2 -r1.44 -r1.45
*** marshal.c	1998/10/08 01:45:47	1.44
--- marshal.c	2000/03/10 23:03:02	1.45
***************
*** 53,56 ****
--- 53,57 ----
  #define TYPE_DICT	'{'
  #define TYPE_CODE	'c'
+ #define TYPE_UNICODE	'u'
  #define TYPE_UNKNOWN	'?'
  
***************
*** 214,221 ****
  	else if (PyString_Check(v)) {
  		w_byte(TYPE_STRING, p);
! 		n = PyString_Size(v);
  		w_long((long)n, p);
! 		w_string(PyString_AsString(v), n, p);
  	}
  	else if (PyTuple_Check(v)) {
  		w_byte(TYPE_TUPLE, p);
--- 215,235 ----
  	else if (PyString_Check(v)) {
  		w_byte(TYPE_STRING, p);
! 		n = PyString_GET_SIZE(v);
  		w_long((long)n, p);
! 		w_string(PyString_AS_STRING(v), n, p);
  	}
+ 	else if (PyUnicode_Check(v)) {
+ 	        PyObject *utf8;
+ 		utf8 = PyUnicode_AsUTF8String(v);
+ 		if (utf8 == NULL) {
+ 		    p->error = 1;
+ 		    return;
+ 		}
+ 		w_byte(TYPE_UNICODE, p);
+ 		n = PyString_GET_SIZE(utf8);
+ 		w_long((long)n, p);
+ 		w_string(PyString_AS_STRING(utf8), n, p);
+ 		Py_DECREF(utf8);
+ 	}
  	else if (PyTuple_Check(v)) {
  		w_byte(TYPE_TUPLE, p);
***************
*** 228,235 ****
  	else if (PyList_Check(v)) {
  		w_byte(TYPE_LIST, p);
! 		n = PyList_Size(v);
  		w_long((long)n, p);
  		for (i = 0; i < n; i++) {
! 			w_object(PyList_GetItem(v, i), p);
  		}
  	}
--- 242,249 ----
  	else if (PyList_Check(v)) {
  		w_byte(TYPE_LIST, p);
! 		n = PyList_GET_SIZE(v);
  		w_long((long)n, p);
  		for (i = 0; i < n; i++) {
! 			w_object(PyList_GET_ITEM(v, i), p);
  		}
  	}
***************
*** 483,487 ****
  		v = PyString_FromStringAndSize((char *)NULL, n);
  		if (v != NULL) {
! 			if (r_string(PyString_AsString(v), (int)n, p) != n) {
  				Py_DECREF(v);
  				v = NULL;
--- 497,501 ----
  		v = PyString_FromStringAndSize((char *)NULL, n);
  		if (v != NULL) {
! 			if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
  				Py_DECREF(v);
  				v = NULL;
***************
*** 492,495 ****
--- 506,532 ----
  		return v;
  	
+ 	case TYPE_UNICODE:
+ 	    {
+ 		char *buffer;
+ 
+ 		n = r_long(p);
+ 		if (n < 0) {
+ 			PyErr_SetString(PyExc_ValueError, "bad marshal data");
+ 			return NULL;
+ 		}
+ 		buffer = (char *)Py_Malloc(n);
+ 		if (buffer == NULL)
+ 		    return NULL;
+ 		if (r_string(buffer, (int)n, p) != n) {
+ 			free(buffer);
+ 			PyErr_SetString(PyExc_EOFError,
+ 				"EOF read where object expected");
+ 			return NULL;
+ 		}
+ 		v = PyUnicode_DecodeUTF8(buffer, n, NULL);
+ 		free(buffer);
+ 		return v;
+ 	    }
+ 	    
  	case TYPE_TUPLE:
  		n = r_long(p);