From 0fea3cc8536afa50ae1c8d1050ce3e94cdeb8deb Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 31 May 2024 14:53:22 +1200 Subject: [PATCH 1/3] Fix printing when show called on invalid variable or constraint --- src/print.jl | 9 +++++++++ test/test_print.jl | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/print.jl b/src/print.jl index 648ea32734d..4e14e51230c 100644 --- a/src/print.jl +++ b/src/print.jl @@ -736,6 +736,9 @@ julia> function_string(MIME("text/plain"), 2 * x + 1) ``` """ function function_string(mode::MIME"text/plain", v::AbstractVariableRef) + if !is_valid(owner_model(v), v) + return "InvalidVariableRef" + end var_name = name(v) if isempty(var_name) return anonymous_name(mode, v) @@ -744,6 +747,9 @@ function function_string(mode::MIME"text/plain", v::AbstractVariableRef) end function function_string(mode::MIME"text/latex", v::AbstractVariableRef) + if !is_valid(owner_model(v), v) + return "InvalidVariableRef" + end var_name = name(v) if isempty(var_name) return anonymous_name(mode, v) @@ -1038,6 +1044,9 @@ julia> constraint_string(MIME("text/plain"), c) ``` """ function constraint_string(mode::MIME, ref::ConstraintRef; in_math_mode = false) + if !is_valid(owner_model(ref), ref) + return "InvalidConstraintRef" + end return constraint_string( mode, name(ref), diff --git a/test/test_print.jl b/test/test_print.jl index 88ff8ae49e7..0214341d996 100644 --- a/test/test_print.jl +++ b/test/test_print.jl @@ -1070,4 +1070,21 @@ function test_print_omit_vector() return end +function test_invalid_references() + model = Model() + @variable(model, x[1:2]) + @constraint(model, c[i=1:2], x[i] <= i) + delete(model, c[1]) + delete(model, x[1]) + mime = MIME("text/plain") + @test sprint(show, mime, x[1]) == "InvalidVariableRef" + @test occursin("InvalidVariableRef", sprint(show, mime, x)) + @test sprint(show, mime, c[1]) == "InvalidConstraintRef" + @test occursin("InvalidConstraintRef", sprint(show, mime, c)) + mime = MIME("text/latex") + @test sprint(show, mime, x[1]) == "\$ InvalidVariableRef \$" + @test sprint(show, mime, c[1]) == "InvalidConstraintRef" + return +end + end # TestPrint From 36cd004104ae50628e24595b74af1f31c0a0db76 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 31 May 2024 15:00:56 +1200 Subject: [PATCH 2/3] Update test/test_print.jl --- test/test_print.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_print.jl b/test/test_print.jl index 0214341d996..d498f0829d3 100644 --- a/test/test_print.jl +++ b/test/test_print.jl @@ -1073,7 +1073,7 @@ end function test_invalid_references() model = Model() @variable(model, x[1:2]) - @constraint(model, c[i=1:2], x[i] <= i) + @constraint(model, c[i in 1:2], x[i] <= i) delete(model, c[1]) delete(model, x[1]) mime = MIME("text/plain") From 78018cc1631b668daa48bf169e51254286ddab06 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 31 May 2024 15:27:17 +1200 Subject: [PATCH 3/3] Update --- test/test_print.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_print.jl b/test/test_print.jl index d498f0829d3..c7730f6eccc 100644 --- a/test/test_print.jl +++ b/test/test_print.jl @@ -119,6 +119,13 @@ function JuMP.add_constraint( ) end +function JuMP.is_valid( + model::M, + con_ref::ConstraintRef{M,CustomIndex}, +) where {M<:GenericModel} + return 1 <= con_ref.index.value <= length(model.ext[:custom_constraints]) +end + function JuMP.constraint_object(cref::ConstraintRef{Model,CustomIndex}) return cref.model.ext[:custom_constraints][cref.index.value] end