-
Notifications
You must be signed in to change notification settings - Fork 170
Initial support for @pythoncall decorator #1863
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8f5894a
ASR: Rename c_header to module_file
Shaikh-Ubaid 7599f19
ASR: Support BindPython abiType
Shaikh-Ubaid f999cc6
C_CPP: Support BindPython Function
Shaikh-Ubaid bb4c39e
C_CPP: Support args and return val for simple types
Shaikh-Ubaid 70b384c
Add --enable-cpython flag
Shaikh-Ubaid be4b110
C_CPP: Initialize and Finalize Py Env Once
Shaikh-Ubaid 2d0a738
lpython.py: Support @ pythoncall() decorator
Shaikh-Ubaid b637e27
TEST: Support ENABLE_CPYTHON in integration_tests
Shaikh-Ubaid 0a0495b
TEST: Add test for @ pythoncall()
Shaikh-Ubaid 3a62f41
TEST: Update reference tests
Shaikh-Ubaid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from lpython import i32, i64, u32, u64, f32, f64, pythoncall | ||
|
||
@pythoncall(module = "bindpy_01_module") | ||
def add_ints(a: i32, b: i64, c: u32, d: u64) -> i64: | ||
pass | ||
|
||
@pythoncall(module = "bindpy_01_module") | ||
def multiply_ints(a: i32, b: i64, c: u32, d: u64) -> i64: | ||
pass | ||
|
||
@pythoncall(module = "bindpy_01_module") | ||
def add_floats(a: f32, b: f64) -> f64: | ||
pass | ||
|
||
@pythoncall(module = "bindpy_01_module") | ||
def multiply_floats(a: f32, b: f64) -> f64: | ||
pass | ||
|
||
@pythoncall(module = "bindpy_01_module") | ||
def get_hello_world(a: str, b: str) -> str: | ||
pass | ||
|
||
@pythoncall(module = "bindpy_01_module") | ||
def str_n_times(a: str, n: i32) -> str: | ||
pass | ||
|
||
@pythoncall(module = "bindpy_01_module") | ||
def get_cpython_version() -> str: | ||
pass | ||
|
||
# Integers: | ||
def test_ints(): | ||
i: i32 | ||
j: i64 | ||
k: u32 | ||
l: u64 | ||
i = -5 | ||
j = i64(24) | ||
k = u32(20) | ||
l = u64(92) | ||
|
||
assert add_ints(i, j, k, l) == i64(131) | ||
assert multiply_ints(i, j, k, l) == i64(-220800) | ||
|
||
# Floats | ||
def test_floats(): | ||
a: f32 | ||
b: f64 | ||
a = f32(3.14) | ||
b = -100.00 | ||
|
||
assert abs(add_floats(a, b) - (-96.86)) <= 1e-4 | ||
assert abs(multiply_floats(a, b) - (-314.0)) <= 1e-4 | ||
|
||
# Strings | ||
def test_strings(): | ||
a: str | ||
b: str | ||
c: str | ||
i: i32 | ||
a = "hello" | ||
b = "world" | ||
i = 3 | ||
|
||
assert get_hello_world(a, b) == "hello world!" | ||
assert str_n_times(a, i) == "hellohellohello" | ||
assert get_hello_world(str_n_times(a, i), b) == "hellohellohello world!" | ||
|
||
def main0(): | ||
print("CPython version: ", get_cpython_version()) | ||
|
||
test_ints() | ||
test_floats() | ||
test_strings() | ||
|
||
|
||
main0() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
def get_cpython_version(): | ||
import platform | ||
return platform.python_version() | ||
|
||
def add_ints(a, b, c, d): | ||
e = a + b + c + d | ||
return e | ||
|
||
def multiply_ints(a, b, c, d): | ||
e = a * b * c * d | ||
return e | ||
|
||
def add_floats(a, b): | ||
return a + b | ||
|
||
def multiply_floats(a, b): | ||
return a * b | ||
|
||
def get_hello_world(a, b): | ||
return f"{a} {b}!" | ||
|
||
def str_n_times(a, n): | ||
return a * n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1246,6 +1246,11 @@ int link_executable(const std::vector<std::string> &infiles, | |
cmd += " -I " + rtlib_header_dir; | ||
cmd += " -L" + base_path | ||
+ " -Wl,-rpath," + base_path + " -l" + runtime_lib + " -lm"; | ||
if (compiler_options.enable_cpython) { | ||
std::string py_version = "3.10"; | ||
std::string py_flags = R"(-I $CONDA_PREFIX/include/python)" + py_version + R"( -L$CONDA_PREFIX/lib -Wl,-rpath -Wl,$CONDA_PREFIX/lib -lpython)" + py_version + R"()"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's fine for now. Conda is currently required. |
||
cmd += " " + py_flags; | ||
} | ||
int err = system(cmd.c_str()); | ||
if (err) { | ||
std::cout << "The command '" + cmd + "' failed." << std::endl; | ||
|
@@ -1556,6 +1561,7 @@ int main(int argc, char *argv[]) | |
app.add_flag("--get-rtl-header-dir", print_rtl_header_dir, "Print the path to the runtime library header file"); | ||
app.add_flag("--get-rtl-dir", print_rtl_dir, "Print the path to the runtime library file"); | ||
app.add_flag("--verbose", compiler_options.verbose, "Print debugging statements"); | ||
app.add_flag("--enable-cpython", compiler_options.enable_cpython, "Enable CPython runtime"); | ||
|
||
// LSP specific options | ||
app.add_flag("--show-errors", show_errors, "Show errors when LSP is running in the background"); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.