Example #1
0
PyObject* PyCodec_Decode(PyObject* object, const char* encoding, const char* errors) noexcept {
    PyObject* decoder = NULL;
    PyObject* args = NULL, * result = NULL;
    PyObject* v;

    decoder = PyCodec_Decoder(encoding);
    if (decoder == NULL)
        goto onError;

    args = args_tuple(object, errors);
    if (args == NULL)
        goto onError;

    result = PyEval_CallObject(decoder, args);
    if (result == NULL)
        goto onError;
    if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 2) {
        PyErr_SetString(PyExc_TypeError, "decoder must return a tuple (object,integer)");
        goto onError;
    }
    v = PyTuple_GET_ITEM(result, 0);
    Py_INCREF(v);
    /* We don't check or use the second (integer) entry. */

    Py_DECREF(args);
    Py_DECREF(decoder);
    Py_DECREF(result);
    return v;

onError:
    Py_XDECREF(args);
    Py_XDECREF(decoder);
    Py_XDECREF(result);
    return NULL;
}
Example #2
0
static PyObject *
_PyCodec_EncodeInternal(PyObject *object,
                        PyObject *encoder,
                        const char *encoding,
                        const char *errors)
{
    PyObject *args = NULL, *result = NULL;
    PyObject *v = NULL;

    args = args_tuple(object, errors);
    if (args == NULL)
        goto onError;

    result = PyEval_CallObject(encoder, args);
    if (result == NULL) {
        wrap_codec_error("encoding", encoding);
        goto onError;
    }

    if (!PyTuple_Check(result) ||
        PyTuple_GET_SIZE(result) != 2) {
        PyErr_SetString(PyExc_TypeError,
                        "encoder must return a tuple (object, integer)");
        goto onError;
    }
    v = PyTuple_GET_ITEM(result,0);
    Py_INCREF(v);
    /* We don't check or use the second (integer) entry. */

    Py_DECREF(args);
    Py_DECREF(encoder);
    Py_DECREF(result);
    return v;

 onError:
    Py_XDECREF(result);
    Py_XDECREF(args);
    Py_XDECREF(encoder);
    return NULL;
}
void EmacsPythonCallCommand::executeCommandImpl()
{
    try
    {
        Py::Module module( "__main__" );
        Py::Dict dict( module.getDict() );

        //
        //    create the tuple of arguments
        //
        Py::Tuple args_tuple( num_args );
        for( int arg=0; arg < num_args; arg++ )
        {
            args_tuple[ arg ] = convertEmacsExpressionToPyObject( python_args[arg] );
        }

        dict[ "__bemacs_call_args__" ] = args_tuple;

        EmacsString command( FormatString
            (
            "__bemacs_eval_tmp__ = apply( %s, __bemacs_call_args__ )\n"
            ) << python_function );

        runPythonStringInsideTryExcept( command );

        if( !failed() )
        {
            Py::Object py_result( dict[ "__bemacs_eval_tmp__" ] );
            commandSucceeded( convertPyObjectToEmacsExpression( py_result ) );
        }
    }
    catch( Py::Exception &e )
    {
        e.clear();
    }
}