Skip to content

Commit c8e32d4

Browse files
committed
Check return value in Py
1 parent 23d82e8 commit c8e32d4

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/Python/Inline/Literal.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ instance FromPy Bool where
167167

168168
instance (ToPy a, ToPy b) => ToPy (a,b) where
169169
basicToPy (a,b) = do
170-
p_a <- basicToPy a
171-
p_b <- basicToPy b
172-
Py [CU.exp| PyObject* { PyTuple_Pack(2, $(PyObject* p_a), $(PyObject* p_b)) } |]
170+
basicToPy a >>= \case
171+
NULL -> pure NULL
172+
p_a -> basicToPy b >>= \case
173+
NULL -> pure $ NULL
174+
p_b -> Py [CU.exp| PyObject* { PyTuple_Pack(2, $(PyObject* p_a), $(PyObject* p_b)) } |]
173175

174176
instance (FromPy a, FromPy b) => FromPy (a,b) where
175177
basicFromPy p_tup = evalContT $ do

src/Python/Internal/Types.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ module Python.Internal.Types
1717
, pattern INLINE_PY_OK
1818
, pattern INLINE_PY_ERR_COMPILE
1919
, pattern INLINE_PY_ERR_EVAL
20+
, pattern NULL
2021
) where
2122

2223
import Control.Exception
2324
import Control.Monad.IO.Class
2425
import Data.Coerce
2526
import Data.Map.Strict qualified as Map
27+
import Foreign.Ptr
2628
import Foreign.ForeignPtr
2729
import Foreign.C.Types
2830
import Language.C.Types
@@ -71,3 +73,8 @@ pattern INLINE_PY_OK, INLINE_PY_ERR_COMPILE, INLINE_PY_ERR_EVAL :: CInt
7173
pattern INLINE_PY_OK = 0
7274
pattern INLINE_PY_ERR_COMPILE = 1
7375
pattern INLINE_PY_ERR_EVAL = 2
76+
77+
78+
pattern NULL :: Ptr a
79+
pattern NULL <- ((== nullPtr) -> True) where
80+
NULL = nullPtr

0 commit comments

Comments
 (0)