blob: 3859cb8ac021e2cf8d9a1a56b4670fb17e6ecb4f [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
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -04005 * Copyright (C) Jean-Paul Calderone 2009, All rights reserved
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006 *
7 * Utility functions.
8 * See the file RATIONALE for a short explanation of why this module was written.
9 *
10 * Reviewed 2001-07-23
11 */
12#include <Python.h>
13#include "util.h"
14
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050015/*
16 * Flush OpenSSL's error queue and return a list of errors (a (library,
17 * function, reason) string tuple)
18 *
19 * Arguments: None
20 * Returns: A list of errors (new reference)
21 */
22PyObject *
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -040023error_queue_to_list(void) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050024 PyObject *errlist, *tuple;
25 long err;
26
27 errlist = PyList_New(0);
28
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -040029 while ((err = ERR_get_error()) != 0) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050030 tuple = Py_BuildValue("(sss)", ERR_lib_error_string(err),
31 ERR_func_error_string(err),
32 ERR_reason_error_string(err));
33 PyList_Append(errlist, tuple);
34 Py_DECREF(tuple);
35 }
36
37 return errlist;
38}
39
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -040040void exception_from_error_queue(PyObject *the_Error) {
Rick Deand369c932009-07-08 11:48:33 -050041 PyObject *errlist = error_queue_to_list();
42 PyErr_SetObject(the_Error, errlist);
43 Py_DECREF(errlist);
44}
45
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050046/*
47 * Flush OpenSSL's error queue and ignore the result
48 *
49 * Arguments: None
50 * Returns: None
51 */
52void
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -040053flush_error_queue(void) {
Jean-Paul Calderone5026f152009-07-16 18:47:27 -040054 /*
55 * Make sure to save the errors to a local. Py_DECREF might expand such
56 * that it evaluates its argument more than once, which would lead to
57 * very nasty things if we just invoked it with error_queue_to_list().
58 */
Jean-Paul Calderonea1933282009-07-16 16:20:42 -040059 PyObject *list = error_queue_to_list();
60 Py_DECREF(list);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050061}
Jean-Paul Calderonea5a032f2010-08-12 20:03:32 -040062
Jean-Paul Calderonedd467fb2010-08-12 20:44:32 -040063#if (PY_VERSION_HEX < 0x02600000)
Jean-Paul Calderonea5a032f2010-08-12 20:03:32 -040064PyObject* PyOpenSSL_LongToHex(PyObject *o) {
65 PyObject *hex = NULL;
66 PyObject *format = NULL;
67 PyObject *format_args = NULL;
68
69 if ((format_args = Py_BuildValue("(O)", o)) == NULL) {
70 goto err;
71 }
72
73 if ((format = PyString_FromString("%x")) == NULL) {
74 goto err;
75 }
76
77 if ((hex = PyString_Format(format, format_args)) == NULL) {
78 goto err;
79 }
80
81 return hex;
82
83 err:
84 if (format_args) {
85 Py_DECREF(format_args);
86 }
87 if (format) {
88 Py_DECREF(format);
89 }
90 if (hex) {
91 Py_DECREF(hex);
92 }
93 return NULL;
94}
Jean-Paul Calderonedd467fb2010-08-12 20:44:32 -040095#endif