Skip to content

Commit 15a345b

Browse files
stevengjJeffBezanson
authored andcommitted
RFC: move stringmime to Base64, rename reprmime -> repr (#25990)
* stringmime(text/plain, x) can be replaced with repr(text/plain, x)
1 parent aed8814 commit 15a345b

File tree

24 files changed

+144
-99
lines changed

24 files changed

+144
-99
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ Language changes
183183
backslashes and the end of the literal while 2n+1 backslashes followed by a quote encodes n
184184
backslashes followed by a quote character ([#22926]).
185185

186+
* `reprmime(mime, x)` has been renamed to `repr(mime, x)`, and along with `repr(x)`
187+
and `sprint` it now accepts an optional `context` keyword for `IOContext` attributes.
188+
`stringmime` has been moved to the Base64 stdlib package ([#25990]).
189+
186190
* The syntax `(x...)` for constructing a tuple is deprecated; use `(x...,)` instead ([#24452]).
187191

188192
* Non-parenthesized interpolated variables in strings, e.g. `"$x"`, must be followed

base/deprecated.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,8 @@ end
13651365
@deprecate IOBuffer(read::Bool, write::Bool) IOBuffer(read=read, write=write)
13661366
@deprecate IOBuffer(maxsize::Integer) IOBuffer(read=true, write=true, maxsize=maxsize)
13671367

1368+
@deprecate reprmime(mime, x) repr(mime, x)
1369+
13681370
# PR #23332
13691371
@deprecate ^(x, p::Integer) Base.power_by_squaring(x,p)
13701372

base/exports.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,6 @@ export
875875
istextmime,
876876
MIME,
877877
@MIME_str,
878-
reprmime,
879-
stringmime,
880878
mimewritable,
881879
popdisplay,
882880
pushdisplay,

base/loading.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ function create_expr_cache(input::String, output::String, concrete_deps::typeof(
11161116
begin
11171117
import Pkg
11181118
empty!(Base.LOAD_PATH)
1119-
append!(Base.LOAD_PATH, $(repr(LOAD_PATH, :module => nothing)))
1119+
append!(Base.LOAD_PATH, $(repr(LOAD_PATH, context=:module=>nothing)))
11201120
empty!(Base.DEPOT_PATH)
11211121
append!(Base.DEPOT_PATH, $(repr(DEPOT_PATH)))
11221122
empty!(Base.LOAD_CACHE_PATH)

base/multimedia.jl

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Multimedia
44

55
export AbstractDisplay, display, pushdisplay, popdisplay, displayable, redisplay,
6-
MIME, @MIME_str, reprmime, stringmime, istextmime,
6+
MIME, @MIME_str, istextmime,
77
mimewritable, TextDisplay
88

99
###########################################################################
@@ -15,8 +15,7 @@ export AbstractDisplay, display, pushdisplay, popdisplay, displayable, redisplay
1515
# struct MIME{mime} end
1616
# macro MIME_str(s)
1717
import Base: MIME, @MIME_str
18-
import Base64
19-
import Base: show, print, string, convert
18+
import Base: show, print, string, convert, repr
2019
MIME(s) = MIME{Symbol(s)}()
2120
show(io::IO, ::MIME{mime}) where {mime} = print(io, "MIME type ", string(mime))
2221
print(io::IO, ::MIME{mime}) where {mime} = print(io, mime)
@@ -79,59 +78,60 @@ show(stream, mime, x)
7978
show(io::IO, m::AbstractString, x) = show(io, MIME(m), x)
8079
mimewritable(m::AbstractString, x) = mimewritable(MIME(m), x)
8180

82-
verbose_show(io, m, x) = show(IOContext(io, :limit => false), m, x)
83-
8481
"""
85-
reprmime(mime, x)
82+
repr(mime, x; context=nothing)
8683
8784
Returns an `AbstractString` or `Vector{UInt8}` containing the representation of
88-
`x` in the requested `mime` type, as written by [`show`](@ref) (throwing a
85+
`x` in the requested `mime` type, as written by [`show(io, mime, x)`](@ref) (throwing a
8986
[`MethodError`](@ref) if no appropriate `show` is available). An `AbstractString` is
9087
returned for MIME types with textual representations (such as `"text/html"` or
9188
`"application/postscript"`), whereas binary data is returned as
9289
`Vector{UInt8}`. (The function `istextmime(mime)` returns whether or not Julia
9390
treats a given `mime` type as text.)
9491
92+
The optional keyword argument `context` can be set to `:key=>value` pair
93+
or an `IO` or [`IOContext`](@ref) object whose attributes are used for the I/O
94+
stream passed to `show`.
95+
9596
As a special case, if `x` is an `AbstractString` (for textual MIME types) or a
96-
`Vector{UInt8}` (for binary MIME types), the `reprmime` function assumes that
97+
`Vector{UInt8}` (for binary MIME types), the `repr` function assumes that
9798
`x` is already in the requested `mime` format and simply returns `x`. This
9899
special case does not apply to the `"text/plain"` MIME type. This is useful so
99100
that raw data can be passed to `display(m::MIME, x)`.
100101
102+
In particular, `repr("text/plain", x)` is typically a "pretty-printed" version
103+
of `x` designed for human consumption. See also [`repr(x)`](@ref) to instead
104+
return a string corresponding to [`show(x)`](@ref) that may be closer to how
105+
the value of `x` would be entered in Julia.
106+
101107
# Examples
102108
```jldoctest
103109
julia> A = [1 2; 3 4];
104110
105-
julia> reprmime("text/plain", A)
111+
julia> repr("text/plain", A)
106112
"2×2 Array{Int64,2}:\\n 1 2\\n 3 4"
107113
```
108114
"""
109-
reprmime(m::MIME, x) = istextmime(m) ? _textreprmime(m, x) : _binreprmime(m, x)
115+
repr(m::MIME, x; context=nothing) = istextmime(m) ? _textrepr(m, x, context) : _binrepr(m, x, context)
116+
repr(m::AbstractString, x; context=nothing) = repr(MIME(m), x; context=context)
110117

111118
# strings are shown escaped for text/plain
112-
_textreprmime(m::MIME, x) = sprint(verbose_show, m, x)
113-
_textreprmime(::MIME, x::AbstractString) = x
114-
_textreprmime(m::MIME"text/plain", x::AbstractString) =
115-
sprint(verbose_show, m, x)
119+
_textrepr(m::MIME, x, context) = String(__binrepr(m, x, context))
120+
_textrepr(::MIME, x::AbstractString, context) = x
121+
_textrepr(m::MIME"text/plain", x::AbstractString, context) = String(__binrepr(m, x, context))
116122

117-
function _binreprmime(m::MIME, x)
123+
124+
function __binrepr(m::MIME, x, context)
118125
s = IOBuffer()
119-
verbose_show(s, m, x)
126+
if context === nothing
127+
show(s, m, x)
128+
else
129+
show(IOContext(s, context), m, x)
130+
end
120131
take!(s)
121132
end
122-
_binreprmime(m::MIME, x::Vector{UInt8}) = x
123-
124-
"""
125-
stringmime(mime, x)
126-
127-
Returns an `AbstractString` containing the representation of `x` in the
128-
requested `mime` type. This is similar to [`reprmime`](@ref) except
129-
that binary data is base64-encoded as an ASCII string.
130-
"""
131-
stringmime(m::MIME, x) = istextmime(m) ? reprmime(m, x) : _binstringmime(m, x)
132-
133-
_binstringmime(m::MIME, x) = Base64.base64encode(verbose_show, m, x)
134-
_binstringmime(m::MIME, x::Vector{UInt8}) = Base64.base64encode(write, x)
133+
_binrepr(m::MIME, x, context) = __binrepr(m, x, context)
134+
_binrepr(m::MIME, x::Vector{UInt8}, context) = x
135135

136136
"""
137137
istextmime(m::MIME)
@@ -149,11 +149,7 @@ false
149149
```
150150
"""
151151
istextmime(m::MIME) = startswith(string(m), "text/")
152-
153-
# it is convenient to accept strings instead of ::MIME
154152
istextmime(m::AbstractString) = istextmime(MIME(m))
155-
reprmime(m::AbstractString, x) = reprmime(MIME(m), x)
156-
stringmime(m::AbstractString, x) = stringmime(MIME(m), x)
157153

158154
for mime in ["application/atom+xml", "application/ecmascript",
159155
"application/javascript", "application/julia",
@@ -169,7 +165,7 @@ end
169165
# We have an abstract AbstractDisplay class that can be subclassed in order to
170166
# define new rich-display output devices. A typical subclass should
171167
# overload display(d::AbstractDisplay, m::MIME, x) for supported MIME types m,
172-
# (typically using reprmime or stringmime to get the MIME
168+
# (typically using show, repr, ..., to get the MIME
173169
# representation of x) and should also overload display(d::AbstractDisplay, x)
174170
# to display x in whatever MIME type is preferred by the AbstractDisplay and
175171
# is writable by x. display(..., x) should throw a MethodError if x

base/strings/io.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,16 @@ println(io::IO, xs...) = print(io, xs..., '\n')
6969
## conversion of general objects to strings ##
7070

7171
"""
72-
sprint(f::Function, args...)
72+
sprint(f::Function, args...; context=nothing, sizehint=0)
7373
7474
Call the given function with an I/O stream and the supplied extra arguments.
7575
Everything written to this I/O stream is returned as a string.
7676
77+
The optional keyword argument `context` can be set to `:key=>value` pair
78+
or an `IO` or [`IOContext`](@ref) object whose attributes are used for the I/O
79+
stream passed to `f`. The optional `sizehint` is a suggersted (in bytes)
80+
to allocate for the buffer used to write the string.
81+
7782
# Examples
7883
```jldoctest
7984
julia> sprint(showcompact, 66.66666)
@@ -147,12 +152,17 @@ function print_quoted_literal(io, s::AbstractString)
147152
end
148153

149154
"""
150-
repr(x)
151-
repr(x, context::Pair{Symbol,<:Any}...)
155+
repr(x; context=nothing)
152156
153157
Create a string from any value using the [`show`](@ref) function.
154-
If context pairs are given, the IO buffer used to capture `show` output
155-
is wrapped in an `IOContext` object with those context pairs.
158+
159+
The optional keyword argument `context` can be set to an `IO` or [`IOContext`](@ref)
160+
object whose attributes are used for the I/O stream passed to `show`.
161+
162+
Note that `repr(x)` is usually similar to how the value of `x` would
163+
be entered in Julia. See also [`repr("text/plain", x)`](@ref) to instead
164+
return a "pretty-printed" version of `x` designed more for human consumption,
165+
equivalent to the REPL display of `x`.
156166
157167
# Examples
158168
```jldoctest
@@ -164,17 +174,7 @@ julia> repr(zeros(3))
164174
165175
```
166176
"""
167-
function repr(x)
168-
s = IOBuffer()
169-
show(s, x)
170-
String(take!(s))
171-
end
172-
173-
function repr(x, context::Pair{Symbol}...)
174-
s = IOBuffer()
175-
show(IOContext(s, context...), x)
176-
String(take!(s))
177-
end
177+
repr(x; context=nothing) = sprint(show, x; context=context)
178178

179179
# IOBuffer views of a (byte)string:
180180

base/sysimg.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,6 @@ let BINDIR = ccall(:jl_get_julia_bindir, Any, ())
448448
init_load_path(BINDIR)
449449
end
450450

451-
INCLUDE_STATE = 3 # include = include_relative
452-
453-
import Base64
454-
455-
INCLUDE_STATE = 2
456-
457451
include("asyncmap.jl")
458452

459453
include("multimedia.jl")
@@ -576,6 +570,7 @@ Base.require(Base, :UUIDs)
576570
@deprecate_stdlib base64decode Base64 true
577571
@deprecate_stdlib Base64EncodePipe Base64 true
578572
@deprecate_stdlib Base64DecodePipe Base64 true
573+
@deprecate_stdlib stringmime Base64 true
579574

580575
@deprecate_stdlib poll_fd FileWatching true
581576
@deprecate_stdlib poll_file FileWatching true

doc/src/base/io-network.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ Base.Multimedia.redisplay
9494
Base.Multimedia.displayable
9595
Base.show(::Any, ::Any, ::Any)
9696
Base.Multimedia.mimewritable
97-
Base.Multimedia.reprmime
98-
Base.Multimedia.stringmime
97+
Base.repr(::Any, ::Any)
9998
```
10099

101100
As mentioned above, one can also define new display backends. For example, a module that can display
@@ -105,8 +104,9 @@ types with PNG representations will automatically display the image using the mo
105104
In order to define a new display backend, one should first create a subtype `D` of the abstract
106105
class `AbstractDisplay`. Then, for each MIME type (`mime` string) that can be displayed on `D`, one should
107106
define a function `display(d::D, ::MIME"mime", x) = ...` that displays `x` as that MIME type,
108-
usually by calling [`reprmime(mime, x)`](@ref). A `MethodError` should be thrown if `x` cannot be displayed
109-
as that MIME type; this is automatic if one calls [`reprmime`](@ref). Finally, one should define a function
107+
usually by calling [`show(io, mime, x)`](@ref) or [`repr(io, mime, x)`](@ref).
108+
A `MethodError` should be thrown if `x` cannot be displayed
109+
as that MIME type; this is automatic if one calls `show` or `repr`. Finally, one should define a function
110110
`display(d::D, x)` that queries [`mimewritable(mime, x)`](@ref) for the `mime` types supported by `D`
111111
and displays the "best" one; a `MethodError` should be thrown if no supported MIME types are found
112112
for `x`. Similarly, some subtypes may wish to override [`redisplay(d::D, ...)`](@ref Base.Multimedia.redisplay). (Again, one should

doc/src/base/strings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Base.:^(::AbstractString, ::Integer)
88
Base.string
99
Base.repeat(::AbstractString, ::Integer)
1010
Base.repeat(::Char, ::Integer)
11-
Base.repr
11+
Base.repr(::Any)
1212
Core.String(::AbstractString)
1313
Base.SubString
1414
Base.transcode

stdlib/Base64/docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ Base64.Base64EncodePipe
55
Base64.base64encode
66
Base64.Base64DecodePipe
77
Base64.base64decode
8+
Base64.stringmime
89
```

0 commit comments

Comments
 (0)