From fd52348fa2639efa47cb3e044b78b1b07d3962a9 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 14 Apr 2024 09:11:46 -0400 Subject: [PATCH 1/3] sage/libs/glpk: remove glpk.error module There are no longer any "expected crashes" in the GLPK backend, and nothing else seems to be using the glpk.error module. This commit removes it. --- src/sage/libs/glpk/error.pyx | 120 ----------------------------------- 1 file changed, 120 deletions(-) delete mode 100644 src/sage/libs/glpk/error.pyx diff --git a/src/sage/libs/glpk/error.pyx b/src/sage/libs/glpk/error.pyx deleted file mode 100644 index 0ba90710023..00000000000 --- a/src/sage/libs/glpk/error.pyx +++ /dev/null @@ -1,120 +0,0 @@ -""" -Error handler for the GLPK library -""" - -# **************************************************************************** -# Copyright (C) 2015 Jeroen Demeyer -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 2 of the License, or -# (at your option) any later version. -# https://www.gnu.org/licenses/ -# **************************************************************************** - -from cysignals.signals cimport sig_error - -from sage.libs.glpk.env cimport * -from cpython.exc cimport PyErr_SetObject -from sage.cpython.string cimport char_to_str -from sage.numerical.mip import MIPSolverException - - -class GLPKError(MIPSolverException): - """ - A low-level error that is raised by ``sage_glpk_term_hook``. - - The GLPK API considers these errors non-recoverable. - User code should not try to catch this exception. - - EXAMPLES:: - - sage: from sage.libs.glpk.error import GLPKError - sage: raise GLPKError("trouble") - Traceback (most recent call last): - ... - GLPKError: trouble - """ - pass - - -# Global error message string -cdef error_message = "" - - -cdef int sage_glpk_term_hook(void *info, const char *s) noexcept with gil: - """ - A hook to intercept all output written by GLPK. - """ - global error_message - if glp_at_error(): - # Save error message and skip normal printing - error_message += char_to_str(s) - return 1 - else: - # Normal non-error output: the return value 0 means that GLPK - # will write the output as usual. - return 0 - - -cdef void sage_glpk_error_hook(void *info) noexcept with gil: - """ - A hook to intercept GLPK errors. - """ - global error_message - PyErr_SetObject(GLPKError, error_message.strip()) - error_message = "" - sig_error() - - -def setup_glpk_error_handler(): - r""" - Setup the GLPK error handler. Called automatically when this module - is imported at Sage startup. - - We install this error handler so that an error does not lead to - an immediate error exit of the process. Instead, we raise a - ``GLPKError`` for the convenience of developers. - - The GLPK API considers errors non-recoverable. - Therefore, user code should not try to catch this exception. - - TESTS:: - - sage: cython( # optional - glpk_error_recovery_patch - ....: ''' - ....: # distutils: libraries = glpk z gmp - ....: from cysignals.signals cimport sig_on, sig_off - ....: from sage.libs.glpk.env cimport glp_term_out - ....: sig_on() - ....: glp_term_out(12345) # invalid value - ....: sig_off() - ....: ''') - Traceback (most recent call last): - ... - GLPKError: glp_term_out: flag = 12345; invalid parameter - Error detected in file env/stdout.c at line ... - - Check that normal terminal output still works, see :issue:`20832`:: - - sage: def verbose_GLPK(): - ....: from sage.numerical.backends.generic_backend import get_solver - ....: s = get_solver(solver = "GLPK") - ....: s.set_verbosity(2) - ....: return s - sage: p = MixedIntegerLinearProgram(solver=verbose_GLPK) - sage: x, y = p['x'], p['y'] - sage: p.add_constraint(2*x + 3*y <= 6) - sage: p.add_constraint(3*x + 2*y <= 6) - sage: p.add_constraint(x >= 0) - sage: p.set_objective(x + y) - sage: print('output', flush=True); res = p.solve() - output ... 0: obj = ... - sage: res # rel tol 1e-15 - 2.4 - """ - glp_term_hook(sage_glpk_term_hook, NULL) - glp_error_hook(sage_glpk_error_hook, NULL) - - -setup_glpk_error_handler() From ef282be4bd54f6d2ce0275904edaa689487c8bfb Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 14 Apr 2024 09:13:33 -0400 Subject: [PATCH 2/3] build/pkgs/glpk: drop GLPK patch that was rejected by upstream. SageMath has been carrying a patch to GLPK's error handling that was rejected as unsupported by upstream. The GLPK backend has been updated to obviate this patch (unless the user ignores a warning and changes the default solver...), so we can finally drop it and bring SageMath's GLPK package to parity with the various distributions. --- build/pkgs/glpk/patches/error_recovery.patch | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 build/pkgs/glpk/patches/error_recovery.patch diff --git a/build/pkgs/glpk/patches/error_recovery.patch b/build/pkgs/glpk/patches/error_recovery.patch deleted file mode 100644 index a383e25769b..00000000000 --- a/build/pkgs/glpk/patches/error_recovery.patch +++ /dev/null @@ -1,16 +0,0 @@ -From: Jeroen Demeyer -Allow error recovery. See discussion at -https://github.com/sagemath/sage/issues/20710#comment:18 - -diff --git a/src/env/error.c b/src/env/error.c -index a898b76..154de0f 100644 ---- a/src/env/error.c -+++ b/src/env/error.c -@@ -48,6 +48,7 @@ static void errfunc(const char *fmt, ...) - va_end(arg); - xprintf("Error detected in file %s at line %d\n", - env->err_file, env->err_line); -+ env->err_st = 0; - if (env->err_hook != NULL) - env->err_hook(env->err_info); - abort(); From 5a3698bd3a90a1b839817574ca55013f4abaff62 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 15 Apr 2024 07:23:11 -0400 Subject: [PATCH 3/3] build/pkgs/glpk: bump patch level to p1 --- build/pkgs/glpk/package-version.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/pkgs/glpk/package-version.txt b/build/pkgs/glpk/package-version.txt index 496a5a8b3ea..11729725c0e 100644 --- a/build/pkgs/glpk/package-version.txt +++ b/build/pkgs/glpk/package-version.txt @@ -1 +1 @@ -5.0.p0 +5.0.p1