Skip to content

ctypes and cffi? #211

Closed
Closed

Description

@paugier

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?

Activity

  1. leouieda commented on Jul 14, 2018

    @leouieda
    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.

  2. added
    question
    on Jul 14, 2018
  3. seisman commented on Mar 15, 2021

    @seisman
    Member

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

  4. weiji14 commented on Mar 15, 2021

    @weiji14
    Member

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

  5. added
    wontfix
    on Mar 15, 2021
  6. seisman commented on Feb 28, 2024

    @seisman
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionwontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions