Skip to content

Update smbus.py - #37

Merged
makermelissa merged 2 commits into
adafruit:mainfrom
zapper1942:main
Sep 24, 2026
Merged

makermelissa merged 2 commits into
adafruit:mainfrom
zapper1942:main

Conversation

@zapper1942

Copy link
Copy Markdown
Contributor

I recently upgraded Pi OS to Trixie for my project and this plugin started failing with the error "TypeError: 'memoryview' object cannot be interpreted as an integer" with Python 3.13.
https://github.com/zapper1942/mechanical-pi-clock/blob/main/pi_clock.py
I had to make this change to get it working again

AI Explanation:

In Python 3.13, when Adafruit's I2C layer reads data blocks, it passes a memoryview object instead of a direct byte array. Without the change (cmd[0] = reg), is rejected as a TypeError

By switching to (cmd[0] = reg[0]), the code now extracts the integer out of the data wrapper regardless of whether that wrapper is an old-school bytes object (Python3.12) or a new (Python 3.13) memoryview

I have not tested it on python3.12 however

Thank you for creating a pull request to contribute to Adafruit's GitHub code!
Before you open the request please review the following guidelines and tips to
help it be more easily integrated:

  • Describe the scope of your change--i.e. what the change does and what parts
    of the code were modified.
    This will help us understand any risks of integrating
    the code.

  • Describe any known limitations with your change. For example if the change
    doesn't apply to a supported platform of the library please mention it.

  • Please run any tests or examples that can exercise your modified code. We
    strive to not break users of the code and running tests/examples helps with this
    process.

Thank you again for contributing! We will try to test and integrate the change
as soon as we can, but be aware we have many GitHub repositories to manage and
can't immediately respond to every request. There is no need to bump or check in
on a pull request (it will clutter the discussion of the request).

Also don't be worried if the request is closed or not integrated--sometimes the
priorities of Adafruit's GitHub code (education, ease of use) might not match the
priorities of the pull request. Don't fret, the open source community thrives on
forks and GitHub makes it easy to keep your changes in a forked repo.

After reviewing the guidelines above you can delete this text from the pull request.

Fix for "TypeError: 'memoryview' object cannot be interpreted as an integer" with python3.13
@makermelissa-ai-assistant

Copy link
Copy Markdown
Contributor

The proposed change fixes a one-byte memoryview, but introduces two correctness regressions:

  • The standard read_i2c_block_data(addr, register_int, length) form now raises TypeError: 'int' object is not subscriptable at reg[0].
  • Blinka passes the complete write-buffer slice here, so a memoryview can contain multiple command bytes. Wrapping it in a one-byte bytearray and using only reg[0] silently truncates that command.

With ioctl mocked, the base version sent 12 for an integer command; this PR raised TypeError. A two-byte memoryview containing 12 34 was accepted by this PR but only 12 was sent.

A minimal fix is to treat memoryview as an existing buffer and retain the original integer conversion:

if not isinstance(cmd, (bytes, bytearray, memoryview)):
    reg = cmd
    cmd = bytearray(1)
    cmd[0] = reg

I tested that version with integer, bytes, bytearray, and one- and two-byte memoryview commands; each preserved the expected command bytes.

@makermelissa makermelissa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please see my AI assistant's comments, which reflected my concern anyways.

@makermelissa

Copy link
Copy Markdown
Collaborator

I updated the repo, so it should pass CI now.

Revert initial changes and instead add the new memoryview to the list of not instances as requested. This should preserve all expected command bytes instead of the introduced regression of discarding some.
@zapper1942

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. I've implemented the suggestion and it is also working locally.

@makermelissa makermelissa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you. Looks good.

@makermelissa
makermelissa merged commit bc28777 into adafruit:main Sep 24, 2026
1 check 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.

3 participants