8000 _ctypes pt. 3 by arihant2math · Pull Request #5530 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

_ctypes pt. 3 #5530

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 10 commits into from
Mar 3, 2025
Merged

_ctypes pt. 3 #5530

merged 10 commits into from
Mar 3, 2025

Conversation

arihant2math
Copy link
Collaborator
@arihant2math arihant2math commented Feb 20, 2025

Here comes the meat of the code and the hardest part implementation-wise.
The main goal is to implement (atleast partially) CFuncPtr. By partially, I mean that not supporting arguments to functions is probably fine. At any rate the cpython implementation can be used as a reference since they use libffi. The main challenge is determining the return type of the loaded function from the cvoid it'll return (unless we don't have to do that for some reason).

  • Use libffi to manage functions (Can't pass in arbitrary arguments to functions through libloading)
  • Constructor for CFuncPtr
  • Give inner function class the ability to load functions
  • Give it the ability to call them and get the return
  • Cast the return properly
  • Callable for CFuncPtr that encapsulated the entire thing

The next pr can probably just do some cleanup on this and get the basics for struct and union return types working.

Known issues

  • Arguments cause errors to be thrown
  • Return type of functions is c_int and cannot be changed
  • Error handling isn't great.
  • import ctypes fails
  • creating string buffers is broken

@arihant2math
Copy link
Collaborator Author

Some updates:

I've gotten arbitrary function argument passing to work on a test, I still am figuring out return type inference however.
Here is some test code for windows that works:

use std::ffi::c_void;
use std::path::Path;
use libffi::middle as libffi;
use libloading::Symbol;

type FP = unsafe extern "C" fn ();

fn main() {
    let path = "C:\\Windows\\System32\\kernel32.dll";
    let path = Path::new(path);
    let lib = unsafe { libloading::Library::new(path) }.unwrap();
    let function_bytes = b"Sleep\0";
    let function: Symbol<FP> = unsafe { lib.get(function_bytes) }.unwrap();
    let function = *function;
    let args = vec![libffi::Type::u32()];
    let cif = libffi::Cif::new(args.into_iter(), libffi::Type::void());
    let ms_wait: u32 = 5000;
    println!("Calling function");
    let now = std::time::Instant::now();
    let _res: *const c_void = unsafe { cif.call(libffi::CodePtr(function as *mut _), &[libffi::arg(&ms_wait)]) };
    println!("Function returned in {}ms", now.elapsed().as_millis());
    println!("Function called");
}
Calling function
Function returned in 5001ms
Function called

@arihant2math
Copy link
Collaborator Author

Nevermind, it seems like return type needs to be manually specified and the default is cint

@arihant2math arihant2math marked this pull request as ready for review February 28, 2025 01:25
@arihant2math
Copy link
Collaborator Author

Ready for review.
The current working scope is no-argument functions that return integers.

(this is from the extra test)

libc = cdll.msvcrt
print("rand", libc.rand())

/cc @youknowone

@arihant2math arihant2math mentioned this pull request Feb 28, 2025
28 tasks
@arihant2math arihant2math force-pushed the ctypes-pt3 branch 5 times, most recently from 3115d8a to 06dbae4 Compare February 28, 2025 04:59
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Copy link
Member
@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

Looks good, thank you so much!
Please check if the extra indent is intended or not.

10000
@arihant2math
Copy link
Collaborator Author

Done.

@arihant2math arihant2math requested a review from youknowone March 3, 2025 04:12
Copy link
Member
@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

👍

Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com>
@arihant2math arihant2math requested a review from youknowone March 3, 2025 05:02
@youknowone youknowone merged commit 40e3f49 into RustPython:main Mar 3, 2025
11 checks passed
@arihant2math arihant2math deleted the ctypes-pt3 branch March 3, 2025 10:54
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.

2 participants
0