Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from test.support.os_helper import TESTFN, unlink
from test.support.warnings_helper import check_warnings
from test import support
import timeit

try:
import _testcapi
Expand Down Expand Up @@ -2002,6 +2003,14 @@ def blech(self):

# Note: name suggestion tests live in `test_traceback`.

class ImportErrorBenchmark(unittest.TestCase):
def test_benchmark(self):
stmt = "repr(ImportError('Cannot import some_missing_module', name='some_missing_module', path='/this/is/a/fake/path/that/is/quite/long/and/descriptive'))"

iterations = 1_000_000
total_time = timeit.timeit(stmt, number=iterations)

print(f"Total time for creating ImportError: {total_time:.10f} seconds")

class ImportErrorTests(unittest.TestCase):

Expand Down
112 changes: 69 additions & 43 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1868,54 +1868,80 @@ static PyObject *
ImportError_repr(PyObject *self)
{
int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0;
PyImportErrorObject *exc = PyImportErrorObject_CAST(self);
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
if (writer == NULL) {
goto error;
}
PyObject *r = BaseException_repr(self);
if (r == NULL) {
goto error;
}
if (PyUnicodeWriter_WriteSubstring(
writer, r, 0, PyUnicode_GET_LENGTH(r) - 1) < 0)
{
Py_XDECREF(r);
goto error;
}
Py_XDECREF(r);
if (exc->name) {
if (hasargs) {
if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) {
goto error;
}
}
if (PyUnicodeWriter_Format(writer, "name=%R", exc->name) < 0) {
goto error;
PyImportErrorObject *exc = PyImportErrorObject_CAST(self);

if (r && (exc->name || exc->path)) {
/* remove ')' */
Py_SETREF(r, PyUnicode_Substring(r, 0, PyUnicode_GET_LENGTH(r) - 1));
if (r && exc->name) {
Py_SETREF(r, PyUnicode_FromFormat("%U%sname=%R",
r, hasargs ? ", " : "", exc->name));
hasargs = 1;
}
hasargs = 1;
}
if (exc->path) {
if (hasargs) {
if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) {
goto error;
}
if (r && exc->path) {
Py_SETREF(r, PyUnicode_FromFormat("%U%spath=%R",
r, hasargs ? ", " : "", exc->path));
}
if (PyUnicodeWriter_Format(writer, "path=%R", exc->path) < 0) {
goto error;
if (r) {
Py_SETREF(r, PyUnicode_FromFormat("%U)", r));
}
}

if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
goto error;
}

return PyUnicodeWriter_Finish(writer);

error:
PyUnicodeWriter_Discard(writer);
return NULL;
}
return r;
}

// static PyObject *
// ImportError_repr(PyObject *self)
// {
// int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0;
// PyImportErrorObject *exc = PyImportErrorObject_CAST(self);
// PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
// if (writer == NULL) {
// goto error;
// }
// PyObject *r = BaseException_repr(self);
// if (r == NULL) {
// goto error;
// }
// if (PyUnicodeWriter_WriteSubstring(
// writer, r, 0, PyUnicode_GET_LENGTH(r) - 1) < 0)
// {
// Py_XDECREF(r);
// goto error;
// }
// Py_XDECREF(r);
// if (exc->name) {
// if (hasargs) {
// if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) {
// goto error;
// }
// }
// if (PyUnicodeWriter_Format(writer, "name=%R", exc->name) < 0) {
// goto error;
// }
// hasargs = 1;
// }
// if (exc->path) {
// if (hasargs) {
// if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) {
// goto error;
// }
// }
// if (PyUnicodeWriter_Format(writer, "path=%R", exc->path) < 0) {
// goto error;
// }
// }

// if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
// goto error;
// }

// return PyUnicodeWriter_Finish(writer);

// error:
// PyUnicodeWriter_Discard(writer);
// return NULL;
// }

static PyMemberDef ImportError_members[] = {
{"msg", _Py_T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
Expand Down
Loading