@@ -152,14 +152,38 @@ builtin_buffer(self, args)
152152}
153153
154154static char buffer_doc [] =
155- "buffer(object [, offset[, size]) -> object\n\
155+ "buffer(object [, offset[, size]] ) -> object\n\
156156\n\
157157Creates a new buffer object which references the given object.\n\
158158The buffer will reference a slice of the target object from the\n\
159159start of the object (or at the specified offset). The slice will\n\
160160extend to the end of the target object (or with the specified size)." ;
161161
162162
163+ static PyObject *
164+ builtin_unicode (self , args )
165+ PyObject * self ;
166+ PyObject * args ;
167+ {
168+ char * s ;
169+ int len ;
170+ char * encoding = NULL ;
171+ char * errors = NULL ;
172+
173+ if ( !PyArg_ParseTuple (args , "s#|ss:unicode" , & s , & len ,
174+ & encoding , & errors ) )
175+ return NULL ;
176+ return PyUnicode_Decode (s , len , encoding , errors );
177+ }
178+
179+ static char unicode_doc [] =
180+ "unicode(string [, encoding[, errors]]) -> object\n\
181+ \n\
182+ Creates a new unicode object from the given encoded string.\n\
183+ encoding defaults to 'utf-8' and errors, defining the error handling,\n\
184+ to 'strict'." ;
185+
186+
163187static PyObject *
164188builtin_callable (self , args )
165189 PyObject * self ;
@@ -311,6 +335,31 @@ static char chr_doc[] =
311335Return a string of one character with ordinal i; 0 <= i < 256." ;
312336
313337
338+ static PyObject *
339+ builtin_unichr (self , args )
340+ PyObject * self ;
341+ PyObject * args ;
342+ {
343+ long x ;
344+ Py_UNICODE s [1 ];
345+
346+ if (!PyArg_ParseTuple (args , "l:unichr" , & x ))
347+ return NULL ;
348+ if (x < 0 || x >= 65536 ) {
349+ PyErr_SetString (PyExc_ValueError ,
350+ "unichr() arg not in range(65536)" );
351+ return NULL ;
352+ }
353+ s [0 ] = (Py_UNICODE )x ;
354+ return PyUnicode_FromUnicode (s , 1 );
355+ }
356+
357+ static char unichr_doc [] =
358+ "unichr(i) -> unicode character\n\
359+ \n\
360+ Return a unicode string of one character with ordinal i; 0 <= i < 65536." ;
361+
362+
314363static PyObject *
315364builtin_cmp (self , args )
316365 PyObject * self ;
@@ -1541,17 +1590,29 @@ builtin_ord(self, args)
15411590 PyObject * self ;
15421591 PyObject * args ;
15431592{
1544- char c ;
1593+ PyObject * obj ;
1594+ long ord ;
15451595
1546- if (!PyArg_ParseTuple (args , "c :ord" , & c ))
1596+ if (!PyArg_ParseTuple (args , "O :ord" , & obj ))
15471597 return NULL ;
1548- return PyInt_FromLong ((long )(c & 0xff ));
1598+
1599+ if (PyString_Check (obj ) && PyString_GET_SIZE (obj ) == 1 )
1600+ ord = (long )((unsigned char )* PyString_AS_STRING (obj ));
1601+ else if (PyUnicode_Check (obj ) && PyUnicode_GET_SIZE (obj ) == 1 )
1602+ ord = (long )* PyUnicode_AS_UNICODE (obj );
1603+ else {
1604+ PyErr_SetString (PyExc_TypeError ,
1605+ "expected a string or unicode character" );
1606+ return NULL ;
1607+ }
1608+
1609+ return PyInt_FromLong (ord );
15491610}
15501611
15511612static char ord_doc [] =
15521613"ord(c) -> integer\n\
15531614\n\
1554- Return the integer ordinal of a one character string." ;
1615+ Return the integer ordinal of a one character [unicode] string." ;
15551616
15561617
15571618static PyObject *
@@ -2227,6 +2288,8 @@ static PyMethodDef builtin_methods[] = {
22272288 {"str" , builtin_str , 1 , str_doc },
22282289 {"tuple" , builtin_tuple , 1 , tuple_doc },
22292290 {"type" , builtin_type , 1 , type_doc },
2291+ {"unicode" , builtin_unicode , 1 , unicode_doc },
2292+ {"unichr" , builtin_unichr , 1 , unichr_doc },
22302293 {"vars" , builtin_vars , 1 , vars_doc },
22312294 {"xrange" , builtin_xrange , 1 , xrange_doc },
22322295 {NULL , NULL },
@@ -2259,6 +2322,7 @@ PyObject *PyExc_SyntaxError;
22592322PyObject * PyExc_SystemError ;
22602323PyObject * PyExc_SystemExit ;
22612324PyObject * PyExc_UnboundLocalError ;
2325+ PyObject * PyExc_UnicodeError ;
22622326PyObject * PyExc_TypeError ;
22632327PyObject * PyExc_ValueError ;
22642328PyObject * PyExc_ZeroDivisionError ;
@@ -2304,6 +2368,7 @@ bltin_exc[] = {
23042368 {"SystemError" , & PyExc_SystemError , 1 },
23052369 {"SystemExit" , & PyExc_SystemExit , 1 },
23062370 {"UnboundLocalError" , & PyExc_UnboundLocalError , 1 },
2371+ {"UnicodeError" , & PyExc_UnicodeError , 1 },
23072372 {"TypeError" , & PyExc_TypeError , 1 },
23082373 {"ValueError" , & PyExc_ValueError , 1 },
23092374#ifdef MS_WINDOWS
@@ -2467,6 +2532,14 @@ initerrors(dict)
24672532 PyExc_NameError ) != 0 )
24682533 Py_FatalError ("Cannot create string-based exceptions" );
24692534
2535+ /* Make UnicodeError an alias for ValueError */
2536+ Py_INCREF (PyExc_ValueError );
2537+ Py_DECREF (PyExc_UnicodeError );
2538+ PyExc_UnicodeError = PyExc_ValueError ;
2539+ if (PyDict_SetItemString (dict , "UnicodeError" ,
2540+ PyExc_ValueError ) != 0 )
2541+ Py_FatalError ("Cannot create string-based exceptions" );
2542+
24702543 /* missing from the StandardError tuple: Exception, StandardError,
24712544 * and SystemExit
24722545 */
0 commit comments