From 226ae6ca122f814dabdc40178c7b9656caf729c2 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Tue, 12 Oct 1999 19:54:53 +0000 Subject: Mainlining the string_methods branch. See branch revision log messages for specific changes. --- Python/bltinmodule.c | 109 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 42 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 9bb8784b0f..c220d84100 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -811,24 +811,6 @@ The globals and locals are dictionaries, defaulting to the current\n\ globals and locals. If only globals is given, locals defaults to it."; -static PyObject * -builtin_float(self, args) - PyObject *self; - PyObject *args; -{ - PyObject *v; - - if (!PyArg_ParseTuple(args, "O:float", &v)) - return NULL; - return PyNumber_Float(v); -} - -static char float_doc[] = -"float(x) -> floating point number\n\ -\n\ -Convert a string or number to a floating point number, if possible."; - - static PyObject * builtin_getattr(self, args) PyObject *self; @@ -1251,17 +1233,79 @@ builtin_int(self, args) PyObject *args; { PyObject *v; + int base = -909; /* unlikely! */ - if (!PyArg_ParseTuple(args, "O:int", &v)) + if (!PyArg_ParseTuple(args, "O|i:int", &v, &base)) return NULL; - return PyNumber_Int(v); + if (base == -909) + return PyNumber_Int(v); + else if (!PyString_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "can't convert non-string with explicit base"); + return NULL; + } + return PyInt_FromString(PyString_AS_STRING(v), NULL, base); } static char int_doc[] = -"int(x) -> integer\n\ +"int(x[, base]) -> integer\n\ +\n\ +Convert a string or number to an integer, if possible. A floating point\n\ +argument will be truncated towards zero (this does not include a string\n\ +representation of a floating point number!) When converting a string, use\n\ +the optional base. It is an error to supply a base when converting a\n\ +non-string."; + + +static PyObject * +builtin_long(self, args) + PyObject *self; + PyObject *args; +{ + PyObject *v; + int base = -909; /* unlikely! */ + + if (!PyArg_ParseTuple(args, "O|i:long", &v, &base)) + return NULL; + if (base == -909) + return PyNumber_Long(v); + else if (!PyString_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "can't convert non-string with explicit base"); + return NULL; + } + return PyLong_FromString(PyString_AS_STRING(v), NULL, base); +} + +static char long_doc[] = +"long(x) -> long integer\n\ +long(x, base) -> long integer\n\ +\n\ +Convert a string or number to a long integer, if possible. A floating\n\ +point argument will be truncated towards zero (this does not include a\n\ +string representation of a floating point number!) When converting a\n\ +string, use the given base. It is an error to supply a base when\n\ +converting a non-string."; + + +static PyObject * +builtin_float(self, args) + PyObject *self; + PyObject *args; +{ + PyObject *v; + + if (!PyArg_ParseTuple(args, "O:float", &v)) + return NULL; + if (PyString_Check(v)) + return PyFloat_FromString(v, NULL); + return PyNumber_Float(v); +} + +static char float_doc[] = +"float(x) -> floating point number\n\ \n\ -Convert a string or number to an integer, if possible.\n\ -A floating point argument will be truncated towards zero."; +Convert a string or number to a floating point number, if possible."; static PyObject * @@ -1351,25 +1395,6 @@ static char locals_doc[] = Return the dictionary containing the current scope's local variables."; -static PyObject * -builtin_long(self, args) - PyObject *self; - PyObject *args; -{ - PyObject *v; - - if (!PyArg_ParseTuple(args, "O:long", &v)) - return NULL; - return PyNumber_Long(v); -} - -static char long_doc[] = -"long(x) -> long integer\n\ -\n\ -Convert a string or number to a long integer, if possible.\n\ -A floating point argument will be truncated towards zero."; - - static PyObject * min_max(args, sign) PyObject *args; -- cgit v1.2.1