Skip to content
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

ctypes and cffi? #211

Closed
paugier opened this issue Jul 12, 2018 · 4 comments
Closed

ctypes and cffi? #211

paugier opened this issue Jul 12, 2018 · 4 comments
Labels
question Further information is requested wontfix This will not be worked on

Comments

@paugier
Copy link

paugier commented Jul 12, 2018

It is just a small and fast question. I see in the documentation: "Interface with the GMT C API directly using ctypes (no system calls)."

I understood that now, cffi was just better than ctypes for new projects (see for example https://www.quora.com/How-can-I-decide-between-using-ctypes-and-CFFI-in-Python).

I was wondering if there is a reason why gmt-python doesn't use cffi instead of ctypes?

@leouieda
Copy link
Member

Hi @paugier, thanks for the link! I hadn't seen that post.

Basically, I didn't really know about CFFI until recently. I've been having a good experience with ctypes and numpy arrays have builtin support for it. Right now, I don't know if it's worth the trouble of switching if ctypes is working. But I'm open to suggestions if there is anything specific that CFFI can address.

Thanks for taking the time to give feedback! I really appreciate it.

@leouieda leouieda added the question Further information is requested label Jul 14, 2018
@seisman
Copy link
Member

seisman commented Mar 15, 2021

Can we close this 2.5-year-old issue? I don't think we will switch to cffi in the next few years.

@weiji14
Copy link
Member

weiji14 commented Mar 15, 2021

Yep, close it as wontfix. But feel free to reopen if someone wants to work on it 🙂

@weiji14 weiji14 closed this as completed Mar 15, 2021
@weiji14 weiji14 added the wontfix This will not be worked on label Mar 15, 2021
@seisman
Copy link
Member

seisman commented Feb 28, 2024

Just tried cffi. The following example shows how to call the API function GMT_Get_Enum:

>>> from cffi import FFI
>>> ffi = FFI()
>>> ffi.cdef("int GMT_Get_Enum (void *V_API, char *key);")
>>> lib = ffi.dlopen("libgmt.dylib")
>>> lib.GMT_Get_Enum(ffi.NULL, b"GMT_VF_LEN")
32

For comparison, the ctypes version is

pygmt/pygmt/clib/session.py

Lines 263 to 280 in 5014591

c_get_enum = self.get_libgmt_func(
"GMT_Get_Enum", argtypes=[ctp.c_void_p, ctp.c_char_p], restype=ctp.c_int
)
# The C lib introduced the void API pointer to GMT_Get_Enum so that
# it's consistent with other functions. It doesn't use the pointer so
# we can pass in None (NULL pointer). We can't give it the actual
# pointer because we need to call GMT_Get_Enum when creating a new API
# session pointer (chicken-and-egg type of thing).
session = None
value = c_get_enum(session, name.encode())
if value is None or value == -99999:
raise GMTCLibError(f"Constant '{name}' doesn't exist in libgmt.")
return value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants