File tree Expand file tree Collapse file tree 4 files changed +27
-0
lines changed Expand file tree Collapse file tree 4 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,8 @@ module CPython.Internal
45
45
, stealObject
46
46
, incref
47
47
, decref
48
+ , callObjectRaw
49
+ , unsafeCast
48
50
49
51
-- * Exceptions
50
52
, Exception (.. )
@@ -153,6 +155,16 @@ stealObject ptr = exceptionIf (ptr == nullPtr) >> unsafeStealObject ptr
153
155
foreign import ccall " hscpython-shim.h &hscpython_Py_DECREF"
154
156
staticDecref :: FunPtr (Ptr a -> IO () )
155
157
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
+
156
168
data Exception = Exception
157
169
{ exceptionType :: SomeObject
158
170
, exceptionValue :: Maybe SomeObject
Original file line number Diff line number Diff line change @@ -78,13 +78,15 @@ module CPython.Types
78
78
, toInteger
79
79
, fromInteger
80
80
, toList
81
+ , iterableToList
81
82
, fromList
82
83
, toSet
83
84
, toFrozenSet
84
85
, iterableToSet
85
86
, iterableToFrozenSet
86
87
, fromSet
87
88
, CPython.Types.Tuple. toTuple
89
+ , iterableToTuple
88
90
, fromTuple
89
91
, toUnicode
90
92
, fromUnicode
Original file line number Diff line number Diff line change @@ -
8000
18,6 +18,7 @@ module CPython.Types.List
18
18
( List
19
19
, listType
20
20
, toList
21
+ , iterableToList
21
22
, fromList
22
23
, length
23
24
, getItem
@@ -33,6 +34,7 @@ module CPython.Types.List
33
34
import Prelude hiding (reverse , length )
34
35
import qualified Prelude as Prelude
35
36
import CPython.Internal hiding (new )
37
+ import qualified CPython.Types.Tuple as T
36
38
37
39
#include <hscpython-shim.h>
38
40
@@ -49,6 +51,11 @@ toList xs =
49
51
{# call hscpython_poke_list # } (fromIntegral count) array
50
52
>>= stealObject
51
53
54
+ iterableToList :: Object iter => iter -> IO List
55
+ iterableToList iter = do
56
+ raw <- callObjectRaw listType =<< T. toTuple [toObject iter]
57
+ return $ unsafeCast raw
58
+
52
59
fromList :: List -> IO [SomeObject ]
53
60
fromList py =
54
61
withObject py $ \ pyPtr ->
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ module CPython.Types.Tuple
18
18
( Tuple
19
19
, tupleType
20
20
, toTuple
21
+ , iterableToTuple
21
22
, fromTuple
22
23
, length
23
24
, getItem
@@ -43,6 +44,11 @@ toTuple xs =
43
44
{# call hscpython_poke_tuple # } (fromIntegral count) array
44
45
>>= stealObject
45
46
47
+ iterableToTuple :: Object iter => iter -> IO Tuple
48
+ iterableToTuple iter = do
49
+ raw <- callObjectRaw tupleType =<< toTuple [toObject iter]
50
+ return $ unsafeCast raw
51
+
46
52
fromTuple :: Tuple -> IO [SomeObject ]
47
53
fromTuple py =
48
54
withObject py $ \ pyPtr ->
You can’t perform that action at this time.
0 commit comments