[Python-checkins] cpython (merge 3.4 -> 3.5): Raise more correct exception on overflow in setting buffer_size attribute of

serhiy.storchaka python-checkins at python.org
Mon Sep 7 21:56:30 CEST 2015


https://hg.python.org/cpython/rev/cacdd5f71cf4
changeset:   97759:cacdd5f71cf4
branch:      3.5
parent:      97755:6006231dcaae
parent:      97758:ef9346be45d0
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Sep 07 22:54:08 2015 +0300
summary:
  Raise more correct exception on overflow in setting buffer_size attribute of
expat parser.

files:
  Lib/test/test_pyexpat.py |   3 +++
  Modules/pyexpat.c        |  13 +++++++------
  2 files changed, 10 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_pyexpat.py b/Lib/test/test_pyexpat.py
--- a/Lib/test/test_pyexpat.py
+++ b/Lib/test/test_pyexpat.py
@@ -3,6 +3,7 @@
 
 from io import BytesIO
 import os
+import sys
 import sysconfig
 import unittest
 import traceback
@@ -543,6 +544,8 @@
             parser.buffer_size = -1
         with self.assertRaises(ValueError):
             parser.buffer_size = 0
+        with self.assertRaises((ValueError, OverflowError)):
+            parser.buffer_size = sys.maxsize + 1
         with self.assertRaises(TypeError):
             parser.buffer_size = 512.0
 
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -1440,17 +1440,18 @@
         return -1;
       }
 
-      new_buffer_size=PyLong_AS_LONG(v);
+      new_buffer_size = PyLong_AsLong(v);
+      if (new_buffer_size <= 0) {
+        if (!PyErr_Occurred())
+          PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
+        return -1;
+      }
+
       /* trivial case -- no change */
       if (new_buffer_size == self->buffer_size) {
         return 0;
       }
 
-      if (new_buffer_size <= 0) {
-        PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
-        return -1;
-      }
-
       /* check maximum */
       if (new_buffer_size > INT_MAX) {
         char errmsg[100];

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list