コード例 #1
0
ファイル: typecast.c プロジェクト: fabioz/coev
static PyObject *
typecast_call(PyObject *obj, PyObject *args, PyObject *kwargs)
{
    PyObject *string, *cursor;

    if (!PyArg_ParseTuple(args, "OO", &string, &cursor)) {
        return NULL;
    }

    return typecast_cast(obj,
                         PyString_AsString(string), PyString_Size(string),
                         cursor);
}
コード例 #2
0
static PyObject *
typecast_call(PyObject *obj, PyObject *args, PyObject *kwargs)
{
    const char *string;
    Py_ssize_t length;
    PyObject *cursor;

    if (!PyArg_ParseTuple(args, "z#O", &string, &length, &cursor)) {
        return NULL;
    }

    // If the string is not a string but a None value we're being called
    // from a Python-defined caster.
    if (!string) {
        Py_RETURN_NONE;
    }

    return typecast_cast(obj, string, length, cursor);
}
コード例 #3
0
ファイル: typecast_array.c プロジェクト: Instagram/psycopg2
RAISES_NEG static int
typecast_array_scan(const char *str, Py_ssize_t strlength,
                    PyObject *curs, PyObject *base, PyObject *array)
{
    int state, quotes = 0;
    Py_ssize_t length = 0, pos = 0;
    char *token;

    PyObject *stack[MAX_DIMENSIONS];
    size_t stack_index = 0;

    while (1) {
        token = NULL;
        state = typecast_array_tokenize(str, strlength,
                                    &pos, &token, &length, &quotes);
        Dprintf("typecast_array_scan: state = %d,"
                " length = " FORMAT_CODE_PY_SSIZE_T ", token = '%s'",
                state, length, token);
        if (state == ASCAN_TOKEN || state == ASCAN_QUOTED) {
            PyObject *obj;
            if (!quotes && length == 4
                && (token[0] == 'n' || token[0] == 'N')
                && (token[1] == 'u' || token[1] == 'U')
                && (token[2] == 'l' || token[2] == 'L')
                && (token[3] == 'l' || token[3] == 'L'))
            {
                obj = typecast_cast(base, NULL, 0, curs);
            } else {
                obj = typecast_cast(base, token, length, curs);
            }

            /* before anything else we free the memory */
            if (state == ASCAN_QUOTED) PyMem_Free(token);
            if (obj == NULL) return -1;

            PyList_Append(array, obj);
            Py_DECREF(obj);
        }

        else if (state == ASCAN_BEGIN) {
            PyObject *sub = PyList_New(0);
            if (sub == NULL) return -1;

            PyList_Append(array, sub);
            Py_DECREF(sub);

            if (stack_index == MAX_DIMENSIONS) {
                PyErr_SetString(DataError, "excessive array dimensions");
                return -1;
            }

            stack[stack_index++] = array;
            array = sub;
        }

        else if (state == ASCAN_ERROR) {
            return -1;
        }

        else if (state == ASCAN_END) {
            if (stack_index == 0) {
                PyErr_SetString(DataError, "unbalanced braces in array");
                return -1;
            }
            array = stack[--stack_index];
        }

        else if (state ==  ASCAN_EOF)
            break;
    }

    return 0;
}