pythran icon indicating copy to clipboard operation
pythran copied to clipboard

[BUG] Possible bug in optional argument parsing

Open Chronum94 opened this issue 5 years ago • 4 comments

In file transition_analyzer.py, which gets compiled to transition_analyzer.so:

import numpy as np

#pythran export transition_analyzer(float[:, :], float, float)
#pythran export transition_analyzer(float[:, :], float)
def transition_analyzer(eigenvalues, qshift=0.0, fermi_level=-0.35):
    rows, cols = eigenvalues.shape
    # eigenvalues = np.asfortranarray(eigenvalues)
    print(rows, cols)
    just_above_fermi_location = np.empty(cols, dtype=np.int32)

    for i in range(cols):
        just_above_fermi_location[i] = np.argwhere(eigenvalues[:, i] > fermi_level)[-1][0]
    x = []
    #pragma omp parallel for
    for k1 in range(cols):
        for k2 in range(cols):
            for energy_of_filled_band in range(rows-1, just_above_fermi_location[k1], -1):
                a = eigenvalues[:just_above_fermi_location[k2] + 1, k2] - eigenvalues[energy_of_filled_band, k1]
                x.append(a)
    print("Done!")
    return x
    # print(just_above_fermi_location)

In file tester.py

import numpy as np
from transition_analyzer import transition_analyzer

np.random.seed(1)
a = np.linspace(-5, 2, 65)[::-1, None].repeat(40, axis=1)
transition_analyzer(a, qshift=-0.0)
transition_analyzer(a, fermi_level=-0.2)

In file tester.py, the first call works, the second call fails.

The error message is:

65 40
Done!
Traceback (most recent call last):
  File "tester.py", line 7, in <module>
    transition_analyzer(a, fermi_level=-0.2)

TypeError: Invalid call to pythranized function `transition_analyzer(ndarray, fermi_level=float)'
Candidates are:
   transition_analyzer(float[:,:],float)
   transition_analyzer(float[:,:].T,float)
   transition_analyzer(float[:,:],float,float)
   transition_analyzer(float[:,:].T,float,float)

Chronum94 avatar Mar 11 '19 14:03 Chronum94

I fail to reproduce on master + python3 :-/

serge-sans-paille avatar Mar 17 '19 08:03 serge-sans-paille

In file pythtest.py which gets compiled to pythtest.so:

#pythran export pythtest(float)
#pythran export pythtest(float, float)
#pythran export pythtest(float, float, float)
def pythtest(a, b=10.0, c=10.0):
    return a + b + c

File pythtest_driver.py:

from pythtest import pythtest as pt

print(pt(1.0))
print(pt(1.0, c=3.0))

I've used the old optional syntax with multiple exports. On running this with: pythran pythtest.py;python pythtest_driver.py I get

21.0
Traceback (most recent call last):
  File "pythtest_driver.py", line 4, in <module>
    print(pt(1.0, c=3.0))
TypeError: Invalid call to pythranized function `pythtest(float, c=float)'
Candidates are:

    - pythtest(float, float, float)
    - pythtest(float, float)
    - pythtest(float)

Chronum94 avatar Mar 17 '19 17:03 Chronum94

Thanks for the test case, I can now reproduce o/

serge-sans-paille avatar Apr 03 '19 11:04 serge-sans-paille

I now understand the error: the first export maps to pythtest(a), the second to pythtest(a, b) and the third to pythtest(a, b, c).

There's currently no way for pythran to understand that you want the default value for b.

serge-sans-paille avatar Apr 03 '19 12:04 serge-sans-paille