Passing a memory address (pointer) to an extension?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip Semanchuk

    Passing a memory address (pointer) to an extension?

    I'm writing a Python extension in C that wraps a function which takes
    a void * as a parameter. (The function is shmat() which attaches a
    chunk of shared memory to the process at the address supplied by the
    caller.) I would like to expose this function to Python, but I don't
    know how to define the interface.

    Specifically, when calling PyArg_ParseTupl e(), what letter should I
    use to represent the pointer in the format string? The best idea I can
    come up with is to use a long and then cast it to a void *, but
    assuming that a long is big enough to store a void * is a shaky
    assumption. I could use a long long (technically still risky, but
    practically probably OK) but I'm not sure how widespread long longs are.

    Any advice appreciated.

    Thanks
    Philip
  • John Nagle

    #2
    Re: Passing a memory address (pointer) to an extension?

    Philip Semanchuk wrote:
    I'm writing a Python extension in C that wraps a function which takes a
    void * as a parameter. (The function is shmat() which attaches a chunk
    of shared memory to the process at the address supplied by the caller.)
    I would like to expose this function to Python, but I don't know how to
    define the interface.
    Use message passing, not shared memory. CPython is so slow that you'll
    never notice any performance gain from using shared memory.

    Also, if you're asking this question, you probably don't know enough
    to keep out of trouble in this area. You need to thoroughly understand
    concurrency, locking, shared memory locking, the Python Global Interpreter
    Lock, Python memory management, and the implications of the interactions
    between all of them. Otherwise you'll get race conditions, crashes within
    the Python system, lockups, and/or terrible performance.

    And none of the usual debugging tools for Python will help you.

    John Nagle

    Comment

    • Robert Kern

      #3
      Re: Passing a memory address (pointer) to an extension?

      John Nagle wrote:
      Philip Semanchuk wrote:
      >I'm writing a Python extension in C that wraps a function which takes
      >a void * as a parameter. (The function is shmat() which attaches a
      >chunk of shared memory to the process at the address supplied by the
      >caller.) I would like to expose this function to Python, but I don't
      >know how to define the interface.
      >
      Use message passing, not shared memory. CPython is so slow that you'll
      never notice any performance gain from using shared memory.
      >
      Also, if you're asking this question, you probably don't know enough
      to keep out of trouble in this area. You need to thoroughly understand
      concurrency, locking, shared memory locking, the Python Global Interpreter
      Lock, Python memory management, and the implications of the interactions
      between all of them. Otherwise you'll get race conditions, crashes within
      the Python system, lockups, and/or terrible performance.
      >
      And none of the usual debugging tools for Python will help you.
      I'm pretty sure the OP is fairly aware of the issues involved in shared memory
      and the rest.



      He's asking about a detail of Python C API for parsing function arguments. His
      ignorance in that area doesn't entail ignorance in other matters. You're being
      abusive without cause.

      --
      Robert Kern

      "I have come to believe that the whole world is an enigma, a harmless enigma
      that is made terrible by our own mad attempt to interpret it as though it had
      an underlying truth."
      -- Umberto Eco

      Comment

      • Thomas Heller

        #4
        Re: Passing a memory address (pointer) to an extension?

        Philip Semanchuk schrieb:
        I'm writing a Python extension in C that wraps a function which takes
        a void * as a parameter. (The function is shmat() which attaches a
        chunk of shared memory to the process at the address supplied by the
        caller.) I would like to expose this function to Python, but I don't
        know how to define the interface.
        >
        Specifically, when calling PyArg_ParseTupl e(), what letter should I
        use to represent the pointer in the format string? The best idea I can
        come up with is to use a long and then cast it to a void *, but
        assuming that a long is big enough to store a void * is a shaky
        assumption. I could use a long long (technically still risky, but
        practically probably OK) but I'm not sure how widespread long longs are.
        I suggest "O!" and a converter function calling PyLong_AsVoidPt r().

        Thomas

        Comment

        • Philip Semanchuk

          #5
          Re: Passing a memory address (pointer) to an extension?


          On Oct 23, 2008, at 2:13 AM, Thomas Heller wrote:
          Philip Semanchuk schrieb:
          >I'm writing a Python extension in C that wraps a function which takes
          >a void * as a parameter. (The function is shmat() which attaches a
          >chunk of shared memory to the process at the address supplied by the
          >caller.) I would like to expose this function to Python, but I don't
          >know how to define the interface.
          >>
          >Specifically , when calling PyArg_ParseTupl e(), what letter should I
          >use to represent the pointer in the format string? The best idea I
          >can
          >come up with is to use a long and then cast it to a void *, but
          >assuming that a long is big enough to store a void * is a shaky
          >assumption. I could use a long long (technically still risky, but
          >practically probably OK) but I'm not sure how widespread long longs
          >are.
          >
          I suggest "O!" and a converter function calling PyLong_AsVoidPt r().

          Thomas, this sounds perfect, thank you.


          bye
          Philip

          Comment

          Working...