Repository: streamlit/streamlit

Issue:
Error during exception handling in st.write - TypeError: exception() takes 3 positional arguments but 4 were given
# Summary

I'm trying to display a dataframe with `st.write` and it fails with a streamlit error while trying to handle an error from rendering the object. 

```python
TypeError: exception() takes 3 positional arguments but 4 were given
Traceback:
File "/home/bs3639/.conda/envs/bosch/lib/python3.6/site-packages/streamlit/ScriptRunner.py", line 322, in _run_script
    exec(code, module.__dict__)
File "/scratch/bs3639/bosch-urban-sound/boschurbansnd/app.py", line 256, in <module>
    data_summary(dflabels, dffiles)
File "/scratch/bs3639/bosch-urban-sound/boschurbansnd/app.py", line 130, in data_summary
    st.write(dffiles.reset_index(drop=True).head())
File "/home/bs3639/.conda/envs/bosch/lib/python3.6/site-packages/streamlit/__init__.py", line 411, in write
    exception(exc, exc_tb)  # noqa: F821
File "/home/bs3639/.conda/envs/bosch/lib/python3.6/site-packages/streamlit/DeltaGenerator.py", line 122, in wrapped_method
    return dg._enqueue_new_element_delta(marshall_element, delta_type, last_index)
File "/home/bs3639/.conda/envs/bosch/lib/python3.6/site-packages/streamlit/DeltaGenerator.py", line 367, in _enqueue_new_element_delta
    rv = marshall_element(msg.delta.new_element)
File "/home/bs3639/.conda/envs/bosch/lib/python3.6/site-packages/streamlit/DeltaGenerator.py", line 120, in marshall_element
    return method(dg, element, *args, **kwargs)
```

# Steps to reproduce

It happens when you raise an exception during type conversion in `st.write`.

Here's the simplest example I could think of that throws the same error.
```python
import streamlit as st

class breakstuff:
    def __str__(self):
        raise ValueError

st.write(breakstuff())
```

## Expected behavior:

It should display the `ValueError`

## Actual behavior:

Instead it throws a streamlit internal `TypeError` error (see above traceback).

It means that I can't actually debug the exception that is throwing that code.

# Debug info

- Streamlit version: `Streamlit, version 0.60.0` (get it with `$ streamlit version`)
- Python version: `Python 3.6.10 :: Anaconda, Inc.` (get it with `$ python --version`)
- Using Conda? PipEnv? PyEnv? Pex?  -  Conda
- OS version: 
- Browser version:

# Additional information

From a quick spin following the traceback, I believe I can see the issue.

The traceback says that the error originates here:
https://github.com/streamlit/streamlit/blob/a9be6773eceba186c4094908c5403575810d760a/lib/streamlit/DeltaGenerator.py#L120

The wrapper provides 2 args and it says it's receiving 4 while expecting 3, so that means that the wrapped method is being called with 2 instead of an expected 1 argument.

Earlier in the traceback, it says that it's being raised in `st.write` by `exception` (notice it's being called with 2 arguments):
https://github.com/streamlit/streamlit/blob/a9be6773eceba186c4094908c5403575810d760a/lib/streamlit/__init__.py#L409-L411

Looking at its definition, `exception` is wrapped with `_with_element` and takes 3 arguments, 2 of which are provided by the wrapper.
https://github.com/streamlit/streamlit/blob/a9be6773eceba186c4094908c5403575810d760a/lib/streamlit/DeltaGenerator.py#L788-L789


tl;dr - you probably shouldn't be passing the traceback into the exception function (or perhaps you meant to and you haven't finished implementing it on the other side.) Either way, it's broken rn. 

# Possible Solution?
From a cursory glance, it looks like you should just simplify to this and this issue will go away:
```python
# change here: https://github.com/streamlit/streamlit/blob/a9be6773eceba186c4094908c5403575810d760a/lib/streamlit/__init__.py#L409-L411
try:
    ...
except Exception as exc:
    exception(exc)
```



Discussion:
I have the exact same problem. I think that the dataframe is too big. By just not printing it out the problem went await

Resolving patch:
diff --git a/lib/streamlit/__init__.py b/lib/streamlit/__init__.py
--- a/lib/streamlit/__init__.py
+++ b/lib/streamlit/__init__.py
@@ -407,9 +407,8 @@ def flush_buffer():
 
         flush_buffer()
 
-    except Exception:
-        _, exc, exc_tb = _sys.exc_info()
-        exception(exc, exc_tb)  # noqa: F821
+    except Exception as exc:
+        exception(exc)
 
 
 def experimental_show(*args):


Test patch:
diff --git a/lib/tests/streamlit/write_test.py b/lib/tests/streamlit/write_test.py
--- a/lib/tests/streamlit/write_test.py
+++ b/lib/tests/streamlit/write_test.py
@@ -184,7 +184,12 @@ def __str__(self):
 
     def test_exception(self):
         """Test st.write that raises an exception."""
-        with patch("streamlit.markdown") as m, patch("streamlit.exception") as e:
+        # We patch streamlit.exception to observe it, but we also make sure
+        # it's still called (via side_effect). This ensures that it's called
+        # with the proper arguments.
+        with patch("streamlit.markdown") as m, patch(
+            "streamlit.exception", side_effect=st.exception
+        ) as e:
             m.side_effect = Exception("some exception")
             st.write("some text")