Skip to content

import binascii if ubinascii not found#111

Open
jerryneedell wants to merge 2 commits into
scientifichackers:masterfrom
jerryneedell:jerryn_binascii
Open

import binascii if ubinascii not found#111
jerryneedell wants to merge 2 commits into
scientifichackers:masterfrom
jerryneedell:jerryn_binascii

Conversation

@jerryneedell
Copy link
Copy Markdown

addresses #110
Circuitpython now uses binascii so the "get" command fails trying to import ubinascii.
This PR adds a try/except to import binascii as ubinascii if ubinascii is not present.

@cameronbunce
Copy link
Copy Markdown

I have no authority here, but issue #110 is something that has been annoying in ampy for some time. I have implemented the above fix locally and confirm that it works and is non-breaking for micropython devices as well.
I would like to see this merged.

@wyattearp
Copy link
Copy Markdown

For what it's worth, I tested this on an HUZZAH32 V2 running:

  • adafruit-ampy (1.1.0)
  • Adafruit CircuitPython 8.1.0 on 2023-05-22; Adafruit Feather ESP32 V2 with ESP32

I was unable to make this work, it appears that the left over item is now:

# files.py
    def get(self, filename):
        """Retrieve the contents of the specified file and return its contents
        as a byte string.
        """
        # Open the file and read it a few bytes at a time and print out the
        # raw bytes.  Be careful not to overload the UART buffer so only write
        # a few bytes at a time, and don't use print since it adds newlines and
        # expects string data.
        command = """
            import sys
            import binascii
            with open('{0}', 'rb') as infile:
                while True:
                    result = infile.read({1})
                    if result == b'':
                        break
                    # len = sys.stdout.write(ubinascii.hexlify(result))
                    # use actual binascii
                    len = sys.stdout.write(binascii.hexlify(result))

The above patch correct it for me, which I assume is just a missed merge between your changes and the circuit python project.

curiouswala added a commit that referenced this pull request Apr 16, 2026
…ii fallback

- PR #111: fall back from ubinascii to binascii in the MicroPython-side
  get command, for forward compatibility with firmware that removes ubinascii
- PR #109: add auto_envvar_prefix='AMPY' to the Click group so any future
  option automatically gets an AMPY_* env var without explicit envvar= params
- PR #107: get now accepts multiple remote files via nargs=-1 and saves them
  to a local directory with --output/-o; rm now accepts multiple files
- PR #93: put shows a click progress bar by default; suppress with --no-progress.
  Progress callback added to files.Files.put() for library users

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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