Thanks for pointing me to the right direction. It seems that GetConsoleScreenBufferInfo() is indeed returning 0 and further investigation points to the error code 6 (ERROR_INVALID_HANDLE). No idea why this is so but just doing a while loop until the call gets a non-zero value seem to work as a fix.
On Sat, Sep 29, 2012 at 6:29 PM, Dave Angel <d...@davea.name> wrote: > On 09/29/2012 10:19 AM, Alexis Lopez-Garcia wrote: > > Hi. > > > > I installed Python3.3.0 with python-3.3.0.amd64.msi on a win7 machine. > > > > While using this funcion (see below) from a script called by double > > clicking on the .py file I get a "invalid variable "right" referenced > > before assignment" error. > > You forgot to include the whole, actual error trace. > > > The weird thing is that launching the script from a cmd windows does no > > give the error and if you repeactedly double click on the .py file > > sometimes the error does not reproduce so I concluded it was a timing > issue > > between win7 and python. > > So by trial and error I ended up fixing it by inserting a time.sleep(0.5) > > on the function and now it works 100% of the time. > > > > These error did not show in Python3.2.3 and thus I don't know if I just > > found a new bug or what. > > I write it here so that more knowledgeable people could: > > > > 1. explains to me why the errors is happening > see below. > > 2. deems it a bug and maybe reports it on the Python site. > > Sure, report it to Microsoft. For whatever reason, your call to > > GetConsoleScreenBufferInfo() is returning false (or something equivalent, > like 0). What does the Windows 7 documentation say about not attaching a > console for a while? > > > > below is the particular function giving the error with the fix line, > which > > is not in the original version > > > > def console_resize(width=80, height=24, buffer_height=600): > > '''Sets up the console size and buffer height. > > > > @param width {int} Width of console in column value. > > @param height {int} Height of console in row value. > > @param buffer_height {int} Buffer console height in row value. > > ''' > > from ctypes import windll, byref, create_string_buffer > > from ctypes.wintypes import SMALL_RECT, _COORD > > # Active console screen buffer > > # STD_OUTPUT_HANDLE -> -11, STD_ERROR_HANDLE -> -12) > > STDERR = -12 > > # SMALL_RECT input > > LEFT = 0 > > TOP = 0 > > RIGHT = width - 1 > > BOTTOM = height - 1 > > # handle > > hdl = windll.kernel32.GetStdHandle(STDERR) > > csbi = create_string_buffer(22) > > > > time.sleep(0.5) # <--- FIX IS THIS LINE > > > > res = windll.kernel32.GetConsoleScreenBufferInfo(hdl, csbi) > > > > if res: > > import struct > > (bufx, bufy, curx, cury, wattr, > > left, top, right, bottom, > > maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw) > > Your problem is you don't have an else clause. How did you expect bufs, > bufy, etc. to be initialized without executing that code. So if the > rest of the function doesn't make sense without a console, you should be > skipping it, or throwing an exception, or returning, or something. > > > > > > > current_width = right - left + 1 > > This happens to be the first time you tried to use one of those > non-variables. Naturally, it fails. > > > current_height = bottom - top + 1 > > current_buffer_height = bufy > > > > if buffer_height < height: > > buffer_height = height > > # order of resizing avoiding some problems > > if current_buffer_height > buffer_height: > > rect = SMALL_RECT(LEFT, TOP, RIGHT, BOTTOM) # (left, top, right, > > bottom) > > windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect)) > > > > bufsize = _COORD(width, buffer_height) # columns, rows > > windll.kernel32.SetConsoleScreenBufferSize(hdl, bufsize) > > else: > > bufsize = _COORD(width, buffer_height) # columns, rows > > windll.kernel32.SetConsoleScreenBufferSize(hdl, bufsize) > > > > rect = SMALL_RECT(LEFT, TOP, RIGHT, BOTTOM) # (left, top, right, > > bottom) > > windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect)) > > > > > > > -- > > DaveA > > -- There are more things in heaven and earth, Horatio, Than are dreamt of in your philosophy. Alexis Lopez-Garcia alexis.lopezgar...@gmail.com
-- http://mail.python.org/mailman/listinfo/python-list