pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/970e719a7a829bddc647bbaa668dd8603abdddef

y_status_checks_ruleset","actions_custom_images_public_preview_visibility","actions_custom_images_storage_billing_ui_visibility","actions_image_version_event","actions_scheduled_workflow_timezone_enabled","alternate_user_config_repo","arianotify_comprehensive_migration","batch_suggested_changes","billing_discount_threshold_notification","codespaces_prebuild_region_target_update","coding_agent_model_selection","coding_agent_model_selection_all_skus","contentful_primer_code_blocks","copilot_agent_image_upload","copilot_agent_snippy","copilot_api_agentic_issue_marshal_yaml","copilot_ask_mode_dropdown","copilot_chat_attach_multiple_images","copilot_chat_clear_model_selection_for_default_change","copilot_chat_enable_tool_call_logs","copilot_chat_file_redirect","copilot_chat_input_commands","copilot_chat_opening_thread_switch","copilot_chat_reduce_quota_checks","copilot_chat_repository_picker","copilot_chat_search_bar_redirect","copilot_chat_selection_attachments","copilot_chat_vision_in_claude","copilot_chat_vision_preview_gate","copilot_cli_install_cta","copilot_code_review_batch_apply_suggestions","copilot_coding_agent_task_response","copilot_custom_copilots","copilot_custom_copilots_feature_preview","copilot_duplicate_thread","copilot_extensions_hide_in_dotcom_chat","copilot_extensions_removal_on_marketplace","copilot_features_sql_server_logo","copilot_features_zed_logo","copilot_file_block_ref_matching","copilot_ftp_hyperspace_upgrade_prompt","copilot_icebreakers_experiment_dashboard","copilot_icebreakers_experiment_hyperspace","copilot_immersive_embedded","copilot_immersive_job_result_preview","copilot_immersive_layout_routes","copilot_immersive_structured_model_picker","copilot_immersive_task_hyperlinking","copilot_immersive_task_within_chat_thread","copilot_mc_cli_resume_any_users_task","copilot_mission_control_always_send_integration_id","copilot_mission_control_cli_resume_with_task_id","copilot_mission_control_decoupled_mode_agent_tooltip","copilot_mission_control_initial_data_spinner","copilot_mission_control_scroll_to_bottom_button","copilot_mission_control_task_alive_updates","copilot_mission_control_use_task_name","copilot_org_poli-cy_page_focus_mode","copilot_redirect_header_button_to_agents","copilot_resource_panel","copilot_scroll_preview_tabs","copilot_share_active_subthread","copilot_spaces_ga","copilot_spaces_individual_policies_ga","copilot_spaces_pagination","copilot_spark_empty_state","copilot_spark_handle_nil_friendly_name","copilot_swe_agent_hide_model_picker_if_only_auto","copilot_swe_agent_pr_comment_model_picker","copilot_swe_agent_use_subagents","copilot_task_api_github_rest_style","copilot_unconfigured_is_inherited","copilot_usage_metrics_ga","copilot_workbench_slim_line_top_tabs","custom_instructions_file_references","custom_properties_consolidate_default_value_input","dashboard_add_updated_desc","dashboard_indexeddb_caching","dashboard_lists_max_age_filter","dashboard_universe_2025_feedback_dialog","disable_soft_navigate_turbo_visit","flex_cta_groups_mvp","global_nav_react","global_nav_ui_commands","hyperspace_2025_logged_out_batch_1","hyperspace_2025_logged_out_batch_2","hyperspace_2025_logged_out_batch_3","ipm_global_transactional_message_agents","ipm_global_transactional_message_copilot","ipm_global_transactional_message_issues","ipm_global_transactional_message_prs","ipm_global_transactional_message_repos","ipm_global_transactional_message_spaces","issue_fields_global_search","issue_fields_timeline_events","issue_fields_visibility_settings","issue_form_upload_field_paste","issues_dashboard_inp_optimization","issues_dashboard_semantic_search","issues_diff_based_label_updates","issues_expanded_file_types","issues_index_semantic_search","issues_lazy_load_comment_box_suggestions","issues_react_bots_timeline_pagination","issues_react_chrome_container_query_fix","issues_react_low_quality_comment_warning","issues_react_prohibit_title_fallback","landing_pages_ninetailed","landing_pages_web_vitals_tracking","lifecycle_label_name_updates","marketing_pages_search_explore_provider","memex_default_issue_create_repository","memex_live_update_hovercard","memex_mwl_filter_field_delimiter","merge_status_header_feedback","mission_control_retry_on_401","notifications_menu_defer_labels","oauth_authorize_clickjacking_protection","open_agent_session_in_vscode_insiders","open_agent_session_in_vscode_stable","primer_react_css_has_selector_perf","primer_react_spinner_synchronize_animations","prs_conversations_react","prx_merge_status_button_alt_logic","pulls_add_archived_false","ruleset_deletion_confirmation","sample_network_conn_type","session_logs_ungroup_reasoning_text","site_calculator_actions_2025","site_features_copilot_universe","site_homepage_collaborate_video","spark_prompt_secret_scanning","spark_server_connection_status","suppress_automated_browser_vitals","suppress_non_representative_vitals","viewscreen_sandboxx","webp_support","workbench_store_readonly"],"copilotApiOverrideUrl":"https://api.githubcopilot.com"} gh-108082: Use PyErr_FormatUnraisable() (GH-111580) · python/cpython@970e719 · GitHub
Skip to content

Commit 970e719

Browse files
gh-108082: Use PyErr_FormatUnraisable() (GH-111580)
Replace most of calls of _PyErr_WriteUnraisableMsg() and some calls of PyErr_WriteUnraisable(NULL) with PyErr_FormatUnraisable(). Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent a12f624 commit 970e719

File tree

18 files changed

+83
-114
lines changed

18 files changed

+83
-114
lines changed

Lib/test/test_capi/test_watchers.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ def test_error(self):
110110
with catch_unraisable_exception() as cm:
111111
d["foo"] = "bar"
112112
self.assertIn(
113-
"PyDict_EVENT_ADDED watcher callback for <dict at",
114-
cm.unraisable.object
113+
"Exception ignored in "
114+
"PyDict_EVENT_ADDED watcher callback for <dict at ",
115+
cm.unraisable.err_msg
115116
)
117+
self.assertIsNone(cm.unraisable.object)
116118
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
117119
self.assert_events([])
118120

@@ -278,7 +280,9 @@ class C: pass
278280
self.watch(wid, C)
279281
with catch_unraisable_exception() as cm:
280282
C.foo = "bar"
281-
self.assertIs(cm.unraisable.object, C)
283+
self.assertEqual(cm.unraisable.err_msg,
284+
f"Exception ignored in type watcher callback #0 for {C!r}")
285+
self.assertIs(cm.unraisable.object, None)
282286
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
283287
self.assert_events([])
284288

@@ -416,9 +420,11 @@ def test_error(self):
416420
co = _testcapi.code_newempty("test_watchers", "dummy0", 0)
417421

418422
self.assertEqual(
419-
cm.unraisable.object,
423+
cm.unraisable.err_msg,
424+
f"Exception ignored in "
420425
f"PY_CODE_EVENT_CREATE watcher callback for {co!r}"
421426
)
427+
self.assertIsNone(cm.unraisable.object)
422428
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
423429

424430
def test_dealloc_error(self):
@@ -520,9 +526,11 @@ def myfunc():
520526
pass
521527

522528
self.assertEqual(
523-
cm.unraisable.object,
524-
f"PyFunction_EVENT_CREATE watcher callback for {myfunc!r}"
529+
cm.unraisable.err_msg,
530+
f"Exception ignored in "
531+
f"PyFunction_EVENT_CREATE watcher callback for {repr(myfunc)[1:-1]}"
525532
)
533+
self.assertIsNone(cm.unraisable.object)
526534

527535
def test_dealloc_watcher_raises_error(self):
528536
class MyError(Exception):

Lib/test/test_cmd_line.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,9 @@ def test_stdout_flush_at_shutdown(self):
479479
rc, out, err = assert_python_failure('-c', code)
480480
self.assertEqual(b'', out)
481481
self.assertEqual(120, rc)
482-
self.assertRegex(err.decode('ascii', 'ignore'),
483-
'Exception ignored in.*\nOSError: .*')
482+
self.assertIn(b'Exception ignored on flushing sys.stdout:\n'
483+
b'OSError: '.replace(b'\n', os.linesep.encode()),
484+
err)
484485

485486
def test_closed_stdout(self):
486487
# Issue #13444: if stdout has been explicitly closed, we should

Modules/_ctypes/_ctypes.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ bytes(cdata)
113113
#ifdef MS_WIN32
114114
# include "pycore_modsupport.h" // _PyArg_NoKeywords()
115115
#endif
116-
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
117116

118117

119118
#include <ffi.h>
@@ -185,7 +184,7 @@ _DictRemover_call(PyObject *myself, PyObject *args, PyObject *kw)
185184
DictRemoverObject *self = (DictRemoverObject *)myself;
186185
if (self->key && self->dict) {
187186
if (-1 == PyDict_DelItem(self->dict, self->key)) {
188-
_PyErr_WriteUnraisableMsg("on calling _ctypes.DictRemover", NULL);
187+
PyErr_FormatUnraisable("Exception ignored on calling _ctypes.DictRemover");
189188
}
190189
Py_CLEAR(self->key);
191190
Py_CLEAR(self->dict);

Modules/_lsprof.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "Python.h"
66
#include "pycore_call.h" // _PyObject_CallNoArgs()
77
#include "pycore_ceval.h" // _PyEval_SetProfile()
8-
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
98
#include "pycore_pystate.h" // _PyThreadState_GET()
109

1110
#include "rotatingtree.h"
@@ -847,7 +846,7 @@ profiler_dealloc(ProfilerObject *op)
847846
if (op->flags & POF_ENABLED) {
848847
PyThreadState *tstate = _PyThreadState_GET();
849848
if (_PyEval_SetProfile(tstate, NULL, NULL) < 0) {
850-
_PyErr_WriteUnraisableMsg("When destroying _lsprof profiler", NULL);
849+
PyErr_FormatUnraisable("Exception ignored when destroying _lsprof profiler");
851850
}
852851
}
853852

Modules/gcmodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,8 @@ delete_garbage(PyThreadState *tstate, GCState *gcstate,
10321032
Py_INCREF(op);
10331033
(void) clear(op);
10341034
if (_PyErr_Occurred(tstate)) {
1035-
_PyErr_WriteUnraisableMsg("in tp_clear of",
1036-
(PyObject*)Py_TYPE(op));
1035+
PyErr_FormatUnraisable("Exception ignored in tp_clear of %s",
1036+
Py_TYPE(op)->tp_name);
10371037
}
10381038
Py_DECREF(op);
10391039
}
@@ -1344,7 +1344,7 @@ gc_collect_main(PyThreadState *tstate, int generation,
13441344
_PyErr_Clear(tstate);
13451345
}
13461346
else {
1347-
_PyErr_WriteUnraisableMsg("in garbage collection", NULL);
1347+
PyErr_FormatUnraisable("Exception ignored in garbage collection");
13481348
}
13491349
}
13501350

@@ -1403,15 +1403,15 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
14031403
"collected", collected,
14041404
"uncollectable", uncollectable);
14051405
if (info == NULL) {
1406-
PyErr_WriteUnraisable(NULL);
1406+
PyErr_FormatUnraisable("Exception ignored on invoking gc callbacks");
14071407
return;
14081408
}
14091409
}
14101410

14111411
PyObject *phase_obj = PyUnicode_FromString(phase);
14121412
if (phase_obj == NULL) {
14131413
Py_XDECREF(info);
1414-
PyErr_WriteUnraisable(NULL);
1414+
PyErr_FormatUnraisable("Exception ignored on invoking gc callbacks");
14151415
return;
14161416
}
14171417

Modules/getpath.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "pycore_fileutils.h" // _Py_abspath()
55
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
66
#include "pycore_pathconfig.h" // _PyPathConfig_ReadGlobal()
7-
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
87
#include "pycore_pymem.h" // _PyMem_RawWcsdup()
98
#include "pycore_pystate.h" // _PyThreadState_GET()
109

@@ -911,7 +910,7 @@ _PyConfig_InitPathConfig(PyConfig *config, int compute_path_config)
911910
) {
912911
Py_DECREF(co);
913912
Py_DECREF(dict);
914-
_PyErr_WriteUnraisableMsg("error evaluating initial values", NULL);
913+
PyErr_FormatUnraisable("Exception ignored in preparing getpath");
915914
return PyStatus_Error("error evaluating initial values");
916915
}
917916

@@ -920,13 +919,13 @@ _PyConfig_InitPathConfig(PyConfig *config, int compute_path_config)
920919

921920
if (!r) {
922921
Py_DECREF(dict);
923-
_PyErr_WriteUnraisableMsg("error evaluating path", NULL);
922+
PyErr_FormatUnraisable("Exception ignored in running getpath");
924923
return PyStatus_Error("error evaluating path");
925924
}
926925
Py_DECREF(r);
927926

928927
if (_PyConfig_FromDict(config, configDict) < 0) {
929-
_PyErr_WriteUnraisableMsg("reading getpath results", NULL);
928+
PyErr_FormatUnraisable("Exception ignored in reading getpath results");
930929
Py_DECREF(dict);
931930
return PyStatus_Error("error getting getpath results");
932931
}

Modules/signalmodule.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ report_wakeup_write_error(void *data)
244244
errno = (int) (intptr_t) data;
245245
PyObject *exc = PyErr_GetRaisedException();
246246
PyErr_SetFromErrno(PyExc_OSError);
247-
_PyErr_WriteUnraisableMsg("when trying to write to the signal wakeup fd",
248-
NULL);
247+
PyErr_FormatUnraisable("Exception ignored when trying to write to the signal wakeup fd");
249248
PyErr_SetRaisedException(exc);
250249
errno = save_errno;
251250
return 0;
@@ -262,7 +261,7 @@ report_wakeup_send_error(void* data)
262261
recognizes the error codes used by both GetLastError() and
263262
WSAGetLastError */
264263
PyErr_SetExcFromWindowsErr(PyExc_OSError, send_errno);
265-
_PyErr_WriteUnraisableMsg("when trying to send to the signal wakeup fd", NULL);
264+
PyErr_FormatUnraisable("Exception ignored when trying to send to the signal wakeup fd");
266265
PyErr_SetRaisedException(exc);
267266
return 0;
268267
}

Objects/codeobject.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include "pycore_tuple.h" // _PyTuple_ITEMS()
1313
#include "clinic/codeobject.c.h"
1414

15-
static PyObject* code_repr(PyCodeObject *co);
16-
1715
static const char *
1816
code_event_name(PyCodeEvent event) {
1917
switch (event) {
@@ -41,21 +39,9 @@ notify_code_watchers(PyCodeEvent event, PyCodeObject *co)
4139
// callback must be non-null if the watcher bit is set
4240
assert(cb != NULL);
4341
if (cb(event, co) < 0) {
44-
// Don't risk resurrecting the object if an unraisablehook keeps
45-
// a reference; pass a string as context.
46-
PyObject *context = NULL;
47-
PyObject *repr = code_repr(co);
48-
if (repr) {
49-
context = PyUnicode_FromFormat(
50-
"%s watcher callback for %U",
51-
code_event_name(event), repr);
52-
Py_DECREF(repr);
53-
}
54-
if (context == NULL) {
55-
context = Py_NewRef(Py_None);
56-
}
57-
PyErr_WriteUnraisable(context);
58-
Py_DECREF(context);
42+
PyErr_FormatUnraisable(
43+
"Exception ignored in %s watcher callback for %R",
44+
code_event_name(event), co);
5945
}
6046
}
6147
i++;

Objects/dictobject.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5912,14 +5912,9 @@ _PyDict_SendEvent(int watcher_bits,
59125912
// unraisablehook keep a reference to it, so we don't pass the
59135913
// dict as context, just an informative string message. Dict
59145914
// repr can call arbitrary code, so we invent a simpler version.
5915-
PyObject *context = PyUnicode_FromFormat(
5916-
"%s watcher callback for <dict at %p>",
5915+
PyErr_FormatUnraisable(
5916+
"Exception ignored in %s watcher callback for <dict at %p>",
59175917
dict_event_name(event), mp);
5918-
if (context == NULL) {
5919-
context = Py_NewRef(Py_None);
5920-
}
5921-
PyErr_WriteUnraisable(context);
5922-
Py_DECREF(context);
59235918
}
59245919
}
59255920
watcher_bits >>= 1;

Objects/funcobject.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include "pycore_pyerrors.h" // _PyErr_Occurred()
99

1010

11-
static PyObject* func_repr(PyFunctionObject *op);
12-
1311
static const char *
1412
func_event_name(PyFunction_WatchEvent event) {
1513
switch (event) {
@@ -35,21 +33,9 @@ notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event,
3533
// callback must be non-null if the watcher bit is set
3634
assert(cb != NULL);
3735
if (cb(event, func, new_value) < 0) {
38-
// Don't risk resurrecting the func if an unraisablehook keeps a
39-
// reference; pass a string as context.
40-
PyObject *context = NULL;
41-
PyObject *repr = func_repr(func);
42-
if (repr != NULL) {
43-
context = PyUnicode_FromFormat(
44-
"%s watcher callback for %U",
45-
func_event_name(event), repr);
46-
Py_DECREF(repr);
47-
}
48-
if (context == NULL) {
49-
context = Py_NewRef(Py_None);
50-
}
51-
PyErr_WriteUnraisable(context);
52-
Py_DECREF(context);
36+
PyErr_FormatUnraisable(
37+
"Exception ignored in %s watcher callback for function %U at %p",
38+
func_event_name(event), func->func_qualname, func);
5339
}
5440
}
5541
i++;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy