Skip to content

pdo: Handle None cob_id correctly in __repr__#640

Merged
acolomb merged 2 commits intocanopen-python:masterfrom
stoicfeats:fix-pdo-repr-crash
Apr 23, 2026
Merged

pdo: Handle None cob_id correctly in __repr__#640
acolomb merged 2 commits intocanopen-python:masterfrom
stoicfeats:fix-pdo-repr-crash

Conversation

@stoicfeats
Copy link
Copy Markdown
Contributor

@stoicfeats stoicfeats commented Apr 16, 2026

fxes a crash when calling repr() on a pdomap that doesn't have a cob_id assigned yet.

before this, working in an interactive session would throw a typeerror if we evaluated uninitialized maps like node.rpdo[1].
the string formatter was trying to apply : X hex-formatting to a nonetype value.

this adds a quick inline fallback so it prints "unassigned" instead of throwing error on the console.

@stoicfeats
Copy link
Copy Markdown
Contributor Author

@acolomb please have a look into this, thanks.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 16, 2026

Codecov Report

❌ Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
canopen/pdo/base.py 0.00% 7 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@acolomb
Copy link
Copy Markdown
Member

acolomb commented Apr 16, 2026

Hi and thanks for your contribution. I've experienced the issue you mention several times. It's usually a sign of weird ordering in the API usage, because you need to assign a valid COB ID by some means first. But sure, the __repr__ case is very easy to hit without even trying any actual bus configuration.

We have several more places that can fail with the same error, mainly logging calls but also send_message() and friends. Ideally, we would forbid the cob_id member to ever be None (rather zero, which is also not valid). But that's a rather large change.

Would you mind checking the other places though, to fix other occurrences where the format strings can easily explode with cob_id = None?

@stoicfeats
Copy link
Copy Markdown
Contributor Author

stoicfeats commented Apr 18, 2026

these were the places where cob_id = None could have cause the issues.

added explicit none-checks to subscribe, start, transmit, and remote_request to raise an immediate value error.

@acolomb

@stoicfeats
Copy link
Copy Markdown
Contributor Author

@acolomb can you please have a look into this once ?

Copy link
Copy Markdown
Member

@acolomb acolomb left a comment

Choose a reason for hiding this comment

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

Looks good in general, thank you!

I just broadened the checks a bit to cover more than just None values. Zero is not a valid COB ID for PDOs either, so checking for the thruth value of self.cob_id catches both cases.

Comment thread canopen/pdo/base.py Outdated
Comment thread canopen/pdo/base.py Outdated
Comment thread canopen/pdo/base.py Outdated
Comment thread canopen/pdo/base.py Outdated
Comment thread canopen/pdo/base.py Outdated
Co-authored-by: André Colomb <github.com@andre.colomb.de>
@acolomb acolomb changed the title pdo: handle none cob_id correctly in __repr__ pdo: Handle None cob_id correctly in __repr__ Apr 23, 2026
@acolomb acolomb merged commit fe494bb into canopen-python:master Apr 23, 2026
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants