[pygobject/py3k: 6/20] Return byte array from read_chars iff encoding is NULL, otherwise return a unicode instance in pytho



commit ec3473468d688922c38f2b8e0f0168ab1c774dd7
Author: John Ehresman <jpe wingware com>
Date:   Tue Apr 13 11:46:28 2010 -0400

    Return byte array from read_chars iff encoding is NULL, otherwise return a unicode instance in python 3

 glib/pygiochannel.c |   28 ++++++++++++++++++++++------
 1 files changed, 22 insertions(+), 6 deletions(-)
---
diff --git a/glib/pygiochannel.c b/glib/pygiochannel.c
index fbaa9bd..15510e5 100644
--- a/glib/pygiochannel.c
+++ b/glib/pygiochannel.c
@@ -204,16 +204,16 @@ py_io_channel_read_chars(PyGIOChannel* self, PyObject *args, PyObject *kwargs)
         }
 	
 	if ( ret_obj == NULL ) {
-	    ret_obj = _PyUnicode_FromStringAndSize((char *)NULL, buf_size);
+	    ret_obj = _PyByteArray_FromStringAndSize((char *)NULL, buf_size);
 	    if (ret_obj == NULL)
 		goto failure;
 	}
-	else if (buf_size + total_read > _PyUnicode_GET_SIZE(ret_obj)) {
-	    if (_PyUnicode_Resize(&ret_obj, buf_size + total_read) == -1)
+	else if (buf_size + total_read > _PyByteArray_Size(ret_obj)) {
+	    if (_PyByteArray_Resize(&ret_obj, buf_size + total_read) == -1)
 		goto failure;
 	}
        
-        buf = _PyUnicode_AS_STRING(ret_obj) + total_read;
+        buf = _PyByteArray_AsString(ret_obj) + total_read;
 
         pyglib_unblock_threads();
         status = g_io_channel_read_chars(self->channel, buf, buf_size, 
@@ -225,10 +225,26 @@ py_io_channel_read_chars(PyGIOChannel* self, PyObject *args, PyObject *kwargs)
 	total_read += single_read;
     }
 	
-    if ( total_read != _PyUnicode_GET_SIZE(ret_obj) ) {
-	if (_PyUnicode_Resize(&ret_obj, total_read) == -1)
+    if ( total_read != _PyByteArray_Size(ret_obj) ) {
+	if (_PyByteArray_Resize(&ret_obj, total_read) == -1)
 	    goto failure;
     }
+    
+    if (g_io_channel_get_encoding(self->channel) == NULL)
+	return ret_obj;
+	
+#if PY_VERSION_HEX >= 0x03000000
+    {
+	PyObject *unicode_obj;
+	
+	unicode_obj = PyUnicode_FromString(PyByteArray_AS_STRING(ret_obj));
+	if (unicode_obj == NULL)
+	    goto failure;
+	Py_DECREF(ret_obj);
+	ret_obj = unicode_obj;
+    }
+#endif
+
     return ret_obj;
 
   failure:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]