@@ -390,6 +390,45 @@ int PyUnicode_AsWideChar(PyUnicodeObject *unicode,
390390
391391#endif
392392
393+ PyObject * PyUnicode_FromOrdinal (int ordinal )
394+ {
395+ Py_UNICODE s [2 ];
396+
397+ #ifdef Py_UNICODE_WIDE
398+ if (ordinal < 0 || ordinal > 0x10ffff ) {
399+ PyErr_SetString (PyExc_ValueError ,
400+ "unichr() arg not in range(0x110000) "
401+ "(wide Python build)" );
402+ return NULL ;
403+ }
404+ #else
405+ if (ordinal < 0 || ordinal > 0xffff ) {
406+ PyErr_SetString (PyExc_ValueError ,
407+ "unichr() arg not in range(0x10000) "
408+ "(narrow Python build)" );
409+ return NULL ;
410+ }
411+ #endif
412+
413+ if (ordinal <= 0xffff ) {
414+ /* UCS-2 character */
415+ s [0 ] = (Py_UNICODE ) ordinal ;
416+ return PyUnicode_FromUnicode (s , 1 );
417+ }
418+ else {
419+ #ifndef Py_UNICODE_WIDE
420+ /* UCS-4 character. store as two surrogate characters */
421+ ordinal -= 0x10000L ;
422+ s [0 ] = 0xD800 + (Py_UNICODE ) (ordinal >> 10 );
423+ s [1 ] = 0xDC00 + (Py_UNICODE ) (ordinal & 0x03FF );
424+ return PyUnicode_FromUnicode (s , 2 );
425+ #else
426+ s [0 ] = (Py_UNICODE )ordinal ;
427+ return PyUnicode_FromUnicode (s , 1 );
428+ #endif
429+ }
430+ }
431+
393432PyObject * PyUnicode_FromObject (register PyObject * obj )
394433{
395434 /* XXX Perhaps we should make this API an alias of
@@ -5373,7 +5412,22 @@ formatchar(Py_UNICODE *buf,
53735412 x = PyInt_AsLong (v );
53745413 if (x == -1 && PyErr_Occurred ())
53755414 goto onError ;
5376- buf [0 ] = (char ) x ;
5415+ #ifdef Py_UNICODE_WIDE
5416+ if (x < 0 || x > 0x10ffff ) {
5417+ PyErr_SetString (PyExc_ValueError ,
5418+ "%c arg not in range(0x110000) "
5419+ "(wide Python build)" );
5420+ return -1 ;
5421+ }
5422+ #else
5423+ if (x < 0 || x > 0xffff ) {
5424+ PyErr_SetString (PyExc_ValueError ,
5425+ "%c arg not in range(0x10000) "
5426+ "(narrow Python build)" );
5427+ return -1 ;
5428+ }
5429+ #endif
5430+ buf [0 ] = (Py_UNICODE ) x ;
53775431 }
53785432 buf [1 ] = '\0' ;
53795433 return 1 ;
0 commit comments