diff -r 0c1b81465d9c -r 323bb572344d Doc/library/sys.rst --- a/Doc/library/sys.rst Wed Oct 31 16:04:22 2012 +0200 +++ b/Doc/library/sys.rst Thu Nov 01 14:02:43 2012 +0100 @@ -766,6 +766,15 @@ This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks. +.. data:: oom_fatal + + If this is true, Python will crash hard when an out of memory condition + occurs. This value is set to ``True`` or ``False`` depending on the ``-o`` + command line option and the ``PYTHONOOMFATAL`` environment variable. You can + not change this flag at runtime. + + .. versionadded:: 3.4 + .. data:: path diff -r 0c1b81465d9c -r 323bb572344d Include/pydebug.h --- a/Include/pydebug.h Wed Oct 31 16:04:22 2012 +0200 +++ b/Include/pydebug.h Thu Nov 01 14:02:43 2012 +0100 @@ -20,6 +20,7 @@ PyAPI_DATA(int) Py_NoUserSiteDirectory; PyAPI_DATA(int) Py_UnbufferedStdioFlag; PyAPI_DATA(int) Py_HashRandomizationFlag; +PyAPI_DATA(int) Py_OomFatalFlag; /* this is a wrapper around getenv() that pays attention to Py_IgnoreEnvironmentFlag. It should be used for getting variables like diff -r 0c1b81465d9c -r 323bb572344d Lib/test/test_sys.py --- a/Lib/test/test_sys.py Wed Oct 31 16:04:22 2012 +0200 +++ b/Lib/test/test_sys.py Thu Nov 01 14:02:43 2012 +0100 @@ -515,7 +515,7 @@ attrs = ("debug", "inspect", "interactive", "optimize", "dont_write_bytecode", "no_user_site", "no_site", "ignore_environment", "verbose", - "bytes_warning", "quiet", "hash_randomization") + "bytes_warning", "quiet", "hash_randomization", "oom_fatal") for attr in attrs: self.assertTrue(hasattr(sys.flags, attr), attr) self.assertEqual(type(getattr(sys.flags, attr)), int, attr) diff -r 0c1b81465d9c -r 323bb572344d Modules/main.c --- a/Modules/main.c Wed Oct 31 16:04:22 2012 +0200 +++ b/Modules/main.c Thu Nov 01 14:02:43 2012 +0100 @@ -43,7 +43,7 @@ static int orig_argc; /* command line options */ -#define BASE_OPTS L"bBc:dEhiJm:OqRsStuvVW:xX:?" +#define BASE_OPTS L"bBc:dEhiJm:oOqRsStuvVW:xX:?" #define PROGRAM_OPTS BASE_OPTS @@ -66,6 +66,7 @@ -i : inspect interactively after running script; forces a prompt even\n\ if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\ -m mod : run library module as a script (terminates option list)\n\ +-o : make MemoryError exceptions fatal\n\ -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\ -OO : remove doc-strings in addition to the -O optimizations\n\ -q : don't print version and copyright messages on interactive startup\n\ @@ -394,6 +395,10 @@ /* case 'J': reserved for Jython */ + case 'o': + Py_OomFatalFlag++; + break; + case 'O': Py_OptimizeFlag++; break; diff -r 0c1b81465d9c -r 323bb572344d Python/errors.c --- a/Python/errors.c Wed Oct 31 16:04:22 2012 +0200 +++ b/Python/errors.c Thu Nov 01 14:02:43 2012 +0100 @@ -27,6 +27,10 @@ PyThreadState *tstate = PyThreadState_GET(); PyObject *oldtype, *oldvalue, *oldtraceback; + if (Py_OomFatalFlag && PyErr_GivenExceptionMatches(type, PyExc_MemoryError)) { + Py_FatalError("Out of memory"); + } + if (traceback != NULL && !PyTraceBack_Check(traceback)) { /* XXX Should never happen -- fatal error instead? */ /* Well, it could be None. */ diff -r 0c1b81465d9c -r 323bb572344d Python/pythonrun.c --- a/Python/pythonrun.c Wed Oct 31 16:04:22 2012 +0200 +++ b/Python/pythonrun.c Thu Nov 01 14:02:43 2012 +0100 @@ -93,6 +93,7 @@ int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */ +int Py_OomFatalFlag = 0; /* for -o and PYTHONOOMFATAL: crash under OOM conditions (MemoryError) */ PyThreadState *_Py_Finalizing = NULL; @@ -274,6 +275,8 @@ check its value further. */ if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0') Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p); + if ((p = Py_GETENV("PYTHONOOMFATAL")) && *p != '\0') + Py_OomFatalFlag = add_flag(Py_OomFatalFlag, p); _PyRandom_Init(); diff -r 0c1b81465d9c -r 323bb572344d Python/sysmodule.c --- a/Python/sysmodule.c Wed Oct 31 16:04:22 2012 +0200 +++ b/Python/sysmodule.c Thu Nov 01 14:02:43 2012 +0100 @@ -1357,6 +1357,7 @@ {"bytes_warning", "-b"}, {"quiet", "-q"}, {"hash_randomization", "-R"}, + {"oom_fatal", "-o"}, {0} }; @@ -1365,9 +1366,9 @@ flags__doc__, /* doc */ flags_fields, /* fields */ #ifdef RISCOS + 14 +#else 13 -#else - 12 #endif }; @@ -1401,6 +1402,7 @@ SetFlag(Py_BytesWarningFlag); SetFlag(Py_QuietFlag); SetFlag(Py_HashRandomizationFlag); + SetFlag(Py_OomFatalFlag); #undef SetFlag if (PyErr_Occurred()) {