Kaydet (Commit) 4581ae5f authored tarafından Guido van Rossum's avatar Guido van Rossum

Make test_base64 pass.

Change binascii.Error to derive from ValueError
and raise binascii.Error everywhere where values are bad
(why on earth did the old code use TypeError?!?).
üst 0e225aa0
This diff is collapsed.
This diff is collapsed.
......@@ -118,8 +118,8 @@ class BinASCIITest(unittest.TestCase):
t = binascii.b2a_hex(s)
u = binascii.a2b_hex(t)
self.assertEqual(s, u)
self.assertRaises(TypeError, binascii.a2b_hex, t[:-1])
self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + b'q')
self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1])
self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q')
# Verify the treatment of Unicode strings
if test_support.have_unicode:
......
......@@ -1000,7 +1000,7 @@ binascii_unhexlify(PyObject *self, PyObject *args)
* raise an exception.
*/
if (arglen % 2) {
PyErr_SetString(PyExc_TypeError, "Odd-length string");
PyErr_SetString(Error, "Odd-length string");
return NULL;
}
......@@ -1013,7 +1013,7 @@ binascii_unhexlify(PyObject *self, PyObject *args)
int top = to_int(Py_CHARMASK(argbuf[i]));
int bot = to_int(Py_CHARMASK(argbuf[i+1]));
if (top == -1 || bot == -1) {
PyErr_SetString(PyExc_TypeError,
PyErr_SetString(Error,
"Non-hexadecimal digit found");
goto finally;
}
......@@ -1371,7 +1371,7 @@ initbinascii(void)
PyDict_SetItemString(d, "__doc__", x);
Py_XDECREF(x);
Error = PyErr_NewException("binascii.Error", NULL, NULL);
Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
PyDict_SetItemString(d, "Error", Error);
Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
PyDict_SetItemString(d, "Incomplete", Incomplete);
......
......@@ -3519,11 +3519,20 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return PyLong_FromLong(0L);
if (base == -909)
return PyNumber_Long(x);
else if (PyString_Check(x)) {
else if (PyString_Check(x) || PyBytes_Check(x)) {
/* Since PyLong_FromString doesn't have a length parameter,
* check here for possible NULs in the string. */
char *string = PyString_AS_STRING(x);
if (strlen(string) != PyString_Size(x)) {
char *string;
int size;
if (PyBytes_Check(x)) {
string = PyBytes_AS_STRING(x);
size = PyBytes_GET_SIZE(x);
}
else {
string = PyString_AS_STRING(x);
size = PyString_GET_SIZE(x);
}
if (strlen(string) != size) {
/* create a repr() of the input string,
* just like PyLong_FromString does. */
PyObject *srepr;
......@@ -3536,7 +3545,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_DECREF(srepr);
return NULL;
}
return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
return PyLong_FromString(string, NULL, base);
}
else if (PyUnicode_Check(x))
return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment