1

I'm working with pythran, a Python to c++ compiler http://pythran.readthedocs.io/

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

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Girardi
  • 2,734
  • 3
  • 35
  • 50

1 Answers1

0

There seems to be a bug in the current version of pythran. It was fixed in the development version of pythran (0.9.9.dev).

Instead of using a pointer to the f.write function, we can define a lambda with no return that does the job and solves the problem:

#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

this modification was advised by the developers in their github bug report page.

Girardi
  • 2,734
  • 3
  • 35
  • 50