upiwin/src/ep_util.c

76 lines
1.6 KiB
C
Raw Normal View History

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "scode.h"
#include "ep_util.h"
#include "log.h"
void Epython_log_object(int level, const char *label, PyObject *object)
{
BOOL traced = FALSE;
PyObject *repr;
PCSTR pstr;
repr = PyObject_Repr(object);
if (repr)
{
ASSERT(PyUnicode_Check(repr));
pstr = PyUnicode_AsUTF8(repr);
Log(level, "object %s: %s", label, pstr);
traced = TRUE;
Py_DECREF(repr);
}
if (!traced)
Log(level, "object %s: could not be logged (memory error)", label);
}
HRESULT Epython_trace_exception(void)
{
HRESULT hr = E_FAIL;
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
if (!type)
return S_OK;
Epython_log_object(LERROR, "exception type", type);
Epython_log_object(LERROR, "exception value", value);
Epython_log_object(LERROR, "exception traceback", value);
Py_DECREF(type);
Py_DECREF(value);
Py_DECREF(traceback);
return hr;
}
2019-12-09 12:28:28 -07:00
HRESULT Epython_register_constants(PyObject *module, PCREGCONSTANT const_table)
{
HRESULT hr = S_OK;
int i = 0, rc;
while (const_table[i].name)
{
switch (const_table[i].regtype)
{
case 'i':
rc = PyModule_AddIntConstant(module, const_table[i].name, const_table[i].value.ival);
break;
case 's':
rc = PyModule_AddStringConstant(module, const_table[i].name, const_table[i].value.sval);
break;
2019-12-09 12:30:27 -07:00
default:
2019-12-09 12:28:28 -07:00
Log(LERROR, "register_constants type '%c' unknown", const_table[i].regtype);
return E_UNEXPECTED;
}
if (!rc)
{
2019-12-09 12:30:54 -07:00
Log(LERROR, "Failed to register constant %s", const_table[i].name);
2019-12-09 12:28:28 -07:00
hr = E_FAIL;
break;
}
++i;
}
return hr;
}