Skip to content
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

BUG: hasattr not working with InstantCamera #806

Open
andreas-h-sie opened this issue Jan 8, 2025 · 2 comments
Open

BUG: hasattr not working with InstantCamera #806

andreas-h-sie opened this issue Jan 8, 2025 · 2 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@andreas-h-sie
Copy link

andreas-h-sie commented Jan 8, 2025

Describe the issue:

hasattr does not seem to work with InstantCamera.

Internally Python implements hasattr with a getattr and checks wether an AttributeError is raised or not. However, pypylon raises a LogicalErrorException which is not according to hasattr expectation.

As a workaround I use the following

def pylon_hasattr(obj, attr: str) -> bool:
    return attr in dir(obj)

Reproduce the code example:

from pypylon import pylon

camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())

hasattr(camera,"GevIEEE1588")  # this raises _genicam.LogicalErrorException

Error message:

No response

Is your camera operational in Basler pylon viewer on your platform

Yes

Hardware setup & camera model(s) used

a2A2590-22gmPRO

Linux 6.1

Runtime information:

python: 3.11.6 (main, Nov 29 2023, 04:47:02) [GCC 10.2.1 20210110]
platform: linux/x86_64/6.1.102-cip26-rt14+ind3
pypylon: 1.9.0 / 6.3.0.18933
@thiesmoeller
Copy link
Collaborator

Thank you for reporting this issue.

The problem arises because the hasattr function in Python expects an AttributeError when an attribute is not present, while pypylon currently raises a _genicam.LogicalErrorException.
This is the exception that the underlying genicam model raises for missing nodes.

Code is here

def __getattr__(self, attribute):

To address this, we propose the following solution that maintains backward compatibility while providing a path for future improvements.


Solution Overview

  1. Introduce a Strict Mode Flag

A new flag, strict_attribute_error, will be introduced to control how missing attributes are handled.

By default, the behavior will remain the same (raising _genicam.LogicalErrorException) to avoid breaking existing code.

When strict_attribute_error is enabled, the library will raise AttributeError instead, aligning with Python's conventions.

  1. Per-Instance and Global Configuration

Global Setting: Developers can enable strict mode globally for all objects in the library.

Per-Instance Setting: Strict mode can be enabled individually for specific objects (e.g., InstantCamera) to allow finer control.

  1. Future Breaking Change (Next Major Version):

In the next major version release, the library will fully transition to raising AttributeError for missing attributes, removing _genicam.LogicalErrorException from this context. The strict mode flag will then be deprecated.

Please share your view on this.

@andreas-h-sie
Copy link
Author

Changing the behavior and becoming more conforming to Python standards in the next major relase sounds like a good plan.

Not sure if a by-instance "strict mode" is really worth it. Maybe not even a global strict mode at all. Good would be in any case to deprecate the current behavior (raise LogicError).

If you make a change in the next major release perhaps a global legacy mode flag (the inverse of strict mode) (which restores the old behavior) makes sense, in case some existing code relies on the old behavior.

In the meantime you perhaps could document the not-working hasattr as known limitation?

@thiesmoeller thiesmoeller added bug Something isn't working enhancement New feature or request labels Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants