Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 853179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:43:03+00:00 2026-05-15T07:43:03+00:00

I’m trying to create a binding to libpython using scheme’s FFI. To do this,

  • 0

I’m trying to create a binding to libpython using scheme’s FFI. To do this, I have to get the location of python, create the ffi-lib, and then create functions from it. So for instance I could do this:

(module pyscheme scheme
  (require foreign)
  (unsafe!)

  (define (link-python [lib "/usr/lib/libpython2.6.so"])
    (ffi-lib lib))

This is all well and good, but I can’t think of a way to export functions. For instance, I could do something like this:

(define Py_Initialize (get-ffi-obj "Py_Initialize" libpython (_fun -> _void)))

…but then I’d have to store a reference to libpython (created by link-python) globally somehow. Is there any way to export these functions once link-python is called? In other words, I’d like someone using the module to be able to do this:

(require pyscheme)
(link-python)
(Py_Initialize)

…or this:

(require pyscheme)
(link-python "/weird/location/for/libpython.so")
(Py_Initialize)

…but have this give an error:

(require pyscheme)
(Py_Initialize)

How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:43:04+00:00Added an answer on May 15, 2026 at 7:43 am

    Probably the easiest way to do something like this is to delay the binding until they’re needed. Something like this (untested) code:

    #lang scheme
    
    (require scheme/foreign)
    (unsafe!)
    
    (define libpython #f)
    
    (define (link-python [lib "/usr/lib/libpython2.6.so"])
      (if libpython
        (error "Foo!")
        (begin (set! libpython (ffi-lib lib))
               (set! Py_Initialize
                     (get-ffi-obj "Py_Initialize" libpython
                                  (_fun -> _void))))))
    
    (define (Py_Initialize . args)
      (error 'Py_Initialize "python not linked yet"))
    

    Ir you can do the setting inside the function itself, so you don’t bind functions that are never called:

    #lang scheme
    
    (require scheme/foreign)
    (unsafe!)
    
    (define libpython #f)
    
    (define (link-python [lib "/usr/lib/libpython2.6.so"])
      (if libpython (error "Foo!") (set! libpython (ffi-lib lib))))
    
    (define (Py_Initialize . args)
      (if libpython
        (begin (set! Py_Initialize
                     (get-ffi-obj "Py_Initialize" libpython
                                  (_fun -> _void)))
               (apply Py_Initialize args))
        (error 'Py_Initialize "python not linked yet")))
    

    and since you won’t want to do this for every single function, you should wrap it in a macro:

    #lang scheme
    
    (require scheme/foreign)
    (unsafe!)
    
    (define libpython #f)
    
    (define (link-python [lib "/usr/lib/libpython2.6.so"])
      (if libpython (error "Foo!") (set! libpython (ffi-lib lib))))
    
    (define-syntax-rule (defpython <name> type)
      (define (<name> . args)
        (if libpython
          (begin (set! <name> (get-ffi-obj '<name> libpython <type>))
                 (apply <name> args))
          (error '<name> "python not linked yet"))))
    
    (defpython Py_Initialize (_fun -> _void))
    (defpython Py_Foo (_fun _int _int -> _whatever))
    ...more...
    

    But two highlevel notes:

    • Even though it’s possible, it seems ugly to delay things this way. I’d rather use some environment variable that is known when the code starts.

    • There have been an attempt in the past to link plt scheme to python, and IIRC, dealing with memory issues was not pleasant. (But this is before we had the current foreign system in place.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 418k
  • Answers 418k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to do this inline: dict["key"] = dict.ContainsKey("key")… May 15, 2026 at 9:59 am
  • Editorial Team
    Editorial Team added an answer Go to Tools > Options > Text Editor > C#… May 15, 2026 at 9:59 am
  • Editorial Team
    Editorial Team added an answer The general case must appear before the specializations, otherwise the… May 15, 2026 at 9:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.