{-# LANGUAGE OverlappingInstances#-}{- arch-tag: Python dict-like objects
Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}{- |
Module : Python.Objects.Dict
Copyright : Copyright (C) 2005 John Goerzen
License : GNU GPL, version 2 or above
Maintainer : John Goerzen,
Maintainer : jgoerzen\@complete.org
Stability : provisional
Portability: portable
Python dict-like objects
Written by John Goerzen, jgoerzen\@complete.org
This module can be used to access Python dicts and dict-like objects such as
dbm databases. For a higher-level interface to creating and working with these
dbm interfaces, please see the functions in "MissingPy.AnyDBM". Also,
for functions that use this, please see "Database.AnyDBM".
-}modulePython.Objects.Dict(PyDict,mkPyDict,fromPyDict)whereimportPython.ForeignImports(pyObject_SetItem,pyObject_DelItem,pyObject_GetItem,pyErr_Clear,pyMapping_Keys)importPython.Objects(toPyObject,fromPyObject,hasattr,noKwParms,noParms,runMethodHs,toPyObject)importPython.Utils(withPyObject,checkCInt,fromCPyObject)importForeign(Ptr,nullPtr)importPython.Exceptions(catchPy,exc2ioerror)importDatabase.AnyDBM(AnyDBM(..))importPython.Types(PyObject,CPyObject){- | The basic type for a Python dict or dict-like object. -}newtypePyDict=PyDictPyObject-- Main dict object{- | Takes a 'PyObject' representing a Python dict or dict-like objext
and makes it into a 'PyDict'. -}mkPyDict::PyObject->PyDictmkPyDicto=PyDicto{- | Takes a 'PyDict' and returns its internal 'PyObject'. -}fromPyDict::PyDict->PyObjectfromPyDict(PyDicto)=o{- | Wrap an operation, raising exceptions in the IO monad as appropriate. -}pydwrap::PyDict->(PyObject->IOa)->IOapydwrap(PyDictpyobj)func=catchPy(funcpyobj)exc2ioerror{- | Give it a CPyObject instead. -}cpydwrap::PyDict->(PtrCPyObject->IOa)->IOacpydwrapxfunc=pydwrapx(\y->withPyObjectyfunc)instanceAnyDBMPyDictwhereinsertAhkv=doko<-toPyObjectkkv<-toPyObjectvwithPyObjectko(\ck->withPyObjectkv(\cv->cpydwraph(\cdict->pyObject_SetItemcdictckcv>>=checkCInt>>return())))deleteAhk=doko<-toPyObjectkwithPyObjectko(\ck->cpydwraph(\cdict->pyObject_DelItemcdictck>>=checkCInt>>return()))lookupAhk=doko<-toPyObjectkwithPyObjectko(\ck->cpydwraph(\cdict->dor<-pyObject_GetItemcdictckifr==nullPtrthendopyErr_Clear-- Ignore this exceptionreturnNothingelsedoretval<-fromCPyObjectr>>=fromPyObjectreturn$Justretval)){-
toListA h =
This used to be:
pydwrap h fromPyObject
but some *dbm's are incompatible with that. Sigh. -}keysAh=cpydwraph(\ch->dokeysobj<-pyMapping_Keysch>>=fromCPyObjectkeys<-(fromPyObjectkeysobj)::IO[String]returnkeys)flushAh=pydwraph(\pyo->doh<-hasattrpyo"sync"ifhthenrunMethodHspyo"sync"noParmsnoKwParmselsereturn())closeAh=pydwraph(\pyo->doh<-hasattrpyo"close"ifhthenrunMethodHspyo"close"noParmsnoKwParmselsereturn())