GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — _ctypes pt. 3 by arihant2math · Pull Request #5530 · RustPython/RustPython

Conversation

@arihant2math

@arihant2math arihant2math commented Feb 20, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@arihant2math
arihant2math force-pushed the ctypes-pt3 branch from ab1f597 to 856bbd5 Compare February 27, 2025 18:15
@arihant2math

arihant2math commented Feb 27, 2025

Copy link
Copy Markdown
Contributor 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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arihant2math

arihant2math commented Feb 27, 2025

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

arihant2math commented Feb 28, 2025

Copy link
Copy Markdown
Contributor 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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arihant2math arihant2math mentioned this pull request Feb 28, 2025
Tracking issue for ctypes #5572
Closed
15 of 28 tasks
@arihant2math
arihant2math force-pushed the ctypes-pt3 branch 5 times, most recently from 3115d8a to 06dbae4 Compare February 28, 2025 04:59
arihant2math added 5 commits March 1, 2025 21:42
@arihant2math
Initial CFuncPtr implementation
b42520e
Signed-off-by: Ashwin Naren <[email protected]>
@arihant2math
function calling via libffi
885a7ab
Signed-off-by: Ashwin Naren <[email protected]>
@arihant2math
working no-arg function call
5bfcccd
Signed-off-by: Ashwin Naren <[email protected]>
@arihant2math
fix warnings
afd1f64
Signed-off-by: Ashwin Naren <[email protected]>
@arihant2math
error handling
aeba7ad
Signed-off-by: Ashwin Naren <[email protected]>
@arihant2math
arihant2math force-pushed the ctypes-pt3 branch from 06dbae4 to 085e5b4 Compare March 2, 2025 05:43
@arihant2math
final CI fixes
3871150
Signed-off-by: Ashwin Naren <[email protected]>
@arihant2math
arihant2math force-pushed the ctypes-pt3 branch from ff4a992 to 3871150 Compare March 2, 2025 05:48
youknowone
youknowone reviewed Mar 3, 2025
View reviewed changes

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread vm/src/stdlib/mod.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@arihant2math
fix overindent
2174d76
@arihant2math

arihant2math commented Mar 3, 2025

Copy link
Copy Markdown
Contributor Author

Done.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arihant2math
arihant2math requested a review from youknowone March 3, 2025 04:12
@arihant2math
formatting
b14dba6
youknowone
youknowone approved these changes Mar 3, 2025
View reviewed changes

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
youknowone
youknowone requested changes Mar 3, 2025
View reviewed changes
Comment thread vm/src/stdlib/ctypes/function.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@arihant2math @youknowone
Update vm/src/stdlib/ctypes/function.rs
00bed71
Co-authored-by: Jeong, YunWon <[email protected]>
@arihant2math
arihant2math requested a review from youknowone March 3, 2025 05:02
@arihant2math
fix clippy
9e9c041
youknowone
youknowone approved these changes Mar 3, 2025
View reviewed changes
@youknowone
youknowone merged commit 40e3f49 into RustPython:main Mar 3, 2025
@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

Reviewers

@youknowone youknowone youknowone approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@arihant2math @youknowone
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.