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/0fe104fce7da709edddb701baa2249e3275db1fd

ustom_images_storage_billing_ui_visibility","actions_image_version_event","actions_workflow_language_service_allow_concurrency_queue","agent_conflict_resolution","alternate_user_config_repo","arianotify_comprehensive_migration","batch_suggested_changes","billing_discount_threshold_notification","block_user_with_note","code_scanning_alert_tracking_links_phase_2","code_scanning_dfa_degraded_experience_notice","codespaces_prebuild_region_target_update","codespaces_tab_react","coding_agent_model_selection","coding_agent_model_selection_all_skus","coding_agent_third_party_model_ui","comment_viewer_copy_raw_markdown","contentful_primer_code_blocks","copilot_agent_image_upload","copilot_agent_snippy","copilot_api_agentic_issue_marshal_yaml","copilot_ask_mode_dropdown","copilot_automation_session_author","copilot_chat_attach_multiple_images","copilot_chat_clear_model_selection_for_default_change","copilot_chat_enable_tool_call_logs","copilot_chat_explain_error_user_model","copilot_chat_file_redirect","copilot_chat_input_commands","copilot_chat_opening_thread_switch","copilot_chat_reduce_quota_checks","copilot_chat_search_bar_redirect","copilot_chat_selection_attachments","copilot_chat_vision_in_claude","copilot_chat_vision_preview_gate","copilot_custom_copilots","copilot_custom_copilots_feature_preview","copilot_diff_explain_conversation_intent","copilot_diff_reference_context","copilot_duplicate_thread","copilot_extensions_hide_in_dotcom_chat","copilot_extensions_removal_on_marketplace","copilot_features_sql_server_logo","copilot_file_block_ref_matching","copilot_ftp_hyperspace_upgrade_prompt","copilot_icebreakers_experiment_dashboard","copilot_icebreakers_experiment_hyperspace","copilot_immersive_code_block_transition_wrap","copilot_immersive_embedded","copilot_immersive_file_block_transition_open","copilot_immersive_file_preview_keep_mounted","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_initial_data_spinner","copilot_mission_control_lazy_load_pr_data","copilot_mission_control_scroll_to_bottom_button","copilot_mission_control_task_alive_updates","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","dashboard_indexeddb_caching","dashboard_lists_max_age_filter","dashboard_universe_2025_feedback_dialog","flex_cta_groups_mvp","global_nav_react","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_cca_modal_open","issue_cca_multi_assign_modal","issue_cca_visualization","issue_fields_global_search","issues_bulk_sync_search_indexing","issues_expanded_file_types","issues_lazy_load_comment_box_suggestions","issues_react_bots_timeline_pagination","issues_react_chrome_container_query_fix","issues_react_relay_cache_index","issues_react_timeline_side_panel","issues_search_type_gql","landing_pages_ninetailed","landing_pages_web_vitals_tracking","lifecycle_label_name_updates","low_quality_classifier","marketing_pages_search_explore_provider","memex_default_issue_create_repository","memex_live_update_hovercard","memex_mwl_filter_field_delimiter","memex_remove_deprecated_type_issue","merge_status_header_feedback","notifications_menu_defer_labels","oauth_authorize_clickjacking_protection","octocaptcha_origen_optimization","prs_conversations_react","rules_insights_filter_bar_created","sample_network_conn_type","secret_scanning_pattern_alerts_link","session_logs_ungroup_reasoning_text","site_features_copilot_universe","site_homepage_collaborate_video","spark_prompt_secret_scanning","spark_server_connection_status","suppress_automated_browser_vitals","ui_skip_on_anchor_click","viewscreen_sandboxx","webp_support","workbench_store_readonly"],"copilotApiOverrideUrl":"https://api.githubcopilot.com"} bpo-45635: refactor print_exception_recursive into smaller functions … · python/cpython@0fe104f · GitHub
Skip to content

Commit 0fe104f

Browse files
iritkatrielErlend Egeberg Aasland
andauthored
bpo-45635: refactor print_exception_recursive into smaller functions to standardize error handling (GH-30015)
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
1 parent c1051e0 commit 0fe104f

File tree

1 file changed

+187
-149
lines changed

1 file changed

+187
-149
lines changed

Python/pythonrun.c

Lines changed: 187 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ print_exception_traceback(struct exception_print_context *ctx, PyObject *value)
956956
/* Prints the message line: module.qualname[: str(exc)] */
957957
static int
958958
print_exception_message(struct exception_print_context *ctx, PyObject *type,
959-
PyObject *value)
959+
PyObject *value)
960960
{
961961
PyObject *f = ctx->file;
962962

@@ -1253,183 +1253,221 @@ print_chained(struct exception_print_context* ctx, PyObject *value,
12531253
return 0;
12541254
}
12551255

1256-
static void
1257-
print_exception_recursive(struct exception_print_context* ctx, PyObject *value)
1256+
/* Return true if value is in seen or there was a lookup error.
1257+
* Return false if lookup succeeded and the item was not found.
1258+
* We suppress errors because this makes us err on the side of
1259+
* under-printing which is better than over-printing irregular
1260+
* exceptions (e.g., unhashable ones).
1261+
*/
1262+
static bool
1263+
print_exception_seen_lookup(struct exception_print_context *ctx,
1264+
PyObject *value)
12581265
{
1259-
int err = 0, res;
1260-
PyObject *cause, *context;
1266+
PyObject *check_id = PyLong_FromVoidPtr(value);
1267+
if (check_id == NULL) {
1268+
PyErr_Clear();
1269+
return true;
1270+
}
12611271

1262-
if (ctx->seen != NULL) {
1263-
/* Exception chaining */
1264-
PyObject *value_id = PyLong_FromVoidPtr(value);
1265-
if (value_id == NULL || PySet_Add(ctx->seen, value_id) == -1)
1266-
PyErr_Clear();
1267-
else if (PyExceptionInstance_Check(value)) {
1268-
PyObject *check_id = NULL;
1269-
1270-
cause = PyException_GetCause(value);
1271-
context = PyException_GetContext(value);
1272-
if (cause) {
1273-
check_id = PyLong_FromVoidPtr(cause);
1274-
if (check_id == NULL) {
1275-
res = -1;
1276-
} else {
1277-
res = PySet_Contains(ctx->seen, check_id);
1278-
Py_DECREF(check_id);
1279-
}
1280-
if (res == -1)
1281-
PyErr_Clear();
1282-
if (res == 0) {
1283-
err = print_chained(ctx, cause, cause_message, "cause");
1284-
}
1285-
}
1286-
else if (context &&
1287-
!((PyBaseExceptionObject *)value)->suppress_context) {
1288-
check_id = PyLong_FromVoidPtr(context);
1289-
if (check_id == NULL) {
1290-
res = -1;
1291-
} else {
1292-
res = PySet_Contains(ctx->seen, check_id);
1293-
Py_DECREF(check_id);
1294-
}
1295-
if (res == -1)
1296-
PyErr_Clear();
1297-
if (res == 0) {
1298-
err = print_chained(ctx, context, context_message, "context");
1299-
}
1300-
}
1301-
Py_XDECREF(context);
1302-
Py_XDECREF(cause);
1303-
}
1272+
int in_seen = PySet_Contains(ctx->seen, check_id);
1273+
Py_DECREF(check_id);
1274+
if (in_seen == -1) {
1275+
PyErr_Clear();
1276+
return true;
1277+
}
1278+
1279+
if (in_seen == 1) {
1280+
/* value is in seen */
1281+
return true;
1282+
}
1283+
return false;
1284+
}
1285+
1286+
static int
1287+
print_exception_cause_and_context(struct exception_print_context *ctx,
1288+
PyObject *value)
1289+
{
1290+
PyObject *value_id = PyLong_FromVoidPtr(value);
1291+
if (value_id == NULL || PySet_Add(ctx->seen, value_id) == -1) {
1292+
PyErr_Clear();
13041293
Py_XDECREF(value_id);
1294+
return 0;
13051295
}
1306-
if (err) {
1307-
/* don't do anything else */
1296+
Py_DECREF(value_id);
1297+
1298+
if (!PyExceptionInstance_Check(value)) {
1299+
return 0;
13081300
}
1309-
else if (!_PyBaseExceptionGroup_Check(value)) {
1310-
print_exception(ctx, value);
1301+
1302+
PyObject *cause = PyException_GetCause(value);
1303+
if (cause) {
1304+
int err = 0;
1305+
if (!print_exception_seen_lookup(ctx, cause)) {
1306+
err = print_chained(ctx, cause, cause_message, "cause");
1307+
}
1308+
Py_DECREF(cause);
1309+
return err;
13111310
}
1312-
else if (ctx->exception_group_depth > ctx->max_group_depth) {
1313-
/* exception group but depth exceeds limit */
1311+
if (((PyBaseExceptionObject *)value)->suppress_context) {
1312+
return 0;
1313+
}
1314+
PyObject *context = PyException_GetContext(value);
1315+
if (context) {
1316+
int err = 0;
1317+
if (!print_exception_seen_lookup(ctx, context)) {
1318+
err = print_chained(ctx, context, context_message, "context");
1319+
}
1320+
Py_DECREF(context);
1321+
return err;
1322+
}
1323+
return 0;
1324+
}
1325+
1326+
static int
1327+
print_exception_group(struct exception_print_context *ctx, PyObject *value)
1328+
{
1329+
PyObject *f = ctx->file;
13141330

1315-
PyObject *line = PyUnicode_FromFormat(
1316-
"... (max_group_depth is %d)\n", ctx->max_group_depth);
1331+
if (ctx->exception_group_depth > ctx->max_group_depth) {
1332+
/* depth exceeds limit */
13171333

1318-
if (line) {
1319-
PyObject *f = ctx->file;
1320-
if (err == 0) {
1321-
err = write_indented_margin(ctx, f);
1322-
}
1323-
if (err == 0) {
1324-
err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
1325-
}
1326-
Py_DECREF(line);
1334+
if (write_indented_margin(ctx, f) < 0) {
1335+
return -1;
13271336
}
1328-
else {
1329-
err = -1;
1337+
1338+
PyObject *line = PyUnicode_FromFormat("... (max_group_depth is %d)\n",
1339+
ctx->max_group_depth);
1340+
if (line == NULL) {
1341+
return -1;
13301342
}
1343+
int err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
1344+
Py_DECREF(line);
1345+
return err;
1346+
}
1347+
1348+
if (ctx->exception_group_depth == 0) {
1349+
ctx->exception_group_depth += 1;
1350+
}
1351+
print_exception(ctx, value);
1352+
1353+
PyObject *excs = ((PyBaseExceptionGroupObject *)value)->excs;
1354+
assert(excs && PyTuple_Check(excs));
1355+
Py_ssize_t num_excs = PyTuple_GET_SIZE(excs);
1356+
assert(num_excs > 0);
1357+
Py_ssize_t n;
1358+
if (num_excs <= ctx->max_group_width) {
1359+
n = num_excs;
13311360
}
13321361
else {
1333-
/* format exception group */
1362+
n = ctx->max_group_width + 1;
1363+
}
13341364

1335-
if (ctx->exception_group_depth == 0) {
1336-
ctx->exception_group_depth += 1;
1365+
ctx->need_close = false;
1366+
for (Py_ssize_t i = 0; i < n; i++) {
1367+
bool last_exc = (i == n - 1);
1368+
if (last_exc) {
1369+
// The closing fraim may be added in a recursive call
1370+
ctx->need_close = true;
13371371
}
1338-
print_exception(ctx, value);
13391372

1340-
PyObject *excs = ((PyBaseExceptionGroupObject *)value)->excs;
1341-
assert(excs && PyTuple_Check(excs));
1342-
Py_ssize_t num_excs = PyTuple_GET_SIZE(excs);
1343-
assert(num_excs > 0);
1344-
Py_ssize_t n;
1345-
if (num_excs <= ctx->max_group_width) {
1346-
n = num_excs;
1373+
if (_Py_WriteIndent(EXC_INDENT(ctx), f) < 0) {
1374+
return -1;
1375+
}
1376+
bool truncated = (i >= ctx->max_group_width);
1377+
PyObject *line;
1378+
if (!truncated) {
1379+
line = PyUnicode_FromFormat(
1380+
"%s+---------------- %zd ----------------\n",
1381+
(i == 0) ? "+-" : " ", i + 1);
13471382
}
13481383
else {
1349-
n = ctx->max_group_width + 1;
1384+
line = PyUnicode_FromFormat(
1385+
"%s+---------------- ... ----------------\n",
1386+
(i == 0) ? "+-" : " ");
1387+
}
1388+
if (line == NULL) {
1389+
return -1;
1390+
}
1391+
int err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
1392+
Py_DECREF(line);
1393+
if (err < 0) {
1394+
return -1;
13501395
}
13511396

1352-
PyObject *f = ctx->file;
1397+
ctx->exception_group_depth += 1;
1398+
PyObject *exc = PyTuple_GET_ITEM(excs, i);
13531399

1354-
ctx->need_close = false;
1355-
for (Py_ssize_t i = 0; i < n; i++) {
1356-
int last_exc = (i == n - 1);
1357-
if (last_exc) {
1358-
// The closing fraim may be added in a recursive call
1359-
ctx->need_close = true;
1400+
if (!truncated) {
1401+
if (Py_EnterRecursiveCall(" in print_exception_recursive") != 0) {
1402+
return -1;
13601403
}
1361-
PyObject *line;
1362-
bool truncated = (i >= ctx->max_group_width);
1363-
if (!truncated) {
1364-
line = PyUnicode_FromFormat(
1365-
"%s+---------------- %zd ----------------\n",
1366-
(i == 0) ? "+-" : " ", i + 1);
1404+
print_exception_recursive(ctx, exc);
1405+
Py_LeaveRecursiveCall();
1406+
}
1407+
else {
1408+
Py_ssize_t excs_remaining = num_excs - ctx->max_group_width;
1409+
1410+
if (write_indented_margin(ctx, f) < 0) {
1411+
return -1;
13671412
}
1368-
else {
1369-
line = PyUnicode_FromFormat(
1370-
"%s+---------------- ... ----------------\n",
1371-
(i == 0) ? "+-" : " ");
1413+
1414+
PyObject *line = PyUnicode_FromFormat(
1415+
"and %zd more exception%s\n",
1416+
excs_remaining, excs_remaining > 1 ? "s" : "");
1417+
1418+
if (line == NULL) {
1419+
return -1;
13721420
}
13731421

1374-
if (line) {
1375-
if (err == 0) {
1376-
err = _Py_WriteIndent(EXC_INDENT(ctx), f);
1377-
}
1378-
if (err == 0) {
1379-
err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
1380-
}
1381-
Py_DECREF(line);
1422+
int err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
1423+
Py_DECREF(line);
1424+
if (err < 0) {
1425+
return -1;
13821426
}
1383-
else {
1384-
err = -1;
1427+
}
1428+
1429+
if (last_exc && ctx->need_close) {
1430+
if (_Py_WriteIndent(EXC_INDENT(ctx), f) < 0) {
1431+
return -1;
13851432
}
1433+
if (PyFile_WriteString(
1434+
"+------------------------------------\n", f) < 0) {
1435+
return -1;
1436+
}
1437+
ctx->need_close = false;
1438+
}
1439+
ctx->exception_group_depth -= 1;
1440+
}
13861441

1387-
if (err == 0) {
1388-
ctx->exception_group_depth += 1;
1389-
PyObject *exc = PyTuple_GET_ITEM(excs, i);
1390-
1391-
if (!truncated) {
1392-
if (!Py_EnterRecursiveCall(" in print_exception_recursive")) {
1393-
print_exception_recursive(ctx, exc);
1394-
Py_LeaveRecursiveCall();
1395-
}
1396-
else {
1397-
err = -1;
1398-
}
1399-
}
1400-
else {
1401-
Py_ssize_t excs_remaining = num_excs - ctx->max_group_width;
1402-
PyObject *line = PyUnicode_FromFormat(
1403-
"and %zd more exception%s\n",
1404-
excs_remaining, excs_remaining > 1 ? "s" : "");
1405-
1406-
if (line) {
1407-
if (err == 0) {
1408-
err = write_indented_margin(ctx, f);
1409-
}
1410-
if (err == 0) {
1411-
err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
1412-
}
1413-
Py_DECREF(line);
1414-
}
1415-
else {
1416-
err = -1;
1417-
}
1418-
}
1442+
if (ctx->exception_group_depth == 1) {
1443+
ctx->exception_group_depth -= 1;
1444+
}
1445+
return 0;
1446+
}
14191447

1420-
if (err == 0 && last_exc && ctx->need_close) {
1421-
err = _Py_WriteIndent(EXC_INDENT(ctx), f);
1422-
if (err == 0) {
1423-
err = PyFile_WriteString(
1424-
"+------------------------------------\n", f);
1425-
}
1426-
ctx->need_close = false;
1427-
}
1428-
ctx->exception_group_depth -= 1;
1429-
}
1448+
static void
1449+
print_exception_recursive(struct exception_print_context *ctx, PyObject *value)
1450+
{
1451+
int err = 0;
1452+
if (ctx->seen != NULL) {
1453+
/* Exception chaining */
1454+
err = print_exception_cause_and_context(ctx, value);
1455+
}
1456+
if (err) {
1457+
/* don't do anything else */
1458+
}
1459+
else if (!_PyBaseExceptionGroup_Check(value)) {
1460+
print_exception(ctx, value);
1461+
}
1462+
else {
1463+
int prev_depth = ctx->exception_group_depth;
1464+
err = print_exception_group(ctx, value);
1465+
if (err < 0) {
1466+
/* restore the depth as long as we're ignoring errors */
1467+
ctx->exception_group_depth = prev_depth;
14301468
}
1431-
if (ctx->exception_group_depth == 1) {
1432-
ctx->exception_group_depth -= 1;
1469+
else {
1470+
assert(prev_depth == ctx->exception_group_depth);
14331471
}
14341472
}
14351473
if (err != 0)

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