blob: 16911b5707746c7041bfa2018cb6474fca96171f [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<title>4.2 Callbacks </title>
5<META NAME="description" CONTENT="4.2 Callbacks ">
6<META NAME="keywords" CONTENT="pyOpenSSL">
7<META NAME="resource-type" CONTENT="document">
8<META NAME="distribution" CONTENT="global">
9<link rel="STYLESHEET" href="pyOpenSSL.css">
10<LINK REL="next" href="socket-methods.html">
11<LINK REL="previous" href="exceptions.html">
12<LINK REL="up" href="internals.html">
13<LINK REL="next" href="socket-methods.html">
14</head>
15<body>
16<DIV CLASS="navigation">
17<table align="center" width="100%" cellpadding="0" cellspacing="2">
18<tr>
19<td><A href="exceptions.html"><img src="previous.gif"
20border="0" height="32"
21 alt="Previous Page" width="32"></A></td>
22<td><A href="internals.html"><img src="up.gif"
23border="0" height="32"
24 alt="Up One Level" width="32"></A></td>
25<td><A href="socket-methods.html"><img src="next.gif"
26border="0" height="32"
27 alt="Next Page" width="32"></A></td>
28<td align="center" width="100%">Python OpenSSL Manual</td>
29<td><A href="contents.html"><img src="contents.gif"
30border="0" height="32"
31 alt="Contents" width="32"></A></td>
32<td><img src="blank.gif"
33 border="0" height="32"
34 alt="" width="32"></td>
35<td><img src="blank.gif"
36 border="0" height="32"
37 alt="" width="32"></td>
38</tr></table>
39<b class="navlabel">Previous:</b> <a class="sectref" href="exceptions.html">4.1 Exceptions</A>
40<b class="navlabel">Up:</b> <a class="sectref" href="internals.html">4 Internals</A>
41<b class="navlabel">Next:</b> <a class="sectref" href="socket-methods.html">4.3 Acessing Socket Methods</A>
42<br><hr>
43</DIV>
44<!--End of Navigation Panel-->
45
46<H2><A NAME="SECTION000520000000000000000">&nbsp;</A>
47<BR>
484.2 Callbacks
49</H2>
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -050050
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050051<P>
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -050052There are a number of problems with callbacks. First of all, OpenSSL is written
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050053as a C library, it's not meant to have Python callbacks, so a way around that
54is needed. Another problem is thread support. A lot of the OpenSSL I/O
55functions can block if the socket is in blocking mode, and then you want other
56Python threads to be able to do other things. The real trouble is if you've
57released the thread lock to do a potentially blocking operation, and the
58operation calls a callback. Then we must take the thread lock back<A NAME="tex2html6"
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -050059 HREF="#foot932"><SUP>5</SUP></A>.
60
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050061<P>
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -050062There are two solutions to the first problem, both of which are necessary. The
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050063first solution to use is if the C callback allows ''userdata'' to be passed to
64it (an arbitrary pointer normally). This is great! We can set our Python
65function object as the real userdata and emulate userdata for the Python
66function in another way. The other solution can be used if an object with an
67''app_data'' system always is passed to the callback. For example, the SSL
68object in OpenSSL has app_data functions and in e.g. the verification
69callbacks, you can retrieve the related SSL object. What we do is to set our
70wrapper <tt class="class">Connection</tt> object as app_data for the SSL object, and we can
71easily find the Python callback.
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -050072
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050073<P>
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -050074The other problem is also partially solved by app_data. Since we're associating
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050075our wrapper objects with the ''real'' objects, we can easily access data from
76the <tt class="class">Connection</tt> object. The solution then is to simply include a
77<tt class="ctype">PyThreadState</tt> variable in the <tt class="class">Connection</tt> declaration, and write
78macros similar to <tt class="cfunction">Py_BEGIN_ALLOW_THREADS</tt> and
79<tt class="cfunction">Py_END_ALLOW_THREADS</tt> that allows specifying of the
80<tt class="ctype">PyThreadState</tt> variable to use. Now we can simply ''begin allow
81threads'' before a potentially blocking operation, and ''end allow threads''
82before calling a callback.
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -050083
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050084<P>
85<BR><HR><H4>Footnotes</H4>
86<DL>
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -050087<DT><A NAME="foot932">... back</A><A
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050088 href="callbacks.html#tex2html6"><SUP>5</SUP></A></DT>
89<DD>I'm
90not sure why this is necessary, but otherwise I get a segmentation violation on
91<tt class="cfunction">PyEval_CallObject</tt>
92
93</DD>
94</DL>
95<DIV CLASS="navigation">
96<p><hr>
97<table align="center" width="100%" cellpadding="0" cellspacing="2">
98<tr>
99<td><A href="exceptions.html"><img src="previous.gif"
100border="0" height="32"
101 alt="Previous Page" width="32"></A></td>
102<td><A href="internals.html"><img src="up.gif"
103border="0" height="32"
104 alt="Up One Level" width="32"></A></td>
105<td><A href="socket-methods.html"><img src="next.gif"
106border="0" height="32"
107 alt="Next Page" width="32"></A></td>
108<td align="center" width="100%">Python OpenSSL Manual</td>
109<td><A href="contents.html"><img src="contents.gif"
110border="0" height="32"
111 alt="Contents" width="32"></A></td>
112<td><img src="blank.gif"
113 border="0" height="32"
114 alt="" width="32"></td>
115<td><img src="blank.gif"
116 border="0" height="32"
117 alt="" width="32"></td>
118</tr></table>
119<b class="navlabel">Previous:</b> <a class="sectref" href="exceptions.html">4.1 Exceptions</A>
120<b class="navlabel">Up:</b> <a class="sectref" href="internals.html">4 Internals</A>
121<b class="navlabel">Next:</b> <a class="sectref" href="socket-methods.html">4.3 Acessing Socket Methods</A>
122<hr>
123<span class="release-info">Release 0.6.</span>
124</DIV>
125<!--End of Navigation Panel-->
126
127</BODY>
128</HTML>