Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fd(::IOStream) returning an integer #55080

Merged
merged 8 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Standard library changes
------------------------

* `gcdx(0, 0)` now returns `(0, 0, 0)` instead of `(0, 1, 0)` ([#40989]).
* `fd` returns a `RawFD` instead of an `Int` ([#55080]).

#### StyledStrings

Expand Down
11 changes: 6 additions & 5 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ macro _lock_ios(s, expr)
end

"""
fd(stream)
fd(stream) -> RawFD

Return the file descriptor backing the stream or file. Note that this function only applies
to synchronous `File`'s and `IOStream`'s not to any of the asynchronous streams.

File descriptors should typically be represented as [`RawFD`](@ref) objects, rather
than as `Int`s, to ensure that they are properly interpreted by Julia functions.
`RawFD` objects can be passed directly to other languages via the `ccall` interface.

Note that `RawFD` objects can be passed directly to other languages via the `ccall` interface.
!!! compat "Julia 1.12"
Prior to 1.12, this function returned an `Int` instead of a `RawFD`. You may use
`RawFD(fd(x))` to produce a `RawFD` in all Julia versions.
"""
fd(s::IOStream) = Int(ccall(:jl_ios_fd, Clong, (Ptr{Cvoid},), s.ios))
fd(s::IOStream) = RawFD(ccall(:jl_ios_fd, Clong, (Ptr{Cvoid},), s.ios))

stat(s::IOStream) = stat(fd(s))

Expand Down
4 changes: 4 additions & 0 deletions test/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@ end
@test all(T -> T <: Union{UInt, Int}, Base.return_types(unsafe_write, (IO, Ptr{UInt8}, UInt)))
@test all(T -> T === Bool, Base.return_types(eof, (IO,)))
end

@testset "fd" begin
@test open(fd, tempname(), "w") isa RawFD
end