unexpected error from Python 3.3.0

Alexis Lopez-Garcia alexis.lopezgarcia at gmail.com
Sat Sep 29 10:19:09 EDT 2012


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.
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
2. deems it a bug and maybe reports it on the Python site.

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)

    current_width = right - left + 1
    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))


-- 
There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy.

Alexis Lopez-Garcia
alexis.lopezgarcia at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120929/427519db/attachment.html>


More information about the Python-list mailing list