Error resolution for passing Unicode strings when ESL python calls C module

Source: Internet
Author: User
Tags freeswitch

Passing a string error when using FreeSWITCH to make Python calls with ESL

TypeError:in ' Eslconnection_api ',2' char const * '     

is because Python passes the string Unicode, the ASCII code used in the C language Char mode
Do the conversion in Swig, the code is as follows

Modify File Esl_wrap.cpp

#####

/* for C or C + + function pointers */
Add definition
#define SWIG_INTERNALNEWPOINTEROBJ (PTR, type, flags) Swig_python_newpointerobj (PTR, type, flags)


Swigintern int
swig_ascharptrandsize (pyobject *obj, char** cptr, size_t* psize, int *alloc)
{
// Increase Unicode Check
if (Pyunicode_check (obj) | | Pystring_check (obj)) {
Char *cstr; py_ssize_t Len;
Pystring_asstringandsize (obj, &cstr, &len);
if (cptr) {
if (alloc) {
/*
in Python the user should not being able to modify the inner
string Represen Tation. To warranty this, if you define
Swig_python_safe_cstrings, a new/copy of the PYTHON string
in buffer is always return Ed.

The default behavior is just to return the pointer value,
So, be careful.
*/
#if defined (swig_python_safe_c STRINGS)
if (*alloc! = swig_oldobj)
#else
if (*alloc = = swig_newobj)
#endif
{
*cptr = Reinterpret_ca st< char* > (memcpy ((new Char[len + 1]), CStr, sizeof (char) * (len + 1));
*alloc = swig_newobj;
}
Else {
*cptr = CStr;
*alloc = Swig_oldobj;
}
} else {
*cptr = pystring_asstring (obj);
}
}
if (psize) *psize = len + 1;
return SWIG_OK;
} else {
swig_type_info* pchar_descriptor = Swig_pchar _descriptor ();
if (pchar_descriptor) {
void* vptr = 0;
if (Swig_convertptr (obj, &vptr, pchar_descriptor, 0) = = SWIG_OK) {
if (cptr) *cptr = (char *) vptr;
if (psize) *psize = vptr? (Strlen (char *) vptr) + 1): 0;
if (alloc) *alloc = swig_oldobj;
return SWIG_OK;
}
}
}
return swig_typeerror;
}

Defining UTF8 Conversion Functions
Swiginterninline Pyobject *
Swig_fromutf8charptrandsize (const char* CArray, size_t size)
{
if (CArray) {
if (Size > Int_max) {
swig_type_info* pchar_descriptor = Swig_pchar_descriptor ();
Return pchar_descriptor?
Swig_internalnewpointerobj (const_cast< char * > (CArray), Pchar_descriptor, 0): Swig_py_void ();
} else {


Const unsigned char *UCP = (const unsigned char *) CArray;
size_t i;
for (i = 0; i < size; ++i) {
if (Ucp[i] >= 0x80)/* UTF-8? */
Return Pyunicode_fromstringandsize (CArray, static_cast< int > (size));
}
Return Pystring_fromstringandsize (CArray, static_cast< int > (size));


}
} else {
return swig_py_void ();
}
}


Swiginterninline Pyobject *
Swig_fromcharptr (const char *cptr)
{
Return Swig_fromcharptrandsize (Cptr, (cptr strlen (cptr): 0));
Invoke transformation
Return Swig_fromutf8charptrandsize (Cptr, (cptr strlen (cptr): 0));
}

Compile when you are done

Freeswitch/libs/esl

Make Pymod

Error resolution for passing Unicode strings when ESL python calls C module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.