[Python-3000-checkins] r57656 - in python/branches/py3k: Include/unicodeobject.h Modules/_sqlite/statement.c Objects/unicodeobject.c Parser/tokenizer.c

guido.van.rossum python-3000-checkins at python.org
Wed Aug 29 15:53:23 CEST 2007


Author: guido.van.rossum
Date: Wed Aug 29 15:53:23 2007
New Revision: 57656

Modified:
   python/branches/py3k/Include/unicodeobject.h
   python/branches/py3k/Modules/_sqlite/statement.c
   python/branches/py3k/Objects/unicodeobject.c
   python/branches/py3k/Parser/tokenizer.c
Log:
Add PyUnicode_AsStringAndSize(), which is like PyUnicode_AsString() but
has an extra (optional) output parameter through which it returns the size.
Use this in a few places where I used PyUnicode_AsString() + strlen(),
and in one new place (which fixes test_pep263).


Modified: python/branches/py3k/Include/unicodeobject.h
==============================================================================
--- python/branches/py3k/Include/unicodeobject.h	(original)
+++ python/branches/py3k/Include/unicodeobject.h	Wed Aug 29 15:53:23 2007
@@ -641,20 +641,25 @@
 PyAPI_FUNC(PyObject *) _PyUnicode_AsDefaultEncodedString(
     PyObject *, const char *);
 
-/* Return a char* holding the default encoded value of the
-   Unicode object. 
+/* Return a char* holding the UTF-8 encoded value of the
+   Unicode object.
+
+   DEPRECATED: use PyUnicode_AsStringAndSize() instead.
 */
 
+PyAPI_FUNC(char *) PyUnicode_AsStringAndSize(PyObject*, Py_ssize_t *);
+
+/* Returns the UTF-8 encoding, and its size.
+
+   If the output argument is NULL, no size is stored.
+ */
+
 PyAPI_FUNC(char *) PyUnicode_AsString(PyObject*);
 
+/* Returns the UTF-8 encoding.
 
-/* Returns the currently active default encoding.
+   This is equivalent to PyUnicode_AsStringAndSize(x, NULL).
 
-   The default encoding is currently implemented as run-time settable
-   process global.  This may change in future versions of the
-   interpreter to become a parameter which is managed on a per-thread
-   basis.
-   
  */
 
 PyAPI_FUNC(const char*) PyUnicode_GetDefaultEncoding(void);

Modified: python/branches/py3k/Modules/_sqlite/statement.c
==============================================================================
--- python/branches/py3k/Modules/_sqlite/statement.c	(original)
+++ python/branches/py3k/Modules/_sqlite/statement.c	Wed Aug 29 15:53:23 2007
@@ -50,12 +50,11 @@
     self->st = NULL;
     self->in_use = 0;
 
-    sql_cstr = PyUnicode_AsString(sql);
+    sql_cstr = PyUnicode_AsStringAndSize(sql, &sql_cstr_len);
     if (sql_cstr == NULL) {
         rc = PYSQLITE_SQL_WRONG_TYPE;
         return rc;
     }
-    sql_cstr_len = strlen(sql_cstr); /* XXX */
 
     self->in_weakreflist = NULL;
     Py_INCREF(sql);
@@ -216,12 +215,11 @@
     Py_ssize_t sql_len;
     sqlite3_stmt* new_st;
 
-    sql_cstr = PyUnicode_AsString(self->sql);
+    sql_cstr = PyUnicode_AsStringAndSize(self->sql, &sql_len);
     if (sql_cstr == NULL) {
         rc = PYSQLITE_SQL_WRONG_TYPE;
         return rc;
     }
-    sql_len = strlen(sql_cstr); /* XXXX */
 
     rc = sqlite3_prepare(self->db,
                          sql_cstr,

Modified: python/branches/py3k/Objects/unicodeobject.c
==============================================================================
--- python/branches/py3k/Objects/unicodeobject.c	(original)
+++ python/branches/py3k/Objects/unicodeobject.c	Wed Aug 29 15:53:23 2007
@@ -1184,16 +1184,25 @@
 }
 
 char*
-PyUnicode_AsString(PyObject *unicode)
+PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize)
 {
+    PyObject *str8;
     if (!PyUnicode_Check(unicode)) {
         PyErr_BadArgument();
         return NULL;
     }
-    unicode = _PyUnicode_AsDefaultEncodedString(unicode, NULL);
-    if (!unicode)
+    str8 = _PyUnicode_AsDefaultEncodedString(unicode, NULL);
+    if (str8 == NULL)
         return NULL;
-    return PyString_AsString(unicode);
+    if (psize != NULL)
+        *psize = PyString_GET_SIZE(str8);
+    return PyString_AS_STRING(str8);
+}
+
+char*
+PyUnicode_AsString(PyObject *unicode)
+{
+    return PyUnicode_AsStringAndSize(unicode, NULL);
 }
 
 Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode)
@@ -8098,6 +8107,7 @@
 
     if (flags & PyBUF_CHARACTER) {
         PyErr_SetString(PyExc_SystemError, "can't use str as char buffer");
+        abort();
         return -1;
     }
     return PyBuffer_FillInfo(view, (void *)self->str,

Modified: python/branches/py3k/Parser/tokenizer.c
==============================================================================
--- python/branches/py3k/Parser/tokenizer.c	(original)
+++ python/branches/py3k/Parser/tokenizer.c	Wed Aug 29 15:53:23 2007
@@ -383,7 +383,8 @@
 			goto error;
 		allocated = 1;
 	}
-        if (PyObject_AsCharBuffer(bufobj, &buf, &buflen) < 0) {
+	buf = PyUnicode_AsStringAndSize(bufobj, &buflen);
+	if (buf == NULL) {
 		goto error;
 	}
 	if (buflen > size) {


More information about the Python-3000-checkins mailing list