[Python-Dev] r84430 - in python/branches/py3k: Include/unicodeobject.h Objects/unicodeobject.c

Georg Brandl g.brandl at gmx.net
Thu Sep 2 10:51:15 CEST 2010


Hi Victor,

1. This function and PyUnicode_strcat are missing documentation.

2. Are you sure they need to be public APIs?  What are they going to
   be used for?  (I'm not sure myself, but I think we usually have a
   short notice here when new C APIs are added.)

Georg

Am 02.09.2010 01:43, schrieb victor.stinner:
> Author: victor.stinner
> Date: Thu Sep  2 01:43:53 2010
> New Revision: 84430
> 
> Log:
> Create PyUnicode_strdup() function
> 
> Modified:
>    python/branches/py3k/Include/unicodeobject.h
>    python/branches/py3k/Objects/unicodeobject.c
> 
> Modified: python/branches/py3k/Include/unicodeobject.h
> ==============================================================================
> --- python/branches/py3k/Include/unicodeobject.h	(original)
> +++ python/branches/py3k/Include/unicodeobject.h	Thu Sep  2 01:43:53 2010
> @@ -220,6 +220,7 @@
>  # define _PyUnicode_AsDefaultEncodedString _PyUnicodeUCS2_AsDefaultEncodedString
>  # define _PyUnicode_Fini _PyUnicodeUCS2_Fini
>  # define _PyUnicode_Init _PyUnicodeUCS2_Init
> +# define PyUnicode_strdup PyUnicodeUCS2_strdup
>  
>  #else
>  
> @@ -302,7 +303,7 @@
>  # define _PyUnicode_AsDefaultEncodedString _PyUnicodeUCS4_AsDefaultEncodedString
>  # define _PyUnicode_Fini _PyUnicodeUCS4_Fini
>  # define _PyUnicode_Init _PyUnicodeUCS4_Init
> -
> +# define PyUnicode_strdup PyUnicodeUCS4_strdup
>  
>  #endif
>  
> @@ -1602,6 +1603,14 @@
>      Py_UNICODE c
>      );
>  
> +/* Create a copy of a unicode string ending with a nul character. Return NULL
> +   and raise a MemoryError exception on memory allocation failure, otherwise
> +   return a new allocated buffer (use PyMem_Free() to free the buffer). */
> +
> +PyAPI_FUNC(Py_UNICODE*) PyUnicode_strdup(
> +    PyObject *unicode
> +    );
> +
>  #ifdef __cplusplus
>  }
>  #endif
> 
> Modified: python/branches/py3k/Objects/unicodeobject.c
> ==============================================================================
> --- python/branches/py3k/Objects/unicodeobject.c	(original)
> +++ python/branches/py3k/Objects/unicodeobject.c	Thu Sep  2 01:43:53 2010
> @@ -10014,6 +10014,28 @@
>      return NULL;
>  }
>  
> +Py_UNICODE*
> +PyUnicode_strdup(PyObject *object)
> +{
> +    PyUnicodeObject *unicode = (PyUnicodeObject *)object;
> +    Py_UNICODE *copy;
> +    Py_ssize_t size;
> +
> +    /* Ensure we won't overflow the size. */
> +    if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
> +        PyErr_NoMemory();
> +        return NULL;
> +    }
> +    size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */
> +    size *= sizeof(Py_UNICODE);
> +    copy = PyMem_Malloc(size);
> +    if (copy == NULL) {
> +        PyErr_NoMemory();
> +        return NULL;
> +    }
> +    memcpy(copy, PyUnicode_AS_UNICODE(unicode), size);
> +    return copy;
> +}
>  
>  #ifdef __cplusplus
>  }


-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.



More information about the Python-Dev mailing list