cpython: Bindings for libpython

[ foreign, gpl, library ] [ Propose Tags ] [ Report a vulnerability ]
Versions [RSS] 3.1.0, 3.1.1.0, 3.1.2.0, 3.1.2.1, 3.1.3, 3.1.4, 3.2.0, 3.3.0, 3.3.1, 3.4.0, 3.5.0, 3.5.1, 3.8.0, 3.9.0
Change log changelog.md
Dependencies base (>=4.0 && <5.0), bytestring (>=0.11.5 && <0.12), text (>=2.0.2 && <2.1) [details]
License GPL-3.0-only
Author John Millikin <jmillikin@gmail.com>
Maintainer Adam Zsigmond <zsedem@gmail.com>
Category Foreign
Home page https://github.com/zsedem/haskell-cpython
Source repo head: git clone https://github.com/zsedem/haskell-cpython
Uploaded by zsedem at 2024-07-08T14:39:12Z
Distributions NixOS:3.9.0
Reverse Dependencies 2 direct, 0 indirect [details]
Downloads 8745 total (26 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for cpython-3.9.0

[back to package description]

Haskell CPython C bindings Build Status

This library provides C bindings to more or less all of the python3 C API.

WARNING: Note that the python 3 C API might be quite stable, BUT the ABI is not, which means if you compiled with a certain minor version (3.7.1) you should run your program with the same minor version (3.7.x). (Using docker or nix to package your program is enough to avoid this problem)

Writing a Haskell wrapper over a Python module

The easiest way to get started is to import CPython.Simple

The Simple API surface is fairly small, so if you're doing something fancy you may need to dip into other parts of CPython

General Info

initialize :: IO () kicks off talking to Python, and will need to be called before using other functions.

The ToPy and FromPy instances are what let us convert Haskell values to and from the corresponding Python values. There are easyToPy and easyFromPy helpers to help you easily write your own instances in common cases. If you find an instance for some common Haskell type is missing, please submit a PR!

Arg is a type representing an argument to a Python function, which lets us use various Haskell types in the same list of arguments.

sampleArgs :: [Arg]
sampleArgs =
  [ arg (7 :: Integer)
  , arg ("hello" :: Text)
  ]

Calling Functions

The most common use case is to call some Python function, which we can do with call

call
  :: FromPy a
  => Text -- ^ module name
  -> Text -- ^ function name
  -> [Arg] -- ^ args
  -> [(Text, Arg)] -- ^ keyword args
  -> IO a

For example, if we wanted to wrap Python's random.randint(low, high), we could write this:

randint :: Integer -> Integer -> IO Integer
randint low high =
  call "random" "randint" [arg low, arg high] []

Because of the FromPy instance in call's type signature, we can infer what to do to convert a Python value back into Haskell, if given the type

uniform :: Integer -> Integer -> IO Double
uniform low high =
  call "random" "uniform" [arg low, arg high] []

We can also use the TypeApplications language extension to do this, if needed

call @Double "random" "uniform" [arg low, arg high] []

Calling a function with mixed positional and keyword arguments is also fairly straightforward:

moveToDuration :: Integer -> Integer -> Double -> IO ()
moveToDuration x y seconds =
  call "pyautogui" "moveTo" [arg x, arg y] [("duration", arg seconds)]

Getting and Setting Attributes

getAttribute lets us get the value of an attribute of some Python module

getAttribute
  :: FromPy a
  => Text -- ^ module name
  -> Text -- ^ attribute name
  -> IO a

Here, we get the value of random.BPF

getBpf :: IO Integer
getBpf = getAttribute "random" "BPF"

Likewise, setAttribute lets us set the value of an attribute

setAttribute
  :: ToPy a
  => Text -- ^ module name
  -> Text -- ^ attribute name
  -> a -- ^ value to set attribute to
  -> IO ()

Here's how we can set random.BPF to some given number n

setBpf :: Integer -> IO ()
setBpf n = setAttribute "random" "BPF" n

Using the Low Level API

Sometimes it might be useful to use the less simpler API, especially if you are already familiar with the CPython C API. This API comes with one-on-one connections between the C API methods and the Haskell methods, but you won't have to write FFI code directly (like calling incref/decref for Python GC).

After you are familiar with the concepts from the C API, you can search for methods in the API docs on hackage

These examples below should help you start with using the API, by showing the equivalent haskell code to implement the same as the python example.

Using builtins.sum function

sumWithPy :: [Integer] -> IO Int
sumWithPy intlist = do
    testList <- traverse toObj intlist >>= PyList.toList >>= (return . Py.toObject)
    builtinsModule <- Py.importModule "builtins"
    sumFunc <- PyUnicode.toUnicode "sum" >>= Py.getAttribute builtinsModule
    args <- PyTuple.toTuple [testList]
    kwargs <- PyDict.new
    Py.call sumFunc args kwargs >>= castToNumber >>= Py.toInteger >>= PyInt.fromInteger
  where
    castToNumber obj = do x <- Py.castToNumber obj
                          return $ fromMaybe (error "not a number returned from the sum") x
    toObj integer = fmap Py.toObject $ PyInt.toInteger integer

This example should show you how different it is to call python from strongly typed code, because you have to handle every bit of the errors, like getting an attribute of a module or just creating new python objects.

intlist = [1, 10, 100, 42]
sum(intlist)

Printing traceback from python

This example is an approach to handle python exceptions, like python would do it, so if an exception comes, we print a traceback

{-# LANGUAGE OverloadedStrings #-}
module Main
  ( main ) where

import qualified CPython as Py
import qualified CPython.Types.Module as Py
import qualified CPython.Types.Dictionary as PyDict
import qualified CPython.Types.List as PyList
import qualified CPython.Types.Tuple as PyTuple
import qualified CPython.Types.Unicode as PyUnicode
import qualified CPython.Types.Exception as PyExc
import Data.Text()
import Control.Exception(handle)

main :: IO ()
main = handle pyExceptionHandler $ do
  Py.initialize
  callingSomePython
  Py.finalize
  where
    pyExceptionHandler :: PyExc.Exception -> IO ()
    pyExceptionHandler exception = handle pyExceptionHandlerWithoutPythonTraceback $ do
        tracebackModule <- Py.importModule "traceback"
        print_exc <- PyUnicode.toUnicode "print_exception" >>= Py.getAttribute tracebackModule
        kwargs <- PyDict.new
        args <- case PyExc.exceptionTraceback exception of
          Just tb -> PyTuple.toTuple [PyExc.exceptionType exception, PyExc.exceptionValue exception, tb]
          _ -> PyTuple.toTuple [PyExc.exceptionType exception, PyExc.exceptionValue exception]
        _ <- Py.call print_exc args kwargs
        return ()
    pyExceptionHandlerWithoutPythonTraceback :: PyExc.Exception -> IO ()
    pyExceptionHandlerWithoutPythonTraceback exception = do
        print exception
        putStrLn "Unexpected Python exception (Please report a bug)"

callingSomePython :: IO ()
callingSomePython = do ...