[Python-checkins] CVS: python/dist/src/Python bltinmodule.c,2.149,2.150

Guido van Rossum guido@cnri.reston.va.us
2000年3月10日 18:00:55 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Python
In directory eric:/home/guido/hp/mal/py-patched/Python
Modified Files:
	bltinmodule.c 
Log Message:
Marc-Andre Lemburg: added new builtin functions unicode() and
unichr(); changed ord() to support Unicode strings; added new
exception UnicodeError; fixed a typo in doc string for buffer().
Index: bltinmodule.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.149
retrieving revision 2.150
diff -C2 -r2.149 -r2.150
*** bltinmodule.c	2000年02月29日 13:59:29	2.149
--- bltinmodule.c	2000年03月10日 23:00:52	2.150
***************
*** 153,157 ****
 
 static char buffer_doc[] =
! "buffer(object [, offset[, size]) -> object\n\
 \n\
 Creates a new buffer object which references the given object.\n\
--- 153,157 ----
 
 static char buffer_doc[] =
! "buffer(object [, offset[, size]]) -> object\n\
 \n\
 Creates a new buffer object which references the given object.\n\
***************
*** 162,165 ****
--- 162,189 ----
 
 static PyObject *
+ builtin_unicode(self, args)
+ 	PyObject *self;
+ 	PyObject *args;
+ {
+ 	char *s;
+ 	int len;
+ 	char *encoding = NULL;
+ 	char *errors = NULL;
+ 
+ 	if ( !PyArg_ParseTuple(args, "s#|ss:unicode", &s, &len, 
+ 			 &encoding, &errors) )
+ 	 return NULL;
+ 	return PyUnicode_Decode(s, len, encoding, errors);
+ }
+ 
+ static char unicode_doc[] =
+ "unicode(string [, encoding[, errors]]) -> object\n\
+ \n\
+ Creates a new unicode object from the given encoded string.\n\
+ encoding defaults to 'utf-8' and errors, defining the error handling,\n\
+ to 'strict'.";
+ 
+ 
+ static PyObject *
 builtin_callable(self, args)
 	PyObject *self;
***************
*** 313,316 ****
--- 337,365 ----
 
 static PyObject *
+ builtin_unichr(self, args)
+ 	PyObject *self;
+ 	PyObject *args;
+ {
+ 	long x;
+ 	Py_UNICODE s[1];
+ 
+ 	if (!PyArg_ParseTuple(args, "l:unichr", &x))
+ 		return NULL;
+ 	if (x < 0 || x >= 65536) {
+ 		PyErr_SetString(PyExc_ValueError,
+ 				"unichr() arg not in range(65536)");
+ 		return NULL;
+ 	}
+ 	s[0] = (Py_UNICODE)x;
+ 	return PyUnicode_FromUnicode(s, 1);
+ }
+ 
+ static char unichr_doc[] =
+ "unichr(i) -> unicode character\n\
+ \n\
+ Return a unicode string of one character with ordinal i; 0 <= i < 65536.";
+ 
+ 
+ static PyObject *
 builtin_cmp(self, args)
 	PyObject *self;
***************
*** 1542,1550 ****
 	PyObject *args;
 {
! 	char c;
 
! 	if (!PyArg_ParseTuple(args, "c:ord", &c))
 		return NULL;
! 	return PyInt_FromLong((long)(c & 0xff));
 }
 
--- 1591,1611 ----
 	PyObject *args;
 {
! 	PyObject *obj;
! 	long ord;
 
! 	if (!PyArg_ParseTuple(args, "O:ord", &obj))
 		return NULL;
! 
! 	if (PyString_Check(obj) && PyString_GET_SIZE(obj) == 1)
! 		ord = (long)((unsigned char)*PyString_AS_STRING(obj));
! 	else if (PyUnicode_Check(obj) && PyUnicode_GET_SIZE(obj) == 1)
! 		ord = (long)*PyUnicode_AS_UNICODE(obj);
! 	else {
! 		PyErr_SetString(PyExc_TypeError,
! 				"expected a string or unicode character");
! 		return NULL;
! 	}
! 
! 	return PyInt_FromLong(ord);
 }
 
***************
*** 1552,1556 ****
 "ord(c) -> integer\n\
 \n\
! Return the integer ordinal of a one character string.";
 
 
--- 1613,1617 ----
 "ord(c) -> integer\n\
 \n\
! Return the integer ordinal of a one character [unicode] string.";
 
 
***************
*** 2228,2231 ****
--- 2289,2294 ----
 	{"tuple",	builtin_tuple, 1, tuple_doc},
 	{"type",	builtin_type, 1, type_doc},
+ 	{"unicode",	builtin_unicode, 1, unicode_doc},
+ 	{"unichr",	builtin_unichr, 1, unichr_doc},
 	{"vars",	builtin_vars, 1, vars_doc},
 	{"xrange",	builtin_xrange, 1, xrange_doc},
***************
*** 2260,2263 ****
--- 2323,2327 ----
 PyObject *PyExc_SystemExit;
 PyObject *PyExc_UnboundLocalError;
+ PyObject *PyExc_UnicodeError;
 PyObject *PyExc_TypeError;
 PyObject *PyExc_ValueError;
***************
*** 2305,2308 ****
--- 2369,2373 ----
 	{"SystemExit", &PyExc_SystemExit, 1},
 	{"UnboundLocalError", &PyExc_UnboundLocalError, 1},
+ 	{"UnicodeError", &PyExc_UnicodeError, 1},
 	{"TypeError", &PyExc_TypeError, 1},
 	{"ValueError", &PyExc_ValueError, 1},
***************
*** 2466,2469 ****
--- 2531,2542 ----
 	if (PyDict_SetItemString(dict, "UnboundLocalError",
 				 PyExc_NameError) != 0)
+ 		Py_FatalError("Cannot create string-based exceptions");
+ 
+ 	/* Make UnicodeError an alias for ValueError */
+ 	Py_INCREF(PyExc_ValueError);
+ 	Py_DECREF(PyExc_UnicodeError);
+ 	PyExc_UnicodeError = PyExc_ValueError;
+ 	if (PyDict_SetItemString(dict, "UnicodeError",
+ 				 PyExc_ValueError) != 0)
 		Py_FatalError("Cannot create string-based exceptions");
 

AltStyle によって変換されたページ (->オリジナル) /