Document how to use drgn with IPython/Jupyter#200
Document how to use drgn with IPython/Jupyter#200shunghsiyu wants to merge 3 commits intoosandov:mainfrom
Conversation
|
|
||
| def test__repr_pretty(self): | ||
| obj = Object(self.prog, "int", value=0) | ||
| pretty_printer_mock = unittest.mock.Mock() |
There was a problem hiding this comment.
I hesitate a bit about using the Mock class from unittest as I don't see Mock being used elsewhere, not sure if there's a better alternative.
There was a problem hiding this comment.
I think Mock makes sense for this test.
21d638f to
c075cc5
Compare
|
Re-push to correct the method name to Still need to implement that same thing for |
osandov
left a comment
There was a problem hiding this comment.
Thanks for doing this! It looks straightforward, but I haven't gotten to test it yet.
| if (cycle) | ||
| return PyObject_CallMethod(p, "text", "s", "..."); | ||
|
|
||
| str_obj = DrgnObject_str(self); |
There was a problem hiding this comment.
If you use PyObject_Str() instead of DrgnObject_str (and make self a PyObject *), then you can reuse the same implementation for all of the types that need it.
There was a problem hiding this comment.
To elaborate on this, you could name this function something more generic (repr_pretty_from_str() or something like that?) put it somewhere common (maybe libdrgn/python/util.c), and use it in the method table for the other classes.
|
|
||
| def test__repr_pretty(self): | ||
| obj = Object(self.prog, "int", value=0) | ||
| pretty_printer_mock = unittest.mock.Mock() |
There was a problem hiding this comment.
I think Mock makes sense for this test.
Add pretty printing support in Jupyter notebook for Object, Type, StackFrame, and StackTrace; it will print out their representation in programming language syntax with str(), similar to what's being done in interactive mode. Link: https://ipython.readthedocs.io/en/stable/api/generated/IPython.lib.pretty.html#extending Signed-off-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
Since _repr_pretty() uses output of str(), and the latter is already heavily tested in tests/test_language_c.py, we can simply test whether p.text is call made instead of duplicating all the test cases. Signed-off-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
c075cc5 to
8019b75
Compare
|
Thanks for the suggestion and especially the elaboration of it, really useful since this is the first time I'm trying to work with Python C-extension. Here's the changes I've made:
btw the vmtest kernel testing facility drgn has is awesome! |
7eb9c88 to
596f5fd
Compare
Briefly mention the synergy between drgn and Jupyter, and showcase an example. Also, mention the web-based mode of Jupyter (aka Jupyter notebook), and some of its feature that drgn users might be interested in. Signed-off-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
596f5fd to
e54f794
Compare
|
Sorry for the force pushes, I regret not testing it somewhere else first. (Every time I thought I finally got reStructuredText right, I was proven wrong...) Anyway added another commit to documentation about how drgn can be used with Jupyter, as well as what feature it provides. It's a bit of Jupyter evangelism, admittedly, which I'm having problem toning down 😅. Feel free to edit it as you see fit. |
|
Thanks for this! I pushed the first two commits for |
|
Thanks! Regarding the documentation changes, I'm also working on something that might better showcase Jupyter & drgn. I'll post it here when it's ready, and then perhaps we can have a more concrete discussion. |
The PR implements #199 to support pretty printing in Jupyter notebook by adding the
_repr_pretty_()method to the base classes, usingstr()for the heavy lifting.This save the user from having to call
print()inside Jupyter notebook, similar to what's being done in interactive mode.An alternative to implementing
_repr_pretty_()is to hack IPython's displayhook to do our bidding, but that requires importing IPython and working around import error, so probably best be avoided.