← 输入框|github.com|刷新
Skip to content

LPython bogusly changes attribute value #1981

Closed

Description

@rebcabin

repro: https://github.com/rebcabin/lpython/tree/brian-lasr/lasr/LP-pycharm/Issue1981

This is related to Issue #1978; i'm digging in to find out what's wrong. I reduced the test case to the bare minimum that illustrates the problem.

The problem is that when I initialize a StringIO object from inside a function, an attribute value gets bogusly changed to a random number.

Consider the following code in Issue1981/lasr_lexer.py

from lpython import (dataclass, InOut, i32,)
from lasr_stringio import (StringIO, __post_init__,)

NEWLINE      : str       = "\n"
WHITESPACE   : list[str] = [" ", ",", NEWLINE, "\r", "\t"]
EOF_SIGNAL   : str       = ''

@dataclass
class LasrLexer:
    fd   : StringIO = StringIO('', 0, 0) # prefer Optional[StringIO]
    line : i32 = 1 # prefer Line
    col  : i32 = 1 # prefer Col
    type : str = '' # prefer LTType
    val  : str = '' # prefer LTVal

def lexer_test():
    print('LEXER TEST')
    lexer : LasrLexer = LasrLexer(StringIO('  foo  bar  '))
    __post_init__(lexer.fd)

if __name__ == '__main__':
    lexer_test()

When I run it with LPython, I get

/Users/brian/CLionProjects/lpython/src/bin/python /Users/brian/CLionProjects/lpython/lasr/LP-pycharm/Issue1981/lasr_lexer.py 
LEXER TEST
StringIO __post_init__ preconditions:
    _buf =   foo  bar  .
    _len = 1.
    _0cursor = 1830662080.  ################ ATTENTION #############
StringIO __post_init__ postconditions:
    _buf =   foo  bar  .
    _len = 12.
    _0cursor = 1830662080.

When I change the code to the following, just moving the initialization inside main and out of the function lexer_test, I get the correct value for _0cursor:

from lpython import (dataclass, InOut, i32,)
from lasr_stringio import (StringIO, __post_init__,)


NEWLINE      : str       = "\n"
WHITESPACE   : list[str] = [" ", ",", NEWLINE, "\r", "\t"]
EOF_SIGNAL   : str       = ''


@dataclass
class LasrLexer:
    fd   : StringIO = StringIO('', 0, 0) # prefer Optional[StringIO]
    line : i32 = 1 # prefer Line
    col  : i32 = 1 # prefer Col
    type : str = '' # prefer LTType
    val  : str = '' # prefer LTVal


if __name__ == '__main__':
    print('LEXER TEST')
    lexer : LasrLexer = LasrLexer(StringIO('  foo  bar  '))
    __post_init__(lexer.fd)

produces:

/Users/brian/CLionProjects/lpython/src/bin/python /Users/brian/CLionProjects/lpython/lasr/LP-pycharm/Issue1981/lasr_lexer.py 
LEXER TEST
StringIO __post_init__ preconditions:
    _buf =   foo  bar  .
    _len = 0.
    _0cursor = 0.  ################ ATTENTION ###############
StringIO __post_init__ postconditions:
    _buf =   foo  bar  .
    _len = 12.
    _0cursor = 0.

Activity

  1. added
    bug
    on Jun 21, 2023
  2. rebcabin commented on Jun 21, 2023

    @rebcabin
    ContributorAuthor

    This could be related to a recent fix that allowed constructors to be called with partial argument lists. Investigating ...

  3. self-assigned this
    on Jun 21, 2023
  4. Smit-create commented on Jun 21, 2023

    @Smit-create
    Collaborator
    @dataclass
    class StringIO:
        _buf     : str
        _0cursor : i32 = 0
        _len     : i32 = 0

    Using this will fix.

  5. rebcabin commented on Jun 21, 2023

    @rebcabin
    ContributorAuthor

    ok, that's fair, but shouldn't i32(0) be allowed as an initial value?

  6. rebcabin commented on Jun 21, 2023

    @rebcabin
    ContributorAuthor

    Also, this fixes it too:

        lexer : LasrLexer = LasrLexer(StringIO('  foo  bar  ', 0, 0))

    instead of

        lexer : LasrLexer = LasrLexer(StringIO('  foo  bar  '))

    probably for the same reason as your fix works.

  7. rebcabin commented on Jun 21, 2023

    @rebcabin
    ContributorAuthor

    This also fixes it:

        lexer : LasrLexer = LasrLexer(StringIO('  foo  bar  ', i32(0), i32(0)))

    So I think this bug still stands. The following ought to work:

    @dataclass
    class StringIO:
        _buf     : str
        _0cursor : i32 = i32(0)
        _len     : i32 = i32(0)
  8. rebcabin commented on Jun 21, 2023

    @rebcabin
    ContributorAuthor

    and the following ought to work, too:

        lexer : LasrLexer = LasrLexer(StringIO('  foo  bar  '))
  9. rebcabin commented on Jun 21, 2023

    @rebcabin
    ContributorAuthor

    The fact that the original code fails with a large random number that looks like a 64-bit address is evidence of a pointer calculation going wrong in the C++ code :)

  10. rebcabin commented on Jun 21, 2023

    @rebcabin
    ContributorAuthor

    I'm leaving the repro case untouched for investigation purposes.

  11. Thirumalai-Shaktivel commented on Jun 21, 2023

    @Thirumalai-Shaktivel
    Collaborator

    Yup, it can also mean that the value 0 in _0cursor : i32 = i32(0) is not getting initialised!

  12. rebcabin commented on Jun 21, 2023

    @rebcabin
    ContributorAuthor

    you guys are great! this is fun!

  13. Thirumalai-Shaktivel commented on Jun 21, 2023

    @Thirumalai-Shaktivel
    Collaborator

    I see, anything apart from constants is skipped in the StructType definition:

    @dataclass
    class StringIO:
        _buf     : str
        _0cursor : i32 = 0
        _len     : i32 = i32(0)
    (StructType
        (SymbolTable
            4
            {
                _0cursor:
                    (Variable
                        4
                        _0cursor
                        []
                        Local
                        ()
                        (IntegerConstant 0 (Integer 4))
                        Default
                        (Integer 4)
                        ()
                        Source
                        Public
                        Required
                        .false.
                    ),
                _buf:
                    (Variable
                        4
                        _buf
                        []
                        Local
                        ()
                        ()
                        Default
                        (Character 1 -2 ())
                        ()
                        Source
                        Public
                        Required
                        .false.
                    ),
                _len:
                    (Variable
                        4
                        _len
                        []
                        Local
                        ()
                        ()
                        Default
                        (Integer 4)
                        ()
                        Source
                        Public
                        Required
                        .false.
                    )
            })
        StringIO
        []
        [_buf
        _0cursor
        _len]
        Source
        Public
        .false.
        .false.
        ()
        ()
    )

    So the struct member seems to be uninitialized here.

  14. Thirumalai-Shaktivel commented on Jun 21, 2023

    @Thirumalai-Shaktivel
    Collaborator

    arg.m_value = member_var->m_value;

    This becomes nullptr for i32(0)

  15. Smit-create commented on Jun 21, 2023

    @Smit-create
    Collaborator

    Struct members for initialization at present need compile-time constant values. And so that's the reason for failure.

  16. Smit-create commented on Jun 21, 2023

    @Smit-create
    Collaborator

    ok, that's fair, but shouldn't i32(0) be allowed as an initial value?

    It should, I'll look into that for sure.

  17. Smit-create commented on Jun 21, 2023

    @Smit-create
    Collaborator

    Okay, the bug here is that i32(0) is a Call AST node that is skipped while visiting the Symbol table, and so, the value for it is kept as nullptr.

  18. Smit-create commented on Jun 21, 2023

    @Smit-create
    Collaborator

    One fix is to make the Classdef node revisit again in body-visitor to capture this.

  19. certik commented on Jun 21, 2023

    @certik
    Contributor

    I think that expressions have to be visited in the symbol table visitor, just like we do in LFortran. Because as part of types as well as initializations you need to visit them.

  20. Smit-create commented on Jun 30, 2023

    @Smit-create
    Collaborator

    Fixed in #2056

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions