Skip to content

Commit 72b7ade

Browse files
committed
Fix iruby helpers
- rewrite against the changes by PyCall version 1.0 - matcher should return true when it is matched - formatters' priorities should be 1 because they must be checked before IRuby's default formatters
1 parent 36826cd commit 72b7ade

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

lib/pycall.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,5 @@ def with(ctx)
8989
end
9090
end
9191
end
92+
93+
require 'pycall/iruby_helper' if defined? IRuby

lib/pycall/iruby_helper.rb

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,34 @@ module PyCall
55
module IRubyHelper
66
private
77

8-
def check_pyobject_respond_to_format_method(obj, method_name)
9-
return false unless obj.kind_of? PyObject
10-
return false unless PyCall.hasattr?(obj, method_name)
11-
PyCall.getattr(obj, method_name).kind_of? PyCall::LibPython.PyMethod_Type
8+
def check_python_object_respond_to_format_method(obj, method_name)
9+
return false unless obj.kind_of? PyObjectWrapper
10+
return false unless obj.respond_to? method_name
11+
true
1212
end
1313

14-
def register_pyobject_formatter(format_name, mime, priority_value=0)
14+
def register_python_object_formatter(format_name, mime, priority_value=0)
1515
method_name = :"_repr_#{format_name}_"
1616
match do |obj|
17-
check_pyobject_respond_to_format_method(obj, method_name)
17+
check_python_object_respond_to_format_method(obj, method_name)
1818
end
1919
priority priority_value
20-
format mime do |obj|
21-
PyCall.getattr(obj, method_name).()
22-
end
20+
format mime, &method_name
2321
end
2422
end
2523
end
2624

2725
::IRuby::Display::Registry.module_eval do
2826
extend PyCall::IRubyHelper
2927

30-
register_pyobject_formatter :html, 'text/html'
31-
register_pyobject_formatter :markdown, 'text/markdown'
32-
register_pyobject_formatter :svg, 'image/svg+xml'
33-
register_pyobject_formatter :png, 'image/png'
34-
register_pyobject_formatter :jpeg, 'image/jpeg'
35-
register_pyobject_formatter :latex, 'text/latex'
36-
register_pyobject_formatter :json, 'application/json'
37-
register_pyobject_formatter :javascript, 'application/javascript'
38-
register_pyobject_formatter :pdf, 'application/pdf'
39-
register_pyobject_formatter :pretty, 'text/plain', -1000
28+
register_python_object_formatter :html, 'text/html', 1
29+
register_python_object_formatter :markdown, 'text/markdown', 1
30+
register_python_object_formatter :svg, 'image/svg+xml', 1
31+
register_python_object_formatter :png, 'image/png', 1
32+
register_python_object_formatter :jpeg, 'image/jpeg', 1
33+
register_python_object_formatter :latex, 'text/latex', 1
34+
register_python_object_formatter :json, 'application/json', 1
35+
register_python_object_formatter :javascript, 'application/javascript', 1
36+
register_python_object_formatter :pdf, 'application/pdf', 1
37+
register_python_object_formatter :pretty, 'text/plain', -1000
4038
end

0 commit comments

Comments
 (0)