File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -308,23 +308,34 @@ static PyObject *
308308builtin_unichr (PyObject * self , PyObject * args )
309309{
310310 long x ;
311- Py_UNICODE s [1 ];
311+ Py_UNICODE s [2 ];
312312
313313 if (!PyArg_ParseTuple (args , "l:unichr" , & x ))
314314 return NULL ;
315- if (x < 0 || x >= 65536 ) {
315+
316+ if (x < 0 || x > 0x10ffff ) {
316317 PyErr_SetString (PyExc_ValueError ,
317- "unichr() arg not in range(65536 )" );
318+ "unichr() arg not in range(0x10ffff )" );
318319 return NULL ;
319320 }
320- s [0 ] = (Py_UNICODE )x ;
321- return PyUnicode_FromUnicode (s , 1 );
321+
322+ if (x <= 0xffff ) {
323+ /* UCS-2 character */
324+ s [0 ] = (Py_UNICODE ) x ;
325+ return PyUnicode_FromUnicode (s , 1 );
326+ } else {
327+ /* UCS-4 character. store as two surrogate characters */
328+ x -= 0x10000L ;
329+ s [0 ] = 0xD800 + (Py_UNICODE ) (x >> 10 );
330+ s [1 ] = 0xDC00 + (Py_UNICODE ) (x & 0x03FF );
331+ return PyUnicode_FromUnicode (s , 2 );
332+ }
322333}
323334
324335static char unichr_doc [] =
325336"unichr(i) -> Unicode character\n\
326337\n\
327- Return a Unicode string of one character with ordinal i; 0 <= i < 65536 ." ;
338+ Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff ." ;
328339
329340
330341static PyObject *
You can’t perform that action at this time.
0 commit comments