mirror of
https://github.com/IHaskell/IHaskell.git
synced 2025-04-16 11:26:08 +00:00
Prevent kernel death on introspection
This commit is contained in:
parent
44ebe75c0c
commit
916c114b34
@ -90,7 +90,7 @@ data KernelConfig m output result =
|
||||
, completion :: T.Text -> T.Text -> Int -> Maybe ([T.Text], T.Text, T.Text)
|
||||
-- | Return the information or documentation for its argument. The returned tuple consists of the
|
||||
-- name, the documentation, and the type, respectively.
|
||||
, objectInfo :: T.Text -> Maybe (T.Text, T.Text, T.Text)
|
||||
, inspectInfo :: T.Text -> Maybe (T.Text, T.Text, T.Text)
|
||||
-- | Execute a cell. The arguments are the contents of the cell, an IO action that will clear the
|
||||
-- current intermediate output, and an IO action that will add a new item to the intermediate
|
||||
-- output. The result consists of the actual result, the status to be sent to IPython, and the
|
||||
@ -228,25 +228,10 @@ replyTo config execCount interface req@ExecuteRequest { getCode = code } replyHe
|
||||
|
||||
replyTo config _ _ req@CompleteRequest{} replyHeader =
|
||||
-- TODO: FIX
|
||||
error "Unimplemented in IPython 3.0"
|
||||
error "Completion: Unimplemented for IPython 3.0"
|
||||
|
||||
replyTo config _ _ ObjectInfoRequest { objectName = obj } replyHeader =
|
||||
return $
|
||||
case objectInfo config obj of
|
||||
Just (name, docs, ty) -> ObjectInfoReply
|
||||
{ header = replyHeader
|
||||
, objectName = obj
|
||||
, objectFound = True
|
||||
, objectTypeString = ty
|
||||
, objectDocString = docs
|
||||
}
|
||||
Nothing -> ObjectInfoReply
|
||||
{ header = replyHeader
|
||||
, objectName = obj
|
||||
, objectFound = False
|
||||
, objectTypeString = ""
|
||||
, objectDocString = ""
|
||||
}
|
||||
replyTo _ _ _ InspectRequest{} _ = do
|
||||
error $ "Inspection: Unimplemented for IPython 3.0"
|
||||
|
||||
replyTo _ _ _ msg _ = do
|
||||
liftIO $ putStrLn "Unknown message: "
|
||||
|
@ -74,7 +74,7 @@ parser :: MessageType -- ^ The message type being parsed.
|
||||
parser KernelInfoRequestMessage = kernelInfoRequestParser
|
||||
parser ExecuteRequestMessage = executeRequestParser
|
||||
parser CompleteRequestMessage = completeRequestParser
|
||||
parser ObjectInfoRequestMessage = objectInfoRequestParser
|
||||
parser InspectRequestMessage = inspectRequestParser
|
||||
parser ShutdownRequestMessage = shutdownRequestParser
|
||||
parser InputReplyMessage = inputReplyParser
|
||||
parser CommOpenMessage = commOpenParser
|
||||
@ -139,11 +139,12 @@ completeRequestParser = requestParser $ \obj -> do
|
||||
pos <- obj .: "cursor_pos"
|
||||
return $ CompleteRequest noHeader code pos
|
||||
|
||||
objectInfoRequestParser :: LByteString -> Message
|
||||
objectInfoRequestParser = requestParser $ \obj -> do
|
||||
oname <- obj .: "oname"
|
||||
inspectRequestParser :: LByteString -> Message
|
||||
inspectRequestParser = requestParser $ \obj -> do
|
||||
code <- obj .: "code"
|
||||
pos <- obj .: "cursor_pos"
|
||||
dlevel <- obj .: "detail_level"
|
||||
return $ ObjectInfoRequest noHeader oname dlevel
|
||||
return $ InspectRequest noHeader code pos dlevel
|
||||
|
||||
shutdownRequestParser :: LByteString -> Message
|
||||
shutdownRequestParser = requestParser $ \obj -> do
|
||||
|
@ -65,15 +65,13 @@ instance ToJSON Message where
|
||||
then string "ok"
|
||||
else "error"
|
||||
]
|
||||
toJSON o@ObjectInfoReply{} =
|
||||
toJSON i@InspectReply{} =
|
||||
object
|
||||
[ "oname" .=
|
||||
objectName o
|
||||
, "found" .= objectFound o
|
||||
, "ismagic" .= False
|
||||
, "isalias" .= False
|
||||
, "type_name" .= objectTypeString o
|
||||
, "docstring" .= objectDocString o
|
||||
[ "status" .= if inspectStatus i
|
||||
then string "ok"
|
||||
else "error"
|
||||
, "data" .= object (map displayDataToJson . inspectData $ i)
|
||||
, "metadata" .= object []
|
||||
]
|
||||
|
||||
toJSON ShutdownReply { restartPending = restart } =
|
||||
|
@ -176,8 +176,8 @@ data MessageType = KernelInfoReplyMessage
|
||||
| InputMessage
|
||||
| CompleteRequestMessage
|
||||
| CompleteReplyMessage
|
||||
| ObjectInfoRequestMessage
|
||||
| ObjectInfoReplyMessage
|
||||
| InspectRequestMessage
|
||||
| InspectReplyMessage
|
||||
| ShutdownRequestMessage
|
||||
| ShutdownReplyMessage
|
||||
| ClearOutputMessage
|
||||
@ -202,8 +202,8 @@ showMessageType OutputMessage = "pyout"
|
||||
showMessageType InputMessage = "pyin"
|
||||
showMessageType CompleteRequestMessage = "complete_request"
|
||||
showMessageType CompleteReplyMessage = "complete_reply"
|
||||
showMessageType ObjectInfoRequestMessage = "object_info_request"
|
||||
showMessageType ObjectInfoReplyMessage = "object_info_reply"
|
||||
showMessageType InspectRequestMessage = "inspect_request"
|
||||
showMessageType InspectReplyMessage = "inspect_reply"
|
||||
showMessageType ShutdownRequestMessage = "shutdown_request"
|
||||
showMessageType ShutdownReplyMessage = "shutdown_reply"
|
||||
showMessageType ClearOutputMessage = "clear_output"
|
||||
@ -229,8 +229,8 @@ instance FromJSON MessageType where
|
||||
"pyin" -> return InputMessage
|
||||
"complete_request" -> return CompleteRequestMessage
|
||||
"complete_reply" -> return CompleteReplyMessage
|
||||
"object_info_request" -> return ObjectInfoRequestMessage
|
||||
"object_info_reply" -> return ObjectInfoReplyMessage
|
||||
"inspect_request" -> return InspectRequestMessage
|
||||
"inspect_reply" -> return InspectReplyMessage
|
||||
"shutdown_request" -> return ShutdownRequestMessage
|
||||
"shutdown_reply" -> return ShutdownReplyMessage
|
||||
"clear_output" -> return ClearOutputMessage
|
||||
@ -326,20 +326,22 @@ data Message =
|
||||
, completionStatus :: Bool
|
||||
}
|
||||
|
|
||||
ObjectInfoRequest
|
||||
InspectRequest
|
||||
{ header :: MessageHeader
|
||||
-- | Name of object being searched for.
|
||||
, objectName :: Text
|
||||
-- | The code context in which introspection is requested
|
||||
, inspectCode :: Text
|
||||
-- | Position of the cursor in unicode characters. json field @cursor_pos@
|
||||
, inspectCursorPos :: Int
|
||||
-- | Level of detail desired (defaults to 0). 0 is equivalent to foo?, 1 is equivalent to foo??.
|
||||
, detailLevel :: Int
|
||||
}
|
||||
|
|
||||
ObjectInfoReply
|
||||
InspectReply
|
||||
{ header :: MessageHeader
|
||||
, objectName :: Text -- ^ Name of object which was searched for.
|
||||
, objectFound :: Bool -- ^ Whether the object was found.
|
||||
, objectTypeString :: Text -- ^ Object type.
|
||||
, objectDocString :: Text
|
||||
-- | whether the request succeeded or failed
|
||||
, inspectStatus :: Bool
|
||||
-- | @inspectData@ can be empty if nothing is found
|
||||
, inspectData :: [DisplayData]
|
||||
}
|
||||
|
|
||||
ShutdownRequest
|
||||
@ -422,7 +424,7 @@ replyType :: MessageType -> Maybe MessageType
|
||||
replyType KernelInfoRequestMessage = Just KernelInfoReplyMessage
|
||||
replyType ExecuteRequestMessage = Just ExecuteReplyMessage
|
||||
replyType CompleteRequestMessage = Just CompleteReplyMessage
|
||||
replyType ObjectInfoRequestMessage = Just ObjectInfoReplyMessage
|
||||
replyType InspectRequestMessage = Just InspectReplyMessage
|
||||
replyType ShutdownRequestMessage = Just ShutdownReplyMessage
|
||||
replyType HistoryRequestMessage = Just HistoryReplyMessage
|
||||
replyType _ = Nothing
|
||||
|
16
src/Main.hs
16
src/Main.hs
@ -19,6 +19,7 @@ import Text.Printf
|
||||
import System.Posix.Signals
|
||||
import qualified Data.Map as Map
|
||||
import Data.String.Here (hereFile)
|
||||
import qualified Data.Text as T
|
||||
|
||||
-- IHaskell imports.
|
||||
import IHaskell.Convert (convert)
|
||||
@ -334,17 +335,10 @@ replyTo _ req@CompleteRequest{} replyHeader state = do
|
||||
reply = CompleteReply replyHeader (map pack completions) start end Map.empty True
|
||||
return (state, reply)
|
||||
|
||||
-- Reply to the object_info_request message. Given an object name, return the associated type
|
||||
-- calculated by GHC.
|
||||
replyTo _ ObjectInfoRequest { objectName = oname } replyHeader state = do
|
||||
docs <- pack <$> info (unpack oname)
|
||||
let reply = ObjectInfoReply
|
||||
{ header = replyHeader
|
||||
, objectName = oname
|
||||
, objectFound = strip docs /= ""
|
||||
, objectTypeString = docs
|
||||
, objectDocString = docs
|
||||
}
|
||||
-- TODO: Implement inspect_reply
|
||||
replyTo _ InspectRequest{} replyHeader state = do
|
||||
-- FIXME
|
||||
let reply = InspectReply { header = replyHeader, inspectStatus = False, inspectData = [] }
|
||||
return (state, reply)
|
||||
|
||||
-- TODO: Implement history_reply.
|
||||
|
Loading…
x
Reference in New Issue
Block a user