Repository: Gallopsled/pwntools

Issue:
Pwntools not working on Notebook (Jupyter and Google Colab)
# Pwntools Issue Template

Thanks for contributing to Pwntools!

When reporting an issue, be sure that you are running the latest released version of pwntools (`pip install --upgrade pwntools`).
After executing `pip install --upgrade pwntools` I got:
```
  File "/usr/local/lib/python3.7/dist-packages/pwnlib/term/term.py", line 167
    def goto((r, c)):
             ^
SyntaxError: invalid syntax
```
So I had to use this installation command: `pip3 install git+https://github.com/arthaud/python3-pwntools.git`

After installing **pwntools** with pip and pip3 I tried to use it on a Jupyter Notebook:


```python
from pwn import *
```


    ---------------------------------------------------------------------------

    AttributeError                            Traceback (most recent call last)

    <ipython-input-4-308ef5371a41> in <module>
    ----> 1 from pwn import *
    

    /usr/local/lib/python3.7/dist-packages/pwn/__init__.py in <module>
          1 # Promote useful stuff to toplevel
    ----> 2 from .toplevel import *
          3 
          4 pwnlib.args.initialize()
          5 pwnlib.log.install_default_handler()


    /usr/local/lib/python3.7/dist-packages/pwn/toplevel.py in <module>
         17 import io
         18 
    ---> 19 import pwnlib
         20 from pwnlib import *
         21 from pwnlib.asm import *


    /usr/local/lib/python3.7/dist-packages/pwnlib/__init__.py in <module>
         35 
         36 for module in __all__:
    ---> 37     importlib.import_module('.%s' % module, 'pwnlib')
    

    /usr/lib/python3.7/importlib/__init__.py in import_module(name, package)
        125                 break
        126             level += 1
    --> 127     return _bootstrap._gcd_import(name[level:], package, level)
        128 
        129 


    /usr/local/lib/python3.7/dist-packages/pwnlib/args.py in <module>
          5 import sys
          6 from .context import context
    ----> 7 from . import term
          8 
          9 term_mode = True


    /usr/local/lib/python3.7/dist-packages/pwnlib/term/__init__.py in <module>
          3 from . import key
          4 from . import keymap
    ----> 5 from . import readline
          6 from . import term
          7 from . import termcap


    /usr/local/lib/python3.7/dist-packages/pwnlib/term/readline.py in <module>
          1 from . import keyconsts as kc
          2 from . import keymap as km
    ----> 3 from . import term
          4 from . import text
          5 


    /usr/local/lib/python3.7/dist-packages/pwnlib/term/term.py in <module>
         28 _graphics_mode = False
         29 
    ---> 30 fd = sys.stdout.buffer
         31 
         32 


    AttributeError: 'OutStream' object has no attribute 'buffer'


I tried this also in Google Colab and with different OS like Ubuntu 16 and Debian 10, and getting the same error.
But if I execute `from pwn import *` in a python2, python3, ipython2 and ipython3 terminal I don't get any error.

Thank you.



Discussion:
I don't know if it will help but have you tried running Jupyter Notebook with the environment variable`PWNLIB_NOTERM=true` set?
The fork you installed from is unmaintained. Please install e.g. pwntools 4.0.0beta0, like `pip install 'pwntools>=4.0.0beta0'`

Resolving patch:
diff --git a/pwnlib/term/__init__.py b/pwnlib/term/__init__.py
--- a/pwnlib/term/__init__.py
+++ b/pwnlib/term/__init__.py
@@ -39,7 +39,7 @@ def can_init():
 
     # Check fancy REPLs
     mods = sys.modules.keys()
-    for repl in ['IPython', 'bpython', 'dreampielib']:
+    for repl in ['IPython', 'bpython', 'dreampielib', 'jupyter_client._version']:
         if repl in mods:
             return False
 
diff --git a/pwnlib/term/unix_termcap.py b/pwnlib/term/unix_termcap.py
--- a/pwnlib/term/unix_termcap.py
+++ b/pwnlib/term/unix_termcap.py
@@ -45,6 +45,14 @@ def get(cap, *args, **kwargs):
 def init():
     global cache
 
+    # Detect running under Jupyter
+    try:
+        if get_ipython().__class__.__name__ == 'ZMQInteractiveShell':
+            os.environ['PWNLIB_NOTERM'] = '1'
+            os.environ['JUPYTER_DETECTED'] ='yes'
+    except NameError:
+        pass
+
     if 'PWNLIB_NOTERM' not in os.environ:
         # Fix for BPython
         try:


Test patch: