Re: Migrating PyCups to Python CFFI for libcups 3.x
Zdenek Dohnal <[email protected]> Thu, 3 Apr 2025 10:52:37 +0200
| Newsgroups | dev.linux.lists.printing-architecture |
|---|---|
| Message-ID | <[email protected]> |
Sounds good! cffi is present at least in Centos, so I don't have a
dependency issue - I'm not sure about other maintainers (added Johannes
and Thorsten I know of).
What we have to make sure (based on the example) that path to .so file
is not hardcoded, but configurable during setup.
Thank you for working on this!
P.S. I see two mails in the list with almost the same content - is there
a difference?
Zdenek
On 4/2/25 15:49, Soumyadeep Ghosh wrote:
> Hello everyone,
>
> This mail is after a long discussion with Till about various different approaches and possibilities. PyCups for libcups 2.4.x was written in C using the Python's C extensions
>
> The problem with that is:
>
> 1. Very low scope of automation
> 2.Very less support from different python communities
> 3.Only source of support is the original python documentation
>
> With libcups 3.x, I am planning to move to use Python CFFI. With this, there is also a scope for automating the bindings too. For an example, how this CFFI bindings work, sharing an example here
>
> #include
>
> const char* isPrime(int a){
> int i, flag=0;
> for(i=2;i
> { if (a%i==0)
> (flag=1);
> else
> (flag=0);
> }
> if (flag==1)
> return ("Number is Non-prime.");
> else
> return ("Number is Prime.");
> }
>
> int main()
> {
> int n;
> printf("Enter any number:");
> scanf("%d",&n);
> isPrime(n);
> }
>
> This is a C function, and now, we complie this as a shared object and call this from python
>
> from cffi import FFI
>
> class DeterminePrime:
> def __init__(self, library_path="../C programme/determine_prime.so"):
> self.ffi = FFI()
>
> self.ffi.cdef("""
> const char* isPrime(int);
> """)
>
> self.lib = self.ffi.dlopen(library_path)
>
> def check_prime(self, number):
> result = self.ffi.string(self.lib.isPrime(number))
> return result.decode('utf-8')
>
>
> As one can see, we just need the signature in many cases, to call the function properly. Please let me know what you think about this change.
>
>
> Thanks and Regards,
> Soumyadeep Ghosh
>
>
--
Zdenek Dohnal
Senior Software Engineer
Red Hat, BRQ-TPBC