Ejemplo n.º 1
0
int uwsgi_python_profiler_call(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg) {

#ifndef UWSGI_PYPY
	static uint64_t last_ts = 0;
        uint64_t now = uwsgi_micros();
        uint64_t delta = 0;

	switch(what) {
		case PyTrace_CALL:
			if (last_ts == 0) delta = 0;
                	else delta = now - last_ts;
                	last_ts = now;
			uwsgi_log("[uWSGI Python profiler %llu] CALL: %s (line %d) -> %s %d args, stacksize %d\n",
				(unsigned long long) delta,
				PyString_AsString(frame->f_code->co_filename),
				PyFrame_GetLineNumber(frame),
				PyString_AsString(frame->f_code->co_name), frame->f_code->co_argcount, frame->f_code->co_stacksize);
			break;
		case PyTrace_C_CALL:
			if (last_ts == 0) delta = 0;
                	else delta = now - last_ts;
                	last_ts = now;
			uwsgi_log("[uWSGI Python profiler %llu] C CALL: %s (line %d) -> %s %d args, stacksize %d\n",
				(unsigned long long) delta,
				PyString_AsString(frame->f_code->co_filename),
				PyFrame_GetLineNumber(frame),
				PyEval_GetFuncName(arg), frame->f_code->co_argcount, frame->f_code->co_stacksize);
			break;
	}
#endif

	return 0;
}
Ejemplo n.º 2
0
int uwsgi_python_profiler_call(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg) {

#ifndef UWSGI_PYPY
	switch(what) {
		case PyTrace_CALL:
			uwsgi_log("[uWSGI Python profiler] CALL: %s (line %d) -> %s %d args, stacksize %d\n",
				PyString_AsString(frame->f_code->co_filename),
				PyFrame_GetLineNumber(frame),
				PyString_AsString(frame->f_code->co_name), frame->f_code->co_argcount, frame->f_code->co_stacksize);
			break;
		case PyTrace_C_CALL:
			uwsgi_log("[uWSGI Python profiler] C CALL: %s (line %d) -> %s %d args, stacksize %d\n",
				PyString_AsString(frame->f_code->co_filename),
				PyFrame_GetLineNumber(frame),
				PyEval_GetFuncName(arg), frame->f_code->co_argcount, frame->f_code->co_stacksize);
			break;
	}
#endif

	return 0;
}
Ejemplo n.º 3
0
static char const *GET_CALLABLE_NAME( PyObject *object )
{
    if ( Nuitka_Function_Check( object ) )
    {
        return Nuitka_String_AsString( Nuitka_Function_GetName( object ) );
    }
    else if ( Nuitka_Generator_Check( object ) )
    {
        return Nuitka_String_AsString( Nuitka_Generator_GetName( object ) );
    }
    else if ( PyMethod_Check( object ) )
    {
        return PyEval_GetFuncName( PyMethod_GET_FUNCTION( object ) );
    }
    else if ( PyFunction_Check( object ) )
    {
        return Nuitka_String_AsString( ((PyFunctionObject*)object)->func_name );
    }
#if PYTHON_VERSION < 300
    else if ( PyInstance_Check( object ) )
    {
        return Nuitka_String_AsString( ((PyInstanceObject*)object)->in_class->cl_name );
    }
    else if ( PyClass_Check( object ) )
    {
        return Nuitka_String_AsString( ((PyClassObject*)object)->cl_name );
    }
#endif
    else if ( PyCFunction_Check( object ) )
    {
        return ((PyCFunctionObject*)object)->m_ml->ml_name;
    }
    else
    {
        return Py_TYPE( object )->tp_name;
    }
}
Ejemplo n.º 4
0
static PyObject* Preprocessor_define(Preprocessor* self, PyObject *args)
{
    PyObject *macro;
    PyObject *value = NULL;
    if (!PyArg_ParseTuple(args, "O|O:define", &macro, &value))
        return NULL;

    try
    {
        // First check if it's a string. If so, convert it to UTF-8 and define
        // an object-like macro.
        if (PyUnicode_Check(macro))
        {
            ScopedPyObject utf8_name(PyUnicode_AsUTF8String(macro));
            const char *macro_name;
            if (utf8_name && (macro_name = PyBytes_AsString(utf8_name)))
            {
                if (value)
                {
                    if (PyUnicode_Check(value)) // define(name, value)
                    {
                        ScopedPyObject utf8_value(
                            PyUnicode_AsUTF8String(value));
                        const char *macro_value;
                        if (utf8_value &&
                            (macro_value = PyBytes_AsString(utf8_value)))
                        {
                            // Value specified:
                            //     "#define macro_name macro_value".
                            self->preprocessor->define(
                                macro_name, macro_value);
                        }
                        else
                        {
                            return NULL;
                        }
                    }
                    else if (PyCallable_Check(value)) // define(name, callable)
                    {
                        boost::shared_ptr<cmonster::core::FunctionMacro>
                            function(new cmonster::python::FunctionMacro(
                                self, value));
                        self->preprocessor->define(macro_name, function);
                    }
                    else
                    {
                        PyErr_SetString(PyExc_TypeError,
                            "expected string or callable for value argument");
                        return NULL;
                    }
                }
                else
                {
                    // No value specified: "#define macro_name".
                    self->preprocessor->define(macro_name);
                }
            }
            else
            {
                return NULL;
            }
        }
        else if (PyCallable_Check(macro)) // define(callable)
        {
            // TODO ensure "value" was not specified.
            const char *name = PyEval_GetFuncName(macro);
            boost::shared_ptr<cmonster::core::FunctionMacro>
                function(new cmonster::python::FunctionMacro(self, macro));
            self->preprocessor->define(name, function);
        }
        else
        {
            PyErr_SetString(PyExc_TypeError, "expected string or callable");
            return NULL;
        }
    }
    catch (...)
    {
        set_python_exception();
        return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}