示例#1
0
static PyObject *
pint_getquoted(pintObject *self, PyObject *args)
{
    PyObject *res = NULL;

    /* Convert subclass to int to handle IntEnum and other subclasses
     * whose str() is not the number. */
    if (PyLong_CheckExact(self->wrapped)
#if PY_MAJOR_VERSION < 2
        || PyInt_CheckExact(self->wrapped)
#endif
       ) {
        res = PyObject_Str(self->wrapped);
    } else {
        PyObject *tmp;
        if (!(tmp = PyObject_CallFunctionObjArgs(
                (PyObject *)&PyLong_Type, self->wrapped, NULL))) {
            goto exit;
        }
        res = PyObject_Str(tmp);
        Py_DECREF(tmp);
    }

    if (!res) {
        goto exit;
    }

#if PY_MAJOR_VERSION > 2
    /* unicode to bytes in Py3 */
    {
        PyObject *tmp = PyUnicode_AsUTF8String(res);
        Py_DECREF(res);
        if (!(res = tmp)) {
            goto exit;
        }
    }
#endif

    if ('-' == Bytes_AS_STRING(res)[0]) {
        /* Prepend a space in front of negative numbers (ticket #57) */
        PyObject *tmp;
        if (!(tmp = Bytes_FromString(" "))) {
            Py_DECREF(res);
            res = NULL;
            goto exit;
        }
        Bytes_ConcatAndDel(&tmp, res);
        if (!(res = tmp)) {
            goto exit;
        }
    }

exit:
    return res;
}
static PyObject *
pfloat_getquoted(pfloatObject *self, PyObject *args)
{
    PyObject *rv;
    double n = PyFloat_AsDouble(self->wrapped);
    if (isnan(n))
        rv = Bytes_FromString("'NaN'::float");
    else if (isinf(n)) {
        if (n > 0)
            rv = Bytes_FromString("'Infinity'::float");
        else
            rv = Bytes_FromString("'-Infinity'::float");
    }
    else {
        if (!(rv = PyObject_Repr(self->wrapped))) {
            goto exit;
        }

#if PY_MAJOR_VERSION > 2
        /* unicode to bytes in Py3 */
        {
            PyObject *tmp = PyUnicode_AsUTF8String(rv);
            Py_DECREF(rv);
            if (!(rv = tmp)) {
                goto exit;
            }
        }
#endif

        if ('-' == Bytes_AS_STRING(rv)[0]) {
            /* Prepend a space in front of negative numbers (ticket #57) */
            PyObject *tmp;
            if (!(tmp = Bytes_FromString(" "))) {
                Py_DECREF(rv);
                rv = NULL;
                goto exit;
            }
            Bytes_ConcatAndDel(&tmp, rv);
            if (!(rv = tmp)) {
                goto exit;
            }
        }
    }

exit:
    return rv;
}