@@ -354,17 +354,46 @@ PyObject *ftpy_decode(
354
354
355
355
356
356
int ftpy_get_charcode_from_unicode (
357
- PyObject * py_unicode , unsigned short platform_id ,
357
+ PyObject * input , unsigned short platform_id ,
358
358
unsigned short encoding_id , unsigned long * charcode )
359
359
{
360
360
const char * encoding ;
361
361
const char * fallback_encoding ;
362
+ PyObject * py_unicode = NULL ;
362
363
PyObject * py_bytes = NULL ;
363
364
char * bytes ;
364
365
Py_ssize_t bytes_size ;
365
366
int result = -1 ;
366
367
367
- if (!PyUnicode_Check (py_unicode )) {
368
+ #if PY_MAJOR_VERSION == 2
369
+ Py_UNICODE numeric_value = 0 ;
370
+ if (PyLong_Check (input )) {
371
+ numeric_value = (Py_UNICODE )PyLong_AsLong (input );
372
+ if (PyErr_Occurred ()) {
373
+ goto exit ;
374
+ }
375
+ py_unicode = PyUnicode_FromUnicode (& numeric_value , 1 );
376
+ } else if (PyInt_Check (input )) {
377
+ numeric_value = (Py_UNICODE )PyInt_AsLong (input );
378
+ if (PyErr_Occurred ()) {
379
+ goto exit ;
380
+ }
381
+ py_unicode = PyUnicode_FromUnicode (& numeric_value , 1 );
382
+ }
383
+ #else // Python 3
384
+ Py_UCS4 numeric_value = 0 ;
385
+ if (PyLong_Check (input )) {
386
+ numeric_value = (Py_UCS4 )PyLong_AsLong (input );
387
+ if (PyErr_Occurred ()) {
388
+ goto exit ;
389
+ }
390
+ py_unicode = PyUnicode_FromKindAndData (PyUnicode_4BYTE_KIND , & numeric_value , 1 );
391
+ }
392
+ #endif
393
+ else if (PyUnicode_Check (input )) {
394
+ Py_INCREF (input );
395
+ py_unicode = input ;
396
+ } else {
368
397
PyErr_SetString (
369
398
PyExc_TypeError ,
370
399
"Must be a single-character unicode string" );
@@ -418,6 +447,7 @@ int ftpy_get_charcode_from_unicode(
418
447
result = 0 ;
419
448
exit :
420
449
450
+ Py_XDECREF (py_unicode );
421
451
Py_XDECREF (py_bytes );
422
452
return result ;
423
453
}
0 commit comments