Added PyNumber_ToBase and supporting routines _PyInt_Format and
_PyLong_Format. In longobject.c, changed long_format to
_PyLong_Format. In intobject.c, changed uses of PyOS_snprintf to
_PyInt_Format instead.
_PyLong_Format is similar to py3k's routine of the same name, except
it has 2 additional parameters: addL and newstyle. addL was existing
in long_format, and controls adding the trailing "L". This is
unneeded in py3k. newstyle is used to control whether octal prepends
"0" (the pre-2.6 style), or "0o" (the 3.0 sytle).
PyNumber_ToBase is needed for PEP 3127 (Integer Literal Support and
Syntax) and PEP 3101 (Advanced String Formatting).
This changeset does not need merging into py3k.
diff --git a/Objects/abstract.c b/Objects/abstract.c
index a3e159a..89a78c6 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1262,6 +1262,25 @@
return PyFloat_FromString(o, NULL);
}
+PyObject *
+PyNumber_ToBase(PyObject *n, int base)
+{
+ PyObject *res = NULL;
+ PyObject *index = PyNumber_Index(n);
+
+ if (!index)
+ return NULL;
+ if (PyLong_Check(index))
+ res = _PyLong_Format(index, base, 0, 1);
+ else if (PyInt_Check(index))
+ res = _PyInt_Format((PyIntObject*)index, base, 1);
+ else
+ assert("PyNumber_ToBase: not long or int");
+ Py_DECREF(index);
+ return res;
+}
+
+
/* Operations on sequences */
int