Support Py2.5 to Py3.4 for thread identity (thanks @lndbrg)

This commit is contained in:
Aarni Koskela 2014-01-21 00:13:22 +02:00 committed by Jeff Forcier
parent d32d457775
commit b0c689d7c8
1 changed files with 16 additions and 3 deletions

View File

@ -28,6 +28,7 @@ import ctypes.wintypes
import platform import platform
import struct import struct
import thread import thread
import threading
from . import _winapi from . import _winapi
@ -38,6 +39,13 @@ _AGENT_MAX_MSGLEN = 8192
win32con_WM_COPYDATA = 74 win32con_WM_COPYDATA = 74
def get_thread_ident():
try: # thread.get_ident() exists from Py2.5 to Py2.7.
return thread.get_ident()
except AttributeError: # threading.current_thread().ident exists from Py2.6 up to Py3.4.
return threading.current_thread().ident
def _get_pageant_window_object(): def _get_pageant_window_object():
return ctypes.windll.user32.FindWindowA('Pageant', 'Pageant') return ctypes.windll.user32.FindWindowA('Pageant', 'Pageant')
@ -51,7 +59,10 @@ def can_talk_to_agent():
""" """
return bool(_get_pageant_window_object()) return bool(_get_pageant_window_object())
ULONG_PTR = ctypes.c_uint64 if platform.architecture()[0] == '64bit' else ctypes.c_uint32 ULONG_PTR = ctypes.c_uint64 if platform.architecture()[0] == '64bit' else ctypes.c_uint32
class COPYDATASTRUCT(ctypes.Structure): class COPYDATASTRUCT(ctypes.Structure):
""" """
ctypes implementation of ctypes implementation of
@ -61,7 +72,8 @@ class COPYDATASTRUCT(ctypes.Structure):
('num_data', ULONG_PTR), ('num_data', ULONG_PTR),
('data_size', ctypes.wintypes.DWORD), ('data_size', ctypes.wintypes.DWORD),
('data_loc', ctypes.c_void_p), ('data_loc', ctypes.c_void_p),
] ]
def _query_pageant(msg): def _query_pageant(msg):
""" """
@ -74,7 +86,7 @@ def _query_pageant(msg):
return None return None
# create a name for the mmap # create a name for the mmap
map_name = 'PageantRequest%08x' % thread.get_ident() map_name = 'PageantRequest%08x' % get_thread_ident()
pymap = _winapi.MemoryMap(map_name, _AGENT_MAX_MSGLEN, pymap = _winapi.MemoryMap(map_name, _AGENT_MAX_MSGLEN,
_winapi.get_security_attributes_for_user(), _winapi.get_security_attributes_for_user(),
@ -98,7 +110,8 @@ def _query_pageant(msg):
return datalen + pymap.read(retlen) return datalen + pymap.read(retlen)
return None return None
class PageantConnection (object):
class PageantConnection(object):
""" """
Mock "connection" to an agent which roughly approximates the behavior of Mock "connection" to an agent which roughly approximates the behavior of
a unix local-domain socket (as used by Agent). Requests are sent to the a unix local-domain socket (as used by Agent). Requests are sent to the