<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
Den 29.03.2011 21:15, skrev Eric Snow:
<blockquote
cite="mid:AANLkTikN0qazvWW8iy+HZ5zf3mdM7RwTO+bg0dxSRWQh@mail.gmail.com"
type="cite"><br>
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">Well people already do this using
ctypes...<br>
<div class="im"><br>
</div>
</blockquote>
<div>Haven't used them myself yet. So you can use them to
expose all of the C-API? Cool!</div>
<div> <br>
</div>
</div>
</blockquote>
<br>
Yes, ctypes.pythonapi exposes the Python C API to Python. <br>
<br>
You can use it for evil code like this hack to prevent thread switch
(doesn't work with Python 3, as the implementation has changed).
Just make sure you don't call extension code that releases the GIL,
or all bets are off ;-)<br>
<br>
Sturla<br>
<br>
<br>
<br>
from contextlib import contextmanager<br>
import ctypes<br>
_Py_Ticker = ctypes.c_int.in_dll(ctypes.pythonapi,"_Py_Ticker")<br>
<br>
@contextmanager<br>
def atomic():<br>
tmp = _Py_Ticker.value<br>
_Py_Ticker.value = 0x7fffffff<br>
yield<br>
_Py_Ticker.value = tmp - 1<br>
<br>
Now we can do<br>
<br>
with atomic():<br>
# whatever<br>
pass <br>
<br>
<br>
</body>
</html>

AltStyle によって変換されたページ (->オリジナル) /