@@ -25,54 +25,10 @@ PyJuliaAnyValue_Type() = begin
2525 as_sequence = (contains = pyjlany_contains,),
2626 methods = [
2727 (name = " __dir__" , flags = Py_METH_NOARGS, meth = pyjlany_dir),
28- (
29- name = " _repr_html_" ,
30- flags = Py_METH_NOARGS,
31- meth = pyjlany_repr_mime (MIME (" text/html" )),
32- ),
33- (
34- name = " _repr_markdown_" ,
35- flags = Py_METH_NOARGS,
36- meth = pyjlany_repr_mime (MIME (" text/markdown" )),
37- ),
38- (
39- name = " _repr_json_" ,
40- flags = Py_METH_NOARGS,
41- meth = pyjlany_repr_mime (MIME (" text/json" )),
42- ),
43- (
44- name = " _repr_javascript_" ,
45- flags = Py_METH_NOARGS,
46- meth = pyjlany_repr_mime (MIME (" application/javascript" )),
47- ),
48- (
49- name = " _repr_pdf_" ,
50- flags = Py_METH_NOARGS,
51- meth = pyjlany_repr_mime (MIME (" application/pdf" )),
52- ),
53- (
54- name = " _repr_jpeg_" ,
55- flags = Py_METH_NOARGS,
56- meth = pyjlany_repr_mime (MIME (" image/jpeg" )),
57- ),
58- (
59- name = " _repr_png_" ,
60- flags = Py_METH_NOARGS,
61- meth = pyjlany_repr_mime (MIME (" image/png" )),
62- ),
63- (
64- name = " _repr_svg_" ,
65- flags = Py_METH_NOARGS,
66- meth = pyjlany_repr_mime (MIME (" image/svg+xml" )),
67- ),
68- (
69- name = " _repr_latex_" ,
70- flags = Py_METH_NOARGS,
71- meth = pyjlany_repr_mime (MIME (" text/latex" )),
72- ),
73- (name = " __jl_raw" , flags = Py_METH_NOARGS, meth = pyjlany_toraw),
74- (name = " __jl_display" , flags = Py_METH_NOARGS, meth = pyjlany_display),
75- (name = " __jl_help" , flags = Py_METH_NOARGS, meth = pyjlany_help),
28+ (name = " _repr_mimebundle_" , flags = Py_METH_VARARGS | Py_METH_KEYWORDS, meth = pyjlany_repr_mimebundle),
29+ (name = " _jl_raw" , flags = Py_METH_NOARGS, meth = pyjlany_toraw),
30+ (name = " _jl_display" , flags = Py_METH_NOARGS, meth = pyjlany_display),
31+ (name = " _jl_help" , flags = Py_METH_NOARGS, meth = pyjlany_help),
7632 ],
7733 getset = [(name = " __name__" , get = pyjlany_name)],
7834 ),
@@ -387,28 +343,68 @@ pyjlany_richcompare(xo::PyPtr, yo::PyPtr, op::Cint) = begin
387343 end
388344end
389345
390- struct pyjlany_repr_mime{M<: MIME }
391- mime:: M
392- end
393- (f:: pyjlany_repr_mime{M} )(xo:: PyPtr , :: PyPtr ) where {M} = begin
346+ const ALL_MIMES = [
347+ " text/plain" ,
348+ " text/html" ,
349+ " text/markdown" ,
350+ " text/json" ,
351+ " text/latex" ,
352+ " text/xml" ,
353+ " text/csv" ,
354+ " application/javascript" ,
355+ " application/pdf" ,
356+ " application/ogg" ,
357+ " image/jpeg" ,
358+ " image/png" ,
359+ " image/svg+xml" ,
360+ " image/gif" ,
361+ " image/webp" ,
362+ " image/tiff" ,
363+ " image/bmp" ,
364+ " audio/aac" ,
365+ " audio/mpeg" ,
366+ " audio/ogg" ,
367+ " audio/opus" ,
368+ " audio/webm" ,
369+ " audio/wav" ,
370+ " audio/midi" ,
371+ " audio/x-midi" ,
372+ " video/mpeg" ,
373+ " video/ogg" ,
374+ " video/webm" ,
375+ ]
376+
377+ pyjlany_repr_mimebundle (xo:: PyPtr , args:: PyPtr , kwargs:: PyPtr ) = begin
394378 x = PyJuliaValue_GetValue (xo)
395- io = IOBuffer ()
396- try
397- show (io, f. mime, x)
398- catch err
399- if err isa MethodError && err. f === show && err. args === (io, f. mime, x)
400- return PyNone_New ()
401- else
402- PyErr_SetJuliaError (err)
403- return PyNULL
379+ ism1 (PyArg_CheckNumArgsEq (" _repr_mimebundle_" , args, 0 )) && return PyNULL
380+ ism1 (PyArg_GetArg (Union{Set{String},Nothing}, " _repr_mimebundle_" , kwargs, " include" , nothing )) && return PyNULL
381+ inc = takeresult (Union{Set{String},Nothing})
382+ ism1 (PyArg_GetArg (Union{Set{String},Nothing}, " _repr_mimebundle_" , kwargs, " exclude" , nothing )) && return PyNULL
383+ exc = takeresult (Union{Set{String},Nothing})
384+ # decide which mimes to include
385+ mimes = inc === nothing ? ALL_MIMES : push! (inc, " text/plain" )
386+ exc === nothing || setdiff! (mimes, exc)
387+ # make the bundle
388+ bundle = PyDict_New ()
389+ isnull (bundle) && return PyNULL
390+ for m in mimes
391+ try
392+ io = IOBuffer ()
393+ show (io, MIME (m), x)
394+ v = take! (io)
395+ mo = PyUnicode_From (m)
396+ isnull (mo) && (Py_DecRef (bundle); return PyNULL)
397+ vo = istextmime (m) ? PyUnicode_From (v) : PyBytes_From (v)
398+ isnull (vo) && (Py_DecRef (mo); Py_DecRef (bundle); return PyNULL)
399+ err = PyDict_SetItem (bundle, mo, vo)
400+ Py_DecRef (mo)
401+ Py_DecRef (vo)
402+ ism1 (err) && (Py_DecRef (bundle); return PyNULL)
403+ catch err
404+ # silently skip anything that didn't work
404405 end
405406 end
406- data = take! (io)
407- if istextmime (f. mime)
408- PyUnicode_From (data)
409- else
410- PyBytes_From (data)
411- end
407+ bundle
412408end
413409
414410pyjlany_name (xo:: PyPtr , :: Ptr{Cvoid} ) =
0 commit comments