@@ -1123,7 +1123,7 @@ typedef void ( *label_func )( PLINT, PLFLT, char *, PLINT, PLPointer );
11231123
11241124 void do_label_callback ( PLINT axis, PLFLT value, char *string, PLINT len, PLPointer data )
11251125 {
1126- PyObject *pdata, *arglist, *result;
1126+ PyObject *pdata, *arglist, *result, *unicode_string ;
11271127 char *pystring;
11281128
11291129 // the data argument is acutally a pointer to a python object
@@ -1152,16 +1152,24 @@ typedef void ( *label_func )( PLINT, PLFLT, char *, PLINT, PLPointer );
11521152 fprintf ( stderr, " label callback failed with 3 arguments\n " );
11531153 PyErr_SetString ( PyExc_RuntimeError, " label callback must take 3 arguments." );
11541154 }
1155- else if ( !PyString_Check ( result ) )
1156- {
1157- fprintf ( stderr, " label callback must return a string\n " );
1158- PyErr_SetString ( PyExc_RuntimeError, " label callback must return a string." );
1159- }
1160- else
1161- {
1155+ else if ( PyString_Check ( result ) )
1156+ {
11621157 // should I test the type here?
11631158 pystring = PyString_AsString ( result );
11641159 strncpy ( string, pystring, len );
1160+ }
1161+ else if ( PyUnicode_Check ( result ))
1162+ {
1163+ // unicode_string is never freed? memory leak here?
1164+ unicode_string = PyUnicode_AsEncodedString ( result , " utf-8" , " Error ~" );
1165+ pystring = PyBytes_AS_STRING ( unicode_string );
1166+ // len may be different then the byte string length w/ unicode?
1167+ strncpy ( string, pystring, len );
1168+ }
1169+ else
1170+ {
1171+ fprintf ( stderr, " label callback must return a string\n " );
1172+ PyErr_SetString ( PyExc_RuntimeError, " label callback must return a string." );
11651173 }
11661174 // release the result
11671175 Py_CLEAR ( result );
@@ -1258,7 +1266,15 @@ typedef void ( *label_func )( PLINT, PLFLT, char *, PLINT, PLPointer );
12581266 PyObject * rep = PyObject_Repr ( input );
12591267 if ( rep )
12601268 {
1261- char * str = PyString_AsString ( rep );
1269+ // Memory leaks here? str and uni_str are not freed?
1270+ char * str;
1271+ if ( PyUnicode_Check ( rep ) ){
1272+ PyObject *uni_str = PyUnicode_AsEncodedString ( rep , " utf-8" , " Error ~" );
1273+ str = PyBytes_AS_STRING ( uni_str );
1274+ }
1275+ else {
1276+ str = PyString_AsString ( rep );
1277+ }
12621278 if ( strcmp ( str, " <built-in function pltr0>" ) == 0 )
12631279 {
12641280 result = pltr0;
@@ -1558,6 +1574,8 @@ typedef void ( *label_func )( PLINT, PLFLT, char *, PLINT, PLPointer );
15581574%typemap( in ) const char *legline[4 ] ( char ** tmp = NULL )
15591575{
15601576 int i;
1577+ PyObject *elt, *unicode_string;
1578+
15611579 if ( !PySequence_Check ( $input ) || PySequence_Size ( $input ) != 4 )
15621580 {
15631581 PyErr_SetString ( PyExc_ValueError, " Requires a sequence of 4 strings." );
@@ -1574,7 +1592,15 @@ typedef void ( *label_func )( PLINT, PLFLT, char *, PLINT, PLPointer );
15741592 $1 = tmp;
15751593 for ( i = 0 ; i < 4 ; i++ )
15761594 {
1577- $1 [i] = PyString_AsString ( PySequence_Fast_GET_ITEM ( $input, i ) );
1595+ $1 [i] = NULL ;
1596+ elt = PySequence_Fast_GET_ITEM ( $input, i );
1597+ if ( PyString_Check ( elt ) ){
1598+ $1 [i] = PyString_AsString ( elt );
1599+ }
1600+ else if ( PyUnicode_Check ( elt ) ){
1601+ unicode_string = PyUnicode_AsEncodedString ( elt , " utf-8" , " Error ~" );
1602+ $1 [i] = PyBytes_AS_STRING ( unicode_string );
1603+ }
15781604 if ( $1 [i] == NULL )
15791605 {
15801606 free ( tmp );
@@ -1592,6 +1618,8 @@ typedef void ( *label_func )( PLINT, PLFLT, char *, PLINT, PLPointer );
15921618%typemap ( in ) ( int *p_argc, char **argv ) ( int tmp )
15931619{
15941620 int i;
1621+ PyObject *unicode_string;
1622+
15951623 if ( !PyList_Check ( $input ) )
15961624 {
15971625 PyErr_SetString ( PyExc_ValueError, " Expecting a list" );
@@ -1603,13 +1631,20 @@ typedef void ( *label_func )( PLINT, PLFLT, char *, PLINT, PLPointer );
16031631 for ( i = 0 ; i < tmp; i++ )
16041632 {
16051633 PyObject *s = PyList_GetItem ( $input, i );
1606- if ( !PyString_Check ( s ) )
1607- {
1634+ if ( PyString_Check ( s ) ){
1635+ $2 [i] = PyString_AsString ( s );
1636+ }
1637+ else if ( PyUnicode_Check ( s ) ){
1638+ // unicode_string is never freed? memory leak here?
1639+ unicode_string = PyUnicode_AsEncodedString ( s , " utf-8" , " Error ~" );
1640+ $2 [i] = PyBytes_AS_STRING ( unicode_string );
1641+ }
1642+ else {
16081643 free ( $2 );
16091644 PyErr_SetString ( PyExc_ValueError, " List items must be strings" );
16101645 return NULL ;
16111646 }
1612- $ 2 [i] = PyString_AsString ( s );
1647+
16131648 }
16141649 $2 [i] = 0 ;
16151650}
0 commit comments