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

write text files in pythran fails #1699

Closed
mgirardis opened this issue Jan 3, 2021 · 6 comments · Fixed by #1701
Closed

write text files in pythran fails #1699

mgirardis opened this issue Jan 3, 2021 · 6 comments · Fixed by #1701

Comments

@mgirardis
Copy link

In its manual page, pythran says it supports the method write from TextIOWrapper

However, trying to compile this simple file

file: mylib.py

#pythran export write_test(str,bool)
#pythran export fake_write(str)

def write_test(fname,r):
    if r:
        print('writing %s'%fname)
        f = open(fname,'w')
        #write_line = f.writelines
        write_line = f.write
    else:
        print('NO FILE WILL BE WRITTEN')
        write_line = fake_write
    for i in range(10):
        write_line(str(i) + '\n')
    if r:
        f.close()

def fake_write(s):
    return 0

with the command line

pythran mylib.py -o mylib.so -O3 -march=native -v

fails with the message:

mylib.py:9:21 error: Unsupported attribute 'write' for this object

Pythran version: 0.9.8.post2

Python version: 3.8.5

Using Ubuntu 20.04.1 LTS

@serge-sans-paille
Copy link
Owner

Pythran indeed supports the write method from TextIOWrapper. There's a bug elsewhere, let me investigate, and thanks for reporting!

@serge-sans-paille
Copy link
Owner

Ok, two problems spotted. First pythran doesn't support bound methods as it should. Second pythran's write method doen't return an integer. I'll fix these two, until then, the following works:

#pythran export write_test(str,bool)
#pythran export fake_write(str)

def write_test(fname,r):
    if r:
        print('writing %s'%fname)
        f = open(fname,'w')
        #write_line = f.writelines
        write_line = lambda s: f.write(s)
    else:
        print('NO FILE WILL BE WRITTEN')
        write_line = fake_write
    for i in range(10):
        write_line(str(i) + '\n')
    if r:
        f.close()

def fake_write(s):
    return None

serge-sans-paille added a commit that referenced this issue Jan 4, 2021
serge-sans-paille added a commit that referenced this issue Jan 4, 2021
serge-sans-paille added a commit that referenced this issue Jan 4, 2021
Using existing support for partial functions.

Fix #1699
@serge-sans-paille
Copy link
Owner

@mgirardis can you confirm #1701 fixes your issue?

@mgirardis
Copy link
Author

mgirardis commented Jan 4, 2021

@serge-sans-paille I'm not the most savvy with github, here is what I've done:

Yesterday, after posting the issue, I uninstalled the pythran 0.9.8.post2 and cloned this repository to try and see if the issue was still there in the 0.9.9.dev... And the error persisted...

so now after you posted this fix, I did the following

git pull

in the pythran clone directory I have in my local machine

then, I installed pythran (I did NOT uninstall the previous pythran 0.9.9.dev)

python setup.py install --user

and I still have the same error with the code I posted... The code you suggested works fine, though (returning None with the lambda function).

Maybe I need to do something different from what I said here to fix the issue in my local copy?

@serge-sans-paille
Copy link
Owner

Almost there! The PR was based on branch feature/support-bound-method so you had to git checkout feature/support-bound-method first. The install step was correct.

@mgirardis
Copy link
Author

Great! It worked now :) thanks! and keep up the good work!

serge-sans-paille added a commit that referenced this issue Jan 5, 2021
Using existing support for partial functions.

Fix #1699
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 a pull request may close this issue.

2 participants