Author: guido.van.rossum
Date: Fri Nov  2 23:16:14 2007
New Revision: 58811

Modified:
   python/branches/py3k-pep3137/Objects/longobject.c
Log:
Make int(x, base) work for isinstance(x, (str, bytes, buffer)).


Modified: python/branches/py3k-pep3137/Objects/longobject.c
==============================================================================
--- python/branches/py3k-pep3137/Objects/longobject.c   (original)
+++ python/branches/py3k-pep3137/Objects/longobject.c   Fri Nov  2 23:16:14 2007
@@ -3462,11 +3462,19 @@
                return PyLong_FromLong(0L);
        if (base == -909)
                return PyNumber_Long(x);
-       else if (PyBytes_Check(x)) {
+       else if (PyUnicode_Check(x))
+               return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
+                                         PyUnicode_GET_SIZE(x),
+                                         base);
+       else if (PyBytes_Check(x) || PyString_Check(x)) {
                /* Since PyLong_FromString doesn't have a length parameter,
                 * check here for possible NULs in the string. */
-               char *string = PyBytes_AS_STRING(x);
-               int size = PyBytes_GET_SIZE(x);
+               char *string;
+               int size = Py_Size(x);
+               if (PyBytes_Check(x))
+                       string = PyBytes_AS_STRING(x);
+               else
+                       string = PyString_AS_STRING(x);
                if (strlen(string) != size) {
                        /* We only see this if there's a null byte in x,
                           x is a str8 or a bytes, *and* a base is given. */
@@ -3477,10 +3485,6 @@
                }
                return PyLong_FromString(string, NULL, base);
        }
-       else if (PyUnicode_Check(x))
-               return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
-                                         PyUnicode_GET_SIZE(x),
-                                         base);
        else {
                PyErr_SetString(PyExc_TypeError,
                        "int() can't convert non-string with explicit base");
_______________________________________________
Python-3000-checkins mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000-checkins

Reply via email to