static char *converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize){ assert(expected != NULL); assert(arg != NULL); PyOS_snprintf(msgbuf, bufsize, "must be %.50s, not %.50s", expected, arg == Py_None ? "None" : arg->ob_type->tp_name); return msgbuf;} #define CONV_UNICODE "(unicode conversion error)" /* explicitly check for float arguments when integers are expected. For now * signal a warning. Returns true if an exception was raised. */static intfloat_argument_error(PyObject *arg){ if (PyFloat_Check(arg) && PyErr_Warn(PyExc_DeprecationWarning, "integer argument expected, got float" )) return 1; else return 0;} /* Convert a non-tuple argument. Return NULL if conversion went OK, or a string with a message describing the failure. The message is formatted as "must be <desired type>, not <actual type>". When failing, an exception may or may not have been raised. Don't call if a tuple is expected. When you add new format codes, please don't forget poor skipitem() below.*/ static char *convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, char *msgbuf, size_t bufsize, PyObject **freelist){ /* For # codes */#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\ if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \ else q=va_arg(*p_va, int*);#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q) const char *format = *p_format; char c = *format++;#ifdef Py_USING_UNICODE PyObject *uarg;#endif switch (c) { case 'b': { /* unsigned byte -- very short int */ char *p = va_arg(*p_va, char *); long ival; if (float_argument_error(arg)) return converterr("integer<b>", arg, msgbuf, bufsize); ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<b>", arg, msgbuf, bufsize); else if (ival < 0) { PyErr_SetString(PyExc_OverflowError, "unsigned byte integer is less than minimum"); return converterr("integer<b>", arg, msgbuf, bufsize); } else if (ival > UCHAR_MAX) { PyErr_SetString(PyExc_OverflowError, "unsigned byte integer is greater than maximum"); return converterr("integer<b>", arg, msgbuf, bufsize); } else *p = (unsigned char) ival; break; } case 'B': {/* byte sized bitfield - both signed and unsigned values allowed */ char *p = va_arg(*p_va, char *); long ival; if (float_argument_error(arg)) return converterr("integer<B>", arg, msgbuf, bufsize); ival = PyInt_AsUnsignedLongMask(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<B>", arg, msgbuf, bufsize); else *p = (unsigned char) ival; break; } case 'h': {/* signed short int */ short *p = va_arg(*p_va, short *); long ival; if (float_argument_error(arg)) return converterr("integer<h>", arg, msgbuf, bufsize); ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<h>", arg, msgbuf, bufsize); else if (ival < SHRT_MIN) { PyErr_SetString(PyExc_OverflowError, "signed short integer is less than minimum"); return converterr("integer<h>", arg, msgbuf, bufsize); } else if (ival > SHRT_MAX) { PyErr_SetString(PyExc_OverflowError, "signed short integer is greater than maximum"); return converterr("integer<h>", arg, msgbuf, bufsize); } else *p = (short) ival; break; } case 'H': { /* short int sized bitfield, both signed and unsigned allowed */ unsigned short *p = va_arg(*p_va, unsigned short *); long ival; if (float_argument_error(arg)) return converterr("integer<H>", arg, msgbuf, bufsize); ival = PyInt_AsUnsignedLongMask(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<H>", arg, msgbuf, bufsize); else *p = (unsigned short) ival; break; } case 'i': {/* signed int */ int *p = va_arg(*p_va, int *); long ival; if (float_argument_error(arg)) return converterr("integer<i>", arg, msgbuf, bufsize); ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<i>", arg, msgbuf, bufsize); else if (ival > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "signed integer is greater than maximum"); return converterr("integer<i>", arg, msgbuf, bufsize); } else if (ival < INT_MIN) { PyErr_SetString(PyExc_OverflowError, "signed integer is less than minimum"); return converterr("integer<i>", arg, msgbuf, bufsize); } else *p = ival; break; } case 'I': { /* int sized bitfield, both signed and unsigned allowed */ unsigned int *p = va_arg(*p_va, unsigned int *); unsigned int ival; if (float_argument_error(arg)) return converterr("integer<I>", arg, msgbuf, bufsize); ival = (unsigned int)PyInt_AsUnsignedLongMask(arg); if (ival == (unsigned int)-1 && PyErr_Occurred()) return converterr("integer<I>", arg, msgbuf, bufsize); else *p = ival; break; } case 'n': /* Py_ssize_t */#if SIZEOF_SIZE_T != SIZEOF_LONG { Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); Py_ssize_t ival; if (float_argument_error(arg)) return converterr("integer<n>", arg, msgbuf, bufsize); ival = PyInt_AsSsize_t(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<n>", arg, msgbuf, bufsize); *p = ival; break; }#endif /* Fall through from 'n' to 'l' if Py_ssize_t is int */ case 'l': {/* long int */ long *p = va_arg(*p_va, long *); long ival; if (float_argument_error(arg)) return converterr("integer<l>", arg, msgbuf, bufsize); ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<l>", arg, msgbuf, bufsize); else *p = ival; break; } case 'k': { /* long sized bitfield */ unsigned long *p = va_arg(*p_va, unsigned long *); unsigned long ival; if (PyInt_Check(arg)) ival = PyInt_AsUnsignedLongMask(arg); else if (PyLong_Check(arg)) ival = PyLong_AsUnsignedLongMask(arg); else return converterr("integer<k>", arg, msgbuf, bufsize); *p = ival; break; } #ifdef HAVE_LONG_LONG case 'L': {/* PY_LONG_LONG */ PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * ); PY_LONG_LONG ival = PyLong_AsLongLong( arg ); if( ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) { return converterr("long<L>", arg, msgbuf, bufsize); } else { *p = ival; } break; } case 'K': { /* long long sized bitfield */ unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *); unsigned PY_LONG_LONG ival; if (PyInt_Check(arg)) ival = PyInt_AsUnsignedLongMask(arg); else if (PyLong_Check(arg)) ival = PyLong_AsUnsignedLongLongMask(arg); else return converterr("integer<K>", arg, msgbuf, bufsize); *p = ival; break; }#endif case 'f': {/* float */ float *p = va_arg(*p_va, float *); double dval = PyFloat_AsDouble(arg); if (PyErr_Occurred()) return converterr("float<f>", arg, msgbuf, bufsize); else *p = (float) dval; break; } case 'd': {/* double */ double *p = va_arg(*p_va, double *); double dval = PyFloat_AsDouble(arg); if (PyErr_Occurred()) return converterr("float<d>", arg, msgbuf, bufsize); else *p = dval; break; } #ifndef WITHOUT_COMPLEX case 'D': {/* complex double */ Py_complex *p = va_arg(*p_va, Py_complex *); Py_complex cval; cval = PyComplex_AsCComplex(arg); if (PyErr_Occurred()) return converterr("complex<D>", arg, msgbuf, bufsize); else *p = cval; break; }#endif /* WITHOUT_COMPLEX */ case 'c': {/* char */ char *p = va_arg(*p_va, char *); if (PyString_Check(arg) && PyString_Size(arg) == 1) *p = PyString_AS_STRING(arg)[0]; else return converterr("char", arg, msgbuf, bufsize); break; } case 's': {/* string */ if (*format == '#') { void **p = (void **)va_arg(*p_va, char **); FETCH_SIZE; if (PyString_Check(arg)) { *p = PyString_AS_STRING(arg); STORE_SIZE(PyString_GET_SIZE(arg)); }#ifdef Py_USING_UNICODE else if (PyUnicode_Check(arg)) { uarg = UNICODE_DEFAULT_ENCODING(arg); if (uarg == NULL) return converterr(CONV_UNICODE, arg, msgbuf, bufsize); *p = PyString_AS_STRING(uarg); STORE_SIZE(PyString_GET_SIZE(uarg)); }#endif else { /* any buffer-like object */ char *buf; Py_ssize_t count = convertbuffer(arg, p, &buf); if (count < 0) return converterr(buf, arg, msgbuf, bufsize); STORE_SIZE(count); } format++; } else { char **p = va_arg(*p_va, char **); if (PyString_Check(arg)) *p = PyString_AS_STRING(arg);#ifdef Py_USING_UNICODE else if (PyUnicode_Check(arg)) { uarg = UNICODE_DEFAULT_ENCODING(arg); if (uarg == NULL) return converterr(CONV_UNICODE, arg, msgbuf, bufsize); *p = PyString_AS_STRING(uarg); }#endif else return converterr("string", arg, msgbuf, bufsize); if ((Py_ssize_t)strlen(*p) != PyString_Size(arg)) return converterr("string without null bytes", arg, msgbuf, bufsize); } break; } case 'z': {/* string, may be NULL (None) */ if (*format == '#') { /* any buffer-like object */ void **p = (void **)va_arg(*p_va, char **); FETCH_SIZE; if (arg == Py_None) { *p = 0; STORE_SIZE(0); } else if (PyString_Check(arg)) { *p = PyString_AS_STRING(arg); STORE_SIZE(PyString_GET_SIZE(arg)); }#ifdef Py_USING_UNICODE else if (PyUnicode_Check(arg)) { uarg = UNICODE_DEFAULT_ENCODING(arg); if (uarg == NULL) return converterr(CONV_UNICODE, arg, msgbuf, bufsize); *p = PyString_AS_STRING(uarg); STORE_SIZE(PyString_GET_SIZE(uarg)); }#endif else { /* any buffer-like object */ char *buf; Py_ssize_t count = convertbuffer(arg, p, &buf); if (count < 0) return converterr(buf, arg, msgbuf, bufsize); STORE_SIZE(count); } format++; } else { char **p = va_arg(*p_va, char **); if (arg == Py_None) *p = 0; else if (PyString_Check(arg)) *p = PyString_AS_STRING(arg);#ifdef Py_USING_UNICODE else if (PyUnicode_Check(arg)) { uarg = UNICODE_DEFAULT_ENCODING(arg); if (uarg == NULL) return converterr(CONV_UNICODE, arg, msgbuf, bufsize); *p = PyString_AS_STRING(uarg); }#endif else return converterr("string or None", arg, msgbuf, bufsize); if (*format == '#') { FETCH_SIZE; assert(0); /* XXX redundant with if-case */ if (arg == Py_None) *q = 0; else *q = PyString_Size(arg); format++; } else if (*p != NULL && (Py_ssize_t)strlen(*p) != PyString_Size(arg)) return converterr( "string without null bytes or None", arg, msgbuf, bufsize); } break; } case 'e': {/* encoded string */ char **buffer; const char *encoding; PyObject *s; int size, recode_strings; /* Get 'e' parameter: the encoding name */ encoding = (const char *)va_arg(*p_va, const char *);#ifdef Py_USING_UNICODE if (encoding == NULL) encoding = PyUnicode_GetDefaultEncoding();#endif /* Get output buffer parameter: 's' (recode all objects via Unicode) or 't' (only recode non-string objects) */ if (*format == 's') recode_strings = 1; else if (*format == 't') recode_strings = 0; else return converterr( "(unknown parser marker combination)", arg, msgbuf, bufsize); buffer = (char **)va_arg(*p_va, char **); format++; if (buffer == NULL) return converterr("(buffer is NULL)", arg, msgbuf, bufsize); /* Encode object */ if (!recode_strings && PyString_Check(arg)) { s = arg; Py_INCREF(s); } else {#ifdef Py_USING_UNICODE PyObject *u; /* Convert object to Unicode */ u = PyUnicode_FromObject(arg); if (u == NULL) return converterr( "string or unicode or text buffer", arg, msgbuf, bufsize); /* Encode object; use default error handling */ s = PyUnicode_AsEncodedString(u, encoding, NULL); Py_DECREF(u); if (s == NULL) return converterr("(encoding failed)", arg, msgbuf, bufsize); if (!PyString_Check(s)) { Py_DECREF(s); return converterr( "(encoder failed to return a string)", arg, msgbuf, bufsize); }#else return converterr("string<e>", arg, msgbuf, bufsize);#endif } size = PyString_GET_SIZE(s); /* Write output; output is guaranteed to be 0-terminated */ if (*format == '#') { /* Using buffer length parameter '#': - if *buffer is NULL, a new buffer of the needed size is allocated and the data copied into it; *buffer is updated to point to the new buffer; the caller is responsible for PyMem_Free()ing it after usage - if *buffer is not NULL, the data is copied to *buffer; *buffer_len has to be set to the size of the buffer on input; buffer overflow is signalled with an error; buffer has to provide enough room for the encoded string plus the trailing 0-byte - in both cases, *buffer_len is updated to the size of the buffer /excluding/ the trailing 0-byte */ FETCH_SIZE; format++; if (q == NULL && q2 == NULL) { Py_DECREF(s); return converterr( "(buffer_len is NULL)", arg, msgbuf, bufsize); } if (*buffer == NULL) { *buffer = PyMem_NEW(char, size + 1); if (*buffer == NULL) { Py_DECREF(s); return converterr( "(memory error)", arg, msgbuf, bufsize); } if(addcleanup(*buffer, freelist)) {Lines 501–1000 of 1771. Previous lines Load more
static char * converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize) { assert(expected != NULL); assert(arg != NULL); PyOS_snprintf(msgbuf, bufsize, "must be %.50s, not %.50s", expected, arg == Py_None ? "None" : arg->ob_type->tp_name); return msgbuf; } #define CONV_UNICODE "(unicode conversion error)" /* explicitly check for float arguments when integers are expected. For now * signal a warning. Returns true if an exception was raised. */ static int float_argument_error(PyObject *arg) { if (PyFloat_Check(arg) && PyErr_Warn(PyExc_DeprecationWarning, "integer argument expected, got float" )) return 1; else return 0; } /* Convert a non-tuple argument. Return NULL if conversion went OK, or a string with a message describing the failure. The message is formatted as "must be <desired type>, not <actual type>". When failing, an exception may or may not have been raised. Don't call if a tuple is expected. When you add new format codes, please don't forget poor skipitem() below. */ static char * convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, char *msgbuf, size_t bufsize, PyObject **freelist) { /* For # codes */ #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\ if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \ else q=va_arg(*p_va, int*); #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s; #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q) const char *format = *p_format; char c = *format++; #ifdef Py_USING_UNICODE PyObject *uarg; #endif switch (c) { case 'b': { /* unsigned byte -- very short int */ char *p = va_arg(*p_va, char *); long ival; if (float_argument_error(arg)) return converterr("integer<b>", arg, msgbuf, bufsize); ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<b>", arg, msgbuf, bufsize); else if (ival < 0) { PyErr_SetString(PyExc_OverflowError, "unsigned byte integer is less than minimum"); return converterr("integer<b>", arg, msgbuf, bufsize); } else if (ival > UCHAR_MAX) { PyErr_SetString(PyExc_OverflowError, "unsigned byte integer is greater than maximum"); return converterr("integer<b>", arg, msgbuf, bufsize); } else *p = (unsigned char) ival; break; } case 'B': {/* byte sized bitfield - both signed and unsigned values allowed */ char *p = va_arg(*p_va, char *); long ival; if (float_argument_error(arg)) return converterr("integer<B>", arg, msgbuf, bufsize); ival = PyInt_AsUnsignedLongMask(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<B>", arg, msgbuf, bufsize); else *p = (unsigned char) ival; break; } case 'h': {/* signed short int */ short *p = va_arg(*p_va, short *); long ival; if (float_argument_error(arg)) return converterr("integer<h>", arg, msgbuf, bufsize); ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<h>", arg, msgbuf, bufsize); else if (ival < SHRT_MIN) { PyErr_SetString(PyExc_OverflowError, "signed short integer is less than minimum"); return converterr("integer<h>", arg, msgbuf, bufsize); } else if (ival > SHRT_MAX) { PyErr_SetString(PyExc_OverflowError, "signed short integer is greater than maximum"); return converterr("integer<h>", arg, msgbuf, bufsize); } else *p = (short) ival; break; } case 'H': { /* short int sized bitfield, both signed and unsigned allowed */ unsigned short *p = va_arg(*p_va, unsigned short *); long ival; if (float_argument_error(arg)) return converterr("integer<H>", arg, msgbuf, bufsize); ival = PyInt_AsUnsignedLongMask(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<H>", arg, msgbuf, bufsize); else *p = (unsigned short) ival; break; } case 'i': {/* signed int */ int *p = va_arg(*p_va, int *); long ival; if (float_argument_error(arg)) return converterr("integer<i>", arg, msgbuf, bufsize); ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<i>", arg, msgbuf, bufsize); else if (ival > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "signed integer is greater than maximum"); return converterr("integer<i>", arg, msgbuf, bufsize); } else if (ival < INT_MIN) { PyErr_SetString(PyExc_OverflowError, "signed integer is less than minimum"); return converterr("integer<i>", arg, msgbuf, bufsize); } else *p = ival; break; } case 'I': { /* int sized bitfield, both signed and unsigned allowed */ unsigned int *p = va_arg(*p_va, unsigned int *); unsigned int ival; if (float_argument_error(arg)) return converterr("integer<I>", arg, msgbuf, bufsize); ival = (unsigned int)PyInt_AsUnsignedLongMask(arg); if (ival == (unsigned int)-1 && PyErr_Occurred()) return converterr("integer<I>", arg, msgbuf, bufsize); else *p = ival; break; } case 'n': /* Py_ssize_t */ #if SIZEOF_SIZE_T != SIZEOF_LONG { Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); Py_ssize_t ival; if (float_argument_error(arg)) return converterr("integer<n>", arg, msgbuf, bufsize); ival = PyInt_AsSsize_t(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<n>", arg, msgbuf, bufsize); *p = ival; break; } #endif /* Fall through from 'n' to 'l' if Py_ssize_t is int */ case 'l': {/* long int */ long *p = va_arg(*p_va, long *); long ival; if (float_argument_error(arg)) return converterr("integer<l>", arg, msgbuf, bufsize); ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer<l>", arg, msgbuf, bufsize); else *p = ival; break; } case 'k': { /* long sized bitfield */ unsigned long *p = va_arg(*p_va, unsigned long *); unsigned long ival; if (PyInt_Check(arg)) ival = PyInt_AsUnsignedLongMask(arg); else if (PyLong_Check(arg)) ival = PyLong_AsUnsignedLongMask(arg); else return converterr("integer<k>", arg, msgbuf, bufsize); *p = ival; break; } #ifdef HAVE_LONG_LONG case 'L': {/* PY_LONG_LONG */ PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * ); PY_LONG_LONG ival = PyLong_AsLongLong( arg ); if( ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) { return converterr("long<L>", arg, msgbuf, bufsize); } else { *p = ival; } break; } case 'K': { /* long long sized bitfield */ unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *); unsigned PY_LONG_LONG ival; if (PyInt_Check(arg)) ival = PyInt_AsUnsignedLongMask(arg); else if (PyLong_Check(arg)) ival = PyLong_AsUnsignedLongLongMask(arg); else return converterr("integer<K>", arg, msgbuf, bufsize); *p = ival; break; } #endif case 'f': {/* float */ float *p = va_arg(*p_va, float *); double dval = PyFloat_AsDouble(arg); if (PyErr_Occurred()) return converterr("float<f>", arg, msgbuf, bufsize); else *p = (float) dval; break; } case 'd': {/* double */ double *p = va_arg(*p_va, double *); double dval = PyFloat_AsDouble(arg); if (PyErr_Occurred()) return converterr("float<d>", arg, msgbuf, bufsize); else *p = dval; break; } #ifndef WITHOUT_COMPLEX case 'D': {/* complex double */ Py_complex *p = va_arg(*p_va, Py_complex *); Py_complex cval; cval = PyComplex_AsCComplex(arg); if (PyErr_Occurred()) return converterr("complex<D>", arg, msgbuf, bufsize); else *p = cval; break; } #endif /* WITHOUT_COMPLEX */ case 'c': {/* char */ char *p = va_arg(*p_va, char *); if (PyString_Check(arg) && PyString_Size(arg) == 1) *p = PyString_AS_STRING(arg)[0]; else return converterr("char", arg, msgbuf, bufsize); break; } case 's': {/* string */ if (*format == '#') { void **p = (void **)va_arg(*p_va, char **); FETCH_SIZE; if (PyString_Check(arg)) { *p = PyString_AS_STRING(arg); STORE_SIZE(PyString_GET_SIZE(arg)); } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(arg)) { uarg = UNICODE_DEFAULT_ENCODING(arg); if (uarg == NULL) return converterr(CONV_UNICODE, arg, msgbuf, bufsize); *p = PyString_AS_STRING(uarg); STORE_SIZE(PyString_GET_SIZE(uarg)); } #endif else { /* any buffer-like object */ char *buf; Py_ssize_t count = convertbuffer(arg, p, &buf); if (count < 0) return converterr(buf, arg, msgbuf, bufsize); STORE_SIZE(count); } format++; } else { char **p = va_arg(*p_va, char **); if (PyString_Check(arg)) *p = PyString_AS_STRING(arg); #ifdef Py_USING_UNICODE else if (PyUnicode_Check(arg)) { uarg = UNICODE_DEFAULT_ENCODING(arg); if (uarg == NULL) return converterr(CONV_UNICODE, arg, msgbuf, bufsize); *p = PyString_AS_STRING(uarg); } #endif else return converterr("string", arg, msgbuf, bufsize); if ((Py_ssize_t)strlen(*p) != PyString_Size(arg)) return converterr("string without null bytes", arg, msgbuf, bufsize); } break; } case 'z': {/* string, may be NULL (None) */ if (*format == '#') { /* any buffer-like object */ void **p = (void **)va_arg(*p_va, char **); FETCH_SIZE; if (arg == Py_None) { *p = 0; STORE_SIZE(0); } else if (PyString_Check(arg)) { *p = PyString_AS_STRING(arg); STORE_SIZE(PyString_GET_SIZE(arg)); } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(arg)) { uarg = UNICODE_DEFAULT_ENCODING(arg); if (uarg == NULL) return converterr(CONV_UNICODE, arg, msgbuf, bufsize); *p = PyString_AS_STRING(uarg); STORE_SIZE(PyString_GET_SIZE(uarg)); } #endif else { /* any buffer-like object */ char *buf; Py_ssize_t count = convertbuffer(arg, p, &buf); if (count < 0) return converterr(buf, arg, msgbuf, bufsize); STORE_SIZE(count); } format++; } else { char **p = va_arg(*p_va, char **); if (arg == Py_None) *p = 0; else if (PyString_Check(arg)) *p = PyString_AS_STRING(arg); #ifdef Py_USING_UNICODE else if (PyUnicode_Check(arg)) { uarg = UNICODE_DEFAULT_ENCODING(arg); if (uarg == NULL) return converterr(CONV_UNICODE, arg, msgbuf, bufsize); *p = PyString_AS_STRING(uarg); } #endif else return converterr("string or None", arg, msgbuf, bufsize); if (*format == '#') { FETCH_SIZE; assert(0); /* XXX redundant with if-case */ if (arg == Py_None) *q = 0; else *q = PyString_Size(arg); format++; } else if (*p != NULL && (Py_ssize_t)strlen(*p) != PyString_Size(arg)) return converterr( "string without null bytes or None", arg, msgbuf, bufsize); } break; } case 'e': {/* encoded string */ char **buffer; const char *encoding; PyObject *s; int size, recode_strings; /* Get 'e' parameter: the encoding name */ encoding = (const char *)va_arg(*p_va, const char *); #ifdef Py_USING_UNICODE if (encoding == NULL) encoding = PyUnicode_GetDefaultEncoding(); #endif /* Get output buffer parameter: 's' (recode all objects via Unicode) or 't' (only recode non-string objects) */ if (*format == 's') recode_strings = 1; else if (*format == 't') recode_strings = 0; else return converterr( "(unknown parser marker combination)", arg, msgbuf, bufsize); buffer = (char **)va_arg(*p_va, char **); format++; if (buffer == NULL) return converterr("(buffer is NULL)", arg, msgbuf, bufsize); /* Encode object */ if (!recode_strings && PyString_Check(arg)) { s = arg; Py_INCREF(s); } else { #ifdef Py_USING_UNICODE PyObject *u; /* Convert object to Unicode */ u = PyUnicode_FromObject(arg); if (u == NULL) return converterr( "string or unicode or text buffer", arg, msgbuf, bufsize); /* Encode object; use default error handling */ s = PyUnicode_AsEncodedString(u, encoding, NULL); Py_DECREF(u); if (s == NULL) return converterr("(encoding failed)", arg, msgbuf, bufsize); if (!PyString_Check(s)) { Py_DECREF(s); return converterr( "(encoder failed to return a string)", arg, msgbuf, bufsize); } #else return converterr("string<e>", arg, msgbuf, bufsize); #endif } size = PyString_GET_SIZE(s); /* Write output; output is guaranteed to be 0-terminated */ if (*format == '#') { /* Using buffer length parameter '#': - if *buffer is NULL, a new buffer of the needed size is allocated and the data copied into it; *buffer is updated to point to the new buffer; the caller is responsible for PyMem_Free()ing it after usage - if *buffer is not NULL, the data is copied to *buffer; *buffer_len has to be set to the size of the buffer on input; buffer overflow is signalled with an error; buffer has to provide enough room for the encoded string plus the trailing 0-byte - in both cases, *buffer_len is updated to the size of the buffer /excluding/ the trailing 0-byte */ FETCH_SIZE; format++; if (q == NULL && q2 == NULL) { Py_DECREF(s); return converterr( "(buffer_len is NULL)", arg, msgbuf, bufsize); } if (*buffer == NULL) { *buffer = PyMem_NEW(char, size + 1); if (*buffer == NULL) { Py_DECREF(s); return converterr( "(memory error)", arg, msgbuf, bufsize); } if(addcleanup(*buffer, freelist)) {