Staging
v0.5.1
https://github.com/python/cpython
Revision a4dd011259fa6f3079bd0efd95b3a136c0e3c190 authored by Guido van Rossum on 15 April 2001, 22:16:26 UTC, committed by Guido van Rossum on 15 April 2001, 22:16:26 UTC
and reported to python-dev: because we were calling dict_resize() in
PyDict_Next(), and because GC's dict_traverse() uses PyDict_Next(),
and because PyTuple_New() can cause GC, and because dict_items() calls
PyTuple_New(), it was possible for dict_items() to have the dict
resized right under its nose.

The solution is convoluted, and touches several places: keys(),
values(), items(), popitem(), PyDict_Next(), and PyDict_SetItem().

There are two parts to it. First, we no longer call dict_resize() in
PyDict_Next(), which seems to solve the immediate problem.  But then
PyDict_SetItem() must have a different policy about when *it* calls
dict_resize(), because we want to guarantee (e.g. for an algorithm
that Jeremy uses in the compiler) that you can loop over a dict using
PyDict_Next() and make changes to the dict as long as those changes
are only value replacements for existing keys using PyDict_SetItem().
This is done by resizing *after* the insertion instead of before, and
by remembering the size before we insert the item, and if the size is
still the same, we don't bother to even check if we might need to
resize.  An additional detail is that if the dict starts out empty, we
must still resize it before the insertion.

That was the first part. :-)

The second part is to make keys(), values(), items(), and popitem()
safe against side effects on the dict caused by allocations, under the
assumption that if the GC can cause arbitrary Python code to run, it
can cause other threads to run, and it's not inconceivable that our
dict could be resized -- it would be insane to write code that relies
on this, but not all code is sane.

Now, I have this nagging feeling that the loops in lookdict probably
are blissfully assuming that doing a simple key comparison does not
change the dict's size.  This is not necessarily true (the keys could
be class instances after all).  But that's a battle for another day.
1 parent 0aa30b0
Raw File
Tip revision: a4dd011259fa6f3079bd0efd95b3a136c0e3c190 authored by Guido van Rossum on 15 April 2001, 22:16:26 UTC
Tentative fix for a problem that Tim discovered at the last moment,
Tip revision: a4dd011
w9xpopen.c
/*
 * w9xpopen.c
 *
 * Serves as an intermediate stub Win32 console application to
 * avoid a hanging pipe when redirecting 16-bit console based
 * programs (including MS-DOS console based programs and batch
 * files) on Window 95 and Windows 98.
 *
 * This program is to be launched with redirected standard
 * handles. It will launch the command line specified 16-bit
 * console based application in the same console, forwarding
 * it's own redirected standard handles to the 16-bit child.

 * AKA solution to the problem described in KB: Q150956.
 */    

#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>

const char *usage =
"This program is used by Python's os.pipe function to\n"
"to work around a limitation in Windows 95/98.  It is\n"
"not designed to be used as stand-alone program.";

int main(int argc, char *argv[])
{
    BOOL bRet;
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    DWORD exit_code=0;

    if (argc != 2) {
        MessageBox(NULL, usage, argv[0], MB_OK);
        return 1;
    }

    /* Make child process use this app's standard files. */
    ZeroMemory(&si, sizeof si);
    si.cb = sizeof si;
    si.dwFlags = STARTF_USESTDHANDLES;
    si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    si.hStdError = GetStdHandle(STD_ERROR_HANDLE);

    bRet = CreateProcess(
        NULL, argv[1],
        NULL, NULL,
        TRUE, 0,
        NULL, NULL,
        &si, &pi
        );

    if (bRet) {
        if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) {
	    GetExitCodeProcess(pi.hProcess, &exit_code);
	}
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
        return exit_code;
    }

    return 1;
}
back to top