diff Python/bltinmodule.c @ 11383:aca7b5eaf5e8 legacy-trunk

Mainlining the string_methods branch. See branch revision log messages for specific changes.
author Barry Warsaw <barry@python.org>
date Tue, 12 Oct 1999 19:54:53 +0000
parents 41a46978a444
children 0ec803cbd872
line wrap: on
line diff
--- a/Python/bltinmodule.c	Tue Oct 12 16:20:13 1999 +0000
+++ b/Python/bltinmodule.c	Tue Oct 12 19:54:53 1999 +0000
@@ -812,24 +812,6 @@
 
 
 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;
 	PyObject *args;
@@ -1251,17 +1233,79 @@
 	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.\n\
-A floating point argument will be truncated towards zero.";
+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 a floating point number, if possible.";
 
 
 static PyObject *
@@ -1352,25 +1396,6 @@
 
 
 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;
 	int sign;