From 7cfbb49fcd4c85f9bab3797302eadf93df490344 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Tue, 25 Oct 2022 23:56:59 +0100 Subject: gh-91058: Add error suggestions to 'import from' import errors (#98305) --- Include/cpython/pyerrors.h | 8 ++++++++ Include/internal/pycore_global_strings.h | 1 + Include/internal/pycore_runtime_init_generated.h | 7 +++++++ 3 files changed, 16 insertions(+) (limited to 'Include') diff --git a/Include/cpython/pyerrors.h b/Include/cpython/pyerrors.h index f33d3caaa2..1413416677 100644 --- a/Include/cpython/pyerrors.h +++ b/Include/cpython/pyerrors.h @@ -37,6 +37,7 @@ typedef struct { PyObject *msg; PyObject *name; PyObject *path; + PyObject *name_from; } PyImportErrorObject; typedef struct { @@ -176,4 +177,11 @@ PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFormat( const char *format, ...); +extern PyObject *_PyErr_SetImportErrorWithNameFrom( + PyObject *, + PyObject *, + PyObject *, + PyObject *); + + #define Py_FatalError(message) _Py_FatalErrorFunc(__func__, (message)) diff --git a/Include/internal/pycore_global_strings.h b/Include/internal/pycore_global_strings.h index 9c716a3012..43f4dac893 100644 --- a/Include/internal/pycore_global_strings.h +++ b/Include/internal/pycore_global_strings.h @@ -478,6 +478,7 @@ struct _Py_global_strings { STRUCT_FOR_ID(n_sequence_fields) STRUCT_FOR_ID(n_unnamed_fields) STRUCT_FOR_ID(name) + STRUCT_FOR_ID(name_from) STRUCT_FOR_ID(namespace_separator) STRUCT_FOR_ID(namespaces) STRUCT_FOR_ID(narg) diff --git a/Include/internal/pycore_runtime_init_generated.h b/Include/internal/pycore_runtime_init_generated.h index 55c7c9e319..f7823d36ef 100644 --- a/Include/internal/pycore_runtime_init_generated.h +++ b/Include/internal/pycore_runtime_init_generated.h @@ -987,6 +987,7 @@ extern "C" { INIT_ID(n_sequence_fields), \ INIT_ID(n_unnamed_fields), \ INIT_ID(name), \ + INIT_ID(name_from), \ INIT_ID(namespace_separator), \ INIT_ID(namespaces), \ INIT_ID(narg), \ @@ -2286,6 +2287,8 @@ _PyUnicode_InitStaticStrings(void) { PyUnicode_InternInPlace(&string); string = &_Py_ID(name); PyUnicode_InternInPlace(&string); + string = &_Py_ID(name_from); + PyUnicode_InternInPlace(&string); string = &_Py_ID(namespace_separator); PyUnicode_InternInPlace(&string); string = &_Py_ID(namespaces); @@ -6505,6 +6508,10 @@ _PyStaticObjects_CheckRefcnt(void) { _PyObject_Dump((PyObject *)&_Py_ID(name)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); }; + if (Py_REFCNT((PyObject *)&_Py_ID(name_from)) < _PyObject_IMMORTAL_REFCNT) { + _PyObject_Dump((PyObject *)&_Py_ID(name_from)); + Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); + }; if (Py_REFCNT((PyObject *)&_Py_ID(namespace_separator)) < _PyObject_IMMORTAL_REFCNT) { _PyObject_Dump((PyObject *)&_Py_ID(namespace_separator)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); -- cgit v1.2.1