Exemplo n.º 1
0
PyObject *
_PyObject_Str(PyObject *v)
{
	PyObject *res;
	int type_ok;
	if (v == NULL)
		return PyString_FromString("<NULL>");
	if (PyString_CheckExact(v)) {
		Py_INCREF(v);
		return v;
	}
#ifdef Py_USING_UNICODE
	if (PyUnicode_CheckExact(v)) {
		Py_INCREF(v);
		return v;
	}
#endif
	if (v->ob_type->tp_str == NULL)
		return PyObject_Repr(v);

	res = (*v->ob_type->tp_str)(v);
	if (res == NULL)
		return NULL;
	type_ok = PyString_Check(res);
#ifdef Py_USING_UNICODE
	type_ok = type_ok || PyUnicode_Check(res);
#endif
	if (!type_ok) {
		PyErr_Format(PyExc_TypeError,
			     "__str__ returned non-string (type %.200s)",
			     res->ob_type->tp_name);
		Py_DECREF(res);
		return NULL;
	}
	return res;
}
Exemplo n.º 2
0
/* pyautocorrs16(window0, window1, data, offset); */
static PyObject* pyautocorrs16(PyObject* self, PyObject* args)
{
    int window0, window1;
    PyObject* data;
    int offset;

    if (!PyArg_ParseTuple(args, "iiOi", &window0, &window1, &data, &offset)) {
	return NULL;
    }

    if (!PyString_CheckExact(data)) {
	PyErr_SetString(PyExc_TypeError, "Must be string");
	return NULL;
    }

    int length = PyString_Size(data) / sizeof(short);
    if (window0 < 0 || window1 < 0 || 
	offset < 0 || length < offset+window0 || length < offset+window1) {
	PyErr_SetString(PyExc_ValueError, "Invalid offset/window");
	return NULL;
    }

    short* seq = (short*)PyString_AsString(data);
    double smax = 0;
    int wmax = autocorrs16(&smax, window0, window1, length-offset, &seq[offset]);
  
    PyObject* tuple;
    {
	PyObject* v1 = PyInt_FromLong(wmax);
	PyObject* v2 = PyFloat_FromDouble(smax);
	tuple = PyTuple_Pack(2, v1, v2);
	Py_DECREF(v1);
	Py_DECREF(v2);
    }
    return tuple;
}
Exemplo n.º 3
0
static PyObject *py_hex_to_sha(PyObject *self, PyObject *py_hexsha)
{
	char *hexsha;
	char sha[20];
	int i;

	if (!PyString_CheckExact(py_hexsha)) {
		PyErr_SetString(PyExc_TypeError, "hex sha is not a string");
		return NULL;
	}

	if (PyString_Size(py_hexsha) != 40) {
		PyErr_SetString(PyExc_ValueError, "hex sha is not 40 bytes long");
		return NULL;
	}

	hexsha = PyString_AsString(py_hexsha);

	for (i = 0; i < 20; i++) {
		sha[i] = (hexbyte(hexsha[i*2]) << 4) + hexbyte(hexsha[i*2+1]);
	}

	return PyString_FromStringAndSize(sha, 20);
}
Exemplo n.º 4
0
NUITKA_MAY_BE_UNUSED static bool BINARY_OPERATION_ADD_INPLACE( PyObject **operand1, PyObject *operand2 )
{
    assert( operand1 );
    CHECK_OBJECT( *operand1 );
    CHECK_OBJECT( operand2 );

#if PYTHON_VERSION < 300
    // Something similar for Python3 should exist too.
    if ( PyInt_CheckExact( *operand1 ) && PyInt_CheckExact( operand2 ) )
    {
        long a, b, i;

        a = PyInt_AS_LONG( *operand1 );
        b = PyInt_AS_LONG( operand2 );

        i = a + b;

        // Detect overflow, in which case, a "long" object would have to be
        // created, which we won't handle here. TODO: Add an else for that
        // case.
        if (likely(!( (i^a) < 0 && (i^b) < 0 ) ))
        {
            PyObject *result = PyInt_FromLong( i );
            Py_DECREF( *operand1 );

            *operand1 = result;

            return true;
        }
    }
#endif

#if PYTHON_VERSION < 300
    if ( Py_REFCNT( *operand1 ) == 1 )
    {
        // We more or less own the operand, so we might re-use its storage and
        // execute stuff in-place.
        if ( PyString_CheckExact( *operand1 ) &&
             !PyString_CHECK_INTERNED( *operand1 ) &&
             PyString_CheckExact( operand2 ) )
        {
            return STRING_ADD_INCREMENTAL( operand1, operand2 );
        }
        else if ( PyFloat_CheckExact( *operand1 ) &&
                  PyFloat_CheckExact( operand2 ) )
        {
            return FLOAT_ADD_INCREMENTAL( operand1, operand2 );

        }
    }

    // Strings are to be treated differently.
    if ( PyString_CheckExact( *operand1 ) && PyString_CheckExact( operand2 ) )
    {
        PyString_Concat( operand1, operand2 );
        return !ERROR_OCCURRED();
    }
#else
    if ( Py_REFCNT( *operand1 ) == 1 )
    {
        // We more or less own the operand, so we might re-use its storage and
        // execute stuff in-place.
        if ( PyUnicode_CheckExact( *operand1 ) &&
             !PyUnicode_CHECK_INTERNED( *operand1 ) &&
             PyUnicode_CheckExact( operand2 ) )
        {
            return UNICODE_ADD_INCREMENTAL( operand1, operand2 );
        }
        else if ( PyFloat_CheckExact( *operand1 ) &&
                  PyFloat_CheckExact( operand2 ) )
        {
            return FLOAT_ADD_INCREMENTAL( operand1, operand2 );
        }
    }

    // Strings are to be treated differently.
    if ( PyUnicode_CheckExact( *operand1 ) && PyUnicode_CheckExact( operand2 ) )
    {
        PyObject *result = PyUnicode_Concat( *operand1, operand2 );

        if (unlikely( result == NULL ))
        {
            return false;
        }

        Py_DECREF( *operand1 );
        *operand1 = result;

        return true;
    }
#endif

    PyObject *result = PyNumber_InPlaceAdd( *operand1, operand2 );

    if (unlikely( result == NULL ))
    {
        return false;
    }

    // We got an object handed, that we have to release.
    Py_DECREF( *operand1 );

    // That's our return value then. As we use a dedicated variable, it's
    // OK that way.
    *operand1 = result;

    return true;
}
Exemplo n.º 5
0
static int __Pyx_ParseOptionalKeywords(
    PyObject *kwds,
    PyObject **argnames[],
    PyObject *kwds2,
    PyObject *values[],
    Py_ssize_t num_pos_args,
    const char* function_name)
{
    PyObject *key = 0, *value = 0;
    Py_ssize_t pos = 0;
    PyObject*** name;
    PyObject*** first_kw_arg = argnames + num_pos_args;

    while (PyDict_Next(kwds, &pos, &key, &value)) {
        name = first_kw_arg;
        while (*name && (**name != key)) name++;
        if (*name) {
            values[name-argnames] = value;
        } else {
            #if PY_MAJOR_VERSION < 3
            if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) {
            #else
            if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key))) {
            #endif
                goto invalid_keyword_type;
            } else {
                for (name = first_kw_arg; *name; name++) {
                    #if PY_MAJOR_VERSION >= 3
                    if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
                        PyUnicode_Compare(**name, key) == 0) break;
                    #else
                    if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
                        _PyString_Eq(**name, key)) break;
                    #endif
                }
                if (*name) {
                    values[name-argnames] = value;
                } else {
                    /* unexpected keyword found */
                    for (name=argnames; name != first_kw_arg; name++) {
                        if (**name == key) goto arg_passed_twice;
                        #if PY_MAJOR_VERSION >= 3
                        if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
                            PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice;
                        #else
                        if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
                            _PyString_Eq(**name, key)) goto arg_passed_twice;
                        #endif
                    }
                    if (kwds2) {
                        if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
                    } else {
                        goto invalid_keyword;
                    }
                }
            }
        }
    }
    return 0;
arg_passed_twice:
    __Pyx_RaiseDoubleKeywordsError(function_name, **name);
    goto bad;
invalid_keyword_type:
    PyErr_Format(PyExc_TypeError,
        "%s() keywords must be strings", function_name);
    goto bad;
invalid_keyword:
    PyErr_Format(PyExc_TypeError,
    #if PY_MAJOR_VERSION < 3
        "%s() got an unexpected keyword argument '%s'",
        function_name, PyString_AsString(key));
    #else
        "%s() got an unexpected keyword argument '%U'",
        function_name, key);
    #endif
bad:
    return -1;
}

static CYTHON_INLINE unsigned char __Pyx_PyInt_AsUnsignedChar(PyObject* x) {
    const unsigned char neg_one = (unsigned char)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(unsigned char) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(unsigned char)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to unsigned char" :
                    "value too large to convert to unsigned char");
            }
            return (unsigned char)-1;
        }
        return (unsigned char)val;
    }
    return (unsigned char)__Pyx_PyInt_AsUnsignedLong(x);
}

static CYTHON_INLINE unsigned short __Pyx_PyInt_AsUnsignedShort(PyObject* x) {
    const unsigned short neg_one = (unsigned short)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(unsigned short) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(unsigned short)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to unsigned short" :
                    "value too large to convert to unsigned short");
            }
            return (unsigned short)-1;
        }
        return (unsigned short)val;
    }
    return (unsigned short)__Pyx_PyInt_AsUnsignedLong(x);
}

static CYTHON_INLINE unsigned int __Pyx_PyInt_AsUnsignedInt(PyObject* x) {
    const unsigned int neg_one = (unsigned int)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(unsigned int) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(unsigned int)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to unsigned int" :
                    "value too large to convert to unsigned int");
            }
            return (unsigned int)-1;
        }
        return (unsigned int)val;
    }
    return (unsigned int)__Pyx_PyInt_AsUnsignedLong(x);
}

static CYTHON_INLINE char __Pyx_PyInt_AsChar(PyObject* x) {
    const char neg_one = (char)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(char) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(char)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to char" :
                    "value too large to convert to char");
            }
            return (char)-1;
        }
        return (char)val;
    }
    return (char)__Pyx_PyInt_AsLong(x);
}

static CYTHON_INLINE short __Pyx_PyInt_AsShort(PyObject* x) {
    const short neg_one = (short)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(short) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(short)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to short" :
                    "value too large to convert to short");
            }
            return (short)-1;
        }
        return (short)val;
    }
    return (short)__Pyx_PyInt_AsLong(x);
}

static CYTHON_INLINE int __Pyx_PyInt_AsInt(PyObject* x) {
    const int neg_one = (int)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(int) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(int)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to int" :
                    "value too large to convert to int");
            }
            return (int)-1;
        }
        return (int)val;
    }
    return (int)__Pyx_PyInt_AsLong(x);
}

static CYTHON_INLINE signed char __Pyx_PyInt_AsSignedChar(PyObject* x) {
    const signed char neg_one = (signed char)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(signed char) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(signed char)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to signed char" :
                    "value too large to convert to signed char");
            }
            return (signed char)-1;
        }
        return (signed char)val;
    }
    return (signed char)__Pyx_PyInt_AsSignedLong(x);
}

static CYTHON_INLINE signed short __Pyx_PyInt_AsSignedShort(PyObject* x) {
    const signed short neg_one = (signed short)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(signed short) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(signed short)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to signed short" :
                    "value too large to convert to signed short");
            }
            return (signed short)-1;
        }
        return (signed short)val;
    }
    return (signed short)__Pyx_PyInt_AsSignedLong(x);
}

static CYTHON_INLINE signed int __Pyx_PyInt_AsSignedInt(PyObject* x) {
    const signed int neg_one = (signed int)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(signed int) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(signed int)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to signed int" :
                    "value too large to convert to signed int");
            }
            return (signed int)-1;
        }
        return (signed int)val;
    }
    return (signed int)__Pyx_PyInt_AsSignedLong(x);
}

static CYTHON_INLINE unsigned long __Pyx_PyInt_AsUnsignedLong(PyObject* x) {
    const unsigned long neg_one = (unsigned long)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_VERSION_HEX < 0x03000000
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to unsigned long");
            return (unsigned long)-1;
        }
        return (unsigned long)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to unsigned long");
                return (unsigned long)-1;
            }
            return PyLong_AsUnsignedLong(x);
        } else {
            return PyLong_AsLong(x);
        }
    } else {
        unsigned long val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (unsigned long)-1;
        val = __Pyx_PyInt_AsUnsignedLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject* x) {
    const unsigned PY_LONG_LONG neg_one = (unsigned PY_LONG_LONG)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_VERSION_HEX < 0x03000000
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to unsigned PY_LONG_LONG");
            return (unsigned PY_LONG_LONG)-1;
        }
        return (unsigned PY_LONG_LONG)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to unsigned PY_LONG_LONG");
                return (unsigned PY_LONG_LONG)-1;
            }
            return PyLong_AsUnsignedLongLong(x);
        } else {
            return PyLong_AsLongLong(x);
        }
    } else {
        unsigned PY_LONG_LONG val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (unsigned PY_LONG_LONG)-1;
        val = __Pyx_PyInt_AsUnsignedLongLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

static CYTHON_INLINE long __Pyx_PyInt_AsLong(PyObject* x) {
    const long neg_one = (long)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_VERSION_HEX < 0x03000000
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to long");
            return (long)-1;
        }
        return (long)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to long");
                return (long)-1;
            }
            return PyLong_AsUnsignedLong(x);
        } else {
            return PyLong_AsLong(x);
        }
    } else {
        long val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (long)-1;
        val = __Pyx_PyInt_AsLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_AsLongLong(PyObject* x) {
    const PY_LONG_LONG neg_one = (PY_LONG_LONG)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_VERSION_HEX < 0x03000000
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to PY_LONG_LONG");
            return (PY_LONG_LONG)-1;
        }
        return (PY_LONG_LONG)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to PY_LONG_LONG");
                return (PY_LONG_LONG)-1;
            }
            return PyLong_AsUnsignedLongLong(x);
        } else {
            return PyLong_AsLongLong(x);
        }
    } else {
        PY_LONG_LONG val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (PY_LONG_LONG)-1;
        val = __Pyx_PyInt_AsLongLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

static CYTHON_INLINE signed long __Pyx_PyInt_AsSignedLong(PyObject* x) {
    const signed long neg_one = (signed long)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_VERSION_HEX < 0x03000000
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to signed long");
            return (signed long)-1;
        }
        return (signed long)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to signed long");
                return (signed long)-1;
            }
            return PyLong_AsUnsignedLong(x);
        } else {
            return PyLong_AsLong(x);
        }
    } else {
        signed long val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (signed long)-1;
        val = __Pyx_PyInt_AsSignedLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

static CYTHON_INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject* x) {
    const signed PY_LONG_LONG neg_one = (signed PY_LONG_LONG)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_VERSION_HEX < 0x03000000
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to signed PY_LONG_LONG");
            return (signed PY_LONG_LONG)-1;
        }
        return (signed PY_LONG_LONG)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to signed PY_LONG_LONG");
                return (signed PY_LONG_LONG)-1;
            }
            return PyLong_AsUnsignedLongLong(x);
        } else {
            return PyLong_AsLongLong(x);
        }
    } else {
        signed PY_LONG_LONG val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (signed PY_LONG_LONG)-1;
        val = __Pyx_PyInt_AsSignedLongLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

#include "compile.h"
#include "frameobject.h"
#include "traceback.h"

static void __Pyx_AddTraceback(const char *funcname) {
    PyObject *py_srcfile = 0;
    PyObject *py_funcname = 0;
    PyObject *py_globals = 0;
    PyCodeObject *py_code = 0;
    PyFrameObject *py_frame = 0;

    #if PY_MAJOR_VERSION < 3
    py_srcfile = PyString_FromString(__pyx_filename);
    #else
    py_srcfile = PyUnicode_FromString(__pyx_filename);
    #endif
    if (!py_srcfile) goto bad;
    if (__pyx_clineno) {
        #if PY_MAJOR_VERSION < 3
        py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno);
        #else
        py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno);
        #endif
    }
    else {
        #if PY_MAJOR_VERSION < 3
        py_funcname = PyString_FromString(funcname);
        #else
        py_funcname = PyUnicode_FromString(funcname);
        #endif
    }
    if (!py_funcname) goto bad;
    py_globals = PyModule_GetDict(__pyx_m);
    if (!py_globals) goto bad;
    py_code = PyCode_New(
        0,            /*int argcount,*/
        #if PY_MAJOR_VERSION >= 3
        0,            /*int kwonlyargcount,*/
        #endif
        0,            /*int nlocals,*/
        0,            /*int stacksize,*/
        0,            /*int flags,*/
        __pyx_empty_bytes, /*PyObject *code,*/
        __pyx_empty_tuple,  /*PyObject *consts,*/
        __pyx_empty_tuple,  /*PyObject *names,*/
        __pyx_empty_tuple,  /*PyObject *varnames,*/
        __pyx_empty_tuple,  /*PyObject *freevars,*/
        __pyx_empty_tuple,  /*PyObject *cellvars,*/
        py_srcfile,   /*PyObject *filename,*/
        py_funcname,  /*PyObject *name,*/
        __pyx_lineno,   /*int firstlineno,*/
        __pyx_empty_bytes  /*PyObject *lnotab*/
    );
    if (!py_code) goto bad;
    py_frame = PyFrame_New(
        PyThreadState_GET(), /*PyThreadState *tstate,*/
        py_code,             /*PyCodeObject *code,*/
        py_globals,          /*PyObject *globals,*/
        0                    /*PyObject *locals*/
    );
    if (!py_frame) goto bad;
    py_frame->f_lineno = __pyx_lineno;
    PyTraceBack_Here(py_frame);
bad:
    Py_XDECREF(py_srcfile);
    Py_XDECREF(py_funcname);
    Py_XDECREF(py_code);
    Py_XDECREF(py_frame);
}
Exemplo n.º 6
0
static int __Pyx_ParseOptionalKeywords(
    PyObject *kwds,
    PyObject **argnames[],
    PyObject *kwds2,
    PyObject *values[],
    Py_ssize_t num_pos_args,
    const char* function_name)
{
    PyObject *key = 0, *value = 0;
    Py_ssize_t pos = 0;
    PyObject*** name;
    PyObject*** first_kw_arg = argnames + num_pos_args;

    while (PyDict_Next(kwds, &pos, &key, &value)) {
        name = first_kw_arg;
        while (*name && (**name != key)) name++;
        if (*name) {
            values[name-argnames] = value;
        } else {
            #if PY_MAJOR_VERSION < 3
            if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) {
            #else
            if (unlikely(!PyUnicode_Check(key))) {
            #endif
                goto invalid_keyword_type;
            } else {
                for (name = first_kw_arg; *name; name++) {
                    #if PY_MAJOR_VERSION >= 3
                    if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
                        PyUnicode_Compare(**name, key) == 0) break;
                    #else
                    if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
                        _PyString_Eq(**name, key)) break;
                    #endif
                }
                if (*name) {
                    values[name-argnames] = value;
                } else {
                    /* unexpected keyword found */
                    for (name=argnames; name != first_kw_arg; name++) {
                        if (**name == key) goto arg_passed_twice;
                        #if PY_MAJOR_VERSION >= 3
                        if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
                            PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice;
                        #else
                        if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
                            _PyString_Eq(**name, key)) goto arg_passed_twice;
                        #endif
                    }
                    if (kwds2) {
                        if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
                    } else {
                        goto invalid_keyword;
                    }
                }
            }
        }
    }
    return 0;
arg_passed_twice:
    __Pyx_RaiseDoubleKeywordsError(function_name, **name);
    goto bad;
invalid_keyword_type:
    PyErr_Format(PyExc_TypeError,
        "%s() keywords must be strings", function_name);
    goto bad;
invalid_keyword:
    PyErr_Format(PyExc_TypeError,
    #if PY_MAJOR_VERSION < 3
        "%s() got an unexpected keyword argument '%s'",
        function_name, PyString_AsString(key));
    #else
        "%s() got an unexpected keyword argument '%U'",
        function_name, key);
    #endif
bad:
    return -1;
}
Exemplo n.º 7
0
:See: `EncoderInterface`");

static PyObject *
TDI_SoupEncoderType_starttag(tdi_soup_encoder_t *self, PyObject *args)
{
    PyObject *name, *attr, *closed, *newattr, *attiter, *tmp, *item, *result;
    tdi_attr_t *attritem;
    char *cresult;
    Py_ssize_t size, j, length;
    int res, is_closed;

    if (!(PyArg_ParseTuple(args, "SOO", &name, &attr, &closed)))
        return NULL;

    /* 1st pass - fixup parameters and count result bytes */
    if (!PyString_CheckExact(name)) {
        if (!(name = PyObject_Str(name)))
            return NULL;
    }
    else
        Py_INCREF(name);
    size = PyString_GET_SIZE(name) + 2; /* <> */

    if ((is_closed = PyObject_IsTrue(closed)) == -1) {
        Py_DECREF(name);
        return NULL;
    }
    else if (is_closed) {
        size += 2;
    }

    if (!(attiter = PyObject_GetIter(attr))) {
        Py_DECREF(name);
        return NULL;
    }

    newattr = PyList_New(0);
    while ((tmp = PyIter_Next(attiter))) {
        item = PySequence_Tuple(tmp);
        Py_DECREF(tmp);
        if (!item)
            break;
        if (PyTuple_GET_SIZE(item) != 2) {
            Py_DECREF(item);
            PyErr_SetString(PyExc_TypeError, "Expected Sequence of length 2");
            break;
        }
        tmp = PyTuple_GET_ITEM(item, 0);
        if (PyString_CheckExact(tmp))
            Py_INCREF(tmp);
        else if (PyString_Check(tmp)) {
            if (!(tmp = PyObject_Str(tmp))) {
                Py_DECREF(item);
                break;
            }
        }
        else {
            Py_DECREF(item);
            PyErr_SetString(PyExc_ValueError, "Attribute key is not a string");
            break;
        }
        size += PyString_GET_SIZE(tmp) + 1; /* ' ' */
        attritem = (tdi_attr_t *)tdi_attr_new(tmp, Py_None);
        Py_DECREF(tmp);
        if (!attritem) {
            Py_DECREF(item);
            break;
        }
        tmp = PyTuple_GET_ITEM(item, 1);
        if (tmp != Py_None) {
            if (PyString_CheckExact(tmp)) {
                Py_INCREF(tmp);
                Py_DECREF(Py_None);
                attritem->value = tmp;
            }
            else if (PyString_Check(tmp)) {
                if (!(attritem->value = PyObject_Str(tmp))) {
                    attritem->value = Py_None;
                    break;
                }
                Py_DECREF(Py_None);
            }
            else {
                Py_DECREF(item);
                PyErr_SetString(PyExc_ValueError,
                    "Attribute value is neither a string nor None");
                break;
            }
            size += PyString_GET_SIZE(attritem->value) + 1; /* '=' */
        }
        Py_DECREF(item);
        res = PyList_Append(newattr, (PyObject *)attritem);
        Py_DECREF(attritem);
        if (res == -1)
            break;
    }
    Py_DECREF(attiter);
    if (PyErr_Occurred()) {
        Py_DECREF(newattr);
        Py_DECREF(name);
        return NULL;
    }

    /* 2nd pass: assemble result */
    if (!(result = PyString_FromStringAndSize(NULL, size))) {
        Py_DECREF(newattr);
        Py_DECREF(name);
        return NULL;
    }
    cresult = PyString_AS_STRING(result);
    *cresult++ = '<';

    size = PyString_GET_SIZE(name);
    (void)memcpy(cresult, PyString_AS_STRING(name), (size_t)size);
    cresult += size;
    Py_DECREF(name);

    length = PyList_GET_SIZE(newattr);
    for (j = 0; j < length; ++j) {
        attritem = (tdi_attr_t *)PyList_GET_ITEM(newattr, j);
        *cresult++ = ' ';

        size = PyString_GET_SIZE(attritem->key);
        (void)memcpy(cresult, PyString_AS_STRING(attritem->key), (size_t)size);
        cresult += size;

        if (attritem->value != Py_None) {
            *cresult++ = '=';
            size = PyString_GET_SIZE(attritem->value);
            (void)memcpy(cresult, PyString_AS_STRING(attritem->value),
                         (size_t)size);
            cresult += size;
        }
    }
    Py_DECREF(newattr);

    if (is_closed) {
        *cresult++ = ' ';
        *cresult++ = '/';
    }
    *cresult = '>';

    return result;
}
Exemplo n.º 8
0
static int
xpybList_init(xpybList *self, PyObject *args, PyObject *kw)
{
    static char *kwlist[] = { "parent", "offset", "length", "type", "size", NULL };
    Py_ssize_t i, datalen, cur, offset, length, size = -1;
    PyObject *parent, *type, *obj, *arglist;
    const char *data;

    if (!PyArg_ParseTupleAndKeywords(args, kw, "OnnO|n", kwlist, &parent,
				     &offset, &length, &type, &size))
	return -1;

    self->list = PyList_New(0);
    if (self->list == NULL)
	return -1;

    if (PyObject_AsReadBuffer(parent, (const void **)&data, &datalen) < 0)
	return -1;
    if (size > 0 && length * size + offset > datalen) {
	PyErr_Format(xpybExcept_base, "Protocol object buffer too short "
		     "(expected %zd got %zd).", length * size + offset, datalen);
	return -1;
    }

    cur = offset;

    for (i = 0; i < length; i++) {
	if (PyString_CheckExact(type)) {
	    obj = xpybList_build(type, length, data + cur);
	    if (obj == NULL)
		return -1;
	    cur += size;
	} else if (size > 0) {
	    arglist = Py_BuildValue("(Onn)", parent, cur, size);
	    obj = PyEval_CallObject(type, arglist);
	    Py_DECREF(arglist);
	    if (obj == NULL)
		return -1;
	    cur += size;
	} else {
	    arglist = Py_BuildValue("(On)", parent, cur);
	    obj = PyEval_CallObject(type, arglist);
	    Py_DECREF(arglist);
	    if (obj == NULL)
		return -1;
	    datalen = PySequence_Size(obj);
	    if (datalen < 0)
		return -1;
	    cur += datalen;
	}

	if (PyList_Append(self->list, obj) < 0)
	    return -1;
        Py_DECREF(obj);
    }

    self->buf = PyBuffer_FromObject(parent, offset, cur - offset);
    if (self->buf == NULL)
	return -1;

    return 0;
}
Exemplo n.º 9
0
static PyObject *
quote_internal(PyObject *string, PyObject *safe_obj, PyObject *encoding_obj,
               PyObject *errors_obj, int plus)
{
    unsigned char localmask[sizeof(wtf_charmask)];
    const unsigned char *safe;
    PyObject *result;
    char *encoding, *errors;
    Py_ssize_t j, slen, tlen;
    unsigned char c = '/';

    /* make it a string */
    if (PyUnicode_CheckExact(string) || PyUnicode_Check(string)) {
        if (encoding_obj) {
            if (!(encoding = PyString_AsString(encoding_obj)))
                return NULL;
            if (errors_obj) {
                if (!(errors = PyString_AsString(errors_obj)))
                    return NULL;
            }
            else {
                errors = "strict";
            }
            string = PyUnicode_AsEncodedString(string, encoding, errors);
            if (!string)
                return NULL;
        }
        else {
            if (!(string = PyUnicode_AsUTF8String(string)))
                return NULL;
        }
    }
    else if (!PyString_CheckExact(string)) {
        string = PyObject_Str(string);
    }
    else {
        Py_INCREF(string);
    }
    result = NULL;

    /* determine safe table */
    if (safe_obj) {
        if (!PyString_CheckExact(safe_obj)) {
            if (!(safe_obj = PyObject_Str(safe_obj))) {
                goto done;
            }
        }
        else
            Py_INCREF(safe_obj);
        slen = PyString_GET_SIZE(safe_obj);
        if (!slen)
            safe = wtf_charmask;

        /* encoding misuse, but we don't it anymore anyway */
        encoding = PyString_AS_STRING(safe_obj);
        memcpy(localmask, wtf_charmask, sizeof(localmask));
        for (j=0; j < slen; ++j)
            localmask[(unsigned char)encoding[j] & 0xFF] |= WTF_SAFE_CHAR;
        safe = localmask;
        Py_DECREF(safe_obj);
    }
    else {
        memcpy(localmask, wtf_charmask, sizeof(localmask));
        localmask[c & 0xFF] |= WTF_SAFE_CHAR;
        safe = localmask;
    }

    /* count target length */
    encoding = PyString_AS_STRING(string);
    slen = PyString_GET_SIZE(string);
    tlen = slen;
    for (j=0; j<slen; ++j) {
        c = encoding[j];
        if (!WTF_IS_SAFE_CHAR(safe, c) && !(plus && c == ' ')) {
            tlen += 2; /* c => %XX */
        }
    }
    if (slen == tlen && !plus) /* shortcut: nothing to quote */
        return string;

    /* generate result */
    if (!(result = PyString_FromStringAndSize(NULL, tlen)))
        goto done;
    /* errors variable misuse */
    errors = PyString_AS_STRING(result);

    for (j=0; j<slen; ++j) {
        c = encoding[j];
        if (WTF_IS_SAFE_CHAR(safe, c)) {
            *errors++ = encoding[j];
        }
        else if (plus && c == ' ') {
            *errors++ = '+';
        }
        else {
            *errors++ = '%';
            *errors++ = WTF_HEXDIGIT_HIGH(c);
            *errors++ = WTF_HEXDIGIT_LOW(c);
        }
    }

done:
    Py_DECREF(string);
    return result;
}
Exemplo n.º 10
0
    bool AllocateMore(SQLLEN cbAdd)
    {
        if (cbAdd == 0)
            return true;

        SQLLEN newSize = bufferSize + cbAdd;

        if (usingStack)
        {
            // This is the first call and `buffer` points to stack memory.  Allocate a new object and copy the stack
            // data into it.
            
            char* stackBuffer = buffer;

            if (dataType == SQL_C_CHAR || dataType == SQL_C_BINARY)
            {
                bufferOwner = PyString_FromStringAndSize(0, newSize);
                buffer      = bufferOwner ? PyString_AS_STRING(bufferOwner) : 0;
            }
            else if (sizeof(wchar_t) == Py_UNICODE_SIZE)
            {
                // Allocate directly into a Unicode object.
                bufferOwner = PyUnicode_FromUnicode(0, newSize / element_size);
                buffer      = bufferOwner ? (char*)PyUnicode_AsUnicode(bufferOwner) : 0;
            }
            else
            {
                // We're Unicode, but wchar_t and Py_UNICODE don't match, so maintain our own wchar_t buffer.
                buffer = (char*)malloc(newSize);
            }

            usingStack = false;

            if (buffer == 0)
                return false;

            memcpy(buffer, stackBuffer, bufferSize);
            bufferSize = newSize;
            return true;
        }

        if (PyString_CheckExact(bufferOwner))
        {
            if (_PyString_Resize(&bufferOwner, newSize) == -1)
                return false;
            buffer = PyString_AS_STRING(bufferOwner);
        }
        else if (PyUnicode_CheckExact(bufferOwner))
        {
            if (PyUnicode_Resize(&bufferOwner, newSize / element_size) == -1)
                return false;
            buffer = (char*)PyUnicode_AsUnicode(bufferOwner);
        }
        else
        {
            char* tmp = (char*)realloc(buffer, newSize);
            if (tmp == 0)
                return false;
            buffer = tmp;
        }

        bufferSize = newSize;

        return true;
    }
Exemplo n.º 11
0
LLBC_PacketHeaderParts *pyllbc_Service::BuildCLayerParts(PyObject *pyLayerParts)
{
    // Python layer parts(dict type) convert rules describe:
    //   python type       c++ type
    // --------------------------
    //   int/long/bool -->   sint64
    //     float4/8    -->  float/double
    //   str/bytearray -->  LLBC_String

    if (!PyDict_Check(pyLayerParts))
    {
        pyllbc_SetError("parts instance not dict type");
        return NULL;
    }

    LLBC_PacketHeaderParts *cLayerParts = LLBC_New(LLBC_PacketHeaderParts);

    Py_ssize_t pos = 0;
    PyObject *key, *value;
    while (PyDict_Next(pyLayerParts, &pos, &key, &value)) // key & value are borrowed.
    {
        const int serialNo = static_cast<int>(PyInt_AsLong(key));
        if (UNLIKELY(serialNo == -1 && PyErr_Occurred()))
        {
            pyllbc_TransferPyError("When fetch header part serial no");
            LLBC_Delete(cLayerParts);

            return NULL;
        }

        // Value type check order:
        //   int->
        //     str->
        //       float->
        //         long->
        //           bool->
        //             bytearray->
        //               other objects
        if (PyInt_CheckExact(value))
        {
            const sint64 cValue = PyInt_AS_LONG(value);
            cLayerParts->SetPart<sint64>(serialNo, cValue);
        }
        else if (PyString_CheckExact(value))
        {
            char *strBeg;
            Py_ssize_t strLen;
            if (UNLIKELY(PyString_AsStringAndSize(value, &strBeg, &strLen) == -1))
            {
                pyllbc_TransferPyError("When fetch header part value");
                LLBC_Delete(cLayerParts);

                return NULL;
            }

            cLayerParts->SetPart(serialNo, strBeg, strLen);

        }
        else if (PyFloat_CheckExact(value))
        {
            const double cValue = PyFloat_AS_DOUBLE(value);
            cLayerParts->SetPart<double>(serialNo, cValue);
        }
        else if (PyLong_CheckExact(value))
        {
            const sint64 cValue = PyLong_AsLongLong(value);
            cLayerParts->SetPart<sint64>(serialNo, cValue);
        }
        else if (PyBool_Check(value))
        {
            const int pyBoolCheck = PyObject_IsTrue(value);
            if (UNLIKELY(pyBoolCheck == -1))
            {
                pyllbc_TransferPyError("when fetch header part value");
                LLBC_Delete(cLayerParts);

                return NULL;
            }

            cLayerParts->SetPart<uint8>(serialNo, pyBoolCheck);
        }
        else if (PyByteArray_CheckExact(value))
        {
            char *bytesBeg = PyByteArray_AS_STRING(value);
            Py_ssize_t bytesLen = PyByteArray_GET_SIZE(value);

            cLayerParts->SetPart(serialNo, bytesBeg, bytesLen);
        }
        else // Other types, we simple get the object string representations.
        {
            LLBC_String strRepr = pyllbc_ObjUtil::GetObjStr(value);
            if (UNLIKELY(strRepr.empty() && PyErr_Occurred()))
            {
                LLBC_Delete(cLayerParts);
                return NULL;
            }

            cLayerParts->SetPart(serialNo, strRepr.data(), strRepr.size());
        }
    }

    return cLayerParts;
}
Exemplo n.º 12
0
Arquivo: split.c Projeto: S-YOU/split
static PyObject*
_csv(PyObject* self, PyObject* arg) {
	size_t sLen = 0, splitterLen = 1, maxsplit = 1, index = 0, insideQuote = 0;
	TYPE *src = NULL, *s, *ss, *sEnd = NULL, *sPrev = NULL, *escaped = NULL;
	TYPE quoteChar = '\'';
	TYPE defaultSplitter = ',', *splitter = &defaultSplitter, *sp, *spEnd = splitter + splitterLen;
	PyObject *list;
	PyTupleObject* argTuple = (PyTupleObject*) arg;

	if (Py_SIZE(argTuple) < 2) return NULL;

	if (PyTYPE_CHECK(argTuple->ob_item[0])) {
		src = PyTYPE_AS_TYPE(argTuple->ob_item[0]);
		sLen = PyTYPE_GET_SIZE(argTuple->ob_item[0]);
	} else {
		return NULL;
	}

	maxsplit = PyInt_AsSsize_t(argTuple->ob_item[1]);
	if (maxsplit > 100) {
		return NULL;
	}

	if (argTuple->ob_size > 2) {
		if (argTuple->ob_item[2] == Py_None) {
			quoteChar = 0;
		} else if (PyString_CheckExact(argTuple->ob_item[2])) {
			quoteChar = *PyString_AS_STRING(argTuple->ob_item[2]);
		} else if (PyUnicode_CheckExact(argTuple->ob_item[2])) {
			quoteChar = *PyUnicode_AS_UNICODE(argTuple->ob_item[2]);
		} else {
			return NULL;
		}
	}

	if (argTuple->ob_size > 3) {
		if (PyTYPE_CHECK(argTuple->ob_item[3])) {
			splitter = PyTYPE_AS_TYPE(argTuple->ob_item[3]);
			splitterLen = PyTYPE_GET_SIZE(argTuple->ob_item[3]);
			spEnd = splitter + splitterLen;
		} else {
			return NULL;
		}
	}

	sPrev = s = src, sEnd = src + sLen;
	list = PyTuple_New(maxsplit);
	while (s < sEnd) {
		if (*s == *splitter && !insideQuote) {
			if (splitterLen > 1) {
				sp = splitter, ss = s;
				while (*++sp == *++ss);
				if (sp >= spEnd) {
					ADD_CSV;
					if (index >= maxsplit) goto done;
					sPrev = s + splitterLen;
					s += splitterLen;
					continue;
				}
			} else {
				ADD_CSV;
				if (index >= maxsplit) goto done;
				sPrev = s + 1;
			}
		} else if (*s == '\\') {
			s++, escaped = s;
		} else if (quoteChar && *s == quoteChar && s != escaped) insideQuote = !insideQuote;
		s++;
	}
	if (index < maxsplit) {
		ADD_CSV;
		if (index < maxsplit) {
			Py_DECREF(list);
			Py_RETURN_NONE;
		}
	}
done:
	return (PyObject*) list;
}
Exemplo n.º 13
0
/* buffer-read a block (return NULL on eof) */
static PyObject *
generic_read(void *self_, Py_ssize_t size)
{
    PyObject *tmp, *result, *sizeobj = NULL;
    bufitem *item;
    genericstreamobject *self=self_;
    char *jptr, *sentinel;
    Py_ssize_t cursize, rsize;

    if (!self->read) {
        PyErr_SetString(PyExc_AttributeError,
            "This stream does not provide a read function"
        );
        return NULL;
    }

    /* return a bufitem */
    if (size == 0) {
        if ((item = self->rbuf_last)) {
            result = item->load;
            Py_INCREF(result);
            if (!(self->rbuf_last = item->next)) {
                self->rbuf = NULL;
                self->rbuf_size = 0;
            }
            else
                self->rbuf_size -= PyString_GET_SIZE(result);
            (void)bufitem_del(item);
            return result;
        }
        else if (self->flags & GENERIC_STREAM_EOF)
            return NULL;
        /* else */
        size = self->chunk_size;
    }

    /* read up to size bytes */
    if (   !(self->flags & GENERIC_STREAM_EOF)
           && (size > 0) && (size > self->rbuf_size)) {
        if (!(sizeobj = optimal_chunk_size(self, size)))
            return NULL;
        if (!(tmp = PyObject_CallObject(self->read, sizeobj)))
            goto error;
        if (PyString_CheckExact(tmp))
            result = tmp;
        else {
            result = PyObject_Str(tmp);
            Py_DECREF(tmp);
            if (!result)
                goto error;
        }

        rsize = PyString_GET_SIZE(result);
        if (rsize > 0) {
            if ((self->rbuf_size + rsize) < self->rbuf_size) {
                PyErr_SetString(PyExc_OverflowError, "Buffer became too big");
                Py_DECREF(result);
                goto error;
            }
            if (!(item = bufitem_new())) {
                Py_DECREF(result);
                goto error;
            }
            item->next = NULL;
            item->load = result;
            if (self->rbuf) {
                self->rbuf->next = item; /* Note to self:         */
                self->rbuf = item;       /* this code is correct. */
                self->rbuf_size += rsize;
            }
            else {
                self->rbuf = self->rbuf_last = item;
                self->rbuf_size = rsize;
            }
        }
        else {
            Py_DECREF(result);
            self->flags |= GENERIC_STREAM_EOF;
        }
    }

    /* slurp it all */
    else if (!(self->flags & GENERIC_STREAM_EOF) && (size < 0)) {
        if (!(sizeobj = optimal_chunk_size(self, size)))
            return NULL;
        while (1) {
            if (!(tmp = PyObject_CallObject(self->read, sizeobj)))
                goto error;
            if (PyString_CheckExact(tmp))
                result = tmp;
            else {
                result = PyObject_Str(tmp);
                Py_DECREF(tmp);
                if (!result)
                    goto error;
            }
            rsize = PyString_GET_SIZE(result);
            if (rsize > 0) {
                if ((self->rbuf_size + rsize) < self->rbuf_size) {
                    PyErr_SetString(PyExc_OverflowError,
                                    "Buffer became too big");
                    Py_DECREF(result);
                    goto error;
                }
                if (!(item = bufitem_new())) {
                    Py_DECREF(result);
                    goto error;
                }
                item->next = NULL;
                item->load = result;
                if (self->rbuf) {
                    self->rbuf->next = item; /* Note to self:         */
                    self->rbuf = item;       /* this code is correct. */
                    self->rbuf_size += rsize;
                }
                else {
                    self->rbuf = self->rbuf_last = item;
                    self->rbuf_size = rsize;
                }
            }
            else {
                Py_DECREF(result);
                self->flags |= GENERIC_STREAM_EOF;
                break;
            }
        }
        size = self->rbuf_size;
    }
    Py_XDECREF(sizeobj);

    if (!self->rbuf_size) {
        self->flags |= GENERIC_STREAM_EOF;
        return NULL;
    }

    /* flatten the bufitems into the result string */
    rsize = size <= self->rbuf_size ? size : self->rbuf_size;
    if (!(result = PyString_FromStringAndSize(NULL, rsize)))
        return NULL;
    jptr = PyString_AS_STRING(result);
    sentinel = jptr + rsize;
    while (jptr < sentinel && (item = self->rbuf_last)) {
        cursize = PyString_GET_SIZE(item->load);
        if (jptr + cursize > sentinel) { /* need to split */
            bufitem *newitem;
            
            if (!(newitem = bufitem_new())) {
                Py_DECREF(result);
                return NULL;
            }
            newitem->next = item->next;
            newitem->load = PyString_FromStringAndSize(
                PyString_AS_STRING(item->load) + (size_t)(sentinel - jptr),
                (cursize - (Py_ssize_t)(sentinel - jptr))
            );
            if (!newitem->load) {
                (void)bufitem_del(newitem);
                Py_DECREF(result);
                return NULL;
            }
            item->next = newitem;
            if (self->rbuf == item)
                self->rbuf = newitem;
            cursize = (Py_ssize_t)(sentinel - jptr);
        }
        (void)memcpy(jptr, PyString_AS_STRING(item->load),
                     (size_t)cursize);
        jptr += cursize;
        self->rbuf_size -= cursize;
        if (self->rbuf == item)
            self->rbuf = NULL;
        self->rbuf_last = bufitem_del(item);
    }

    return result;

error:
    Py_XDECREF(sizeobj);
    return NULL;
}
Exemplo n.º 14
0
static PyObject * table_subscript(PyObject *self, register PyObject *key)
{
    char *k;
    const apr_array_header_t *ah;
    apr_table_entry_t *elts;
    register int i;
    PyObject *list;

    if (key && !PyString_CheckExact(key)) {
        PyErr_SetString(PyExc_TypeError,
                        "table keys must be strings");
        return NULL;
    }
    k = PyString_AsString(key);

    /* it's possible that we have duplicate keys, so
       we can't simply use apr_table_get since that just
       returns the first match.
    */

    /* PYTHON 2.5: 'PyList_New' uses Py_ssize_t for input parameters */
    list = PyList_New(0);
    if (!list)
        return NULL;

    ah = apr_table_elts (((tableobject *)self)->table);
    elts = (apr_table_entry_t *) ah->elts;

    i = ah->nelts;

    while (i--)
        if (elts[i].key) {
            if (apr_strnatcasecmp(elts[i].key, k) == 0) {
                PyObject *v = NULL;
                if (elts[i].val != NULL) {
                    v = PyString_FromString(elts[i].val);
                } else {
                    v = Py_None;
                    Py_INCREF(v);
                }
                /* PYTHON 2.5: 'PyList_Insert' uses Py_ssize_t for input parameters */
                PyList_Insert(list, 0, v);
                Py_DECREF(v);
            }
        }

    /* if no match */
    /* PYTHON 2.5: 'PyList_Size' uses Py_ssize_t for return values (may need overflow check) */
    if (PyList_Size(list) == 0) {
        Py_DECREF(list);
        PyErr_SetObject(PyExc_KeyError, key);
        return NULL;
    }

    /* if we got one match */
    /* PYTHON 2.5: 'PyList_Size' uses Py_ssize_t for return values (may need overflow check) */
    if (PyList_Size(list) == 1) {
        /* PYTHON 2.5: 'PyList_GetItem' uses Py_ssize_t for input parameters */
        PyObject *v = PyList_GetItem(list, 0);
        Py_INCREF(v);
        Py_DECREF(list);
        return v;
    }

    /* else we return a list */
    return list;
}
Exemplo n.º 15
0
static PyObject *
CapObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {

    if (kwds == NULL) {
        // PyThreadState *tstate = PyEval_SaveThread();
        PyFrameObject *frame = PyThreadState_GET()->frame;
        PyFrame_FastToLocals(frame);
        kwds = frame->f_locals;
        // PyFrame_LocalsToFast(frame, 0);
        // PyEval_RestoreThread(tstate);
    }

    PyDictObject *environ = (PyDictObject *)kwds;

    PyObject *env, *keys;
    Py_ssize_t i, size;

    Py_ssize_t mask, key_len;
    PyObject *key, *value;
    PyDictEntry *entries;
	const char* key_str;

    /* Preallocate the list of tuples, to avoid allocations during
     * the loop over the items, which could trigger GC, which
     * could resize the dict. :-(
     */

    again:

    size = environ->ma_used;

    keys = PyList_New(0);
    if (keys == NULL)
        return NULL;

    env = PyList_New(0);
    if (env == NULL)
        return NULL;

    if (size != environ->ma_used) {
        /* Durnit.  The allocations caused the dict to resize.
         * Just start over, this shouldn't normally happen.
         */
        Py_DECREF(keys);
        Py_DECREF(env);
        goto again;
    }

    entries = environ->ma_table;
    mask = environ->ma_mask;

    for (i = 0; i <= mask; i++) {
        if ((value=entries[i].me_value) != NULL) {
            key = entries[i].me_key;
            if (!PyString_CheckExact(key)) {
                Py_DECREF(keys);
                Py_DECREF(env);
                PyErr_SetString(PyExc_ValueError, "Only string keys can be used in Objects.");
                return NULL;
            }
            key_len = PyString_GET_SIZE(key);
            if (!key_len)
                continue;
            key_str = PyString_AS_STRING(key);
            if (!memcmp(key_str, single_underscore, 1)) {
                if (key_len == 1 || (memcmp(key_str, double_underscore, 2)))
                    continue;
            }
            Py_INCREF(key);
            PyList_Append(keys, key);
            Py_INCREF(value);
            PyList_Append(env, value);
       }
    }

    /* We could save more memory by sorting the keys. */

    PyObject *keys_tuple;
    PyObject **p, **q;

    size = Py_SIZE(keys);
    keys_tuple = PyTuple_New(size);
    if (keys_tuple == NULL)
        return NULL;

    p = ((PyTupleObject *)keys_tuple)->ob_item;
    q = ((PyListObject *)keys)->ob_item;

    while (--size >= 0) {
        Py_INCREF(*q);
        *p = *q;
        p++;
        q++;
    }

    long hash;
    long id;

    hash = PyObject_Hash(keys_tuple);
    if (hash == -1) {
        PyErr_SetString(PyExc_ValueError, "Cannot hash the environ keys.");
        return NULL;
    }

    environ = (PyDictObject *)_key2id_store;
    entries = (environ->ma_lookup)(environ, keys_tuple, hash);

    if ((entries == NULL) || (entries->me_value == NULL)) {
        id = _id_counter;
        _id_counter++;
        value = PyLong_FromLong(id);
        if (value == NULL)
            return NULL;
        PyDict_SetItem(_key2id_store, keys_tuple, value);
        PyDict_SetItem(_id2key_store, value, keys_tuple);
    } else {
        id = PyLong_AsLong(entries->me_value);
        if (id == -1)
            return NULL;
    }

    CapObject *self;

    self = (CapObject *)type->tp_alloc(type, 0);
    if (self != NULL) {
        self->id = id;
        self->env = env;
        self->__weakref__ = NULL;
    }

    return (PyObject *)self;

}
Exemplo n.º 16
0
static bool
download_proc(std::string &download_task, std::string &cfg)
{
        log_trace("Enter the proc");
        if (download_task == "" || cfg == "") {
            log_error("Download task parameters error!");
            return false;
        }

        bool ret = false;

        PyObject *dl_param = Py_BuildValue("(ss)", cfg.c_str(),download_task.c_str());
        if(dl_param == NULL) {
                log_error("Download_proc argument build return null");
                return false;
        }
        
        PyObject *sys = NULL, *path = NULL, *py_dl_func = NULL,
                 *py_module = NULL, *py_ret = NULL;

        const char *err_msg = NULL;
        do {
                if(!Py_IsInitialized()) {
                        err_msg = "Py environment initial error";
                        break;
                }

                /* Setup Python runtime enviroments. */
                char pymod_path[NAME_MAX];
                bzero(pymod_path, sizeof(pymod_path));
                get_agent_pymod_path(pymod_path, sizeof(pymod_path), "pymod");
                sys = PyImport_ImportModule("sys");         //new reference
                path = PyObject_GetAttrString(sys, "path"); // new reference
                
                int app_ret = PyList_Append(path, PyString_FromString(pymod_path));
                if (app_ret != 0) {
                        err_msg = "Py path append failed";
                        break;
                }

                /* Load target Python module. */
                py_module = PyImport_ImportModule("remote_file_operation");//new reference
                if(!py_module) {
                        err_msg = "Py import module failed";
                        break;
                }

                /* Search for main entry. */
                py_dl_func = PyObject_GetAttrString(py_module, "main");//new reference
                if(!py_dl_func || !PyCallable_Check(py_dl_func)) {
                        err_msg = "Py main function is not executable";
                        break;
                }

                /* Execute and get return args. */
                log_info("Go into the pydownload");
                py_ret = PyObject_CallObject(py_dl_func, dl_param);//new reference
                if(!py_ret) {
                        err_msg = "Py download execute error";
                        break;
                }
                log_info("Get out of the pydownload");

                /* Parse the return and upload to redis */
                if (PyString_CheckExact(py_ret)) {
                        Json::Reader reader;
                        Json::Value root;
                        /* borrowed pointer */
                        char *s = PyString_AsString(py_ret);
                        if(reader.parse(s, root)) {
                                log_trace("seq=%d, %s\n", root["seq"].asInt(), root.toStyledString().c_str());
                                redis_upload_rsp(root);
                                ret = true;
                        } else {
                                err_msg = "Py download return invalid json";
                                break;
                        }
                } else {
                        err_msg = "Py download return error type";
                        break;
                }
        } while(0);

        Py_XDECREF(sys);
        Py_XDECREF(path);
        Py_XDECREF(py_module);
        Py_XDECREF(py_dl_func);
        Py_XDECREF(py_ret);
        Py_XDECREF(dl_param);

        log_trace("One download thread execution finished!!!");

        if(err_msg) {
                log_error(err_msg);
        }
        return ret;
}
Exemplo n.º 17
0
static int __Pyx_ParseOptionalKeywords(
    PyObject *kwds,
    PyObject **argnames[],
    PyObject *kwds2,
    PyObject *values[],
    Py_ssize_t num_pos_args,
    const char* function_name)
{
    PyObject *key = 0, *value = 0;
    Py_ssize_t pos = 0;
    PyObject*** name;
    PyObject*** first_kw_arg = argnames + num_pos_args;

    while (PyDict_Next(kwds, &pos, &key, &value)) {
        #if PY_MAJOR_VERSION < 3
        if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) {
        #else
        if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key))) {
        #endif
            goto invalid_keyword_type;
        } else {
            name = argnames;
            while (*name && (**name != key)) name++;
            if (*name) {
                if (name < first_kw_arg) goto arg_passed_twice;
                values[name-argnames] = value;
            } else {
                for (name = first_kw_arg; *name; name++) {
                    #if PY_MAJOR_VERSION >= 3
                    if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
                        PyUnicode_Compare(**name, key) == 0) break;
                    #else
                    if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
                        strcmp(PyString_AS_STRING(**name),
                               PyString_AS_STRING(key)) == 0) break;
                    #endif
                }
                if (*name) {
                    values[name-argnames] = value;
                } else {
                    /* unexpected keyword found */
                    for (name=argnames; name != first_kw_arg; name++) {
                        if (**name == key) goto arg_passed_twice;
                        #if PY_MAJOR_VERSION >= 3
                        if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
                            PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice;
                        #else
                        if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
                            strcmp(PyString_AS_STRING(**name),
                                   PyString_AS_STRING(key)) == 0) goto arg_passed_twice;
                        #endif
                    }
                    if (kwds2) {
                        if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
                    } else {
                        goto invalid_keyword;
                    }
                }
            }
        }
    }
    return 0;
arg_passed_twice:
    __Pyx_RaiseDoubleKeywordsError(function_name, **name);
    goto bad;
invalid_keyword_type:
    PyErr_Format(PyExc_TypeError,
        "%s() keywords must be strings", function_name);
    goto bad;
invalid_keyword:
    PyErr_Format(PyExc_TypeError,
    #if PY_MAJOR_VERSION < 3
        "%s() got an unexpected keyword argument '%s'",
        function_name, PyString_AsString(key));
    #else
        "%s() got an unexpected keyword argument '%U'",
        function_name, key);
    #endif
bad:
    return -1;
}

#include "compile.h"
#include "frameobject.h"
#include "traceback.h"

static void __Pyx_AddTraceback(const char *funcname) {
    PyObject *py_srcfile = 0;
    PyObject *py_funcname = 0;
    PyObject *py_globals = 0;
    PyObject *empty_string = 0;
    PyCodeObject *py_code = 0;
    PyFrameObject *py_frame = 0;

    #if PY_MAJOR_VERSION < 3
    py_srcfile = PyString_FromString(__pyx_filename);
    #else
    py_srcfile = PyUnicode_FromString(__pyx_filename);
    #endif
    if (!py_srcfile) goto bad;
    if (__pyx_clineno) {
        #if PY_MAJOR_VERSION < 3
        py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno);
        #else
        py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno);
        #endif
    }
    else {
        #if PY_MAJOR_VERSION < 3
        py_funcname = PyString_FromString(funcname);
        #else
        py_funcname = PyUnicode_FromString(funcname);
        #endif
    }
    if (!py_funcname) goto bad;
    py_globals = PyModule_GetDict(__pyx_m);
    if (!py_globals) goto bad;
    #if PY_MAJOR_VERSION < 3
    empty_string = PyString_FromStringAndSize("", 0);
    #else
    empty_string = PyBytes_FromStringAndSize("", 0);
    #endif
    if (!empty_string) goto bad;
    py_code = PyCode_New(
        0,            /*int argcount,*/
        #if PY_MAJOR_VERSION >= 3
        0,            /*int kwonlyargcount,*/
        #endif
        0,            /*int nlocals,*/
        0,            /*int stacksize,*/
        0,            /*int flags,*/
        empty_string, /*PyObject *code,*/
        __pyx_empty_tuple,  /*PyObject *consts,*/
        __pyx_empty_tuple,  /*PyObject *names,*/
        __pyx_empty_tuple,  /*PyObject *varnames,*/
        __pyx_empty_tuple,  /*PyObject *freevars,*/
        __pyx_empty_tuple,  /*PyObject *cellvars,*/
        py_srcfile,   /*PyObject *filename,*/
        py_funcname,  /*PyObject *name,*/
        __pyx_lineno,   /*int firstlineno,*/
        empty_string  /*PyObject *lnotab*/
    );
    if (!py_code) goto bad;
    py_frame = PyFrame_New(
        PyThreadState_GET(), /*PyThreadState *tstate,*/
        py_code,             /*PyCodeObject *code,*/
        py_globals,          /*PyObject *globals,*/
        0                    /*PyObject *locals*/
    );
    if (!py_frame) goto bad;
    py_frame->f_lineno = __pyx_lineno;
    PyTraceBack_Here(py_frame);
bad:
    Py_XDECREF(py_srcfile);
    Py_XDECREF(py_funcname);
    Py_XDECREF(empty_string);
    Py_XDECREF(py_code);
    Py_XDECREF(py_frame);
}
Exemplo n.º 18
0
int
set_str_list(
    const char* propname,
    PyObject* value,
    Py_ssize_t len,
    Py_ssize_t maxlen,
    char (*dest)[72]) {

  PyObject*  str      = NULL;
  char*      str_char = NULL;
  Py_ssize_t str_len  = 0;
  Py_ssize_t i        = 0;

  if (check_delete(propname, value)) {
    return -1;
  }

  if (maxlen == 0) {
    maxlen = 68;
  }

  if (!PySequence_Check(value)) {
    PyErr_Format(
        PyExc_TypeError,
        "'%s' must be a sequence of strings",
        propname);
    return -1;
  }

  if (PySequence_Size(value) != len) {
    PyErr_Format(
        PyExc_ValueError,
        "len(%s) must be %u",
        propname,
        (unsigned int)len);
    return -1;
  }

  /* We go through the list twice, once to verify that the list is
     in the correct format, and then again to do the data copy.  This
     way, we won't partially copy the contents and then throw an
     exception. */
  for (i = 0; i < len; ++i) {
    str = PySequence_GetItem(value, i);
    if (str == NULL) {
      return -1;
    }

    #if PY3K
    if (!PyBytes_CheckExact(str)) {
    #else
    if (!PyString_CheckExact(str)) {
    #endif
      PyErr_Format(
          PyExc_TypeError,
          #if PY3K
          "'%s' must be a sequence of bytes",
          #else
          "'%s' must be a sequence of strings",
          #endif
          propname);
      Py_DECREF(str);
      return -1;
    }

    #if PY3K
    if (PyBytes_Size(str) > maxlen) {
    #else
    if (PyString_Size(str) > maxlen) {
    #endif
      PyErr_Format(
          PyExc_TypeError,
          #if PY3K
          "Each bytes in '%s' must be less than %u characters",
          #else
          "Each string in '%s' must be less than %u characters",
          #endif
          propname, (unsigned int)maxlen);
      Py_DECREF(str);
      return -1;
    }

    Py_DECREF(str);
  }

  for (i = 0; i < len; ++i) {
    str = PySequence_GetItem(value, i);
    if (str == NULL) {
      /* Theoretically, something has gone really wrong here, since
         we've already verified the list. */
      PyErr_Format(
          PyExc_RuntimeError,
          "Input values have changed underneath us.  Something is seriously wrong.");
      return -1;
    }

    /* We already know its a string of the correct length */
    #if PY3K
    if (PyBytes_AsStringAndSize(str, &str_char, &str_len)) {
    #else
    if (PyString_AsStringAndSize(str, &str_char, &str_len)) {
    #endif
      /* Theoretically, something has gone really wrong here, since
         we've already verified the list. */
      PyErr_Format(
          PyExc_RuntimeError,
          "Input values have changed underneath us.  Something is seriously wrong.");
      Py_DECREF(str);
      return -1;
    }

    strncpy(dest[i], str_char, (size_t)maxlen);

    Py_DECREF(str);
  }

  return 0;
}


/*@null@*/ PyObject*
get_pscards(
    /*@unused@*/ const char* propname,
    struct pscard* ps,
    int nps) {

  PyObject*  result    = NULL;
  PyObject*  subresult = NULL;
  Py_ssize_t i         = 0;

  if (nps < 0) {
    PyErr_SetString(PyExc_ValueError, "Object has no pscards");
    return NULL;
  }

  result = PyList_New((Py_ssize_t)nps);
  if (result == NULL) {
    return NULL;
  }

  for (i = 0; i < (Py_ssize_t)nps; ++i) {
    subresult = Py_BuildValue("iis", ps[i].i, ps[i].m, ps[i].value);
    if (subresult == NULL) {
      Py_DECREF(result);
      return NULL;
    }

    if (PyList_SetItem(result, i, subresult)) {
      Py_DECREF(subresult);
      Py_DECREF(result);
      return NULL;
    }
  }

  return result;
}

int
set_pscards(
    /*@unused@*/ const char* propname,
    PyObject* value,
    struct pscard** ps,
    int *nps,
    int *npsmax) {

  PyObject*   subvalue  = NULL;
  Py_ssize_t  i         = 0;
  Py_ssize_t  size      = 0;
  int         ival      = 0;
  int         mval      = 0;
  const char* strvalue  = 0;
  void*       newmem    = NULL;

  if (!PySequence_Check(value))
    return -1;
  size = PySequence_Size(value);
  if (size > 0x7fffffff) {
    /* Must be a 32-bit size */
    return -1;
  }

  if (size > (Py_ssize_t)*npsmax) {
    newmem = malloc(sizeof(struct pscard) * size);
    if (newmem == NULL) {
      PyErr_SetString(PyExc_MemoryError, "Could not allocate memory.");
      return -1;
    }
    free(*ps);
    *ps = newmem;
    *npsmax = (int)size;
  }

  /* Verify the entire list for correct types first, so we don't have
     to undo anything copied into the canonical array. */
  for (i = 0; i < size; ++i) {
    subvalue = PySequence_GetItem(value, i);
    if (subvalue == NULL) {
      return -1;
    }
    if (!PyArg_ParseTuple(subvalue, "iis", &ival, &mval, &strvalue)) {
      Py_DECREF(subvalue);
      return -1;
    }
    Py_DECREF(subvalue);
  }

  for (i = 0; i < size; ++i) {
    subvalue = PySequence_GetItem(value, i);
    if (subvalue == NULL) {
      return -1;
    }
    if (!PyArg_ParseTuple(subvalue, "iis", &ival, &mval, &strvalue)) {
      Py_DECREF(subvalue);
      return -1;
    }
    Py_DECREF(subvalue);

    (*ps)[i].i = ival;
    (*ps)[i].m = mval;
    strncpy((*ps)[i].value, strvalue, 72);
    (*ps)[i].value[71] = '\0';
    (*nps) = (int)(i + 1);
  }

  return 0;
}

/*@null@*/ PyObject*
get_pvcards(
    /*@unused@*/ const char* propname,
    struct pvcard* pv,
    int npv) {

  PyObject*  result    = NULL;
  PyObject*  subresult = NULL;
  Py_ssize_t i         = 0;

  if (npv < 0) {
    PyErr_SetString(PyExc_ValueError, "Object has no pvcards");
    return NULL;
  }

  result = PyList_New((Py_ssize_t)npv);
  if (result == NULL) {
    return NULL;
  }

  for (i = 0; i < (Py_ssize_t)npv; ++i) {
    subresult = Py_BuildValue("iid", pv[i].i, pv[i].m, pv[i].value);
    if (subresult == NULL) {
      Py_DECREF(result);
      return NULL;
    }

    if (PyList_SetItem(result, i, subresult)) {
      Py_DECREF(subresult);
      Py_DECREF(result);
      return NULL;
    }
  }

  return result;
}
Exemplo n.º 19
0
 bool Bytes::isa ( Handle handle, bool exact )
 {
     return (exact? PyString_CheckExact(handle)
                  : PyString_Check     (handle));
 }
Exemplo n.º 20
0
/*
 * HTML attribute encoder
 */
PyObject *
tdi_soup_encode_attribute(PyObject *value, PyObject *encoding)
{
    PyObject *tmp;
    Py_ssize_t size, length;
    char *cvalue, *ctmp;
    int is_unicode = (PyUnicode_CheckExact(value) || PyUnicode_Check(value));
    char c;

    if (is_unicode) {
        if (!(value = PyUnicode_AsUTF8String(value)))
            return NULL;
    }
    else {
        if (!PyString_CheckExact(value) && !PyString_Check(value)) {
            PyErr_SetString(TDI_E_TemplateEncodingError,
                            "Attribute encoder takes string or unicode");
            return NULL;
        }
        Py_INCREF(value);
    }

    length = size = PyString_GET_SIZE(value);
    cvalue = PyString_AS_STRING(value);
    while (length--) {
        switch (*cvalue++) {
        case '&':
            size += 4;  /* amp; */
            break;
        case '<':
        case '>':
            size += 3;  /* lt; or gt; */
            break;
        case '"':
            size += 5;  /* quot; */
            break;
        }
    }
    /* +2 = surrounding quotes */
    if (!(tmp = PyString_FromStringAndSize(NULL, size + 2))) {
        Py_DECREF(value);
        return NULL;
    }
    ctmp = PyString_AS_STRING(tmp);
    cvalue = PyString_AS_STRING(value);
    length = PyString_GET_SIZE(value);
    *ctmp++ = '"';
    while (length--) {
        switch (c = *cvalue++) {
        case '&':
            (void)memcpy(ctmp, "&amp;", 5);
            ctmp += 5;
            break;
        case '<':
            (void)memcpy(ctmp, "&lt;", 4);
            ctmp += 4;
            break;
        case '>':
            (void)memcpy(ctmp, "&gt;", 4);
            ctmp += 4;
            break;
        case '"':
            (void)memcpy(ctmp, "&quot;", 6);
            ctmp += 6;
            break;
        default:
            *ctmp++ = c;
            break;
        }
    }
    *ctmp = '"';
    Py_DECREF(value);

    if (is_unicode) {
        const char *cencoding = PyString_AsString(encoding);
        if (!cencoding) {
            Py_DECREF(tmp);
            return NULL;
        }
        value = PyUnicode_DecodeUTF8(PyString_AS_STRING(tmp),
                                     PyString_GET_SIZE(tmp), "strict");
        Py_DECREF(tmp);
        if (!value)
            return NULL;
        tmp = PyUnicode_AsEncodedString(value, cencoding,
                                        "xmlcharrefreplace");
        Py_DECREF(value);
        if (!tmp)
            return NULL;
    }

    return tmp;
}
Exemplo n.º 21
0
static PyObject *
eval_frame(PyFrameObject *f)
{
	LOG("> eval_frame\n"); {
	PyObject **stack_pointer; /* Next free slot in value stack */
	register unsigned char *next_instr;
	register int opcode=0;	/* Current opcode */
	register int oparg=0;	/* Current opcode argument, if any */
	register enum why_code why; /* Reason for block stack unwind */
	register int err;	/* Error status -- nonzero if error */
	register PyObject *x;	/* Result object -- NULL if error */
	register PyObject *t, *u, *v;	/* Temporary objects popped off stack */
	register PyObject *w;
	register PyObject **fastlocals, **freevars;
	PyObject *retval = NULL;	/* Return value */
	PyThreadState *tstate = PyThreadState_GET();
	PyCodeObject *co;

	unsigned char *first_instr;
	PyObject *names;
	PyObject *consts;

/* Tuple access macros */

#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))

/* Code access macros */

#define INSTR_OFFSET()	(next_instr - first_instr)
#define NEXTOP()	(*next_instr++)
#define NEXTARG()	(next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
#define JUMPTO(x)	(next_instr = first_instr + (x))
#define JUMPBY(x)	(next_instr += (x))

/* OpCode prediction macros
	Some opcodes tend to come in pairs thus making it possible to predict
	the second code when the first is run.  For example, COMPARE_OP is often
	followed by JUMP_IF_FALSE or JUMP_IF_TRUE.  And, those opcodes are often
	followed by a POP_TOP.

	Verifying the prediction costs a single high-speed test of register
	variable against a constant.  If the pairing was good, then the
	processor has a high likelihood of making its own successful branch
	prediction which results in a nearly zero overhead transition to the
	next opcode.

	A successful prediction saves a trip through the eval-loop including
	its two unpredictable branches, the HASARG test and the switch-case.
*/

#define PREDICT(op)		if (*next_instr == op) goto PRED_##op
#define PREDICTED(op)		PRED_##op: next_instr++
#define PREDICTED_WITH_ARG(op)	PRED_##op: oparg = (next_instr[2]<<8) + \
				next_instr[1]; next_instr += 3

/* Stack manipulation macros */

#define STACK_LEVEL()	(stack_pointer - f->f_valuestack)
#define EMPTY()		(STACK_LEVEL() == 0)
#define TOP()		(stack_pointer[-1])
#define SECOND()	(stack_pointer[-2])
#define THIRD() 	(stack_pointer[-3])
#define FOURTH()	(stack_pointer[-4])
#define SET_TOP(v)	(stack_pointer[-1] = (v))
#define SET_SECOND(v)	(stack_pointer[-2] = (v))
#define SET_THIRD(v)	(stack_pointer[-3] = (v))
#define SET_FOURTH(v)	(stack_pointer[-4] = (v))
#define BASIC_STACKADJ(n)	(stack_pointer += n)
#define BASIC_PUSH(v)	(*stack_pointer++ = (v))
#define BASIC_POP()	(*--stack_pointer)

#define PUSH(v)		BASIC_PUSH(v)
#define POP()		BASIC_POP()
#define STACKADJ(n)	BASIC_STACKADJ(n)

/* Local variable macros */

#define GETLOCAL(i)	(fastlocals[i])

/* The SETLOCAL() macro must not DECREF the local variable in-place and
   then store the new value; it must copy the old value to a temporary
   value, then store the new value, and then DECREF the temporary value.
   This is because it is possible that during the DECREF the frame is
   accessed by other code (e.g. a __del__ method or gc.collect()) and the
   variable would be pointing to already-freed memory. */
#define SETLOCAL(i, value)	do { PyObject *tmp = GETLOCAL(i); \
				     GETLOCAL(i) = value; \
                                     Py_XDECREF(tmp); } while (0)

/* Start of code */

	if (f == NULL)
		return NULL;

	/* push frame */
	if (++tstate->recursion_depth > recursion_limit) {
		--tstate->recursion_depth;
		/* ERROR */
		tstate->frame = f->f_back;
		return NULL;
	}

	tstate->frame = f;

	/* tracing elided */

	co = f->f_code;
	names = co->co_names;
	consts = co->co_consts;
	fastlocals = f->f_localsplus;
	freevars = f->f_localsplus + f->f_nlocals;

	_PyCode_GETCODEPTR(co, &first_instr);

	/* An explanation is in order for the next line.

	   f->f_lasti now refers to the index of the last instruction
	   executed.  You might think this was obvious from the name, but
	   this wasn't always true before 2.3!  PyFrame_New now sets
	   f->f_lasti to -1 (i.e. the index *before* the first instruction)
	   and YIELD_VALUE doesn't fiddle with f_lasti any more.  So this
	   does work.  Promise. */
	next_instr = first_instr + f->f_lasti + 1;
	stack_pointer = f->f_stacktop;

	f->f_stacktop = NULL;	/* remains NULL unless yield suspends frame */

	why = WHY_NOT;
	err = 0;
	x = Py_None;	/* Not a reference, just anything non-NULL */
	w = NULL;

	for (;;) {

		/* Do periodic things.  Doing this every time through
		   the loop would add too much overhead, so we do it
		   only every Nth instruction.  We also do it if
		   ``things_to_do'' is set, i.e. when an asynchronous
		   event needs attention (e.g. a signal handler or
		   async I/O handler); see Py_AddPendingCall() and
		   Py_MakePendingCalls() above. */

		if (--_Py_Ticker < 0) {
			/* @@@ check for SETUP_FINALLY elided */

			_Py_Ticker = _Py_CheckInterval;
			tstate->tick_counter++;
			if (things_to_do) {
				if (Py_MakePendingCalls() < 0) {
					why = WHY_EXCEPTION;
					goto on_error;
				}
			}
		}

	fast_next_opcode:
		f->f_lasti = INSTR_OFFSET();

		/* Extract opcode and argument */

		opcode = NEXTOP();
		if (HAS_ARG(opcode))
			oparg = NEXTARG();

		/* Main switch on opcode */

		switch (opcode) {

		/* BEWARE!
		   It is essential that any operation that fails sets either
		   x to NULL, err to nonzero, or why to anything but WHY_NOT,
		   and that no operation that succeeds does this! */

		/* case STOP_CODE: this is an error! */

		case LOAD_FAST:
			x = GETLOCAL(oparg);
			if (x != NULL) {
				Py_INCREF(x);
				PUSH(x);
				goto fast_next_opcode;
			}
			/* ERROR? */
			break;

                case STORE_FAST:
                        v = POP();
                        SETLOCAL(oparg, v);
                        continue;

		case LOAD_CONST:
			x = GETITEM(consts, oparg);
			Py_INCREF(x);
			PUSH(x);
			goto fast_next_opcode;

		PREDICTED(POP_TOP);
		case POP_TOP:
			v = POP();
			Py_DECREF(v);
			goto fast_next_opcode;

		case UNARY_NOT:
			v = TOP();
			err = PyObject_IsTrue(v);
			Py_DECREF(v);
			if (err == 0) {
				Py_INCREF(Py_True);
				SET_TOP(Py_True);
				continue;
			}
			else if (err > 0) {
				Py_INCREF(Py_False);
				SET_TOP(Py_False);
				err = 0;
				continue;
			}
			STACKADJ(-1);
			break;

		case BINARY_MODULO:
			w = POP();
			v = TOP();
			x = PyNumber_Remainder(v, w);
			Py_DECREF(v);
			Py_DECREF(w);
			SET_TOP(x);
			if (x != NULL) continue;
			break;
			
		case BINARY_ADD:
			w = POP();
			v = TOP();
			if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
				/* INLINE: int + int */
				register long a, b, i;
				a = PyInt_AS_LONG(v);
				b = PyInt_AS_LONG(w);
				i = a + b;
				if ((i^a) < 0 && (i^b) < 0)
					goto slow_add;
				x = PyInt_FromLong(i);
			}
			else {
			  slow_add:
				Py_FatalError("slow add not supported.");
			}
			Py_DECREF(v);
			Py_DECREF(w);
			SET_TOP(x);
			if (x != NULL) continue;
			break;

                case STORE_SLICE+0:
                case STORE_SLICE+1:
                case STORE_SLICE+2:
                case STORE_SLICE+3:
                        if ((opcode-STORE_SLICE) & 2)
                                w = POP();
                        else
                                w = NULL;
                        if ((opcode-STORE_SLICE) & 1)
                                v = POP();
                        else
                                v = NULL;
                        u = POP();
                        t = POP();
                        err = assign_slice(u, v, w, t); /* u[v:w] = t */
                        Py_DECREF(t);
                        Py_DECREF(u);
                        Py_XDECREF(v);
                        Py_XDECREF(w);
                        if (err == 0) continue;
                        break;

                case STORE_SUBSCR:
                        w = POP();
                        v = POP();
                        u = POP();
                        /* v[w] = u */
                        err = PyObject_SetItem(v, w, u);
                        Py_DECREF(u);
                        Py_DECREF(v);
                        Py_DECREF(w);
                        if (err == 0) continue;
                        break;

                case BINARY_SUBSCR:
                        w = POP();
                        v = TOP();
                        if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
                                /* INLINE: list[int] */
                                long i = PyInt_AsLong(w);
                                if (i < 0)
                                        i += PyList_GET_SIZE(v);
                                if (i < 0 ||
                                    i >= PyList_GET_SIZE(v)) {
                                        /* ERROR */
                                        printf("list index out of range\n");
                                        x = NULL;
                                }
                                else {
                                        x = PyList_GET_ITEM(v, i);
                                        Py_INCREF(x);
                                }
                        }
                        else
                                x = PyObject_GetItem(v, w);
                        Py_DECREF(v);
                        Py_DECREF(w);
                        SET_TOP(x);
                        if (x != NULL) continue;
                        break;

		case BINARY_AND:
			w = POP();
			v = TOP();
			x = PyNumber_And(v, w);
			Py_DECREF(v);
			Py_DECREF(w);
			SET_TOP(x);
			if (x != NULL) continue;
			break;

		case PRINT_ITEM:
			v = POP();

			PyObject_Print(v);
			Py_DECREF(v);
			break;

		case PRINT_NEWLINE:
			printf("\n");
			break;

		case RETURN_VALUE:
			retval = POP();
			why = WHY_RETURN;
			break;

		case POP_BLOCK:
			{
				PyTryBlock *b = PyFrame_BlockPop(f);
				while (STACK_LEVEL() > b->b_level) {
					v = POP();
					Py_DECREF(v);
				}
			}
			break;

		case STORE_NAME:
			w = GETITEM(names, oparg);
			v = POP();
			if ((x = f->f_locals) == NULL) {
				/* ERROR */
				printf("STORE_NAME ERROR\n");
				break;
			}
			err = PyDict_SetItem(x, w, v);
			Py_DECREF(v);
			break;

		case LOAD_NAME:
			w = GETITEM(names, oparg);
			if ((x = f->f_locals) == NULL) {
				/* ERROR */
				printf("LOAD_NAME ERROR\n");
				break;
			}
			x = PyDict_GetItem(x, w);
			if (x == NULL) {
				x = PyDict_GetItem(f->f_globals, w);
				if (x == NULL) {
					x = PyDict_GetItem(f->f_builtins, w);
					if (x == NULL) {
						printf("can't find %s\n", ((PyStringObject *)w)->ob_sval);
						/* format_exc_check_arg */
						break;
					}
				}
			}
			Py_INCREF(x);
			PUSH(x);
			break;
			
		case LOAD_GLOBAL:
			w = GETITEM(names, oparg);
			if (PyString_CheckExact(w)) {
				/* Inline the PyDict_GetItem() calls.
				   WARNING: this is an extreme speed hack.
				   Do not try this at home. */
				long hash = ((PyStringObject *)w)->ob_shash;
				if (hash != -1) {
					PyDictObject *d;
					d = (PyDictObject *)(f->f_globals);
					x = d->ma_lookup(d, w, hash)->me_value;
					if (x != NULL) {
						Py_INCREF(x);
						PUSH(x);
						continue;
					}
					d = (PyDictObject *)(f->f_builtins);
					x = d->ma_lookup(d, w, hash)->me_value;
					if (x != NULL) {
						Py_INCREF(x);
						PUSH(x);
						continue;
					}
					goto load_global_error;
				}
			}
			/* This is the un-inlined version of the code above */
			x = PyDict_GetItem(f->f_globals, w);
			if (x == NULL) {
				x = PyDict_GetItem(f->f_builtins, w);
				if (x == NULL) {
				  load_global_error:
					printf("LOAD_GLOBAL ERROR %s", ((PyStringObject *)w)->ob_sval);
					break;
				}
			}
			Py_INCREF(x);
			PUSH(x);
			break;

		case LOAD_ATTR:
			w = GETITEM(names, oparg);
			v = TOP();
			x = PyObject_GetAttr(v, w);
			Py_DECREF(v);
			SET_TOP(x);
			if (x != NULL) continue;
			break;

		case IMPORT_NAME:
			w = GETITEM(names, oparg);
			x = PyDict_GetItemString(f->f_builtins, "__import__");
			if (x == NULL) {
				printf("__import__ not found");
				break;
			}
			u = TOP();
			w = Py_BuildValue("(O)", w);
			Py_DECREF(u);
			if (w == NULL) {
				u = POP();
				x = NULL;
				break;
			}
			x = PyEval_CallObject(x, w);
			Py_DECREF(w);
			SET_TOP(x);
			if (x != NULL) continue;
			break;

		case JUMP_FORWARD:
			JUMPBY(oparg);
			goto fast_next_opcode;

		PREDICTED_WITH_ARG(JUMP_IF_FALSE);
		case JUMP_IF_FALSE:
			w = TOP();
			if (w == Py_True) {
				PREDICT(POP_TOP);
				goto fast_next_opcode;
			}
			if (w == Py_False) {
				JUMPBY(oparg);
				goto fast_next_opcode;
			}
			err = PyObject_IsTrue(w);
			if (err > 0)
				err = 0;
			else if (err == 0)
				JUMPBY(oparg);
			else
				break;
			continue;
			
		case JUMP_ABSOLUTE:
			JUMPTO(oparg);
			continue;

		case SETUP_LOOP:
			PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, STACK_LEVEL());
			continue;

		case CALL_FUNCTION:
			x = call_function(&stack_pointer, oparg);
			PUSH(x);
			if (x != NULL)
				continue;
			break;
			
		case MAKE_FUNCTION:
			v = POP(); /* code object */
			x = PyFunction_New(v, f->f_globals);
			Py_DECREF(v);
			/* XXX Maybe this should be a separate opcode? */
			if (x != NULL && oparg > 0) {
				v = PyTuple_New(oparg);
				if (v == NULL) {
					Py_DECREF(x);
					x = NULL;
			break;
				}
				while (--oparg >= 0) {
					w = POP();
					PyTuple_SET_ITEM(v, oparg, w);
				}
				err = PyFunction_SetDefaults(x, v);
				Py_DECREF(v);
			}
			PUSH(x);
			break;
			
		case SET_LINENO:
			break;

		default:
			printf("opcode: %d\n", opcode);
			Py_FatalError("unknown opcode");
		} /* switch */

	  on_error:
		
		if (why == WHY_NOT) {
			if (err == 0 && x != NULL) {
					continue; /* Normal, fast path */
			}
			why = WHY_EXCEPTION;
			x = Py_None;
			err = 0;
		}

		/* End the loop if we still have an error (or return) */

		if (why != WHY_NOT)
			break;

	} /* main loop */

	if (why != WHY_YIELD) {
		/* Pop remaining stack entries -- but when yielding */
		while (!EMPTY()) {
			v = POP();
			Py_XDECREF(v);
		}
	}

	if (why != WHY_RETURN && why != WHY_YIELD)
		retval = NULL;

	/* pop frame */
	--tstate->recursion_depth;
	tstate->frame = f->f_back;

	return retval;
}}
Exemplo n.º 22
0
static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
{
    struct tree_item *qsort_entries;
    int num, i;
    PyObject *ret;
    Py_ssize_t pos = 0;
    PyObject *key, *value;

    if (!PyDict_Check(entries)) {
        PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
        return NULL;
    }

    num = PyDict_Size(entries);
    qsort_entries = malloc(num * sizeof(struct tree_item));
    if (qsort_entries == NULL) {
        PyErr_NoMemory();
        return NULL;
    }

    i = 0;
    while (PyDict_Next(entries, &pos, &key, &value)) {
        PyObject *py_mode, *py_sha;

        if (PyTuple_Size(value) != 2) {
            PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
            free(qsort_entries);
            return NULL;
        }

        py_mode = PyTuple_GET_ITEM(value, 0);
        py_sha = PyTuple_GET_ITEM(value, 1);
        qsort_entries[i].tuple = Py_BuildValue("(OOO)", key, py_mode, py_sha);
        if (!PyString_CheckExact(key)) {
            PyErr_SetString(PyExc_TypeError, "Name is not a string");
            free(qsort_entries);
            return NULL;
        }
        qsort_entries[i].name = PyString_AS_STRING(key);
        if (!PyInt_CheckExact(py_mode)) {
            PyErr_SetString(PyExc_TypeError, "Mode is not an int");
            free(qsort_entries);
            return NULL;
        }
        qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
        i++;
    }

    qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);

    ret = PyList_New(num);
    if (ret == NULL) {
        free(qsort_entries);
        PyErr_NoMemory();
        return NULL;
    }

    for (i = 0; i < num; i++) {
        PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
    }

    free(qsort_entries);

    return ret;
}
Exemplo n.º 23
0
PyObject *string_join(PyStringObject *self, PyObject *orig)
{
    char *sep = PyString_AS_STRING(self);
    const Py_ssize_t seplen = PyString_GET_SIZE(self);
    PyObject *res = NULL;
    char *p;
    Py_ssize_t seqlen = 0;
    size_t sz = 0;
    Py_ssize_t i;
    PyObject *seq, *item;

    seq = PySequence_Fast(orig, "");
    if (seq == NULL) {
        return NULL;
    }

    seqlen = PySequence_Size(seq);
    if (seqlen == 0) {
        Py_DECREF(seq);
        return PyString_FromString("");
    }
    if (seqlen == 1) {
        item = PySequence_Fast_GET_ITEM(seq, 0);
        if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) {
            Py_INCREF(item);
            Py_DECREF(seq);
            return item;
        }
    }

    /* There are at least two things to join, or else we have a subclass
     * of the builtin types in the sequence.
     * Do a pre-pass to figure out the total amount of space we'll
     * need (sz), see whether any argument is absurd, and defer to
     * the Unicode join if appropriate.
     */
    for (i = 0; i < seqlen; i++) {
        const size_t old_sz = sz;
        item = PySequence_Fast_GET_ITEM(seq, i);
        if (!PyString_Check(item)){
#ifdef Py_USING_UNICODE
            if (PyUnicode_Check(item)) {
                /* Defer to Unicode join.
                 * CAUTION:  There's no gurantee that the
                 * original sequence can be iterated over
                 * again, so we must pass seq here.
                 */
                PyObject *result;
                result = PyUnicode_Join((PyObject *)self, seq);
                Py_DECREF(seq);
                return result;
            }
#endif
            PyErr_Format(PyExc_TypeError,
                         "sequence item %zd: expected string,"
                         " %.80s found",
                         i, Py_TYPE(item)->tp_name);
            Py_DECREF(seq);
            return NULL;
        }
        sz += PyString_GET_SIZE(item);
        if (i != 0)
            sz += seplen;
        if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
            PyErr_SetString(PyExc_OverflowError,
                "join() result is too long for a Python string");
            Py_DECREF(seq);
            return NULL;
        }
    }

    /* Allocate result space. */
    res = PyString_FromStringAndSize((char*)NULL, sz);
    if (res == NULL) {
        Py_DECREF(seq);
        return NULL;
    }

    /* Catenate everything. */
    p = PyString_AS_STRING(res);
    for (i = 0; i < seqlen; ++i) {
        size_t n;
        item = PySequence_Fast_GET_ITEM(seq, i);
        n = PyString_GET_SIZE(item);
        Py_MEMCPY(p, PyString_AS_STRING(item), n);
        p += n;
        if (i < seqlen - 1) {
            Py_MEMCPY(p, sep, seplen);
            p += seplen;
        }
    }

    Py_DECREF(seq);
    return res;
}
Exemplo n.º 24
0
static void mod_vptuple(TALLOC_CTX *ctx, VALUE_PAIR **vps, PyObject *pValue,
			char const *funcname)
{
	int	     i;
	int	     tuplesize;
	VALUE_PAIR      *vp;

	/*
	 *	If the Python function gave us None for the tuple,
	 *	then just return.
	 */
	if (pValue == Py_None)
		return;

	if (!PyTuple_CheckExact(pValue)) {
		ERROR("rlm_python:%s: non-tuple passed", funcname);
		return;
	}
	/* Get the tuple tuplesize. */
	tuplesize = PyTuple_GET_SIZE(pValue);
	for (i = 0; i < tuplesize; i++) {
		PyObject *pTupleElement = PyTuple_GET_ITEM(pValue, i);
		PyObject *pStr1;
		PyObject *pStr2;
		PyObject *pOp;
		int pairsize;
		char const *s1;
		char const *s2;
		long op;

		if (!PyTuple_CheckExact(pTupleElement)) {
			ERROR("rlm_python:%s: tuple element %d is not a tuple", funcname, i);
			continue;
		}
		/* Check if it's a pair */

		pairsize = PyTuple_GET_SIZE(pTupleElement);
		if ((pairsize < 2) || (pairsize > 3)) {
			ERROR("rlm_python:%s: tuple element %d is a tuple of size %d. Must be 2 or 3.", funcname, i, pairsize);
			continue;
		}

		if (pairsize == 2) {
			pStr1	= PyTuple_GET_ITEM(pTupleElement, 0);
			pStr2	= PyTuple_GET_ITEM(pTupleElement, 1);
			op	= T_OP_EQ;
		} else {
			pStr1	= PyTuple_GET_ITEM(pTupleElement, 0);
			pStr2	= PyTuple_GET_ITEM(pTupleElement, 2);
			pOp	= PyTuple_GET_ITEM(pTupleElement, 1);
			op	= PyInt_AsLong(pOp);
		}

		if ((!PyString_CheckExact(pStr1)) || (!PyString_CheckExact(pStr2))) {
			ERROR("rlm_python:%s: tuple element %d must be as (str, str)", funcname, i);
			continue;
		}
		s1 = PyString_AsString(pStr1);
		s2 = PyString_AsString(pStr2);
		vp = pairmake(ctx, vps, s1, s2, op);
		if (vp != NULL) {
			DEBUG("rlm_python:%s: '%s' = '%s'", funcname, s1, s2);
		} else {
			DEBUG("rlm_python:%s: Failed: '%s' = '%s'", funcname, s1, s2);
		}
	}
}
Exemplo n.º 25
0
// python_apply implements the function call:
// (python-apply ("module.submodule" 'obj 'func)
//               (arg1 arg2 arg3)
//               (('keyword1 . val4) ('keyword2 . val5))
//               sargtemplate
//               skwtemplate)
// which is the basic way to invoke a Python function.
//
// sfunc specifies the function to be invoked.  The possibilities
// are:
//   String - denotes a top level function ("func" means __main__.func).
//   pysmob - assumed to be a callable object.
//   ("module.submodule" ...) - a List of strings/symbols/keywords
//     in which the first item must be a string denotes:
//     Module "module.submodule" (which should have already been imported
//       using python-import)
//     followed by name of object in that module, followed by attribute,...,
//     until the final callable attribute.
//   (pysmob ...) - a List starting with pysmob followed by
//     strings/symbols/keywords - processed similarly, except that the
//     pysmob stands for the module.
// sarg is a list of arguments (in Python it's *arg)
// skw is an alist (in Python it's **kw).
// sargtemplate - specifies how to convert sarg - optional argument.
// skwtemplate - specifies how to convert skw - optional argument.
// srestemplate - specifies how to convert the result back into
//     SCM - optional argument.
SCM
python_apply(SCM sfunc, SCM sarg, SCM skw,
	     SCM sargtemplate, SCM skwtemplate,	SCM srestemplate)
{
  PyObject *pfunc = NULL;
  PyObject *parg = NULL;
  PyObject *pkw = NULL;

  PyObject *pfuncobj = NULL;
  PyObject *pres = NULL;
  SCM sres = SCM_UNDEFINED;

  if (SCM_UNBNDP(sargtemplate)) { //(sargtemplate == SCM_UNDEFINED) // SCM_UNSPECIFIED
    sargtemplate = sargtemplate_default;
  }
  if (SCM_UNBNDP(skwtemplate)) {
    skwtemplate = skwtemplate_default;
  }
  if (SCM_UNBNDP(srestemplate)) {
    srestemplate = srestemplate_default;
  }

  // Retrieve the function object.

  pfunc = guile2python(sfunc,SCM_UNSPECIFIED);
  if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) {
    char *preprfunc = PyString_AsString(PyObject_Repr(pfunc));
    scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: decoded pfunc ~S\n"),scm_list_1(scm_makfrom0str(preprfunc)));
  }
  if (NULL == pfunc) {
    scm_misc_error("python-apply","conversion failure (~S)",
		   scm_list_1(SCM_CDR(sfunc)));
  }
  // If it is a string, prepend it with "__main__".
  if (PyString_CheckExact(pfunc)) {
    // Convert it into a List of two items, to unify
    // subsequent treatment.
    PyObject *plist = PyList_New(2);
    if (NULL == plist) {
      Py_DECREF(pfunc);  // NOT COVERED BY TESTS
      scm_memory_error("python-apply");  // NOT COVERED BY TESTS
    }
    if (-1 == PyList_SetItem(plist,0,PyString_FromString("__main__"))) {
      Py_DECREF(pfunc);  // NOT COVERED BY TESTS
      Py_DECREF(plist);  // NOT COVERED BY TESTS
      scm_misc_error("python-apply","PyList_SetItem 0 failure (~S)",  // NOT COVERED BY TESTS
		     scm_list_1(SCM_CAR(sfunc)));
    }
    if (-1 == PyList_SetItem(plist,1,pfunc)) {
      Py_DECREF(pfunc);  // NOT COVERED BY TESTS
      Py_DECREF(plist);  // NOT COVERED BY TESTS
      scm_misc_error("python-apply","PyList_SetItem 1 failure (~S)",  // NOT COVERED BY TESTS
		     scm_list_1(SCM_CAR(sfunc)));
    }
    pfunc = plist;  // plist stole previous pfunc's value's reference.
  }
  else if (IS_PYSMOBP(sfunc)) {
    // We check the SCM object because guile2python() destroys
    // the indication whether the SCM was originally a pysmob, when it
    // converts it into PyObject.
    PyObject *plist1 = PyList_New(1);
    if (NULL == plist1) {
      Py_DECREF(pfunc);  // NOT COVERED BY TESTS
      scm_memory_error("python-apply");  // NOT COVERED BY TESTS
    }
    if (-1 == PyList_SetItem(plist1,0,pfunc)) {
      Py_DECREF(pfunc);  // NOT COVERED BY TESTS
      Py_DECREF(plist1);  // NOT COVERED BY TESTS
      scm_misc_error("python-apply","PyList_SetItem 0 failure (~S)",  // NOT COVERED BY TESTS
		     scm_list_1(SCM_CAR(sfunc)));
    }
    pfunc = plist1;  // plist1 stole previous pfunc's value's reference.
    // Now pfunc is an 1-member list, and this member is
    // expected to be callable.
  }
  else if (!PyList_CheckExact(pfunc)) {
    // Now, the qualified function name must be a proper list.
    scm_wrong_type_arg("python-apply",SCM_ARG1,sfunc);
  }

  if (1 > PyList_Size(pfunc)) {
    // The list must consist of at least one callable module name/object.
    scm_misc_error("python-apply",
		   "first argument must contain at least one callable object (~S)",
		   scm_list_1(SCM_CAR(sfunc)));
  }

  if (PyString_CheckExact(PyList_GetItem(pfunc,0))) {
    // If it is a string, we assume it to be the name of a module
    // which has already been imported.
    // Due to the existence of dots, 
    // we don't allow it to be symbol or keyword.

    pfuncobj = PyImport_AddModule(PyString_AsString(PyList_GetItem(pfunc,0)));
    if (NULL == pfuncobj) {
      Py_DECREF(pfunc);
      scm_misc_error("python-apply",
		     "module ~S could not be accessed - probably not imported",
		     scm_list_1(SCM_CAR(sfunc)));
    }
    Py_INCREF(pfuncobj);
  }
  else {
    // We assume that it is a callable or object with attributes.
    pfuncobj = PyList_GetItem(pfunc,0);
    if (NULL == pfuncobj) {
      Py_DECREF(pfunc);
      scm_misc_error("python-apply",
		     "could not access object starting ~S",
		     scm_list_1(sfunc));
    }
    Py_INCREF(pfuncobj);
  }

  // Here we dereference attributes (if any).
  int listsize = PyList_Size(pfunc);
  int ind;

  for (ind = 1; ind < listsize; ++ind) {
    PyObject *pnextobj = PyObject_GetAttr(pfuncobj,PyList_GetItem(pfunc,ind));
    if (NULL == pnextobj) {
      PyObject *pexception = PyErr_Occurred();
      Py_DECREF(pfunc);
      Py_DECREF(pfuncobj);
      if (pexception) {
	PyErr_Clear();
	// An AttributeError exception is expected here.
	if (!PyErr_GivenExceptionMatches(pexception,PyExc_AttributeError)) {
	  PyObject *prepr = PyObject_Repr(pexception);
	  if (NULL == prepr) {
	    scm_misc_error("python-apply",
			   "python exception - could not be identified",
			   SCM_UNSPECIFIED);
	  }
	  else {
	    int strlength = PyString_Size(prepr);
	    char *pstr = PyString_AsString(prepr);
	    SCM srepr = scm_list_1(scm_mem2string(pstr,strlength));
	    Py_DECREF(prepr);
	    scm_misc_error("python-apply",
			   "Python exception (~A) while dereferencing object attribute",
			   srepr);
	  }
	}
	// else we got the expected AttributeError exception.
      }
      // else we got NULL==pnextobj without Python exception.
      scm_misc_error("python-apply",
		     "could not dereference ~Ath level attribute in ~S",
		     scm_list_2(scm_long2num(ind),sfunc));
    }
    Py_INCREF(pnextobj);
    Py_DECREF(pfuncobj);
    pfuncobj = pnextobj;
  }
  Py_DECREF(pfunc);  // We do not need it anymore.  pfuncobj points at
                     // the function actually to be invoked.
  if (!PyCallable_Check(pfuncobj)) {
    Py_DECREF(pfuncobj);
    scm_misc_error("python-apply","function denoted by ~S is not callable",scm_list_1(sfunc));
  }

  if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) {
    char *preprfuncobj = PyString_AsString(PyObject_Repr(pfuncobj));
    scm_simple_format(scm_current_output_port(),
		      scm_makfrom0str("# python_apply: decoded function actually to be invoked: ~S\n"),
		      scm_list_1(scm_makfrom0str(preprfuncobj)));
  }

  // Retrieve positional arguments

  parg = g2p_apply(sarg,sargtemplate);
  if (NULL == parg) {
    Py_DECREF(pfuncobj);
    scm_misc_error("python-apply","positional arguments conversion failure (~S)",
		   scm_list_1(sarg));
  }
  // Validate that it is indeed a tuple.
  if (!PyTuple_CheckExact(parg)) {
    Py_DECREF(pfuncobj);
    Py_DECREF(parg);
    scm_wrong_type_arg("python-apply",SCM_ARG2,sarg);
  }
  if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) {
    char *pposarg = PyString_AsString(PyObject_Repr(parg));
    scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: decoded positional arguments ~S\n"),scm_list_1(scm_makfrom0str(pposarg)));
  }

  // Retrieve keyword arguments.

  pkw = guileassoc2pythondict(skw,skwtemplate);
  if (NULL == pkw) {
    // Seems that PyDict_CheckExact() does not handle NULL argument gracefully.
     Py_DECREF(pfuncobj);
     Py_DECREF(parg);
     scm_misc_error("python-apply","keyword arguments conversion failure (~S)",
		    scm_list_1(skw));
  }
  if (!PyDict_CheckExact(pkw)) {
     Py_DECREF(pfuncobj);
     Py_DECREF(parg);
     Py_DECREF(pkw);
     scm_misc_error("python-apply",
		    "keyword arguments (~S) not properly converted into Python Dict",
		    scm_list_1(skw));
  }
  if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) {
    char *pkwarg = PyString_AsString(PyObject_Repr(pkw));
    scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: decoded keyword arguments ~S\n"),scm_list_1(scm_makfrom0str(pkwarg)));
  }

  // Ready to invoke the function.

  pres = PyEval_CallObjectWithKeywords(pfuncobj,parg,pkw);
  PyObject *pexception = PyErr_Occurred();
  if (pexception) {
    PyObject *prepr = PyObject_Repr(pexception);
    Py_DECREF(pfuncobj);
    Py_DECREF(parg);
    Py_DECREF(pkw);
    Py_XDECREF(pres);
    PyErr_Clear();
    if (NULL == prepr) {
      scm_misc_error("python-apply",
		     "python exception - could not be identified",
		     SCM_UNSPECIFIED);
    }
    else {
      int strlength = PyString_Size(prepr);
      char *pstr = PyString_AsString(prepr);
      SCM srepr = scm_list_1(scm_mem2string(pstr,strlength));
      Py_DECREF(prepr);
      scm_misc_error("python-apply","Python exception: ~A",
		     srepr);
    }
  }
  if (NULL != pres) {
    sres = p2g_apply(pres,srestemplate);
    if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) {
      char *presstr = PyString_AsString(PyObject_Repr(pres));
      scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: decoded results:\n#     Python: ~S\n#     Scheme: ~S\n"),scm_list_2(scm_makfrom0str(presstr),sres));
    }
  }
  else {
    // else sres remains SCM_UNDEFINED.
    if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) {
      scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: Python code returned <NULL>\n"),SCM_EOL);
    }
  }
  return(sres);
}
Exemplo n.º 26
0
static int __Pyx_ParseOptionalKeywords(
    PyObject *kwds,
    PyObject **argnames[],
    PyObject *kwds2,
    PyObject *values[],
    Py_ssize_t num_pos_args,
    const char* function_name)
{
    PyObject *key = 0, *value = 0;
    Py_ssize_t pos = 0;
    PyObject*** name;
    PyObject*** first_kw_arg = argnames + num_pos_args;

    while (PyDict_Next(kwds, &pos, &key, &value)) {
        name = first_kw_arg;
        while (*name && (**name != key)) name++;
        if (*name) {
            values[name-argnames] = value;
            continue;
        }

        name = first_kw_arg;
        #if PY_MAJOR_VERSION < 3
        if (likely(PyString_CheckExact(key)) || likely(PyString_Check(key))) {
            while (*name) {
                if ((CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**name) == PyString_GET_SIZE(key))
                        && _PyString_Eq(**name, key)) {
                    values[name-argnames] = value;
                    break;
                }
                name++;
            }
            if (*name) continue;
            else {
                // not found after positional args, check for duplicate
                PyObject*** argname = argnames;
                while (argname != first_kw_arg) {
                    if ((**argname == key) || (
                            (CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**argname) == PyString_GET_SIZE(key))
                             && _PyString_Eq(**argname, key))) {
                        goto arg_passed_twice;
                    }
                    argname++;
                }
            }
        } else
        #endif
        if (likely(PyUnicode_Check(key))) {
            while (*name) {
                int cmp = (**name == key) ? 0 :
                #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3
                    (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                #endif
                    // need to convert argument name from bytes to unicode for comparison
                    PyUnicode_Compare(**name, key);
                if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;
                if (cmp == 0) {
                    values[name-argnames] = value;
                    break;
                }
                name++;
            }
            if (*name) continue;
            else {
                // not found after positional args, check for duplicate
                PyObject*** argname = argnames;
                while (argname != first_kw_arg) {
                    int cmp = (**argname == key) ? 0 :
                    #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3
                        (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                    #endif
                        // need to convert argument name from bytes to unicode for comparison
                        PyUnicode_Compare(**argname, key);
                    if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;
                    if (cmp == 0) goto arg_passed_twice;
                    argname++;
                }
            }
        } else
            goto invalid_keyword_type;

        if (kwds2) {
            if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
        } else {
            goto invalid_keyword;
        }
    }
    return 0;
arg_passed_twice:
    __Pyx_RaiseDoubleKeywordsError(function_name, key);
    goto bad;
invalid_keyword_type:
    PyErr_Format(PyExc_TypeError,
        "%.200s() keywords must be strings", function_name);
    goto bad;
invalid_keyword:
    PyErr_Format(PyExc_TypeError,
    #if PY_MAJOR_VERSION < 3
        "%.200s() got an unexpected keyword argument '%.200s'",
        function_name, PyString_AsString(key));
    #else
        "%s() got an unexpected keyword argument '%U'",
        function_name, key);
    #endif
bad:
    return -1;
}
Exemplo n.º 27
0
/* FlvScreen.blit_rgba(x,y,w,h,data)
 *   copy the rgba data.
 */
static PyObject*
FlvScreen_blit_rgba(PyFlvScreen* self, PyObject* args)
{
    PyObject* data;
    int px, py, pw, ph;
    int changes = 0;
    int blk_size = self->blk_size;

    if (!PyArg_ParseTuple(args, "iiiiO", &px, &py, &pw, &ph, &data)) {
	return NULL;
    }

    /* check the data type */
    if (!PyString_CheckExact(data)) {
	PyErr_SetString(PyExc_TypeError, "must be string");
	return NULL;
    }
    /* check the data size */
    if (PyString_Size(data) != pw*ph*sizeof(RGBAPixel)) {
	PyErr_SetString(PyExc_FlvError, "invalid data size");
	return NULL;
    }

    /* copy the image data */
    {
	RGBAPixel* src = (RGBAPixel*)PyString_AsString(data);
	int bx0 = idiv(px, blk_size);
	int bx1 = idiv(px+pw-1, blk_size);
	for (; 0 < ph; ph--, py++, src += pw) {
	    int bx;
	    int by = idiv(py, blk_size);
	    int changed = 0;
	    if (by < 0 || self->blk_height <= by) continue;
	    for (bx = bx0; bx <= bx1; bx++) {
		int px0 = bx * blk_size;
		int px1 = (bx+1) * blk_size;
		unsigned char* blk = &self->blocks[by*self->blk_width + bx];
		RGBAPixel* dst = &self->pixels[py*self->pix_width + px0];
		int i, j, n;
		if (bx < 0 || self->blk_width <= bx) continue;
		if (px0 < px) {
		    i = 0;
		    j = px-px0;
		    if (px+pw < px1) {
			/*     px
			 *     |<-pw->|
			 *  +--|------|-+
			 *  |  |<====>| |
			 *  px0         px1
			 */
			n = pw;
		    } else {
			/*     px
			 *     |<-pw-|-->
			 *  +--|-----+
			 *  |  |<===>|
			 *  px0      px1
			 */
			n = px1-px;
		    }
		} else {
		    i = px0-px;
		    j = 0;
		    if (px+pw < px1) {
			/*  px
			 *  |<--|-pw->|
			 *      +-----|-+
			 *      |<===>| |
			 *      px0     px1
			 */
			n = px+pw-px0;
		    } else {
			/*  px
			 *  |<--|-pw-|-->|
			 *      +----+
			 *      |<==>|
			 *      px0  px1
			 */
			n = blk_size;
		    }
		}
		n *= sizeof(RGBAPixel);
		if (memcmp(&dst[j], &src[i], n)) {
		    *blk = 1;
		    changed = 1;
		}
		memcpy(&dst[j], &src[i], n);
	    }
	    if (changed) {
		changes++;
	    }
	}
    }

    return PyInt_FromLong(changes);
}