8000 Added computations for converting any iterable to a list or tuple. · packetloop/haskell-cpython@292b1f8 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 292b1f8

Browse files
committed
Added computations for converting any iterable to a list or tuple.
1 parent 6e01d32 commit 292b1f8

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

CPython/Internal.chs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ module CPython.Internal
4545
, stealObject
4646
, incref
4747
, decref
48+
, callObjectRaw
49+
, unsafeCast
4850

4951
-- * Exceptions
5052
, Exception (..)
@@ -153,6 +155,16 @@ stealObject ptr = exceptionIf (ptr == nullPtr) >> unsafeStealObject ptr
153155
foreign import ccall "hscpython-shim.h &hscpython_Py_DECREF"
154156
staticDecref :: FunPtr (Ptr a -> IO ())
155157

158+
{# fun PyObject_CallObject as callObjectRaw
159+
`(Object self, Object args)' =>
160+
{ withObject* `self'
161+
, withObject* `args'
162+
} -> `SomeObject' stealObject* #}
163+
164+
unsafeCast :: (Object a, Object b) => a -> b
165+
unsafeCast a = case toObject a of
166+
SomeObject ptr -> fromForeignPtr (castForeignPtr ptr)
167+
156168
data Exception = Exception
157169
{ exceptionType :: SomeObject
158170
, exceptionValue :: Maybe SomeObject

CPython/Types.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ module CPython.Types
7878
, toInteger
7979
, fromInteger
8080
, toList
81+
, iterableToList
8182
, fromList
8283
, toSet
8384
, toFrozenSet
8485
, iterableToSet
8586
, iterableToFrozenSet
8687
, fromSet
8788
, CPython.Types.Tuple.toTuple
89+
, iterableToTuple
8890
, fromTuple
8991
, toUnicode
9092
, fromUnicode

CPython/Types/List.chs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ - 8000 18,6 +18,7 @@ module CPython.Types.List
1818
( List
1919
, listType
2020
, toList
21+
, iterableToList
2122
, fromList
2223
, length
2324
, getItem
@@ -33,6 +34,7 @@ module CPython.Types.List
3334
import Prelude hiding (reverse, length)
3435
import qualified Prelude as Prelude
3536
import CPython.Internal hiding (new)
37+
import qualified CPython.Types.Tuple as T
3638

3739
#include <hscpython-shim.h>
3840

@@ -49,6 +51,11 @@ toList xs =
4951
{# call hscpython_poke_list #} (fromIntegral count) array
5052
>>= stealObject
5153

54+
iterableToList :: Object iter => iter -> IO List
55+
iterableToList iter = do
56+
raw <- callObjectRaw listType =<< T.toTuple [toObject iter]
57+
return $ unsafeCast raw
58+
5259
fromList :: List -> IO [SomeObject]
5360
fromList py =
5461
withObject py $ \pyPtr ->

CPython/Types/Tuple.chs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module CPython.Types.Tuple
1818
( Tuple
1919
, tupleType
2020
, toTuple
21+
, iterableToTuple
2122
, fromTuple
2223
, length
2324
, getItem
@@ -43,6 +44,11 @@ toTuple xs =
4344
{# call hscpython_poke_tuple #} (fromIntegral count) array
4445
>>= stealObject
4546

47+
iterableToTuple :: Object iter => iter -> IO Tuple
48+
iterableToTuple iter = do
49+
raw <- callObjectRaw tupleType =<< toTuple [toObject iter]
50+
return $ unsafeCast raw
51+
4652
fromTuple :: Tuple -> IO [SomeObject]
4753
fromTuple py =
4854
withObject py $ \pyPtr ->

0 commit comments

Comments
 (0)
0