Commit dbf398ee authored by 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?!?).
parent 78321295
This diff is collapsed.
This diff is collapsed.
...@@ -118,8 +118,8 @@ class BinASCIITest(unittest.TestCase): ...@@ -118,8 +118,8 @@ class BinASCIITest(unittest.TestCase):
t = binascii.b2a_hex(s) t = binascii.b2a_hex(s)
u = binascii.a2b_hex(t) u = binascii.a2b_hex(t)
self.assertEqual(s, u) self.assertEqual(s, u)
self.assertRaises(TypeError, binascii.a2b_hex, t[:-1]) self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1])
self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + b'q') self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q')
# Verify the treatment of Unicode strings # Verify the treatment of Unicode strings
if test_support.have_unicode: if test_support.have_unicode:
......
...@@ -1000,7 +1000,7 @@ binascii_unhexlify(PyObject *self, PyObject *args) ...@@ -1000,7 +1000,7 @@ binascii_unhexlify(PyObject *self, PyObject *args)
* raise an exception. * raise an exception.
*/ */
if (arglen % 2) { if (arglen % 2) {
PyErr_SetString(PyExc_TypeError, "Odd-length string"); PyErr_SetString(Error, "Odd-length string");
return NULL; return NULL;
} }
...@@ -1013,7 +1013,7 @@ binascii_unhexlify(PyObject *self, PyObject *args) ...@@ -1013,7 +1013,7 @@ binascii_unhexlify(PyObject *self, PyObject *args)
int top = to_int(Py_CHARMASK(argbuf[i])); int top = to_int(Py_CHARMASK(argbuf[i]));
int bot = to_int(Py_CHARMASK(argbuf[i+1])); int bot = to_int(Py_CHARMASK(argbuf[i+1]));
if (top == -1 || bot == -1) { if (top == -1 || bot == -1) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(Error,
"Non-hexadecimal digit found"); "Non-hexadecimal digit found");
goto finally; goto finally;
} }
...@@ -1371,7 +1371,7 @@ initbinascii(void) ...@@ -1371,7 +1371,7 @@ initbinascii(void)
PyDict_SetItemString(d, "__doc__", x); PyDict_SetItemString(d, "__doc__", x);
Py_XDECREF(x); Py_XDECREF(x);
Error = PyErr_NewException("binascii.Error", NULL, NULL); Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
PyDict_SetItemString(d, "Error", Error); PyDict_SetItemString(d, "Error", Error);
Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL); Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
PyDict_SetItemString(d, "Incomplete", Incomplete); PyDict_SetItemString(d, "Incomplete", Incomplete);
......
...@@ -3519,11 +3519,20 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -3519,11 +3519,20 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return PyLong_FromLong(0L); return PyLong_FromLong(0L);
if (base == -909) if (base == -909)
return PyNumber_Long(x); 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, /* Since PyLong_FromString doesn't have a length parameter,
* check here for possible NULs in the string. */ * check here for possible NULs in the string. */
char *string = PyString_AS_STRING(x); char *string;
if (strlen(string) != PyString_Size(x)) { 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, /* create a repr() of the input string,
* just like PyLong_FromString does. */ * just like PyLong_FromString does. */
PyObject *srepr; PyObject *srepr;
...@@ -3536,7 +3545,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -3536,7 +3545,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_DECREF(srepr); Py_DECREF(srepr);
return NULL; return NULL;
} }
return PyLong_FromString(PyString_AS_STRING(x), NULL, base); return PyLong_FromString(string, NULL, base);
} }
else if (PyUnicode_Check(x)) else if (PyUnicode_Check(x))
return PyLong_FromUnicode(PyUnicode_AS_UNICODE(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