summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-06-11 16:04:59 (GMT)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-06-11 16:04:59 (GMT)
commit177e8530cbf5ece4264d48da3c11a7e1fc45dcb4 (patch)
tree3c0fa69fff00f24eb799f7cfad03669e73d7bf7f
parent9b88b916a903d97e3125b4436ac5c14620a78238 (diff)
downloadcpython-177e8530cbf5ece4264d48da3c11a7e1fc45dcb4.zip
cpython-177e8530cbf5ece4264d48da3c11a7e1fc45dcb4.tar.gz
cpython-177e8530cbf5ece4264d48da3c11a7e1fc45dcb4.tar.bz2
Issue #3129: Trailing digits in format string are no longer ignored.
-rw-r--r--Lib/test/test_struct.py28
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/_struct.c7
3 files changed, 36 insertions, 3 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index 0c8cd2c..b9faa28 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -443,7 +443,7 @@ class StructTest(unittest.TestCase):
# Test bogus offset (issue 3694)
sb = small_buf
- self.assertRaises(TypeError, struct.pack_into, b'1', sb, None)
+ self.assertRaises(TypeError, struct.pack_into, b'', sb, None)
def test_pack_into_fn(self):
test_string = b'Reykjavik rocks, eow!'
@@ -510,6 +510,32 @@ class StructTest(unittest.TestCase):
def test_crasher(self):
self.assertRaises(MemoryError, struct.pack, "357913941b", "a")
+ def test_trailing_counter(self):
+ store = array.array('b', b' '*100)
+
+ # format lists containing only count spec should result in an error
+ self.assertRaises(struct.error, struct.pack, '12345')
+ self.assertRaises(struct.error, struct.unpack, '12345', '')
+ self.assertRaises(struct.error, struct.pack_into, '12345', store, 0)
+ self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0)
+
+ # Format lists with trailing count spec should result in an error
+ self.assertRaises(struct.error, struct.pack, 'c12345', 'x')
+ self.assertRaises(struct.error, struct.unpack, 'c12345', 'x')
+ self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0,
+ 'x')
+ self.assertRaises(struct.error, struct.unpack_from, 'c12345', store,
+ 0)
+
+ # Mixed format tests
+ self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs')
+ self.assertRaises(struct.error, struct.unpack, '14s42',
+ 'spam and eggs')
+ self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0,
+ 'spam and eggs')
+ self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0)
+
+
def test_main():
run_unittest(StructTest)
diff --git a/Misc/NEWS b/Misc/NEWS
index 21b3962..48a7322 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1283,6 +1283,10 @@ Library
Extension Modules
-----------------
+- Issue #3129: Trailing digits in format string are no longer ignored.
+ For example, "1" or "ilib123" are now invalid formats and cause
+ ``struct.error`` to be raised.
+
- Issue #7384: If the system readline library is linked against ncurses,
the curses module must be linked against ncurses as well. Otherwise it
is not safe to load both the readline and curses modules in an application.
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 26179cc..2e594e8 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1195,8 +1195,11 @@ prepare_s(PyStructObject *self)
}
num = x;
}
- if (c == '\0')
- break;
+ if (c == '\0') {
+ PyErr_SetString(StructError,
+ "repeat count given without format specifier");
+ return -1;
+ }
}
else
num = 1;