Merged revisions 80325 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r80325 | antoine.pitrou | 2010-04-22 00:53:29 +0200 (jeu., 22 avril 2010) | 6 lines

  Issue #7332: Remove the 16KB stack-based buffer in
  PyMarshal_ReadLastObjectFromFile, which doesn't bring any noticeable
  benefit compared to the dynamic memory allocation fallback.  Patch by
  Charles-François Natali.
........
This commit is contained in:
Antoine Pitrou 2010-04-21 22:56:22 +00:00
parent 5ae6810819
commit 5bc7ec9476
3 changed files with 10 additions and 16 deletions

View File

@ -540,6 +540,7 @@ Piotr Meyer
John Nagle John Nagle
Takahiro Nakayama Takahiro Nakayama
Travers Naran Travers Naran
Charles-François Natali
Fredrik Nehr Fredrik Nehr
Trent Nelson Trent Nelson
Tony Nelson Tony Nelson

View File

@ -12,6 +12,11 @@ What's New in Python 3.2 Alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #7332: Remove the 16KB stack-based buffer in
PyMarshal_ReadLastObjectFromFile, which doesn't bring any noticeable
benefit compared to the dynamic memory allocation fallback. Patch by
Charles-François Natali.
- Issue #8417: Raise an OverflowError when an integer larger than sys.maxsize is - Issue #8417: Raise an OverflowError when an integer larger than sys.maxsize is
passed to bytes or bytearray. passed to bytes or bytearray.

View File

@ -1078,23 +1078,13 @@ getfilesize(FILE *fp)
PyObject * PyObject *
PyMarshal_ReadLastObjectFromFile(FILE *fp) PyMarshal_ReadLastObjectFromFile(FILE *fp)
{ {
/* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT. /* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */
* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
*/
#define SMALL_FILE_LIMIT (1L << 14)
#define REASONABLE_FILE_LIMIT (1L << 18) #define REASONABLE_FILE_LIMIT (1L << 18)
#ifdef HAVE_FSTAT #ifdef HAVE_FSTAT
off_t filesize; off_t filesize;
#endif
#ifdef HAVE_FSTAT
filesize = getfilesize(fp); filesize = getfilesize(fp);
if (filesize > 0) { if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) {
char buf[SMALL_FILE_LIMIT]; char* pBuf = (char *)PyMem_MALLOC(filesize);
char* pBuf = NULL;
if (filesize <= SMALL_FILE_LIMIT)
pBuf = buf;
else if (filesize <= REASONABLE_FILE_LIMIT)
pBuf = (char *)PyMem_MALLOC(filesize);
if (pBuf != NULL) { if (pBuf != NULL) {
PyObject* v; PyObject* v;
size_t n; size_t n;
@ -1102,8 +1092,7 @@ PyMarshal_ReadLastObjectFromFile(FILE *fp)
is smaller than REASONABLE_FILE_LIMIT */ is smaller than REASONABLE_FILE_LIMIT */
n = fread(pBuf, 1, (int)filesize, fp); n = fread(pBuf, 1, (int)filesize, fp);
v = PyMarshal_ReadObjectFromString(pBuf, n); v = PyMarshal_ReadObjectFromString(pBuf, n);
if (pBuf != buf) PyMem_FREE(pBuf);
PyMem_FREE(pBuf);
return v; return v;
} }
@ -1114,7 +1103,6 @@ PyMarshal_ReadLastObjectFromFile(FILE *fp)
*/ */
return PyMarshal_ReadObjectFromFile(fp); return PyMarshal_ReadObjectFromFile(fp);
#undef SMALL_FILE_LIMIT
#undef REASONABLE_FILE_LIMIT #undef REASONABLE_FILE_LIMIT
} }