blob: ca60ccf2806873a39bccf49bad7e263b35029a30 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * util.c
3 *
Jean-Paul Calderone8671c852011-03-02 19:26:20 -05004 * Copyright (C) AB Strakt
5 * Copyright (C) Jean-Paul Calderone
6 * See LICENSE for details.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05007 *
8 * Utility functions.
9 * See the file RATIONALE for a short explanation of why this module was written.
10 *
11 * Reviewed 2001-07-23
12 */
13#include <Python.h>
14#include "util.h"
15
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050016/*
17 * Flush OpenSSL's error queue and return a list of errors (a (library,
18 * function, reason) string tuple)
19 *
20 * Arguments: None
21 * Returns: A list of errors (new reference)
22 */
23PyObject *
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -040024error_queue_to_list(void) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050025 PyObject *errlist, *tuple;
26 long err;
27
28 errlist = PyList_New(0);
29
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -040030 while ((err = ERR_get_error()) != 0) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050031 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
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -040041void exception_from_error_queue(PyObject *the_Error) {
Rick Deand369c932009-07-08 11:48:33 -050042 PyObject *errlist = error_queue_to_list();
43 PyErr_SetObject(the_Error, errlist);
44 Py_DECREF(errlist);
45}
46
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050047/*
48 * Flush OpenSSL's error queue and ignore the result
49 *
50 * Arguments: None
51 * Returns: None
52 */
53void
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -040054flush_error_queue(void) {
Jean-Paul Calderone5026f152009-07-16 18:47:27 -040055 /*
56 * Make sure to save the errors to a local. Py_DECREF might expand such
57 * that it evaluates its argument more than once, which would lead to
58 * very nasty things if we just invoked it with error_queue_to_list().
59 */
Jean-Paul Calderonea1933282009-07-16 16:20:42 -040060 PyObject *list = error_queue_to_list();
61 Py_DECREF(list);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050062}
Jean-Paul Calderonea5a032f2010-08-12 20:03:32 -040063
Jean-Paul Calderonedd467fb2010-08-12 20:44:32 -040064#if (PY_VERSION_HEX < 0x02600000)
Jean-Paul Calderonea5a032f2010-08-12 20:03:32 -040065PyObject* PyOpenSSL_LongToHex(PyObject *o) {
66 PyObject *hex = NULL;
67 PyObject *format = NULL;
68 PyObject *format_args = NULL;
69
70 if ((format_args = Py_BuildValue("(O)", o)) == NULL) {
71 goto err;
72 }
73
74 if ((format = PyString_FromString("%x")) == NULL) {
75 goto err;
76 }
77
78 if ((hex = PyString_Format(format, format_args)) == NULL) {
79 goto err;
80 }
81
82 return hex;
83
84 err:
85 if (format_args) {
86 Py_DECREF(format_args);
87 }
88 if (format) {
89 Py_DECREF(format);
90 }
91 if (hex) {
92 Py_DECREF(hex);
93 }
94 return NULL;
95}
Jean-Paul Calderonedd467fb2010-08-12 20:44:32 -040096#endif