~pythonregexp2.7/python/issue2636-01

« back to all changes in this revision

Viewing changes to Objects/longobject.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-06-09 14:37:21 UTC
  • mfrom: (39022.1.14 Regexp-2.6)
  • Revision ID: darklord@timehorse.com-20080609143721-bj0g1mwta28038da
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
#include "Python.h"
8
8
#include "longintrepr.h"
9
 
#include "formatter_string.h"
10
9
 
11
10
#include <ctype.h>
12
11
 
3414
3413
 
3415
3414
        if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
3416
3415
                return NULL;
3417
 
        if (PyString_Check(format_spec))
3418
 
                return string_long__format__(self, args);
 
3416
        if (PyBytes_Check(format_spec))
 
3417
                return _PyLong_FormatAdvanced(self,
 
3418
                                              PyBytes_AS_STRING(format_spec),
 
3419
                                              PyBytes_GET_SIZE(format_spec));
3419
3420
        if (PyUnicode_Check(format_spec)) {
3420
3421
                /* Convert format_spec to a str */
3421
 
                PyObject *result = NULL;
3422
 
                PyObject *newargs = NULL;
3423
 
                PyObject *string_format_spec = NULL;
3424
 
 
3425
 
                string_format_spec = PyObject_Str(format_spec);
3426
 
                if (string_format_spec == NULL)
3427
 
                        goto done;
3428
 
 
3429
 
                newargs = Py_BuildValue("(O)", string_format_spec);
3430
 
                if (newargs == NULL)
3431
 
                        goto done;
3432
 
 
3433
 
                result = string_long__format__(self, newargs);
3434
 
 
3435
 
                done:
3436
 
                Py_XDECREF(string_format_spec);
3437
 
                Py_XDECREF(newargs);
 
3422
                PyObject *result;
 
3423
                PyObject *str_spec = PyObject_Str(format_spec);
 
3424
 
 
3425
                if (str_spec == NULL)
 
3426
                        return NULL;
 
3427
 
 
3428
                result = _PyLong_FormatAdvanced(self,
 
3429
                                                PyBytes_AS_STRING(str_spec),
 
3430
                                                PyBytes_GET_SIZE(str_spec));
 
3431
 
 
3432
                Py_DECREF(str_spec);
3438
3433
                return result;
3439
3434
        }
3440
3435
        PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
3441
3436
        return NULL;
3442
3437
}
3443
3438
 
 
3439
static PyObject *
 
3440
long_sizeof(PyLongObject *v)
 
3441
{
 
3442
        Py_ssize_t res;
 
3443
 
 
3444
        res = sizeof(PyLongObject) + abs(v->ob_size) * sizeof(digit);
 
3445
        if (v->ob_size != 0)
 
3446
                res -=  sizeof(digit);
 
3447
        return PyInt_FromSsize_t(res);
 
3448
}
 
3449
 
3444
3450
#if 0
3445
3451
static PyObject *
3446
3452
long_is_finite(PyObject *v)
3460
3466
         "Truncating an Integral returns itself."},
3461
3467
        {"__getnewargs__",      (PyCFunction)long_getnewargs,   METH_NOARGS},
3462
3468
        {"__format__", (PyCFunction)long__format__, METH_VARARGS},
 
3469
        {"__sizeof__",  (PyCFunction)long_sizeof, METH_NOARGS,
 
3470
         "Returns size in memory, in bytes"},
3463
3471
        {NULL,          NULL}           /* sentinel */
3464
3472
};
3465
3473