Skip to content

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

Merged
dpgeorge merged 3 commits into
micropython:masterfrom
dpgeorge:py-bytes-hex-empty-arg
Mar 4, 2026
Merged

dpgeorge merged 3 commits into
micropython:masterfrom
dpgeorge:py-bytes-hex-empty-arg

Conversation

@dpgeorge

@dpgeorge dpgeorge commented Feb 12, 2026 •

Copy link
Copy Markdown
Member

Summary

As per CPython behaviour, b"".hex() should return an empty str object (not an empty bytes 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.

@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

codecov Bot commented Feb 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.42%. Comparing base (bfc69db) to head (06bfefe).
⚠️ Report is 3 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       22331    22329       -2     
==========================================
- Hits        21980    21978       -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

github-actions Bot commented Feb 12, 2026 •

Copy link
Copy Markdown

Code size report:

Reference:  extmod,rp2: Keep LWIP timer running if lwip poll_sockets() is called. [bfc69db]
Comparison: py/objstr: Fix b"".hex() so it returns an empty str object. [merge of 06bfefe]
  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

@DvdGiessen

DvdGiessen commented Feb 16, 2026 •

Copy link
Copy Markdown
Contributor

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

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

Isn't it the other way round?

  • Fix b"".hex() so it returns an empty str object
  • As per CPython behaviour, b"".hex() should return an empty str object (not an empty bytes object).

(Code seems correct, but PR / commit message is confusing.)

@dpgeorge
dpgeorge force-pushed the py-bytes-hex-empty-arg branch from d8b11db to be6de9e Compare February 16, 2026 23:29
@dpgeorge dpgeorge changed the title py/objstr: Fix b"".hex() so it returns an empty bytes object. py/objstr: Fix b"".hex() so it returns an empty str object. Feb 16, 2026
@dpgeorge

Copy link
Copy Markdown
Member Author

Isn't it the other way round?

You are right! Thanks for picking that up, I've now updated the commit message and this PR text.

dpgeorge added 3 commits March 4, 2026 12:06
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 <[email protected]>
This reduces code duplication, and may help reduce code size.

Signed-off-by: Damien George <[email protected]>
As per CPython behaviour, `b"".hex()` should return an empty str object
(not an empty bytes object).

Fixes issue micropython#18807.

Signed-off-by: Damien George <[email protected]>
@dpgeorge
dpgeorge force-pushed the py-bytes-hex-empty-arg branch from be6de9e to 06bfefe Compare March 4, 2026 01:07
@dpgeorge
dpgeorge merged commit 06bfefe into micropython:master Mar 4, 2026
74 checks passed
@dpgeorge
dpgeorge deleted the py-bytes-hex-empty-arg branch March 4, 2026 01:31
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.

2 participants