diff options
author | Eric Smith <eric@trueblade.com> | 2008-02-10 01:36:53 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2008-02-10 01:36:53 (GMT) |
commit | 5e527ebee1580f052fc53aacabe3906ffcdd4805 (patch) | |
tree | b77c8ca7e6fcd8a2cd31c184c764a79633e2fb85 /Objects/longobject.c | |
parent | 14a1b8cc469375577ea42152bfe559fa52d6ece7 (diff) | |
download | cpython-5e527ebee1580f052fc53aacabe3906ffcdd4805.zip cpython-5e527ebee1580f052fc53aacabe3906ffcdd4805.tar.gz cpython-5e527ebee1580f052fc53aacabe3906ffcdd4805.tar.bz2 |
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.
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r-- | Objects/longobject.c | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index 9fb5832..3ee2992 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -35,7 +35,6 @@ static PyLongObject *long_normalize(PyLongObject *); static PyLongObject *mul1(PyLongObject *, wdigit); static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit); static PyLongObject *divrem1(PyLongObject *, digit, digit *); -static PyObject *long_format(PyObject *aa, int base, int addL); #define SIGCHECK(PyTryBlock) \ if (--_Py_Ticker < 0) { \ @@ -1140,7 +1139,7 @@ muladd1(PyLongObject *a, wdigit n, wdigit extra) /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient in pout, and returning the remainder. pin and pout point at the LSD. It's OK for pin == pout on entry, which saves oodles of mallocs/frees in - long_format, but that should be done with great care since longs are + _PyLong_Format, but that should be done with great care since longs are immutable. */ static digit @@ -1178,12 +1177,13 @@ divrem1(PyLongObject *a, digit n, digit *prem) return long_normalize(z); } -/* Convert a long int object to a string, using a given conversion base. - Return a string object. - If base is 8 or 16, add the proper prefix '0' or '0x'. */ - -static PyObject * -long_format(PyObject *aa, int base, int addL) +/* Convert the long to a string object with given base, + appending a base prefix of 0[box] if base is 2, 8 or 16. + Add a trailing "L" if addL is non-zero. + If newstyle is zero, then use the pre-2.6 behavior of octal having + a leading "0", instead of the prefix "0o" */ +PyAPI_FUNC(PyObject *) +_PyLong_Format(PyObject *aa, int base, int addL, int newstyle) { register PyLongObject *a = (PyLongObject *)aa; PyStringObject *str; @@ -1309,9 +1309,18 @@ long_format(PyObject *aa, int base, int addL) Py_DECREF(scratch); } - if (base == 8) { - if (size_a != 0) + if (base == 2) { + *--p = 'b'; + *--p = '0'; + } + else if (base == 8) { + if (newstyle) { + *--p = 'o'; *--p = '0'; + } + else + if (size_a != 0) + *--p = '0'; } else if (base == 16) { *--p = 'x'; @@ -1888,13 +1897,13 @@ long_dealloc(PyObject *v) static PyObject * long_repr(PyObject *v) { - return long_format(v, 10, 1); + return _PyLong_Format(v, 10, 1, 0); } static PyObject * long_str(PyObject *v) { - return long_format(v, 10, 0); + return _PyLong_Format(v, 10, 0, 0); } static int @@ -3268,13 +3277,13 @@ long_float(PyObject *v) static PyObject * long_oct(PyObject *v) { - return long_format(v, 8, 1); + return _PyLong_Format(v, 8, 1, 0); } static PyObject * long_hex(PyObject *v) { - return long_format(v, 16, 1); + return _PyLong_Format(v, 16, 1, 0); } static PyObject * |