Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add special handling in PycodeSerializer for bytes #879

Merged
merged 2 commits into from
Dec 16, 2023

Conversation

SrirachaHorse
Copy link
Contributor

@SrirachaHorse SrirachaHorse commented Dec 8, 2023

📒 Description

Adds special handling to PycodeSerializer to ensure that byte strings are not modified and that their representation is handled literally.

Resolves #878

🔗 What I've Done

Update literal_value() to add special handling for byte strings, so that the literal repr() of the string is used, instead of falling back to the default call to repr() which subsequently replaces single quotes in the resulting string.

Also adds a quick unit test to exhibit the fix.

💬 Comments

🛫 Checklist

Copy link

codecov bot commented Dec 8, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0b2fe8b) 99.89% compared to head (55fad70) 99.89%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #879   +/-   ##
=======================================
  Coverage   99.89%   99.89%           
=======================================
  Files         105      105           
  Lines        9304     9307    +3     
  Branches     2079     2080    +1     
=======================================
+ Hits         9294     9297    +3     
  Misses          3        3           
  Partials        7        7           

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

Copy link
Owner

@tefra tefra left a comment

Choose a reason for hiding this comment

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

Can you maintain the double quotes, please?

@SrirachaHorse
Copy link
Contributor Author

SrirachaHorse commented Dec 10, 2023

Can you maintain the double quotes, please?

Bit confused - based on the unit test I think the repr() of a bytes object is printing as double quotes already. e.g. repr(b'a') will be b"a". Is there something I'm missing?

@tefra
Copy link
Owner

tefra commented Dec 11, 2023

Can you maintain the double quotes, please?

Bit confused - based on the unit test I think the repr() of a bytes object is printing as double quotes already. e.g. repr(b'a') will be b"a". Is there something I'm missing?

Python 3.11.4+ (heads/3.11:ff5dd9d, Jul 30 2023, 17:40:33) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = b'a'
>>> repr(foo)
"b'a'"
>>>

from the w3c-tests

--- a/output/instances/ibmData/valid/D3_3_16/d3_3_16v01.py
+++ b/output/instances/ibmData/valid/D3_3_16/d3_3_16v01.py
@@ -3,6 +3,6 @@ from output.models.ibm_data.valid.d3_3_16.d3_3_16v01_xsd.d3_3_16v01 import Root

 obj = Root(
     ele=[
-        b"\x124V",
+        b'\x124V',
     ]
 )
diff --git a/output/instances/ibmData/valid/D3_3_17/d3_3_17v01.py b/output/instances/ibmData/valid/D3_3_17/d3_3_17v01.py
index 1d9122686..1db8afcf3 100644
--- a/output/instances/ibmData/valid/D3_3_17/d3_3_17v01.py
+++ b/output/instances/ibmData/valid/D3_3_17/d3_3_17v01.py
@@ -3,6 +3,6 @@ from output.models.ibm_data.valid.d3_3_17.d3_3_17v01_xsd.d3_3_17v01 import Root

 obj = Root(
     ele=[
-        b"\xd7m\xf8\xe7\xae\xfc",
+        b'\xd7m\xf8\xe7\xae\xfc',
     ]
 )

@SrirachaHorse
Copy link
Contributor Author

Ah whoops, got it.

I've added a fix that should maintain the double quotes for bytes. It's a bit messier than I was hoping, just because there's the chance that a byte string could contain both single quotes and double quotes, which makes a blind replace() call much more difficult. I've expanded out the unit test to validate that case.

Copy link

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link
Owner

@tefra tefra left a comment

Choose a reason for hiding this comment

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

Thank you1

@tefra tefra merged commit 80ba4f4 into tefra:main Dec 16, 2023
14 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.

PycodeSerializer can modify contents of byte strings
2 participants