5

I have that PyCharm is halting on all my exceptions, even the ones I am handling in a try except block. I do not want it to break there - I am handling and perhaps expecting an error. But every other exception I do want it to halt and suspend execution (e.g. so that I have the program state and debug it).

How does one do that?

I tried going into the python exception breakpoint option but I didn't see an option like "break only on unhandled exceptions" e.g as suggested by these:

note this is my current state, note how it stopped in my try block... :(

crossposted: https://intellij-support.jetbrains.com/hc/en-us/community/posts/4415666598546-How-to-stop-PyCharm-s-break-stop-halt-feature-on-handled-exceptions-i-e-only-break-on-python-unhandled-exceptions-

enter image description here


I tried:

In your link here intellij-support.jetbrains.com/hc/en-us/community/posts/… the poster Okke said they solved this issue adding --pdb to the 'addition arguments', which someone later said they probably meant interpreter options.

but didn't work got error:

/Users/brandomiranda/opt/anaconda3/envs/meta_learning/bin/python --pdb /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --cmd-line --multiproc --qt-support=auto --client 127.0.0.1 --port 58378 --file /Users/brandomiranda/ultimate-utils/tutorials_for_myself/try_catch_pycharm_issues/try_catch_with_pickle.py
unknown option --pdb
usage: /Users/brandomiranda/opt/anaconda3/envs/meta_learning/bin/python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.

Process finished with exit code 2
8
  • 1
    I think IDE has given a fair option on halting at exceptions. You can see that, you have an option on making it conditional, one approach I can think of is raising a custom exception for unhandled ones, and then customizing the option to break only on that
    – Kris
    Commented Jan 18, 2022 at 19:45
  • 1
    @Kris sounds promising, but I unfortunately don't understand what you want me to do. I'm happy to try it, but I don't understand your suggestion. Commented Jan 18, 2022 at 19:55
  • 1
    In your link here intellij-support.jetbrains.com/hc/en-us/community/posts/… the poster Okke said they solved this issue adding --pdb to the 'addition arguments', which someone later said they probably meant interpreter options. Have you tried this?
    – nanotek
    Commented Jan 20, 2022 at 17:04
  • 3
    The catch you have is for pickle.PicklingError -> pickle.PickleError -> Exception, but the error you have is for AttributeError. If you create an exception to catch AttributeError, do you still have this problem?
    – nanotek
    Commented Jan 20, 2022 at 22:46
  • 1
    Bad practice, but try changing your except statement to a bare except, to see if it stops try: code except: code
    – gotenks
    Commented Jan 21, 2022 at 17:32

2 Answers 2

2
+50

I think it is already working actually, but you are in fact not catching the correct error. In your code you have:

try:
    pickle.dumps(obj)
except pickle.PicklingError:
    return False

But the error thrown is AttributeError. So to avoid that you need something like this:

try:
    pickle.dumps(obj)
except (pickle.PicklingError, AttributeError):
    return False
2
  • doing except worked. Although bad practice at least it work, if the dumps gives any type of error I prefer it returns False (not pickable) Commented Jan 26, 2022 at 0:09
  • 1
    It's actually not bad practice, it's standard in Python to use try statements. It only becomes a problem if the except clause catches something that is too broad and doesn't log it somewhere. Commented Jan 26, 2022 at 0:50
0

Not sure why pycharm behaves the way it does if the except is wrong but this worked:

def is_picklable(obj: Any) -> bool:
    """
    Checks if somehting is pickable.

    Ref:
        - https://stackoverflow.com/questions/70128335/what-is-the-proper-way-to-make-an-object-with-unpickable-fields-pickable
        - pycharm halting all the time issue: https://stackoverflow.com/questions/70761481/how-to-stop-pycharms-break-stop-halt-feature-on-handled-exceptions-i-e-only-b
    """
    import pickle
    try:
        pickle.dumps(obj)
    except:
        return False
    return True

whole file:

"""
trying to resolve:
- https://intellij-support.jetbrains.com/hc/en-us/requests/3764538


You will need to run
    pip install transformers
    pip install fairseq

On mac
    pip3 install torch torchvision torchaudio
on linux
    pip3 install torch==1.10.1+cpu torchvision==0.11.2+cpu torchaudio==0.10.1+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html
on windows
    pip3 install torch torchvision torchaudio
"""
import argparse
from argparse import Namespace
from typing import Any

from fairseq import optim
from torch import nn
from torch.optim import Optimizer
from torch.optim.lr_scheduler import _LRScheduler
from transformers.optimization import AdafactorSchedule


def invoke_handled_exception():
    try:
        1 / 0
    except ZeroDivisionError:
        print('exception caught')


def make_args_pickable(args: Namespace) -> Namespace:
    """
    Returns a copy of the args namespace but with unpickable objects as strings.

    note: implementation not tested against deep copying.
    ref:
        - https://stackoverflow.com/questions/70128335/what-is-the-proper-way-to-make-an-object-with-unpickable-fields-pickable
        - pycharm halting all the time issues: https://stackoverflow.com/questions/70761481/how-to-stop-pycharms-break-stop-halt-feature-on-handled-exceptions-i-e-only-b
        - stop progressbar from printing progress when checking if it's pickable: https://stackoverflow.com/questions/70762899/how-does-one-stop-progressbar-from-printing-eta-progress-when-checking-if-the
    """
    pickable_args = argparse.Namespace()
    # - go through fields in args, if they are not pickable make it a string else leave as it
    # The vars() function returns the __dict__ attribute of the given object.
    for field in vars(args):
        # print(f'-----{field}')
        field_val: Any = getattr(args, field)
        if not is_picklable(field_val):
            field_val: str = str(field_val)
        # - after this line the invariant is that it should be pickable, so set it in the new args obj
        setattr(pickable_args, field, field_val)
        # print('f-----')
    return pickable_args


def is_picklable(obj: Any) -> bool:
    """
    Checks if somehting is pickable.

    Ref:
        - https://stackoverflow.com/questions/70128335/what-is-the-proper-way-to-make-an-object-with-unpickable-fields-pickable
        - pycharm halting all the time issue: https://stackoverflow.com/questions/70761481/how-to-stop-pycharms-break-stop-halt-feature-on-handled-exceptions-i-e-only-b
    """
    import pickle
    try:
        pickle.dumps(obj)
    except:
        return False
    return True


def invoke_handled_exception_brandos_pickle_version():
    mdl: nn.Module = nn.Linear(4, 3)
    optimizer: Optimizer = optim.adafactor.Adafactor(params=mdl.parameters())
    scheduler: _LRScheduler = AdafactorSchedule(optimizer)
    args: Namespace = Namespace(scheduler=scheduler, optimizer=optimizer, model=mdl)
    make_args_pickable(args)
    print('Success if this line printed! Args was made into a pickable args without error')


# -- tests
invoke_handled_exception()
invoke_handled_exception_brandos_pickle_version()

Not the answer you're looking for? Browse other questions tagged or ask your own question.