Skip to content

Commit 89f3a12

Browse files
committed
change finalizer argument order to allow for do finalizer functions
1 parent 2579a64 commit 89f3a12

19 files changed

+47
-40
lines changed

base/REPL.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
682682
if !repl.no_history_file
683683
try
684684
f = open(find_hist_file(), true, true, true, false, false)
685-
finalizer(replc, replc->close(f))
685+
finalizer(_->close(f), replc)
686686
hist_from_file(hp, f)
687687
catch e
688688
print_response(repl, e, catch_backtrace(), true, Base.have_color)

base/base.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ const (:) = Colon()
139139
==(w::WeakRef, v) = isequal(w.value, v)
140140
==(w, v::WeakRef) = isequal(w, v.value)
141141

142-
function finalizer(o::ANY, f::Union(Function,Ptr))
143-
if isimmutable(o)
144-
error("objects of type ", typeof(o), " cannot be finalized")
145-
end
142+
finalizer(o::Function, f::Function) = error("invalid finalizer(f::Function, o::Function)")
143+
finalizer(o::Ptr, f::Ptr) = error("invalid finalizer(f::Ptr, o::Ptr)")
144+
finalizer(f::Union(Function,Ptr), o::ANY) = begin
145+
isimmutable(o) && error("objects of type ", typeof(o), " cannot be finalized")
146146
ccall(:jl_gc_add_finalizer, Void, (Any,Any), o, f)
147147
end
148148

base/base64.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Base64Pipe <: IO
2121

2222
function Base64Pipe(io::IO)
2323
b = new(io,0,0,0)
24-
finalizer(b, close)
24+
finalizer(close, b)
2525
return b
2626
end
2727
end

base/deprecated.jl

+7
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,14 @@ end
206206

207207
@deprecate oftype{T}(::Type{T},c) convert(T,c)
208208

209+
209210
@deprecate inf(x::FloatingPoint) oftype(x,Inf)
210211
@deprecate nan(x::FloatingPoint) oftype(x,NaN)
211212
@deprecate inf{T<:FloatingPoint}(::Type{T}) convert(T,Inf)
212213
@deprecate nan{T<:FloatingPoint}(::Type{T}) convert(T,NaN)
214+
215+
function finalizer(o::Any, f::Union(Function,Ptr))
216+
depwarn("finalizer(o::Any, f::Union(Function,Ptr)) is deprecated, " *
217+
"use finalizer(f::Union(Function,Ptr), o::Any) instead", :finalizer)
218+
finalizer(f, o)
219+
end

base/dict.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ function add_weak_key(t::Dict, k, v)
717717
# TODO: it might be better to avoid the finalizer, allow
718718
# wiped WeakRefs to remain in the table, and delete them as
719719
# they are discovered by getindex and setindex!.
720-
finalizer(k, t.deleter)
720+
finalizer(t.deleter, k)
721721
return t
722722
end
723723

@@ -731,7 +731,7 @@ end
731731

732732
function add_weak_value(t::Dict, k, v)
733733
t[k] = WeakRef(v)
734-
finalizer(v, x->weak_value_delete!(t, k, x))
734+
finalizer(x->weak_value_delete!(t, k, x), v)
735735
return t
736736
end
737737

base/fftw.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ type Plan{T<:fftwNumber}
222222
ialign::Int32 # alignment mod 16 of input
223223
function Plan(plan::Ptr{Void}, sz::Dims, istride::Dims, ialign::Int32)
224224
p = new(plan,sz,istride,ialign)
225-
finalizer(p, p -> destroy_plan(T, p.plan))
225+
finalizer(p -> destroy_plan(T, p.plan), p)
226226
return p
227227
end
228228
end

base/gmp.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type BigInt <: Integer
2525
function BigInt()
2626
b = new(zero(Cint), zero(Cint), C_NULL)
2727
ccall((:__gmpz_init,:libgmp), Void, (Ptr{BigInt},), &b)
28-
finalizer(b, _gmp_clear_func)
28+
finalizer(_gmp_clear_func, b)
2929
return b
3030
end
3131
end

base/iostream.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function IOStream(name::String, finalize::Bool)
1616
buf = zeros(Uint8,sizeof_ios_t)
1717
x = IOStream(name, buf)
1818
if finalize
19-
finalizer(x, close)
19+
finalizer(close, x)
2020
end
2121
return x
2222
end

base/linalg/umfpack.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function lufact{Tv<:UMFVTypes,Ti<:UMFITypes}(S::SparseMatrixCSC{Tv,Ti})
114114
zerobased ? copy(S.colptr) : decrement(S.colptr),
115115
zerobased ? copy(S.rowval) : decrement(S.rowval),
116116
copy(S.nzval))
117-
finalizer(res, umfpack_free_symbolic)
117+
finalizer(umfpack_free_symbolic, res)
118118
umfpack_numeric!(res)
119119
end
120120

@@ -126,7 +126,7 @@ function lufact!{Tv<:UMFVTypes,Ti<:UMFITypes}(S::SparseMatrixCSC{Tv,Ti})
126126
zerobased ? S.colptr : decrement!(S.colptr),
127127
zerobased ? S.rowval : decrement!(S.rowval),
128128
S.nzval)
129-
finalizer(res, umfpack_free_symbolic)
129+
finalizer(umfpack_free_symbolic, res)
130130
umfpack_numeric!(res)
131131
end
132132

base/mmap.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function mmap_array{T,N}(::Type{T}, dims::NTuple{N,Integer}, s::IO, offset::File
110110
pmap, delta = mmap(len, prot, flags, fd(s), offset)
111111
end
112112
A = pointer_to_array(convert(Ptr{T}, uint(pmap)+delta), dims)
113-
finalizer(A,x->munmap(pmap,len+delta))
113+
finalizer(x->munmap(pmap,len+delta), A)
114114
return A
115115
end
116116

@@ -150,7 +150,7 @@ function mmap_array{T,N}(::Type{T}, dims::NTuple{N,Integer}, s::IO, offset::File
150150
error("could not create mapping view: $(FormatMessage())")
151151
end
152152
A = pointer_to_array(convert(Ptr{T}, viewhandle+offset-offset_page), dims)
153-
finalizer(A, x->munmap(viewhandle, mmaphandle))
153+
finalizer(x->munmap(viewhandle, mmaphandle), A)
154154
return A
155155
end
156156

base/mpfr.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type BigFloat <: FloatingPoint
3838
N = get_bigfloat_precision()
3939
z = new(zero(Clong), zero(Cint), zero(Clong), C_NULL)
4040
ccall((:mpfr_init2,:libmpfr), Void, (Ptr{BigFloat}, Clong), &z, N)
41-
finalizer(z, Base.GMP._mpfr_clear_func)
41+
finalizer(Base.GMP._mpfr_clear_func, z)
4242
return z
4343
end
4444
# Not recommended for general use

base/multi.jl

+4-3
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ type RemoteRef
412412
return found
413413
end
414414
client_refs[r] = true
415-
finalizer(r, send_del_client)
415+
finalizer(send_del_client, r)
416416
r
417417
end
418418

@@ -1072,8 +1072,9 @@ function create_worker(bind_addr, port, pubhost, stream, config, manager)
10721072
end
10731073

10741074
# install a finalizer to perform cleanup if necessary
1075-
finalizer(w, (w)->if myid() == 1 manage(w.manager, w.id, w.config, :finalize) end)
1076-
1075+
finalizer(w) do w
1076+
myid() == 1 && manage(w.manager, w.id, w.config, :finalize)
1077+
end
10771078
w
10781079
end
10791080

base/poll.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type FileMonitor
1212
end
1313
this = new(handle,cb,false,Condition())
1414
associate_julia_struct(handle,this)
15-
finalizer(this,uvfinalize)
15+
finalizer(uvfinalize, this)
1616
this
1717
end
1818
FileMonitor(file) = FileMonitor(false,file)
@@ -80,7 +80,7 @@ type PollingFileWatcher <: UVPollingWatcher
8080
end
8181
this = new(handle, file, false, Condition(), cb)
8282
associate_julia_struct(handle,this)
83-
finalizer(this,uvfinalize)
83+
finalizer(uvfinalize, this)
8484
this
8585
end
8686
PollingFileWatcher(file) = PollingFileWatcher(false,file)
@@ -116,7 +116,7 @@ function FDWatcher(fd::RawFD)
116116
end
117117
this = FDWatcher(handle,fd,false,Condition(),false,FDEvent())
118118
associate_julia_struct(handle,this)
119-
finalizer(this,uvfinalize)
119+
finalizer(uvfinalize, this)
120120
this
121121
end
122122
@windows_only function FDWatcher(fd::WindowsRawSocket)
@@ -129,7 +129,7 @@ end
129129
end
130130
this = FDWatcher(handle,fd,false,Condition(),false,FDEvent())
131131
associate_julia_struct(handle,this)
132-
finalizer(this,uvfinalize)
132+
finalizer(uvfinalize, this)
133133
this
134134
end
135135

base/process.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ type Process
187187
err=DevNull
188188
end
189189
this = new(cmd, handle, in, out, err, typemin(Int32), typemin(Int32), false, Condition(), false, Condition())
190-
finalizer(this, uvfinalize)
190+
finalizer(uvfinalize, this)
191191
this
192192
end
193193
end

base/regex.jl

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ type Regex
1919
error("invalid regex options: $options")
2020
end
2121
re = compile(new(pattern, options, C_NULL, C_NULL, Array(Int32, 0)))
22-
finalizer(re,
23-
function(re::Regex)
24-
re.extra != C_NULL && PCRE.free_study(re.extra)
25-
re.regex != C_NULL && PCRE.free(re.regex)
26-
end)
22+
finalizer(re) do re
23+
re.extra != C_NULL && PCRE.free_study(re.extra)
24+
re.regex != C_NULL && PCRE.free(re.regex)
25+
end
2726
re
2827
end
2928
end

base/socket.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ end
264264
function TCPSocket()
265265
this = TCPSocket(c_malloc(_sizeof_uv_tcp))
266266
associate_julia_struct(this.handle,this)
267-
finalizer(this,uvfinalize)
267+
finalizer(uvfinalize, this)
268268
err = ccall(:uv_tcp_init,Cint,(Ptr{Void},Ptr{Void}),
269269
eventloop(),this.handle)
270270
if err != 0
@@ -294,7 +294,7 @@ end
294294
function TCPServer()
295295
this = TCPServer(c_malloc(_sizeof_uv_tcp))
296296
associate_julia_struct(this.handle, this)
297-
finalizer(this,uvfinalize)
297+
finalizer(uvfinalize, this)
298298
err = ccall(:uv_tcp_init,Cint,(Ptr{Void},Ptr{Void}),
299299
eventloop(),this.handle)
300300
if err != 0
@@ -364,7 +364,7 @@ function UDPSocket()
364364
associate_julia_struct(this.handle, this)
365365
err = ccall(:uv_udp_init,Cint,(Ptr{Void},Ptr{Void}),
366366
eventloop(),this.handle)
367-
finalizer(this, uvfinalize)
367+
finalizer(uvfinalize, this)
368368
if err != 0
369369
c_free(this.handle)
370370
this.handle = C_NULL

base/stream.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function Pipe()
121121
try
122122
ret = Pipe(handle)
123123
associate_julia_struct(ret.handle,ret)
124-
finalizer(ret,uvfinalize)
124+
finalizer(uvfinalize, ret)
125125
return init_pipe!(ret;readable=true)
126126
catch
127127
c_free(handle)
@@ -159,7 +159,7 @@ function PipeServer()
159159
try
160160
ret = PipeServer(handle)
161161
associate_julia_struct(ret.handle,ret)
162-
finalizer(ret,uvfinalize)
162+
finalizer(uvfinalize, ret)
163163
return init_pipe!(ret;readable=true)
164164
catch
165165
c_free(handle)
@@ -199,7 +199,7 @@ function TTY(fd::RawFD; readable::Bool = false)
199199
handle = c_malloc(_sizeof_uv_tty)
200200
ret = TTY(handle)
201201
associate_julia_struct(handle,ret)
202-
finalizer(ret,uvfinalize)
202+
finalizer(uvfinalize, ret)
203203
# This needs to go after associate_julia_struct so that there
204204
# is no garbage in the ->data field
205205
uv_error("TTY",ccall(:uv_tty_init,Int32,(Ptr{Void},Ptr{Void},Int32,Int32),eventloop(),handle,fd.fd,readable))
@@ -252,7 +252,7 @@ function init_stdio(handle)
252252
ret.status = StatusOpen
253253
ret.line_buffered = false
254254
associate_julia_struct(ret.handle,ret)
255-
finalizer(ret,uvfinalize)
255+
finalizer(uvfinalize, ret)
256256
return ret
257257
end
258258
end
@@ -458,7 +458,7 @@ type Timer <: AsyncWork
458458
this.handle = C_NULL
459459
error(UVError("uv_make_timer",err))
460460
end
461-
finalizer(this,uvfinalize)
461+
finalizer(uvfinalize, this)
462462
this
463463
end
464464
end
@@ -544,7 +544,7 @@ end
544544
function malloc_julia_pipe(x)
545545
x.handle = c_malloc(_sizeof_uv_named_pipe)
546546
associate_julia_struct(x.handle,x)
547-
finalizer(x,uvfinalize)
547+
finalizer(uvfinalize, x)
548548
end
549549

550550
_link_pipe(read_end::Ptr{Void},write_end::Ptr{Void}) = uv_error("pipe_link",ccall(:uv_pipe_link, Int32, (Ptr{Void}, Ptr{Void}), read_end, write_end))

doc/helpdb.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ Any[
319319
320320
"),
321321

322-
("Base","finalizer","finalizer(x, function)
322+
("Base","finalizer","finalizer(function, x)
323323
324324
Register a function \"f(x)\" to be called when there are no
325325
program-accessible references to \"x\". The behavior of this

doc/stdlib/base.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ All Objects
209209
The optional second argument ``h`` is a hash code to be mixed with the result.
210210
New types should implement the 2-argument form.
211211

212-
.. function:: finalizer(x, function)
212+
.. function:: finalizer(function, x)
213213

214214
Register a function ``f(x)`` to be called when there are no program-accessible references to ``x``. The behavior of this function is unpredictable if ``x`` is of a bits type.
215215

0 commit comments

Comments
 (0)