Skip to content

Fixed compatibility of sep, end keyword with list/tuple containers #1512

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

Merged
merged 15 commits into from
Feb 26, 2023

Conversation

anutosh491
Copy link
Collaborator

Fixes #1477

Some issues that were fixed are

  1. sep and end keyword were assumed to be functioning correctly beforehand with data types like i32, str etc but trying out some cases showed that they aren't fully compatible . Pointed out in Variable of list[i32] type has unnecessary newline character while printing. #1477 (comment) . This has been addressed.

  2. Secondly all sorts of variations of print statements involving the list/tuple containers have been fixed .

def main0():
    a: i32 = 1
    b: i32 = 2
    c: str = "abc"
    d: str = "def"
    e: list[i32] = [1, 2]
    f: list[i32] = [3, 4]
    g: tuple[i32, i32, i32] = (1, 2, 3)
    h: tuple[i32, i32] = (4, 5)
    l : list[i32] = [1,2,3]
    l2 : list[i32] = [4, 5, 6]
    s : str = "abc"
    print(a, g, c,sep = "xyz", end = "pqr")
    print(g, a, c, sep = "xyz", end = "pqr")
    print(a, e, g, sep = "xyz", end = "pqr")
    print(a, l, s)
    print(l , a, s)
    print(a, s, l)
    print(a, l, s, end = " pqr ")
    print(l, a, s, end = " pqr ")
    print(a, s, l, end = " pqr ")
    print(a, l, s, sep = "xyz")
    print(l, a, s, sep = "xyz")
    print(a, s, l, sep = "xyz")
    print(a, l, s, sep = "xyz", end = "pqr")
    print(l, a, s, sep = "xyz", end = "pqr")
    print(a, s, l, sep = "xyz", end = "pqr")
    print(e, f, a, sep = "xyz")
    print(g, e, f, end = "pqr")

main0()
(lf) anutosh491@spbhat68:~/lpython/lpython$ ./src/bin/lpython examples/expr2.py 
1xyz(1, 2, 3)xyzabcpqr(1, 2, 3)xyz1xyzabcpqr1xyz[1, 2]xyz(1, 2, 3)pqr1 [1, 2, 3] abc
[1, 2, 3] 1 abc
1 abc [1, 2, 3]
1 [1, 2, 3] abc pqr [1, 2, 3] 1 abc pqr 1 abc [1, 2, 3] pqr 1xyz[1, 2, 3]xyzabc
[1, 2, 3]xyz1xyzabc
1xyzabcxyz[1, 2, 3]
1xyz[1, 2, 3]xyzabcpqr[1, 2, 3]xyz1xyzabcpqr1xyzabcxyz[1, 2, 3]pqr[1, 2]xyz[3, 4]xyz1
(1, 2, 3) [1, 2] [3, 4]pqr(lf) anutosh491@spbhat68:~/lpython/lpython$ python examples/expr2.py 
1xyz(1, 2, 3)xyzabcpqr(1, 2, 3)xyz1xyzabcpqr1xyz[1, 2]xyz(1, 2, 3)pqr1 [1, 2, 3] abc
[1, 2, 3] 1 abc
1 abc [1, 2, 3]
1 [1, 2, 3] abc pqr [1, 2, 3] 1 abc pqr 1 abc [1, 2, 3] pqr 1xyz[1, 2, 3]xyzabc
[1, 2, 3]xyz1xyzabc
1xyzabcxyz[1, 2, 3]
1xyz[1, 2, 3]xyzabcpqr[1, 2, 3]xyz1xyzabcpqr1xyzabcxyz[1, 2, 3]pqr[1, 2]xyz[3, 4]xyz1
(1, 2, 3) [1, 2] [3, 4]pqr(lf) anutosh491@spbhat68:~/lpython/lpython$

These are like some random cases I thought and none of these work on master !

@anutosh491
Copy link
Collaborator Author

I'll pick some tests from the above shown function and add an integration file soon @Thirumalai-Shaktivel !

@Thirumalai-Shaktivel
Copy link
Collaborator

I think we have to add runtime tests.
@certik @czgdp1807 What do you say?

@Thirumalai-Shaktivel
Copy link
Collaborator

Great job, @anutosh491. Thanks for fixing this.

@czgdp1807
Copy link
Collaborator

I think we have to add runtime tests.

Could you please elaborate more? If you are talking about adding a file in integration_test, then I guess @anutosh491 has said he will do it soon (#1512 (comment)). Is there anything more to this? If not then I am marking it as draft.

@czgdp1807 czgdp1807 marked this pull request as draft February 11, 2023 05:24
@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Feb 11, 2023

I was talking about saving the runtime output using tests.toml: run

@czgdp1807
Copy link
Collaborator

I see. That's very much needed. For printing we can't use assert statements. However I guess, a better way is that print function should call str over its inputs and then just send the output of str for printing. That way we will be able to do assert with str calls and no need to store reference files for outputs.

@anutosh491
Copy link
Collaborator Author

I think we have to add runtime tests.
I was talking about saving the runtime output using tests.toml: run
I see. That's very much needed. For printing we can't use assert statements. However I guess, a better way is that print function should call str over its inputs and then just send the output of str for printing. That way we will be able to do assert with str calls and no need to store reference files for outputs.

I think I am not fully sure how do I address these comments . I get that we could use the assert keyword if we make use of str() function , but where and what sort of tests am I suppossed to add is not very clear. Any references I could look into ?

@anutosh491
Copy link
Collaborator Author

I think I am not fully sure how do I address these comments . I get that we could use the assert keyword if we make use of str() function , but where and what sort of tests am I suppossed to add is not very clear. Any references I could look into ?

cc @Thirumalai-Shaktivel

@Thirumalai-Shaktivel
Copy link
Collaborator

ping @czgdp1807

@czgdp1807
Copy link
Collaborator

I think I am not fully sure how do I address these comments . I get that we could use the assert keyword if we make use of str() function , but where and what sort of tests am I suppossed to add is not very clear. Any references I could look into ?

Nothing to worry about here. Just add normal print statements with constant strings and register only in tests.toml with asr=true, ast=true. That's all. No need to worry about printing of runtime strings.

@anutosh491 anutosh491 marked this pull request as ready for review February 21, 2023 06:30
@Thirumalai-Shaktivel
Copy link
Collaborator

LGTM! But, Let @czgdp1807 also review and approve this.
Also, It seems you made changes to print_arr.cpp, do you wanna add tests (in test_sep_end_kws.py) for it?

@anutosh491
Copy link
Collaborator Author

Also, It seems you made changes to print_arr.cpp

Yess I'll be updating my integration test file with tests for printing arrays . Just want to get this output confirmed (#1512 (comment)) before I go on with that ! The changes makes printing arrays compatible with other data types/containers including arrays along with the end and sep keywords !

@certik
Copy link
Contributor

certik commented Feb 25, 2023

Does CPython and LPython print exactly the same thing, or does it differ?

@anutosh491
Copy link
Collaborator Author

anutosh491 commented Feb 25, 2023

  1. For containers like lists/tuples and other fundamental data types, both outputs are the same while using end and sep keyword . For example (Fixed compatibility of sep, end keyword with list/tuple containers #1512 (comment)) as shown in PR description

  2. For arrays though , they do differ

For something like

from ltypes import i32
from numpy import empty, int32

def main0():
    x: i32 = 2
    arr1: i32[x] = empty(x, dtype=int32)
    arr2: i32[x] = empty(x, dtype=int32)
    arr1[0] = 100
    arr1[1] = 200
    arr2[0] = 300
    arr2[1] = 400
    print(arr1, arr2)
    print(arr1, arr2, sep="abc")
    print(arr1, arr2, end="pqr\n")
    print(arr1, arr2, sep="abc", end="pqr\n")

main0()

The CPython output is

[100 200] [300 400]
[100 200]abc[300 400]
[100 200] [300 400]pqr
[100 200]abc[300 400]pqr

But the lpython output is

100 200 
300 400 
100 200abc300 400 
100 200 
300 400pqr
100 200abc300 400pqr

Differences are

  1. We haven't incorporated square brackets
  2. Our default separator between 2 arrays is a newline rather than enclosing the array elements with brackets and using space in between !

You coudl go through the thread (#1512 (comment)) where we have been discussing this . If you feel something more needs to be done for arrays , I can maybe open up a subsequent pr for it because I am quite confident about the initial changes which were mentioned in the issue description (#1512 (comment))

@anutosh491
Copy link
Collaborator Author

cc @certik

@certik
Copy link
Contributor

certik commented Feb 25, 2023

I left some more comments. Otherwise I think this can be merged, and subsequent PRs can improve the rest.

@anutosh491
Copy link
Collaborator Author

anutosh491 commented Feb 26, 2023

I've removed the ast based reference files which wouldn't be required anymore . I think now this maybe good to go !

@anutosh491 anutosh491 requested a review from certik February 26, 2023 02:17
Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

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

I think that this is fine for now. Squashing and merging.

@certik certik merged commit c1563c7 into lcompilers:main Feb 26, 2023
@anutosh491 anutosh491 deleted the branch03 branch February 27, 2023 03:02
@anutosh491
Copy link
Collaborator Author

Thanks @Thirumalai-Shaktivel @czgdp1807 and @certik for the great reviews on this one !

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.

Variable of list[i32] type has unnecessary newline character while printing.
5 participants