macos: KeyboardInterrupt exception not working w/Phidget22.Devices.DigitalOutput

Supporting 2.7 and 3.2+
Post Reply
stepheneb
Phidgetly
Posts: 19
Joined: Tue Oct 10, 2023 3:46 pm
Contact:

macos: KeyboardInterrupt exception not working w/Phidget22.Devices.DigitalOutput

Post by stepheneb »

When using the following to catch ctrl-C on macos the handler is never called if I have also opened a Phidget22.Devices.DigitalOutput device.

Code: Select all

try
    code ...
except KeyboardInterrupt
    ctrl-C exception handling code ...
I am using a REL1100 4-channel solid state relay with a VINT HUB0000.

Here's my test code. It opens a Phidget22.Devices.DigitalOutput device and runs through a loop counting up to 20.

Code: Select all

from Phidget22.Phidget import *
from Phidget22.Devices.DigitalOutput import *
import time

import argparse

open_phidget = True

parser = argparse.ArgumentParser(
    description='show phidget python issue w/ctrl-C on macos.')

parser.add_argument('--open-phidget', default=open_phidget, action=argparse.BooleanOptionalAction,
                    help=f"disable to skip opening phidget: {open_phidget}")

args = parser.parse_args()

open_phidget= args.open_phidget

def main():
    exit_reason = "count completed"
    running = True

    count = 1
    max_count = 20
    sleep = 0.1

    d0 = DigitalOutput()

    if open_phidget:
        d0.openWaitForAttachment(5000)
        d0.setDutyCycle(0.5)


    print(f"\nCounting to {max_count} over {max_count * sleep} seconds. Try interrupting with ctrl-C")

    try:
        while running:
            print(count)
            count += 1
            running = count <= max_count
            time.sleep(sleep)

    except KeyboardInterrupt:
        exit_reason = "ctrl-C"

    print(f"\nExit reason: {exit_reason}\n")

    if open_phidget:
        d0.close()

main()
By default it opens a Phidget22.Devices.DigitalOutput and then sets the dutycycle before entering the loop.

It includes an optional argument to skip opening the Phidget22.Devices.DigitalOutput device.

Code: Select all

% python test-ctrl-c.py --help           
usage: test-ctrl-c.py [-h] [--open-phidget | --no-open-phidget]

show phidget python issue w/ctrl-C on macos.

options:
  -h, --help            show this help message and exit
  --open-phidget, --no-open-phidget
                        disable to skip opening phidget: True
This is what a normal run looks like.

Code: Select all

Counting to 20 over 2.0 seconds. Try interrupting with ctrl-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Exit reason: count completed
Here's what the same run looks like when interrupting the loop with ctrl-C. NOTE: the exit_reason: "ctrl-C", IS NOT printed.

Code: Select all

% python test-ctrl-c.py --open-phidget 

Counting to 20 over 2.0 seconds. Try interrupting with ctrl-C
1
2
3
4
5
6
7
8
9
10
^C%
Here the same test except I am NOT opening the Phidget22.Devices.DigitalOutput. NOTE: the exit_reason: "ctrl-C", IS printed.

Code: Select all

% python test-ctrl-c.py --no-open-phidget

Counting to 20 over 2.0 seconds. Try interrupting with ctrl-C
1
2
3
4
5
6
^C
Exit reason: ctrl-C
stepheneb
Phidgetly
Posts: 19
Joined: Tue Oct 10, 2023 3:46 pm
Contact:

Re: macos: KeyboardInterrupt exception not working w/Phidget22.Devices.DigitalOutput

Post by stepheneb »

FYI: works fine on Windows11.
stepheneb
Phidgetly
Posts: 19
Joined: Tue Oct 10, 2023 3:46 pm
Contact:

Re: macos: KeyboardInterrupt exception not working w/Phidget22.Devices.DigitalOutput

Post by stepheneb »

Phidget library developers: Curious if you can reproduce this bug and whether a fix might be in the works? I'd rather not have to dig into the phidget python library and fix it myself.
User avatar
Patrick
Lead Developer
Posts: 634
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: macos: KeyboardInterrupt exception not working w/Phidget22.Devices.DigitalOutput

Post by Patrick »

Hi,

I'm looking into this now. I can recreate the issue.

-Patrick
User avatar
Patrick
Lead Developer
Posts: 634
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: macos: KeyboardInterrupt exception not working w/Phidget22.Devices.DigitalOutput

Post by Patrick »

Hi,

This issue has been resolved in the latest release. Thanks for reporting.

-Patrick
stepheneb
Phidgetly
Posts: 19
Joined: Tue Oct 10, 2023 3:46 pm
Contact:

Re: macos: KeyboardInterrupt exception not working w/Phidget22.Devices.DigitalOutput

Post by stepheneb »

That's great! When will it be deployed to pypi.org? Right now the latest here https://pypi.org/project/Phidget22/ appears to be: 1.17.20231004
stepheneb
Phidgetly
Posts: 19
Joined: Tue Oct 10, 2023 3:46 pm
Contact:

Re: macos: KeyboardInterrupt exception not working w/Phidget22.Devices.DigitalOutput

Post by stepheneb »

Looks like the update was to the macos installer available on this page: https://www.phidgets.com/docs/Language_-_Python and not to the Python package (as I assumed).

Have installed the latest macos library and ctrl-C/sigint is now caught properly by my python program.
User avatar
Patrick
Lead Developer
Posts: 634
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: macos: KeyboardInterrupt exception not working w/Phidget22.Devices.DigitalOutput

Post by Patrick »

Yes, the issue was in the C library not the python library.

-Patrick
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest