Skip to content

Commit cdb158b

Browse files
restore fallback 3-arg setprecision method (#58586)
fixes #55899 --------- Co-authored-by: Sukera <11753998+Seelengrab@users.noreply.github.com>
1 parent abb4942 commit cdb158b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ New library features
134134
* `Timer` now has readable `timeout` and `interval` properties, and a more descriptive `show` method ([#57081]).
135135
* `sort` now supports `NTuple`s ([#54494]).
136136
* `map!(f, A)` now stores the results in `A`, like `map!(f, A, A)` or `A .= f.(A)` ([#40632]).
137+
* `setprecision` with a function argument (typically a `do` block) is now thread safe. Other forms
138+
should be avoided, and types should switch to an implementation using `ScopedValue` ([#51362]).
137139

138140
Standard library changes
139141
------------------------

base/mpfr.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,9 +1183,29 @@ Often used as `setprecision(T, precision) do ... end`
11831183
Note: `nextfloat()`, `prevfloat()` do not use the precision mentioned by
11841184
`setprecision`.
11851185
1186+
!!! warning
1187+
There is a fallback implementation of this method that calls `precision`
1188+
and `setprecision`, but it should no longer be relied on. Instead, you
1189+
should define the 3-argument form directly in a way that uses `ScopedValue`,
1190+
or recommend that callers use `ScopedValue` and `@with` themselves.
1191+
11861192
!!! compat "Julia 1.8"
11871193
The `base` keyword requires at least Julia 1.8.
11881194
"""
1195+
function setprecision(f::Function, ::Type{T}, prec::Integer; kws...) where T
1196+
depwarn("""
1197+
The fallback `setprecision(::Function, ...)` method is deprecated. Packages overloading this method should
1198+
implement their own specialization using `ScopedValue` instead.
1199+
""", :setprecision)
1200+
old_prec = precision(T)
1201+
setprecision(T, prec; kws...)
1202+
try
1203+
return f()
1204+
finally
1205+
setprecision(T, old_prec)
1206+
end
1207+
end
1208+
11891209
function setprecision(f::Function, ::Type{BigFloat}, prec::Integer; base::Integer=2)
11901210
Base.ScopedValues.@with(CURRENT_PRECISION => _convert_precision_from_base(prec, base), f())
11911211
end

0 commit comments

Comments
 (0)