[Python-checkins] CVS: python/dist/src/Python bltinmodule.c,2.198.2.3,2.198.2.4
Guido van Rossum
gvanrossum@users.sourceforge.net
Thu, 14 Jun 2001 11:12:04 -0700
Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv5083/Python
Modified Files:
Tag: descr-branch
bltinmodule.c
Log Message:
Two more bite the dust: built-ins unicode and complex are now also types.
Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.198.2.3
retrieving revision 2.198.2.4
diff -C2 -r2.198.2.3 -r2.198.2.4
*** bltinmodule.c 2001/06/14 14:19:51 2.198.2.3
--- bltinmodule.c 2001/06/14 18:12:02 2.198.2.4
***************
*** 124,147 ****
static PyObject *
- builtin_unicode(PyObject *self, PyObject *args)
- {
- PyObject *v;
- char *encoding = NULL;
- char *errors = NULL;
-
- if ( !PyArg_ParseTuple(args, "O|ss:unicode", &v, &encoding, &errors) )
- return NULL;
- return PyUnicode_FromEncodedObject(v, encoding, errors);
- }
-
- static char unicode_doc[] =
- "unicode(string [, encoding[, errors]]) -> object\n\
- \n\
- Create a new Unicode object from the given encoded string.\n\
- encoding defaults to the current default string encoding and \n\
- errors, defining the error handling, to 'strict'.";
-
-
- static PyObject *
builtin_callable(PyObject *self, PyObject *args)
{
--- 124,127 ----
***************
*** 392,647 ****
- #ifndef WITHOUT_COMPLEX
-
static PyObject *
- complex_from_string(PyObject *v)
- {
- extern double strtod(const char *, char **);
- const char *s, *start;
- char *end;
- double x=0.0, y=0.0, z;
- int got_re=0, got_im=0, done=0;
- int digit_or_dot;
- int sw_error=0;
- int sign;
- char buffer[256]; /* For errors */
- char s_buffer[256];
- int len;
-
- if (PyString_Check(v)) {
- s = PyString_AS_STRING(v);
- len = PyString_GET_SIZE(v);
- }
- else if (PyUnicode_Check(v)) {
- if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
- PyErr_SetString(PyExc_ValueError,
- "complex() literal too large to convert");
- return NULL;
- }
- if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
- PyUnicode_GET_SIZE(v),
- s_buffer,
- NULL))
- return NULL;
- s = s_buffer;
- len = (int)strlen(s);
- }
- else if (PyObject_AsCharBuffer(v, &s, &len)) {
- PyErr_SetString(PyExc_TypeError,
- "complex() arg is not a string");
- return NULL;
- }
-
- /* position on first nonblank */
- start = s;
- while (*s && isspace(Py_CHARMASK(*s)))
- s++;
- if (s[0] == '\0') {
- PyErr_SetString(PyExc_ValueError,
- "complex() arg is an empty string");
- return NULL;
- }
-
- z = -1.0;
- sign = 1;
- do {
-
- switch (*s) {
-
- case '\0':
- if (s-start != len) {
- PyErr_SetString(
- PyExc_ValueError,
- "complex() arg contains a null byte");
- return NULL;
- }
- if(!done) sw_error=1;
- break;
-
- case '-':
- sign = -1;
- /* Fallthrough */
- case '+':
- if (done) sw_error=1;
- s++;
- if ( *s=='\0'||*s=='+'||*s=='-' ||
- isspace(Py_CHARMASK(*s)) ) sw_error=1;
- break;
-
- case 'J':
- case 'j':
- if (got_im || done) {
- sw_error = 1;
- break;
- }
- if (z<0.0) {
- y=sign;
- }
- else{
- y=sign*z;
- }
- got_im=1;
- s++;
- if (*s!='+' && *s!='-' )
- done=1;
- break;
-
- default:
- if (isspace(Py_CHARMASK(*s))) {
- while (*s && isspace(Py_CHARMASK(*s)))
- s++;
- if (s[0] != '\0')
- sw_error=1;
- else
- done = 1;
- break;
- }
- digit_or_dot =
- (*s=='.' || isdigit(Py_CHARMASK(*s)));
- if (done||!digit_or_dot) {
- sw_error=1;
- break;
- }
- errno = 0;
- PyFPE_START_PROTECT("strtod", return 0)
- z = strtod(s, &end) ;
- PyFPE_END_PROTECT(z)
- if (errno != 0) {
- sprintf(buffer,
- "float() out of range: %.150s", s);
- PyErr_SetString(
- PyExc_ValueError,
- buffer);
- return NULL;
- }
- s=end;
- if (*s=='J' || *s=='j') {
-
- break;
- }
- if (got_re) {
- sw_error=1;
- break;
- }
-
- /* accept a real part */
- x=sign*z;
- got_re=1;
- if (got_im) done=1;
- z = -1.0;
- sign = 1;
- break;
-
- } /* end of switch */
-
- } while (*s!='\0' && !sw_error);
-
- if (sw_error) {
- PyErr_SetString(PyExc_ValueError,
- "complex() arg is a malformed string");
- return NULL;
- }
-
- return PyComplex_FromDoubles(x,y);
- }
-
- static PyObject *
- builtin_complex(PyObject *self, PyObject *args)
- {
- PyObject *r, *i, *tmp;
- PyNumberMethods *nbr, *nbi = NULL;
- Py_complex cr, ci;
- int own_r = 0;
-
- i = NULL;
- if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
- return NULL;
- if (PyString_Check(r) || PyUnicode_Check(r))
- return complex_from_string(r);
- if ((nbr = r->ob_type->tp_as_number) == NULL ||
- nbr->nb_float == NULL ||
- (i != NULL &&
- ((nbi = i->ob_type->tp_as_number) == NULL ||
- nbi->nb_float == NULL))) {
- PyErr_SetString(PyExc_TypeError,
- "complex() arg can't be converted to complex");
- return NULL;
- }
- /* XXX Hack to support classes with __complex__ method */
- if (PyInstance_Check(r)) {
- static PyObject *complexstr;
- PyObject *f;
- if (complexstr == NULL) {
- complexstr = PyString_InternFromString("__complex__");
- if (complexstr == NULL)
- return NULL;
- }
- f = PyObject_GetAttr(r, complexstr);
- if (f == NULL)
- PyErr_Clear();
- else {
- PyObject *args = Py_BuildValue("()");
- if (args == NULL)
- return NULL;
- r = PyEval_CallObject(f, args);
- Py_DECREF(args);
- Py_DECREF(f);
- if (r == NULL)
- return NULL;
- own_r = 1;
- }
- }
- if (PyComplex_Check(r)) {
- cr = ((PyComplexObject*)r)->cval;
- if (own_r) {
- Py_DECREF(r);
- }
- }
- else {
- tmp = PyNumber_Float(r);
- if (own_r) {
- Py_DECREF(r);
- }
- if (tmp == NULL)
- return NULL;
- if (!PyFloat_Check(tmp)) {
- PyErr_SetString(PyExc_TypeError,
- "float(r) didn't return a float");
- Py_DECREF(tmp);
- return NULL;
- }
- cr.real = PyFloat_AsDouble(tmp);
- Py_DECREF(tmp);
- cr.imag = 0.0;
- }
- if (i == NULL) {
- ci.real = 0.0;
- ci.imag = 0.0;
- }
- else if (PyComplex_Check(i))
- ci = ((PyComplexObject*)i)->cval;
- else {
- tmp = (*nbi->nb_float)(i);
- if (tmp == NULL)
- return NULL;
- ci.real = PyFloat_AsDouble(tmp);
- Py_DECREF(tmp);
- ci.imag = 0.;
- }
- cr.real -= ci.imag;
- cr.imag += ci.real;
- return PyComplex_FromCComplex(cr);
- }
-
- static char complex_doc[] =
- "complex(real[, imag]) -> complex number\n\
- \n\
- Create a complex number from a real part and an optional imaginary part.\n\
- This is equivalent to (real + imag*1j) where imag defaults to 0.";
-
-
- #endif
-
- static PyObject *
builtin_dir(PyObject *self, PyObject *args)
{
--- 372,376 ----
***************
*** 2001,2007 ****
{"coerce", builtin_coerce, 1, coerce_doc},
{"compile", builtin_compile, 1, compile_doc},
- #ifndef WITHOUT_COMPLEX
- {"complex", builtin_complex, 1, complex_doc},
- #endif
{"delattr", builtin_delattr, 1, delattr_doc},
{"dir", builtin_dir, 1, dir_doc},
--- 1730,1733 ----
***************
*** 2038,2042 ****
{"setattr", builtin_setattr, 1, setattr_doc},
{"slice", builtin_slice, 1, slice_doc},
- {"unicode", builtin_unicode, 1, unicode_doc},
{"unichr", builtin_unichr, 1, unichr_doc},
{"vars", builtin_vars, 1, vars_doc},
--- 1764,1767 ----
***************
*** 2068,2071 ****
--- 1793,1801 ----
Py_NotImplemented) < 0)
return NULL;
+ #ifndef WITHOUT_COMPLEX
+ if (PyDict_SetItemString(dict, "complex",
+ (PyObject *) &PyComplex_Type) < 0)
+ return NULL;
+ #endif
if (PyDict_SetItemString(dict, "dictionary",
(PyObject *) &PyDict_Type) < 0)
***************
*** 2089,2092 ****
--- 1819,1825 ----
return NULL;
if (PyDict_SetItemString(dict, "type", (PyObject *) &PyType_Type) < 0)
+ return NULL;
+ if (PyDict_SetItemString(dict, "unicode",
+ (PyObject *) &PyUnicode_Type) < 0)
return NULL;
debug = PyInt_FromLong(Py_OptimizeFlag == 0);