blob: 2ce370dddfbee1f3bbf1914ddb6dc9f538dd31c2 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * util.c
3 *
4 * Copyright (C) AB Strakt 2001, All rights reserved
5 *
6 * Utility functions.
7 * See the file RATIONALE for a short explanation of why this module was written.
8 *
9 * Reviewed 2001-07-23
10 */
11#include <Python.h>
12#include "util.h"
13
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050014/*
15 * Flush OpenSSL's error queue and return a list of errors (a (library,
16 * function, reason) string tuple)
17 *
18 * Arguments: None
19 * Returns: A list of errors (new reference)
20 */
21PyObject *
22error_queue_to_list(void)
23{
24 PyObject *errlist, *tuple;
25 long err;
26
27 errlist = PyList_New(0);
28
29 while ((err = ERR_get_error()) != 0)
30 {
31 tuple = Py_BuildValue("(sss)", ERR_lib_error_string(err),
32 ERR_func_error_string(err),
33 ERR_reason_error_string(err));
34 PyList_Append(errlist, tuple);
35 Py_DECREF(tuple);
36 }
37
38 return errlist;
39}
40
Rick Deand369c932009-07-08 11:48:33 -050041void exception_from_error_queue(PyObject *the_Error)
42{
43 PyObject *errlist = error_queue_to_list();
44 PyErr_SetObject(the_Error, errlist);
45 Py_DECREF(errlist);
46}
47
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050048/*
49 * Flush OpenSSL's error queue and ignore the result
50 *
51 * Arguments: None
52 * Returns: None
53 */
54void
55flush_error_queue(void)
56{
Jean-Paul Calderonea1933282009-07-16 16:20:42 -040057 PyObject *list = error_queue_to_list();
58 Py_DECREF(list);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050059}
60