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

Bug Fix: Correct Handling of Asterisk (*) in user_input Mode #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amstokely
Copy link
Owner

Description:

This PR addresses an issue in the abserdes library where an asterisk (*) in the user_input mode is incorrectly interpreted as an integer instead of a string. This caused deserialization to fail with a ValueError when the input contained an asterisk, as described in the issue below:

Reproduction of the issue:

from abserdes import Serializer

class My_class(Serializer):
    def __init__(self):
        self.string_value = "whatever"
        return

my_xml = """<?xml version="1.0" ?>
<my_instance class='My_class'>
    <string_value>*</string_value>
</my_instance>"""
xml_filename = "abserdes_bug.xml"
with open(xml_filename, "w") as f:
    f.write(my_xml)

my_instance = My_class()
my_instance.deserialize(xml_filename, user_input=True)

Running the script generated the following error:

ValueError: invalid literal for int() with base 10: '*'

Root cause:

The bug was traced to the regular expression used in strcast.py to determine how strings are interpreted. The current implementation did not handle asterisks or other special characters correctly, leading to incorrect typecasting when it should have treated such inputs as strings.

Fix:

  • Modified the regex in strcast.py to ensure that inputs containing *, @, and other non-numeric characters are correctly interpreted as strings.
  • This fix allows abserdes to properly handle these special characters when deserializing from XML files in user_input mode.

Testing:

  • The fix was tested with the provided reproduction script and successfully resolved the issue.
  • Further tests were added to ensure that other special characters are also correctly handled.

Note:

Until a more robust solution is implemented, please avoid using numeric-only strings in user_input fields that you want abserdes to deserialize as strings.

@amstokely amstokely self-assigned this Oct 17, 2024
@lvotapka
Copy link
Collaborator

As a suggestion, perhaps we could support the type="str" field within the user_input mode to allow even numeric-only strings to be correctly interpreted.

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.

2 participants