Skip to content

Removed raise_from_traceback #29174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ def set_function_name(f, name, cls):
return f


def raise_with_traceback(exc, traceback=Ellipsis):
"""
Raise exception with existing traceback.
If traceback is not passed, uses sys.exc_info() to get traceback.
"""
if traceback == Ellipsis:
_, _, traceback = sys.exc_info()
raise exc.with_traceback(traceback)


# https://github.com/pandas-dev/pandas/pull/9123
def is_platform_little_endian():
""" am I little endian """
Expand Down
9 changes: 4 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from pandas._config import get_option

from pandas._libs import algos as libalgos, lib
from pandas.compat import PY36, raise_with_traceback
from pandas.compat import PY36
from pandas.compat.numpy import function as nv
from pandas.util._decorators import (
Appender,
Expand Down Expand Up @@ -485,7 +485,7 @@ def __init__(
"DataFrame constructor called with "
"incompatible data and dtype: {e}".format(e=e)
)
raise_with_traceback(exc)
raise exc from e

if arr.ndim == 0 and index is not None and columns is not None:
values = cast_scalar_to_array(
Expand Down Expand Up @@ -7816,11 +7816,10 @@ def f(x):
elif filter_type == "bool":
data = self._get_bool_data()
else: # pragma: no cover
e = NotImplementedError(
raise NotImplementedError(
"Handling exception with filter_type {f} not"
"implemented.".format(f=filter_type)
)
raise_with_traceback(e)
) from e
with np.errstate(all="ignore"):
result = f(data.values)
labels = data._get_agg_axis(axis)
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pandas._libs import lib
import pandas.compat as compat
from pandas.compat import PY36, raise_with_traceback
from pandas.compat import PY36

from pandas.core.dtypes.cast import (
construct_1d_arraylike_from_scalar,
Expand Down Expand Up @@ -164,11 +164,10 @@ def init_ndarray(values, index, columns, dtype=None, copy=False):
try:
values = values.astype(dtype)
except Exception as orig:
e = ValueError(
raise ValueError(
"failed to cast to '{dtype}' (Exception "
"was: {orig})".format(dtype=dtype, orig=orig)
)
raise_with_traceback(e)
) from orig

index, columns = _get_axes(*values.shape, index=index, columns=columns)
values = values.T
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import os
import re

from pandas.compat import raise_with_traceback
from pandas.compat._optional import import_optional_dependency
from pandas.errors import AbstractMethodError, EmptyDataError

Expand Down Expand Up @@ -889,7 +888,6 @@ def _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs):
flavor = _validate_flavor(flavor)
compiled_match = re.compile(match) # you can pass a compiled regex here

# hack around python 3 deleting the exception variable
retained = None
for flav in flavor:
parser = _parser_dispatch(flav)
Expand All @@ -916,7 +914,7 @@ def _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs):
else:
break
else:
raise_with_traceback(retained)
raise retained

ret = []
for table in tables:
Expand Down
7 changes: 3 additions & 4 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import numpy as np

import pandas._libs.lib as lib
from pandas.compat import raise_with_traceback

from pandas.core.dtypes.common import is_datetime64tz_dtype, is_dict_like, is_list_like
from pandas.core.dtypes.dtypes import DatetimeTZDtype
Expand Down Expand Up @@ -1596,17 +1595,17 @@ def execute(self, *args, **kwargs):
except Exception as exc:
try:
self.con.rollback()
except Exception: # pragma: no cover
except Exception as inner_exc: # pragma: no cover
ex = DatabaseError(
"Execution failed on sql: {sql}\n{exc}\nunable "
"to rollback".format(sql=args[0], exc=exc)
)
raise_with_traceback(ex)
raise ex from inner_exc

ex = DatabaseError(
"Execution failed on sql '{sql}': {exc}".format(sql=args[0], exc=exc)
)
raise_with_traceback(ex)
raise ex from exc

@staticmethod
def _query_iterator(
Expand Down
18 changes: 0 additions & 18 deletions pandas/tests/util/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

import pandas.compat as compat
from pandas.compat import raise_with_traceback

import pandas.util.testing as tm

Expand Down Expand Up @@ -34,23 +33,6 @@ def test_numpy_err_state_is_default():
assert np.geterr() == expected


def test_raise_with_traceback():
with pytest.raises(LookupError, match="error_text"):
try:
raise ValueError("THIS IS AN ERROR")
except ValueError:
e = LookupError("error_text")
raise_with_traceback(e)

with pytest.raises(LookupError, match="error_text"):
try:
raise ValueError("This is another error")
except ValueError:
e = LookupError("error_text")
_, _, traceback = sys.exc_info()
raise_with_traceback(e, traceback)


def test_convert_rows_list_to_csv_str():
rows_list = ["aaa", "bbb", "ccc"]
ret = tm.convert_rows_list_to_csv_str(rows_list)
Expand Down
4 changes: 2 additions & 2 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)

import pandas._libs.testing as _testing
from pandas.compat import _get_lzma_file, _import_lzma, raise_with_traceback
from pandas.compat import _get_lzma_file, _import_lzma

from pandas.core.dtypes.common import (
is_bool,
Expand Down Expand Up @@ -2533,7 +2533,7 @@ def exception_matches(self, exc_type, exc_value, trace_back):
pat=self.regexp.pattern, val=val
)
e = AssertionError(msg)
raise_with_traceback(e, trace_back)
raise e.with_traceback(trace_back)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since assert_raises_regex has been deprecated internally, can we just remove the complete _AssertRaisesContextmanager class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not overly familiar with this code but seems very logical...I'll give it a shot and see what happens

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm well thinking this through some more some of our pandas.util.testing items have leaked into the public API, so we might want to deprecate first. I'll open an issue to discuss

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was publicly deprecated in 0.24, so with the new version policy, I think ok to remove in 1.0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... missed that. Makes sense - thanks


return True
else:
Expand Down