Skip to content

py/objstr: Fix b"".hex() so it returns an empty bytes object.#18808

Open
dpgeorge wants to merge 3 commits intomicropython:masterfrom
dpgeorge:py-bytes-hex-empty-arg
Open

py/objstr: Fix b"".hex() so it returns an empty bytes object.#18808
dpgeorge wants to merge 3 commits intomicropython:masterfrom
dpgeorge:py-bytes-hex-empty-arg

Conversation

@dpgeorge
Copy link
Member

Summary

As per CPython behaviour, b"".hex() should return an empty bytes object (not an empty str object).

Fixes issue #18807.

I also took the opportunity to remove the .exp file for the corresponding test, because now that we use CPython 3.8 as the baseline for testing MicroPython matches CPython for this test file.

Testing

Tested locally on the unix port. Will also be tested by CI.

Trade-offs and Alternatives

I made a helper function make_empty_str_of_type() to try and mitigate code size change, and reduce code duplication.

As of CPython 3.7 `bytes.fromhex()` skips all ASCII whitespace.  And as of
CPython 3.8 `bytes.hex()` supports the optional separator argument.  So
this test no longer needs a .exp file.

Signed-off-by: Damien George <damien@micropython.org>
This reduces code duplication, and may help reduce code size.

Signed-off-by: Damien George <damien@micropython.org>
Fixes issue micropython#18807.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Feb 12, 2026
@dpgeorge dpgeorge mentioned this pull request Feb 12, 2026
@codecov
Copy link

codecov bot commented Feb 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.42%. Comparing base (6ae08ea) to head (d8b11db).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #18808      +/-   ##
==========================================
- Coverage   98.42%   98.42%   -0.01%     
==========================================
  Files         174      174              
  Lines       22333    22331       -2     
==========================================
- Hits        21982    21980       -2     
  Misses        351      351              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link

Code size report:

Reference:  zephyr: Add support for RISC-V RV32IMC native code emitter. [9721955]
Comparison: py/objstr: Fix b"".hex() so it returns an empty bytes object. [merge of d8b11db]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   +16 +0.002% standard
      stm32:    +8 +0.002% PYBV10
      esp32:   +24 +0.001% ESP32_GENERIC
     mimxrt:    +8 +0.002% TEENSY40
        rp2:   +16 +0.002% RPI_PICO_W
       samd:   +16 +0.006% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   +16 +0.004% VIRT_RV32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant