Merging fixes for 64-bit

Thanks to Christoph Gohlke for helping with the fixes.
This commit is contained in:
Jorgen Stenarson
2011-10-06 19:48:11 +02:00
2 changed files with 90 additions and 20 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ else:
success = True
except ImportError:
pass
raise
if not success:
raise ImportError(
+89 -19
View File
@@ -25,8 +25,10 @@ from pyreadline.keysyms import make_KeyPress, KeyPress
from pyreadline.console.ansi import AnsiState,AnsiWriter
try:
import ctypes.util
from ctypes import *
from _ctypes import call_function
from ctypes.wintypes import *
except ImportError:
raise ImportError(u"You need ctypes to run this code")
@@ -192,14 +194,13 @@ class Console(object):
self.hout = self.GetStdHandle(STD_OUTPUT_HANDLE)
self.hin = self.GetStdHandle(STD_INPUT_HANDLE)
self.inmode = c_int(0)
self.inmode = DWORD(0)
self.GetConsoleMode(self.hin, byref(self.inmode))
self.SetConsoleMode(self.hin, 0xf)
info = CONSOLE_SCREEN_BUFFER_INFO()
self.GetConsoleScreenBufferInfo(self.hout, byref(info))
self.attr = info.wAttributes
self.saveattr = info.wAttributes # remember the initial colors
self.defaultstate = AnsiState()
self.defaultstate.winattr = info.wAttributes
self.ansiwriter = AnsiWriter(self.defaultstate)
@@ -214,8 +215,10 @@ class Console(object):
self.pythondll = \
CDLL(u'python%s%s' % (sys.version[0], sys.version[2]))
self.pythondll.PyMem_Malloc.restype = c_size_t
self.pythondll.PyMem_Malloc.argtypes = [c_size_t]
self.inputHookPtr = \
c_int.from_address(addressof(self.pythondll.PyOS_InputHook)).value
c_void_p.from_address(addressof(self.pythondll.PyOS_InputHook)).value
setattr(Console, u'PyMem_Malloc', self.pythondll.PyMem_Malloc)
def __del__(self):
@@ -347,7 +350,7 @@ class Console(object):
def write_color(self, text, attr=None):
text = ensure_unicode(text)
n, res= self.ansiwriter.write_color(text, attr)
junk = c_int(0)
junk = DWORD(0)
for attr,chunk in res:
log(u"console.attr:%s"%unicode(attr))
log(u"console.chunk:%s"%unicode(chunk))
@@ -363,7 +366,7 @@ class Console(object):
log(u'write("%s", %s)' %(text, attr))
if attr is None:
attr = self.attr
n = c_int(0)
junk = DWORD(0)
self.SetConsoleTextAttribute(self.hout, attr)
for short_chunk in split_block(chunk):
self.WriteConsoleW(self.hout, ensure_unicode(short_chunk),
@@ -375,7 +378,7 @@ class Console(object):
if os.environ.has_key(u"EMACS"):
def write_color(self, text, attr=None):
text = ensure_str(text)
junk = c_int(0)
junk = DWORD(0)
self.WriteFile(self.hout, text, len(text), byref(junk), None)
return len(text)
write_plain = write_color
@@ -406,7 +409,7 @@ class Console(object):
self.SetConsoleCursorPosition(self.hout, self.fixcoord(0, 0))
w = info.dwSize.X
n = c_int(0)
n = DWORD(0)
for y in range(info.dwSize.Y):
self.FillConsoleOutputAttribute(self.hout, attr,
w, self.fixcoord(0, y), byref(n))
@@ -421,7 +424,7 @@ class Console(object):
attr = self.attr
pos = self.fixcoord(x, y)
n = c_int(0)
n = DWORD(0)
self.WriteConsoleOutputCharacterW(self.hout, text,
len(text), pos, byref(n))
self.FillConsoleOutputAttribute(self.hout, attr, n, pos, byref(n))
@@ -437,7 +440,7 @@ class Console(object):
def rectangle(self, rect, attr=None, fill=u' '):
u'''Fill Rectangle.'''
x0, y0, x1, y1 = rect
n = c_int(0)
n = DWORD(0)
if attr is None:
attr = self.attr
for y in range(y0, y1):
@@ -489,10 +492,10 @@ class Console(object):
def get(self):
u'''Get next event from queue.'''
inputHookFunc = c_int.from_address(self.inputHookPtr).value
inputHookFunc = c_void_p.from_address(self.inputHookPtr).value
Cevent = INPUT_RECORD()
count = c_int(0)
count = DWORD(0)
while 1:
if inputHookFunc:
call_function(inputHookFunc, ())
@@ -524,7 +527,7 @@ class Console(object):
u'''Get next character from queue.'''
Cevent = INPUT_RECORD()
count = c_int(0)
count = DWORD(0)
while 1:
status = self.ReadConsoleInputW(self.hin,
byref(Cevent), 1, byref(count))
@@ -540,7 +543,7 @@ class Console(object):
def peek(self):
u'''Check event queue.'''
Cevent = INPUT_RECORD()
count = c_int(0)
count = DWORD(0)
status = self.PeekConsoleInputW(self.hin,
byref(Cevent), 1, byref(count))
if status and count == 1:
@@ -595,8 +598,75 @@ class Console(object):
# add the functions from the dll to the class
for func in funcs:
setattr(Console, func, getattr(windll.kernel32, func))
windll.kernel32.SetConsoleTitleW.argtypes = [c_wchar_p]
windll.kernel32.GetConsoleTitleW.argtypes = [c_wchar_p,c_short]
if sys.version_info[:2] < (2, 6):
msvcrt = cdll.msvcrt
else:
msvcrt = cdll.LoadLibrary(ctypes.util.find_msvcrt())
_strncpy = msvcrt.strncpy
_strncpy.restype = c_char_p
_strncpy.argtypes = [c_char_p, c_char_p, c_size_t]
_strdup = msvcrt._strdup
_strdup.restype = c_char_p
_strdup.argtypes = [c_char_p]
LPVOID = c_void_p
LPCVOID = c_void_p
FARPROC = c_void_p
LPDWORD = POINTER(DWORD)
Console.AllocConsole.restype = BOOL
Console.AllocConsole.argtypes = [] #void
Console.CreateConsoleScreenBuffer.restype = HANDLE
Console.CreateConsoleScreenBuffer.argtypes = [DWORD, DWORD, c_void_p, DWORD, LPVOID] #DWORD, DWORD, SECURITY_ATTRIBUTES*, DWORD, LPVOID
Console.FillConsoleOutputAttribute.restype = BOOL
Console.FillConsoleOutputAttribute.argtypes = [HANDLE, WORD, DWORD, c_int, LPDWORD] #HANDLE, WORD, DWORD, COORD, LPDWORD
Console.FillConsoleOutputCharacterW.restype = BOOL
Console.FillConsoleOutputCharacterW.argtypes = [HANDLE, c_ushort, DWORD, c_int, LPDWORD] #HANDLE, TCHAR, DWORD, COORD, LPDWORD
Console.FreeConsole.restype = BOOL
Console.FreeConsole.argtypes = [] #void
Console.GetConsoleCursorInfo.restype = BOOL
Console.GetConsoleCursorInfo.argtypes = [HANDLE, c_void_p] #HANDLE, PCONSOLE_CURSOR_INFO
Console.GetConsoleMode.restype = BOOL
Console.GetConsoleMode.argtypes = [HANDLE, LPDWORD] #HANDLE, LPDWORD
Console.GetConsoleScreenBufferInfo.restype = BOOL
Console.GetConsoleScreenBufferInfo.argtypes = [HANDLE, c_void_p] #HANDLE, PCONSOLE_SCREEN_BUFFER_INFO
Console.GetConsoleTitleW.restype = DWORD
Console.GetConsoleTitleW.argtypes = [c_wchar_p, DWORD] #LPTSTR , DWORD
Console.GetProcAddress.restype = FARPROC
Console.GetProcAddress.argtypes = [HMODULE, c_char_p] #HMODULE , LPCSTR
Console.GetStdHandle.restype = HANDLE
Console.GetStdHandle.argtypes = [DWORD]
Console.PeekConsoleInputW.restype = BOOL
Console.PeekConsoleInputW.argtypes = [HANDLE, c_void_p, DWORD, LPDWORD] #HANDLE, PINPUT_RECORD, DWORD, LPDWORD
Console.ReadConsoleInputW.restype = BOOL
Console.ReadConsoleInputW.argtypes = [HANDLE, c_void_p, DWORD, LPDWORD] #HANDLE, PINPUT_RECORD, DWORD, LPDWORD
Console.ScrollConsoleScreenBufferW.restype = BOOL
Console.ScrollConsoleScreenBufferW.argtypes = [HANDLE, c_void_p, c_void_p, c_int, c_void_p] #HANDLE, SMALL_RECT*, SMALL_RECT*, COORD, LPDWORD
Console.SetConsoleActiveScreenBuffer.restype = BOOL
Console.SetConsoleActiveScreenBuffer.argtypes = [HANDLE] #HANDLE
Console.SetConsoleCursorInfo.restype = BOOL
Console.SetConsoleCursorInfo.argtypes = [HANDLE, c_void_p] #HANDLE, CONSOLE_CURSOR_INFO*
Console.SetConsoleCursorPosition.restype = BOOL
Console.SetConsoleCursorPosition.argtypes = [HANDLE, c_int] #HANDLE, COORD
Console.SetConsoleMode.restype = BOOL
Console.SetConsoleMode.argtypes = [HANDLE, DWORD] #HANDLE, DWORD
Console.SetConsoleScreenBufferSize.restype = BOOL
Console.SetConsoleScreenBufferSize.argtypes = [HANDLE, c_int] #HANDLE, COORD
Console.SetConsoleTextAttribute.restype = BOOL
Console.SetConsoleTextAttribute.argtypes = [HANDLE, WORD] #HANDLE, WORD
Console.SetConsoleTitleW.restype = BOOL
Console.SetConsoleTitleW.argtypes = [c_wchar_p] #LPCTSTR
Console.SetConsoleWindowInfo.restype = BOOL
Console.SetConsoleWindowInfo.argtypes = [HANDLE, BOOL, c_void_p] #HANDLE, BOOL, SMALL_RECT*
Console.WriteConsoleW.restype = BOOL
Console.WriteConsoleW.argtypes = [HANDLE, c_void_p, DWORD, LPDWORD, LPVOID] #HANDLE, VOID*, DWORD, LPDWORD, LPVOID
Console.WriteConsoleOutputCharacterW.restype = BOOL
Console.WriteConsoleOutputCharacterW.argtypes = [HANDLE, c_wchar_p, DWORD, c_int, LPDWORD] #HANDLE, LPCTSTR, DWORD, COORD, LPDWORD
Console.WriteFile.restype = BOOL
Console.WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, c_void_p] #HANDLE, LPCVOID , DWORD, LPDWORD , LPOVERLAPPED
from event import Event
@@ -705,7 +775,7 @@ def hook_wrapper_23(stdin, stdout, prompt):
# we have to make a copy because the caller expects to free the result
n = len(res)
p = Console.PyMem_Malloc(n + 1)
cdll.msvcrt.strncpy(p, res, n + 1)
_strncpy(cast(p, c_char_p), res, n + 1)
return p
def hook_wrapper(prompt):
@@ -727,7 +797,7 @@ def hook_wrapper(prompt):
traceback.print_exc()
res = u'\n'
# we have to make a copy because the caller expects to free the result
p = cdll.msvcrt._strdup(res)
p = _strdup(res)
return p
def install_readline(hook):
@@ -737,7 +807,7 @@ def install_readline(hook):
# save the hook so the wrapper can call it
readline_hook = hook
# get the address of PyOS_ReadlineFunctionPointer so we can update it
PyOS_RFP = c_int.from_address(Console.GetProcAddress(sys.dllhandle,
PyOS_RFP = c_void_p.from_address(Console.GetProcAddress(sys.dllhandle,
"PyOS_ReadlineFunctionPointer"))
# save a reference to the generated C-callable so it doesn't go away
if sys.version < '2.3':
@@ -745,7 +815,7 @@ def install_readline(hook):
else:
readline_ref = HOOKFUNC23(hook_wrapper_23)
# get the address of the function
func_start = c_int.from_address(addressof(readline_ref)).value
func_start = c_void_p.from_address(addressof(readline_ref)).value
# write the function address into PyOS_ReadlineFunctionPointer
PyOS_RFP.value = func_start