Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a612dc0

Browse files
committed
Merged revisions 61034-61036,61038-61048 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r61034 | georg.brandl | 2008-02-24 01:03:22 +0100 (Sun, 24 Feb 2008) | 4 lines #900744: If an invalid chunked-encoding header is sent by a server, httplib will now raise IncompleteRead and close the connection instead of raising ValueError. ........ r61035 | georg.brandl | 2008-02-24 01:14:24 +0100 (Sun, 24 Feb 2008) | 2 lines #1627: httplib now ignores negative Content-Length headers. ........ r61039 | andrew.kuchling | 2008-02-24 03:39:15 +0100 (Sun, 24 Feb 2008) | 1 line Remove stray word ........ r61040 | neal.norwitz | 2008-02-24 03:40:58 +0100 (Sun, 24 Feb 2008) | 3 lines Add a little info to the 3k deprecation warnings about what to use instead. Suggested by Raymond Hettinger. ........ r61041 | facundo.batista | 2008-02-24 04:17:21 +0100 (Sun, 24 Feb 2008) | 4 lines Issue 1742669. Now %d accepts very big float numbers. Thanks Gabriel Genellina. ........ r61046 | neal.norwitz | 2008-02-24 08:21:56 +0100 (Sun, 24 Feb 2008) | 5 lines Get ctypes working on the Alpha (Tru64). The problem was that there were two module_methods and the one used depended on the order the modules were loaded. By making the test module_methods static, it is not exported and the correct version is picked up. ........ r61048 | neal.norwitz | 2008-02-24 09:27:49 +0100 (Sun, 24 Feb 2008) | 1 line Fix typo of hexidecimal ........
1 parent 8e21a3c commit a612dc0

8 files changed

Lines changed: 110 additions & 21 deletions

File tree

Doc/howto/regex.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ this RE against the string ``abcbd``.
203203
| | | ``bc``. |
204204
+------+-----------+---------------------------------+
205205
| 6 | ``abcb`` | Try ``b`` again. This time |
206-
| | | but the character at the |
206+
| | | the character at the |
207207
| | | current position is ``'b'``, so |
208208
| | | it succeeds. |
209209
+------+-----------+---------------------------------+

Lib/httplib.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,12 @@ def begin(self):
448448
try:
449449
self.length = int(length)
450450
except ValueError:
451-
pass
451+
self.length = None
452+
else:
453+
if self.length < 0: # ignore nonsensical negative lengths
454+
self.length = None
455+
else:
456+
self.length = None
452457

453458
# does the body have a fixed length? (of zero)
454459
if (status == NO_CONTENT or status == NOT_MODIFIED or
@@ -569,7 +574,13 @@ def _read_chunked(self, amt):
569574
i = line.find(b";")
570575
if i >= 0:
571576
line = line[:i] # strip chunk-extensions
572-
chunk_left = int(line, 16)
577+
try:
578+
chunk_left = int(line, 16)
579+
except ValueError:
580+
# close the connection as protocol synchronisation is
581+
# probably lost
582+
self.close()
583+
raise IncompleteRead(value)
573584
if chunk_left == 0:
574585
break
575586
if amt is None:

Lib/test/string_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,13 @@ def test_formatting(self):
10531053
# unicode raises ValueError, str raises OverflowError
10541054
self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
10551055

1056+
longvalue = sys.maxsize + 10
1057+
slongvalue = str(longvalue)
1058+
if slongvalue[-1] in ("L","l"): slongvalue = slongvalue[:-1]
10561059
self.checkequal(' 42', '%3ld', '__mod__', 42)
1060+
self.checkequal('42', '%d', '__mod__', 42.0)
1061+
self.checkequal(slongvalue, '%d', '__mod__', longvalue)
1062+
self.checkcall('%d', '__mod__', float(longvalue))
10571063
self.checkequal('0042.00', '%07.2f', '__mod__', 42)
10581064
self.checkequal('0042.00', '%07.2F', '__mod__', 42)
10591065

@@ -1063,6 +1069,8 @@ def test_formatting(self):
10631069
self.checkraises(TypeError, '%c', '__mod__', (None,))
10641070
self.checkraises(ValueError, '%(foo', '__mod__', {})
10651071
self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
1072+
self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
1073+
self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int/long conversion provided
10661074

10671075
# argument names with properly nested brackets are supported
10681076
self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})

Lib/test/test_format.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
overflowok = 1
1212
overflowrequired = 0
1313

14-
def testformat(formatstr, args, output=None):
14+
def testformat(formatstr, args, output=None, limit=None):
1515
if verbose:
1616
if output:
1717
print("%r %% %r =? %r ..." %\
@@ -30,11 +30,22 @@ def testformat(formatstr, args, output=None):
3030
if verbose:
3131
print('no')
3232
print("overflow expected on %r %% %r" % (formatstr, args))
33-
elif output and result != output:
33+
elif output and limit is None and result != output:
3434
if verbose:
3535
print('no')
3636
print("%r %% %r == %r != %r" %\
3737
(formatstr, args, result, output))
38+
# when 'limit' is specified, it determines how many characters
39+
# must match exactly; lengths must always match.
40+
# ex: limit=5, '12345678' matches '12345___'
41+
# (mainly for floating point format tests for which an exact match
42+
# can't be guaranteed due to rounding and representation errors)
43+
elif output and limit is not None and (
44+
len(result)!=len(output) or result[:limit]!=output[:limit]):
45+
if verbose:
46+
print('no')
47+
print("%s %% %s == %s != %s" % \
48+
(repr(formatstr), repr(args), repr(result), repr(output)))
3849
else:
3950
if verbose:
4051
print('yes')
@@ -91,6 +102,7 @@ def testformat(formatstr, args, output=None):
91102
testformat("%.30d", big, "123456789012345678901234567890")
92103
testformat("%.31d", big, "0123456789012345678901234567890")
93104
testformat("%32.31d", big, " 0123456789012345678901234567890")
105+
testformat("%d", float(big), "123456________________________", 6)
94106

95107
big = 0x1234567890abcdef12345 # 21 hex digits
96108
testformat("%x", big, "1234567890abcdef12345")
@@ -128,6 +140,7 @@ def testformat(formatstr, args, output=None):
128140
testformat("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
129141
# same, except no 0 flag
130142
testformat("%#+27.23X", big, " +0X001234567890ABCDEF12345")
143+
testformat("%x", float(big), "123456_______________", 6)
131144

132145
big = 0o12345670123456701234567012345670 # 32 octal digits
133146
testformat("%o", big, "12345670123456701234567012345670")
@@ -169,6 +182,7 @@ def testformat(formatstr, args, output=None):
169182
testformat("%0#38.33o", big, "0o000012345670123456701234567012345670")
170183
# padding spaces come before marker
171184
testformat("%#36.33o", big, " 0o012345670123456701234567012345670")
185+
testformat("%o", float(big), "123456__________________________", 6)
172186

173187
# Some small ints, in both Python int and long flavors).
174188
testformat("%d", 42, "42")
@@ -186,11 +200,13 @@ def testformat(formatstr, args, output=None):
186200

187201
testformat("%x", 0x42, "42")
188202
testformat("%x", -0x42, "-42")
203+
testformat("%x", float(0x42), "42")
189204

190205
testformat("%o", 0o42, "42")
191206
testformat("%o", -0o42, "-42")
192207
testformat("%o", 0o42, "42")
193208
testformat("%o", -0o42, "-42")
209+
testformat("%o", float(0o42), "42")
194210

195211
# Test exception for unknown format characters
196212
if verbose:

Lib/test/test_httplib.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,42 @@ def test_send_file(self):
159159
self.assertTrue(sock.data.startswith(expected), '%r != %r' %
160160
(sock.data[:len(expected)], expected))
161161

162+
def test_chunked(self):
163+
chunked_start = (
164+
'HTTP/1.1 200 OK\r\n'
165+
'Transfer-Encoding: chunked\r\n\r\n'
166+
'a\r\n'
167+
'hello worl\r\n'
168+
'1\r\n'
169+
'd\r\n'
170+
)
171+
sock = FakeSocket(chunked_start + '0\r\n')
172+
resp = httplib.HTTPResponse(sock, method="GET")
173+
resp.begin()
174+
self.assertEquals(resp.read(), b'hello world')
175+
resp.close()
176+
177+
for x in ('', 'foo\r\n'):
178+
sock = FakeSocket(chunked_start + x)
179+
resp = httplib.HTTPResponse(sock, method="GET")
180+
resp.begin()
181+
try:
182+
resp.read()
183+
except httplib.IncompleteRead as i:
184+
self.assertEquals(i.partial, b'hello world')
185+
else:
186+
self.fail('IncompleteRead expected')
187+
finally:
188+
resp.close()
189+
190+
def test_negative_content_length(self):
191+
sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n')
192+
resp = httplib.HTTPResponse(sock, method="GET")
193+
resp.begin()
194+
self.assertEquals(resp.read(), b'Hello\r\n')
195+
resp.close()
196+
197+
162198
class OfflineTest(TestCase):
163199
def test_responses(self):
164200
self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found")

Misc/cheatsheet

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,8 @@ d Signed integer decimal.
561561
i Signed integer decimal.
562562
o Unsigned octal.
563563
u Unsigned decimal.
564-
x Unsigned hexidecimal (lowercase).
565-
X Unsigned hexidecimal (uppercase).
564+
x Unsigned hexadecimal (lowercase).
565+
X Unsigned hexadecimal (uppercase).
566566
e Floating point exponential format (lowercase).
567567
E Floating point exponential format (uppercase).
568568
f Floating point decimal format.

Modules/_ctypes/_ctypes_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
398398
return 0;
399399
}
400400

401-
PyMethodDef module_methods[] = {
401+
static PyMethodDef module_methods[] = {
402402
/* {"get_last_tf_arg_s", get_last_tf_arg_s, METH_NOARGS},
403403
{"get_last_tf_arg_u", get_last_tf_arg_u, METH_NOARGS},
404404
*/

Objects/unicodeobject.c

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8600,6 +8600,7 @@ PyObject *PyUnicode_Format(PyObject *format,
86008600
int prec = -1;
86018601
Py_UNICODE c = '\0';
86028602
Py_UNICODE fill;
8603+
int isnumok;
86038604
PyObject *v = NULL;
86048605
PyObject *temp = NULL;
86058606
Py_UNICODE *pbuf;
@@ -8804,21 +8805,38 @@ PyObject *PyUnicode_Format(PyObject *format,
88048805
case 'X':
88058806
if (c == 'i')
88068807
c = 'd';
8807-
if (PyLong_Check(v)) {
8808-
temp = formatlong(v, flags, prec, c);
8809-
if (!temp)
8810-
goto onError;
8811-
pbuf = PyUnicode_AS_UNICODE(temp);
8812-
len = PyUnicode_GET_SIZE(temp);
8813-
sign = 1;
8808+
isnumok = 0;
8809+
if (PyNumber_Check(v)) {
8810+
PyObject *iobj=NULL;
8811+
8812+
if (PyLong_Check(v)) {
8813+
iobj = v;
8814+
Py_INCREF(iobj);
8815+
}
8816+
else {
8817+
iobj = PyNumber_Long(v);
8818+
}
8819+
if (iobj!=NULL) {
8820+
if (PyLong_Check(iobj)) {
8821+
isnumok = 1;
8822+
temp = formatlong(iobj, flags, prec, c);
8823+
Py_DECREF(iobj);
8824+
if (!temp)
8825+
goto onError;
8826+
pbuf = PyUnicode_AS_UNICODE(temp);
8827+
len = PyUnicode_GET_SIZE(temp);
8828+
sign = 1;
8829+
}
8830+
else {
8831+
Py_DECREF(iobj);
8832+
}
8833+
}
88148834
}
8815-
else {
8816-
pbuf = formatbuf;
8817-
len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE),
8818-
flags, prec, c, v);
8819-
if (len < 0)
8835+
if (!isnumok) {
8836+
PyErr_Format(PyExc_TypeError,
8837+
"%%%c format: a number is required, "
8838+
"not %.200s", c, Py_TYPE(v)->tp_name);
88208839
goto onError;
8821-
sign = 1;
88228840
}
88238841
if (flags & F_ZERO)
88248842
fill = '0';

0 commit comments

Comments
 (0)