Skip to content

Commit

Permalink
Allow non-Function args to retry (#30382)
Browse files Browse the repository at this point in the history
I hit this while trying to use a `PyObject`
  • Loading branch information
simonbyrne authored Feb 12, 2019
1 parent 236c69b commit 36d062d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
10 changes: 6 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ New library functions
Standard library changes
------------------------

* The `extrema` function now accepts a function argument in the same manner as `minimum` and
`maximum` ([#30323]).
* `hasmethod` can now check for matching keyword argument names ([#30712]).
* `startswith` and `endswith` now accept a `Regex` for the second argument ([#29790]).
* The `extrema` function now accepts a function argument in the same manner as `minimum` and
`maximum` ([#30323]).
* `hasmethod` can now check for matching keyword argument names ([#30712]).
* `startswith` and `endswith` now accept a `Regex` for the second argument ([#29790]).
* `retry` supports arbitrary callable objects ([#30382]).

#### LinearAlgebra

Expand Down Expand Up @@ -86,6 +87,7 @@ Deprecated or removed
[#30323]: https://github.com/JuliaLang/julia/issues/30323
[#30349]: https://github.com/JuliaLang/julia/issues/30349
[#30372]: https://github.com/JuliaLang/julia/issues/30372
[#30382]: https://github.com/JuliaLang/julia/issues/30382
[#30583]: https://github.com/JuliaLang/julia/issues/30583
[#30584]: https://github.com/JuliaLang/julia/issues/30584
[#30593]: https://github.com/JuliaLang/julia/issues/30593
Expand Down
7 changes: 5 additions & 2 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,16 @@ length(ebo::ExponentialBackOff) = ebo.n
eltype(::Type{ExponentialBackOff}) = Float64

"""
retry(f::Function; delays=ExponentialBackOff(), check=nothing) -> Function
retry(f; delays=ExponentialBackOff(), check=nothing) -> Function
Return an anonymous function that calls function `f`. If an exception arises,
`f` is repeatedly called again, each time `check` returns `true`, after waiting the
number of seconds specified in `delays`. `check` should input `delays`'s
current state and the `Exception`.
!!! compat "Julia 1.2"
Before Julia 1.2 this signature was restricted to `f::Function`.
# Examples
```julia
retry(f, delays=fill(5.0, 3))
Expand All @@ -237,7 +240,7 @@ retry(http_get, check=(s,e)->e.status == "503")(url)
retry(read, check=(s,e)->isa(e, IOError))(io, 128; all=false)
```
"""
function retry(f::Function; delays=ExponentialBackOff(), check=nothing)
function retry(f; delays=ExponentialBackOff(), check=nothing)
(args...; kwargs...) -> begin
y = iterate(delays)
while y !== nothing
Expand Down
3 changes: 3 additions & 0 deletions test/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ end
foo_kwargs(x; y=5) = x + y
@test retry(foo_kwargs)(3) == 8
@test retry(foo_kwargs)(3; y=4) == 7

# non-Functions
@test retry(Float64)(1) === 1.0
end

0 comments on commit 36d062d

Please sign in to comment.