Skip to content

Commit 8a971a6

Browse files
restore fallback 3-arg setprecision method (#58586)
fixes #55899 --------- Co-authored-by: Sukera <[email protected]> (cherry picked from commit cdb158b)
1 parent 600ac61 commit 8a971a6

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

NEWS.md

Lines changed: 5 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
------------------------
@@ -225,8 +227,10 @@ Tooling Improvements
225227
[#40989]: https://github.com/JuliaLang/julia/issues/40989
226228
[#45793]: https://github.com/JuliaLang/julia/issues/45793
227229
[#49355]: https://github.com/JuliaLang/julia/issues/49355
230+
[#49933]: https://github.com/JuliaLang/julia/issues/49933
228231
[#50988]: https://github.com/JuliaLang/julia/issues/50988
229232
[#51149]: https://github.com/JuliaLang/julia/issues/51149
233+
[#51362]: https://github.com/JuliaLang/julia/issues/51362
230234
[#51810]: https://github.com/JuliaLang/julia/issues/51810
231235
[#52103]: https://github.com/JuliaLang/julia/issues/52103
232236
[#52999]: https://github.com/JuliaLang/julia/issues/52999
@@ -284,3 +288,4 @@ Tooling Improvements
284288
[#57087]: https://github.com/JuliaLang/julia/issues/57087
285289
[#57109]: https://github.com/JuliaLang/julia/issues/57109
286290
[#57253]: https://github.com/JuliaLang/julia/issues/57253
291+
[#57727]: https://github.com/JuliaLang/julia/issues/57727

base/mpfr.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,9 +1178,29 @@ Often used as `setprecision(T, precision) do ... end`
11781178
Note: `nextfloat()`, `prevfloat()` do not use the precision mentioned by
11791179
`setprecision`.
11801180
1181+
!!! warning
1182+
There is a fallback implementation of this method that calls `precision`
1183+
and `setprecision`, but it should no longer be relied on. Instead, you
1184+
should define the 3-argument form directly in a way that uses `ScopedValue`,
1185+
or recommend that callers use `ScopedValue` and `@with` themselves.
1186+
11811187
!!! compat "Julia 1.8"
11821188
The `base` keyword requires at least Julia 1.8.
11831189
"""
1190+
function setprecision(f::Function, ::Type{T}, prec::Integer; kws...) where T
1191+
depwarn("""
1192+
The fallback `setprecision(::Function, ...)` method is deprecated. Packages overloading this method should
1193+
implement their own specialization using `ScopedValue` instead.
1194+
""", :setprecision)
1195+
old_prec = precision(T)
1196+
setprecision(T, prec; kws...)
1197+
try
1198+
return f()
1199+
finally
1200+
setprecision(T, old_prec)
1201+
end
1202+
end
1203+
11841204
function setprecision(f::Function, ::Type{BigFloat}, prec::Integer; base::Integer=2)
11851205
Base.ScopedValues.@with(CURRENT_PRECISION => _convert_precision_from_base(prec, base), f())
11861206
end

0 commit comments

Comments
 (0)