Skip to content

Commit d19fd95

Browse files
committed
Support integers as unicode code points
1 parent a349634 commit d19fd95

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

docstrings/face.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
136136
Parameters
137137
----------
138-
charcode : length-1 unicode string
138+
charcode : length-1 unicode string or int
139139
The Unicode character.
140140
141141
Returns
@@ -420,7 +420,7 @@
420420
421421
Parameters
422422
----------
423-
char_code : unicode character (1-element unicode string)
423+
char_code : unicode character (1-element unicode string) or int
424424
The unicode character.
425425
426426
load_flags : int, optional

src/encoding.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,46 @@ PyObject *ftpy_decode(
354354

355355

356356
int ftpy_get_charcode_from_unicode(
357-
PyObject *py_unicode, unsigned short platform_id,
357+
PyObject *input, unsigned short platform_id,
358358
unsigned short encoding_id, unsigned long *charcode)
359359
{
360360
const char *encoding;
361361
const char *fallback_encoding;
362+
PyObject *py_unicode = NULL;
362363
PyObject *py_bytes = NULL;
363364
char *bytes;
364365
Py_ssize_t bytes_size;
365366
int result = -1;
366367

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 {
368397
PyErr_SetString(
369398
PyExc_TypeError,
370399
"Must be a single-character unicode string");
@@ -418,6 +447,7 @@ int ftpy_get_charcode_from_unicode(
418447
result = 0;
419448
exit:
420449

450+
Py_XDECREF(py_unicode);
421451
Py_XDECREF(py_bytes);
422452
return result;
423453
}

0 commit comments

Comments
 (0)