Skip to content

Commit 632d8b5

Browse files
committed
restore fallback 3-arg setprecision method
fixes #55899
1 parent b61664e commit 632d8b5

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
@@ -133,6 +133,8 @@ New library features
133133
* `Timer` now has readable `timeout` and `interval` properties, and a more descriptive `show` method ([#57081]).
134134
* `sort` now supports `NTuple`s ([#54494]).
135135
* `map!(f, A)` now stores the results in `A`, like `map!(f, A, A)` or `A .= f.(A)` ([#40632]).
136+
* `setprecision` with a function argument (typically a `do` block) is now thread safe. Other forms
137+
should be avoided, and types should switch to an implementation using `ScopedValue` ([#51362]).
136138

137139
Standard library changes
138140
------------------------

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. Types 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)