cpython 3.1.3 → 3.1.4
raw patch · 36 files changed
+603/−658 lines, 36 files
Files
- cbits/hscpython-shim.c +119/−110
- cbits/hscpython-shim.h +52/−52
- cpython.cabal +4/−8
- lib/CPython.chs +32/−48
- lib/CPython/Constants.chs +14/−15
- lib/CPython/Internal.chs +25/−13
- lib/CPython/Protocols/Iterator.chs +8/−8
- lib/CPython/Protocols/Mapping.chs +8/−6
- lib/CPython/Protocols/Number.chs +18/−15
- lib/CPython/Protocols/Object.chs +14/−34
- lib/CPython/Protocols/Sequence.chs +14/−16
- lib/CPython/Reflection.chs +10/−13
- lib/CPython/System.chs +10/−13
- lib/CPython/Types.hs +27/−26
- lib/CPython/Types/ByteArray.chs +12/−10
- lib/CPython/Types/Bytes.chs +12/−11
- lib/CPython/Types/Capsule.chs +14/−18
- lib/CPython/Types/Cell.chs +9/−10
- lib/CPython/Types/Code.chs +9/−7
- lib/CPython/Types/Complex.chs +11/−8
- lib/CPython/Types/Dictionary.chs +10/−20
- lib/CPython/Types/Exception.chs +7/−7
- lib/CPython/Types/Float.chs +11/−8
- lib/CPython/Types/Function.chs +12/−20
- lib/CPython/Types/InstanceMethod.chs +9/−7
- lib/CPython/Types/Integer.chs +14/−11
- lib/CPython/Types/Iterator.chs +10/−10
- lib/CPython/Types/List.chs +12/−19
- lib/CPython/Types/Method.chs +10/−7
- lib/CPython/Types/Module.chs +16/−21
- lib/CPython/Types/Set.chs +10/−18
- lib/CPython/Types/Slice.chs +11/−10
- lib/CPython/Types/Tuple.chs +11/−11
- lib/CPython/Types/Type.chs +9/−8
- lib/CPython/Types/Unicode.chs +21/−31
- lib/CPython/Types/WeakReference.chs +8/−9
cbits/hscpython-shim.c view
@@ -1,16 +1,16 @@ /** * Copyright (C) 2009 John Millikin <jmillikin@gmail.com>- * + * * 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 3 of the License, or * 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, see <http://www.gnu.org/licenses/>. **/@@ -21,227 +21,236 @@ static wchar_t *program_name = NULL; static wchar_t *python_home = NULL; -static wchar_t *strdupw (wchar_t *s)+static wchar_t *hscpython_wcsdup(wchar_t *s) { size_t len = 0; wchar_t *orig = s, *new, *new0;- if (!s) { return s; }- - while (*(s++)) { len++; }- new = new0 = malloc (sizeof (wchar_t) * len);++ if (!s) {+ return s;+ }++ while (*(s++)) {+ len++;+ }++ new = new0 = malloc(sizeof(wchar_t) * len); s = orig;- while (*(new++) = *(s++)) {}+ while (*(new++) = *(s++)) {+ } return new0; } -void hscpython_SetProgramName (wchar_t *name)+void hscpython_SetProgramName(wchar_t *s) {- free (program_name);- program_name = strdupw (name);+ free(program_name);+ program_name = hscpython_wcsdup(s);+ Py_SetProgramName(program_name); } -void hscpython_SetPythonHome (wchar_t *home)+void hscpython_SetPythonHome(wchar_t *s) {- free (python_home);- python_home = strdupw (home);+ free(python_home);+ python_home = hscpython_wcsdup(s);+ Py_SetPythonHome(python_home); } /* Object */-void hscpython_Py_INCREF (PyObject *o)-{ Py_INCREF (o); }+void hscpython_Py_INCREF(PyObject *o)+{ Py_INCREF(o); } -void hscpython_Py_DECREF (PyObject *o)-{ Py_DECREF (o); }+void hscpython_Py_DECREF(PyObject *o)+{ Py_DECREF(o); } int hscpython_PyObject_DelAttr(PyObject *o, PyObject *name)-{ return PyObject_DelAttr (o, name); }+{ return PyObject_DelAttr(o, name); } -int hscpython_PyObject_TypeCheck (PyObject *o, PyTypeObject *type)-{ return PyObject_TypeCheck (o, type); }+int hscpython_PyObject_TypeCheck(PyObject *o, PyTypeObject *type)+{ return PyObject_TypeCheck(o, type); } int hscpython_PyIter_Check(PyObject *o) { return PyIter_Check(o); } /* Types */-PyTypeObject *hscpython_PyType_Type ()+PyTypeObject *hscpython_PyType_Type() { return &PyType_Type; } -PyTypeObject *hscpython_PyTuple_Type ()+PyTypeObject *hscpython_PyTuple_Type() { return &PyTuple_Type; } -PyTypeObject *hscpython_PyList_Type ()+PyTypeObject *hscpython_PyList_Type() { return &PyList_Type; } -PyTypeObject *hscpython_PyDict_Type ()+PyTypeObject *hscpython_PyDict_Type() { return &PyDict_Type; } -PyTypeObject *hscpython_PyLong_Type ()+PyTypeObject *hscpython_PyLong_Type() { return &PyLong_Type; } -PyTypeObject *hscpython_PyFloat_Type ()+PyTypeObject *hscpython_PyFloat_Type() { return &PyFloat_Type; } -PyTypeObject *hscpython_PyComplex_Type ()+PyTypeObject *hscpython_PyComplex_Type() { return &PyComplex_Type; } -PyTypeObject *hscpython_PyUnicode_Type ()+PyTypeObject *hscpython_PyUnicode_Type() { return &PyUnicode_Type; } -PyTypeObject *hscpython_PyBytes_Type ()+PyTypeObject *hscpython_PyBytes_Type() { return &PyBytes_Type; } -PyTypeObject *hscpython_PyByteArray_Type ()+PyTypeObject *hscpython_PyByteArray_Type() { return &PyByteArray_Type; } -PyTypeObject *hscpython_PyCell_Type ()+PyTypeObject *hscpython_PyCell_Type() { return &PyCell_Type; } -PyTypeObject *hscpython_PyCode_Type ()+PyTypeObject *hscpython_PyCode_Type() { return &PyCode_Type; } -PyTypeObject *hscpython_PyFunction_Type ()+PyTypeObject *hscpython_PyFunction_Type() { return &PyFunction_Type; } -PyTypeObject *hscpython_PyInstanceMethod_Type ()+PyTypeObject *hscpython_PyInstanceMethod_Type() { return &PyInstanceMethod_Type; } -PyTypeObject *hscpython_PyMethod_Type ()+PyTypeObject *hscpython_PyMethod_Type() { return &PyMethod_Type; } -PyTypeObject *hscpython_PySet_Type ()+PyTypeObject *hscpython_PySet_Type() { return &PySet_Type; } -PyTypeObject *hscpython_PyFrozenSet_Type ()+PyTypeObject *hscpython_PyFrozenSet_Type() { return &PyFrozenSet_Type; } -PyTypeObject *hscpython_PySeqIter_Type ()+PyTypeObject *hscpython_PySeqIter_Type() { return &PySeqIter_Type; } -PyTypeObject *hscpython_PyCallIter_Type ()+PyTypeObject *hscpython_PyCallIter_Type() { return &PyCallIter_Type; } -PyTypeObject *hscpython_PySlice_Type ()+PyTypeObject *hscpython_PySlice_Type() { return &PySlice_Type; } -PyTypeObject *hscpython_PyModule_Type ()+PyTypeObject *hscpython_PyModule_Type() { return &PyModule_Type; } -PyTypeObject *hscpython_PyCapsule_Type ()+PyTypeObject *hscpython_PyCapsule_Type() { return &PyCapsule_Type; } /* Constants */-PyObject *hscpython_Py_None ()+PyObject *hscpython_Py_None() { return Py_None; } -PyObject *hscpython_Py_True ()+PyObject *hscpython_Py_True() { return Py_True; } -PyObject *hscpython_Py_False ()+PyObject *hscpython_Py_False() { return Py_False; } /* Unicode */-Py_ssize_t hscpython_PyUnicode_GetSize (PyObject *o)-{ return PyUnicode_GetSize (o); }+Py_ssize_t hscpython_PyUnicode_GetSize(PyObject *o)+{ return PyUnicode_GetSize(o); } -Py_UNICODE *hscpython_PyUnicode_AsUnicode (PyObject *o)-{ return PyUnicode_AsUnicode (o); }+Py_UNICODE *hscpython_PyUnicode_AsUnicode(PyObject *o)+{ return PyUnicode_AsUnicode(o); } -PyObject *hscpython_PyUnicode_FromUnicode (Py_UNICODE *u, Py_ssize_t size)-{ return PyUnicode_FromUnicode (u, size); }+PyObject *hscpython_PyUnicode_FromUnicode(Py_UNICODE *u, Py_ssize_t size)+{ return PyUnicode_FromUnicode(u, size); } -PyObject *hscpython_PyUnicode_FromEncodedObject (PyObject *o, const char *enc, const char *err)-{ return PyUnicode_FromEncodedObject (o, enc, err); }+PyObject *hscpython_PyUnicode_FromEncodedObject(PyObject *o, const char *enc, const char *err)+{ return PyUnicode_FromEncodedObject(o, enc, err); } -PyObject *hscpython_PyUnicode_AsEncodedString (PyObject *o, const char *enc, const char *err)-{ return PyUnicode_AsEncodedString (o, enc, err); }+PyObject *hscpython_PyUnicode_AsEncodedString(PyObject *o, const char *enc, const char *err)+{ return PyUnicode_AsEncodedString(o, enc, err); } -PyObject *hscpython_PyUnicode_FromObject (PyObject *o)-{ return PyUnicode_FromObject (o); }+PyObject *hscpython_PyUnicode_FromObject(PyObject *o)+{ return PyUnicode_FromObject(o); } -PyObject *hscpython_PyUnicode_Decode (const char *s, Py_ssize_t len, const char *enc, const char *err)-{ return PyUnicode_Decode (s, len, enc, err); }+PyObject *hscpython_PyUnicode_Decode(const char *s, Py_ssize_t len, const char *enc, const char *err)+{ return PyUnicode_Decode(s, len, enc, err); } -PyObject *hscpython_PyUnicode_Concat (PyObject *l, PyObject *r)-{ return PyUnicode_Concat (l, r); }+PyObject *hscpython_PyUnicode_Concat(PyObject *l, PyObject *r)+{ return PyUnicode_Concat(l, r); } -PyObject *hscpython_PyUnicode_Split (PyObject *s, PyObject *sep, Py_ssize_t max)-{ return PyUnicode_Split (s, sep, max); }+PyObject *hscpython_PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t max)+{ return PyUnicode_Split(s, sep, max); } -PyObject *hscpython_PyUnicode_Splitlines (PyObject *s, int keep)-{ return PyUnicode_Splitlines (s, keep); }+PyObject *hscpython_PyUnicode_Splitlines(PyObject *s, int keep)+{ return PyUnicode_Splitlines(s, keep); } -PyObject *hscpython_PyUnicode_Translate (PyObject *str, PyObject *table, const char *err)-{ return PyUnicode_Translate (str, table, err); }+PyObject *hscpython_PyUnicode_Translate(PyObject *str, PyObject *table, const char *err)+{ return PyUnicode_Translate(str, table, err); } -PyObject *hscpython_PyUnicode_Join (PyObject *sep, PyObject *seq)-{ return PyUnicode_Join (sep, seq); }+PyObject *hscpython_PyUnicode_Join(PyObject *sep, PyObject *seq)+{ return PyUnicode_Join(sep, seq); } -int hscpython_PyUnicode_Tailmatch (PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int dir)-{ return PyUnicode_Tailmatch (str, substr, start, end, dir); }+int hscpython_PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int dir)+{ return PyUnicode_Tailmatch(str, substr, start, end, dir); } -Py_ssize_t hscpython_PyUnicode_Find (PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int dir)-{ return PyUnicode_Find (str, substr, start, end, dir); }+Py_ssize_t hscpython_PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int dir)+{ return PyUnicode_Find(str, substr, start, end, dir); } -Py_ssize_t hscpython_PyUnicode_Count (PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)-{ return PyUnicode_Count (str, substr, start, end); }+Py_ssize_t hscpython_PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)+{ return PyUnicode_Count(str, substr, start, end); } -PyObject *hscpython_PyUnicode_Replace (PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t max)-{ return PyUnicode_Replace (str, substr, replstr, max); }+PyObject *hscpython_PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t max)+{ return PyUnicode_Replace(str, substr, replstr, max); } -PyObject *hscpython_PyUnicode_Format (PyObject *format, PyObject *args)-{ return PyUnicode_Format (format, args); }+PyObject *hscpython_PyUnicode_Format(PyObject *format, PyObject *args)+{ return PyUnicode_Format(format, args); } -int hscpython_PyUnicode_Contains (PyObject *a, PyObject *b)-{ return PyUnicode_Contains (a, b); }+int hscpython_PyUnicode_Contains(PyObject *a, PyObject *b)+{ return PyUnicode_Contains(a, b); } /* Lists */-void hscpython_peek_list (PyObject *list, Py_ssize_t size, PyObject **objs)+void hscpython_peek_list(PyObject *list, Py_ssize_t size, PyObject **objs) { Py_ssize_t ii;- for (ii = 0; ii < size; ii++)- {- objs[ii] = PyList_GET_ITEM (list, ii);++ for (ii = 0; ii < size; ii++) {+ objs[ii] = PyList_GET_ITEM(list, ii); } } -PyObject *hscpython_poke_list (size_t count, PyObject **objs)+PyObject *hscpython_poke_list(size_t count, PyObject **objs) { PyObject *list; size_t ii;- - if (!(list = PyList_New (count)))- { return NULL; }- - for (ii = 0; ii < count; ii++)- {- Py_INCREF (objs[ii]);- PyList_SET_ITEM (list, ii, objs[ii]);++ if (!(list = PyList_New(count))) {+ return NULL; }++ for (ii = 0; ii < count; ii++) {+ Py_INCREF(objs[ii]);+ PyList_SET_ITEM(list, ii, objs[ii]);+ } return list; } -/* Tuple */-void hscpython_peek_tuple (PyObject *tuple, Py_ssize_t size, PyObject **objs)+/* Tuples */+void hscpython_peek_tuple(PyObject *tuple, Py_ssize_t size, PyObject **objs) { Py_ssize_t ii;- for (ii = 0; ii < size; ii++)- {- objs[ii] = PyTuple_GET_ITEM (tuple, ii);++ for (ii = 0; ii < size; ii++) {+ objs[ii] = PyTuple_GET_ITEM(tuple, ii); } } -PyObject *hscpython_poke_tuple (size_t count, PyObject **objs)+PyObject *hscpython_poke_tuple(size_t count, PyObject **objs) { PyObject *tuple; size_t ii;- - if (!(tuple = PyTuple_New (count)))- { return NULL; }- - for (ii = 0; ii < count; ii++)- {- Py_INCREF (objs[ii]);- PyTuple_SET_ITEM (tuple, ii, objs[ii]);++ if (!(tuple = PyTuple_New(count))) {+ return NULL;+ }++ for (ii = 0; ii < count; ii++) {+ Py_INCREF(objs[ii]);+ PyTuple_SET_ITEM(tuple, ii, objs[ii]); } return tuple; }
cbits/hscpython-shim.h view
@@ -4,14 +4,14 @@ #include <Python.h> /* Initialization helpers */-void hscpython_SetProgramName (wchar_t *);-void hscpython_SetPythonHome (wchar_t *);+void hscpython_SetProgramName(wchar_t *);+void hscpython_SetPythonHome(wchar_t *); /* Object */-void hscpython_Py_INCREF (PyObject *);-void hscpython_Py_DECREF (PyObject *);+void hscpython_Py_INCREF(PyObject *);+void hscpython_Py_DECREF(PyObject *); int hscpython_PyObject_DelAttr(PyObject *, PyObject *);-int hscpython_PyObject_TypeCheck (PyObject *, PyTypeObject *);+int hscpython_PyObject_TypeCheck(PyObject *, PyTypeObject *); int hscpython_PyIter_Check(PyObject *); enum HSCPythonComparisonEnum@@ -24,60 +24,60 @@ }; /* Types */-PyTypeObject *hscpython_PyType_Type ();-PyTypeObject *hscpython_PyTuple_Type ();-PyTypeObject *hscpython_PyList_Type ();-PyTypeObject *hscpython_PyDict_Type ();-PyTypeObject *hscpython_PyLong_Type ();-PyTypeObject *hscpython_PyFloat_Type ();-PyTypeObject *hscpython_PyComplex_Type ();-PyTypeObject *hscpython_PyUnicode_Type ();-PyTypeObject *hscpython_PyBytes_Type ();-PyTypeObject *hscpython_PyByteArray_Type ();-PyTypeObject *hscpython_PyCell_Type ();-PyTypeObject *hscpython_PyCode_Type ();-PyTypeObject *hscpython_PyFunction_Type ();-PyTypeObject *hscpython_PyInstanceMethod_Type ();-PyTypeObject *hscpython_PyMethod_Type ();-PyTypeObject *hscpython_PySet_Type ();-PyTypeObject *hscpython_PyFrozenSet_Type ();-PyTypeObject *hscpython_PySeqIter_Type ();-PyTypeObject *hscpython_PyCallIter_Type ();-PyTypeObject *hscpython_PySlice_Type ();-PyTypeObject *hscpython_PyModule_Type ();-PyTypeObject *hscpython_PyCapsule_Type ();+PyTypeObject *hscpython_PyType_Type();+PyTypeObject *hscpython_PyTuple_Type();+PyTypeObject *hscpython_PyList_Type();+PyTypeObject *hscpython_PyDict_Type();+PyTypeObject *hscpython_PyLong_Type();+PyTypeObject *hscpython_PyFloat_Type();+PyTypeObject *hscpython_PyComplex_Type();+PyTypeObject *hscpython_PyUnicode_Type();+PyTypeObject *hscpython_PyBytes_Type();+PyTypeObject *hscpython_PyByteArray_Type();+PyTypeObject *hscpython_PyCell_Type();+PyTypeObject *hscpython_PyCode_Type();+PyTypeObject *hscpython_PyFunction_Type();+PyTypeObject *hscpython_PyInstanceMethod_Type();+PyTypeObject *hscpython_PyMethod_Type();+PyTypeObject *hscpython_PySet_Type();+PyTypeObject *hscpython_PyFrozenSet_Type();+PyTypeObject *hscpython_PySeqIter_Type();+PyTypeObject *hscpython_PyCallIter_Type();+PyTypeObject *hscpython_PySlice_Type();+PyTypeObject *hscpython_PyModule_Type();+PyTypeObject *hscpython_PyCapsule_Type(); /* Constants */-PyObject *hscpython_Py_None ();-PyObject *hscpython_Py_True ();-PyObject *hscpython_Py_False ();+PyObject *hscpython_Py_None();+PyObject *hscpython_Py_True();+PyObject *hscpython_Py_False(); /* Unicode */-Py_ssize_t hscpython_PyUnicode_GetSize (PyObject *);-Py_UNICODE *hscpython_PyUnicode_AsUnicode (PyObject *);-PyObject *hscpython_PyUnicode_FromUnicode (Py_UNICODE *, Py_ssize_t);-PyObject *hscpython_PyUnicode_FromEncodedObject (PyObject *, const char *, const char *);-PyObject *hscpython_PyUnicode_AsEncodedString (PyObject *, const char *, const char *);-PyObject *hscpython_PyUnicode_FromObject (PyObject *);-PyObject *hscpython_PyUnicode_Decode (const char *, Py_ssize_t, const char *, const char *);-PyObject *hscpython_PyUnicode_Concat (PyObject *, PyObject *);-PyObject *hscpython_PyUnicode_Split (PyObject *, PyObject *, Py_ssize_t);-PyObject *hscpython_PyUnicode_Splitlines (PyObject *, int);-PyObject *hscpython_PyUnicode_Translate (PyObject *, PyObject *, const char *);-PyObject *hscpython_PyUnicode_Join (PyObject *, PyObject *);-int hscpython_PyUnicode_Tailmatch (PyObject *, PyObject *, Py_ssize_t, Py_ssize_t, int);-Py_ssize_t hscpython_PyUnicode_Find (PyObject *, PyObject *, Py_ssize_t, Py_ssize_t, int);-Py_ssize_t hscpython_PyUnicode_Count (PyObject *, PyObject *, Py_ssize_t, Py_ssize_t);-PyObject *hscpython_PyUnicode_Replace (PyObject *, PyObject *, PyObject *, Py_ssize_t);-PyObject *hscpython_PyUnicode_Format (PyObject *, PyObject *);-int hscpython_PyUnicode_Contains (PyObject *, PyObject *);+Py_ssize_t hscpython_PyUnicode_GetSize(PyObject *);+Py_UNICODE *hscpython_PyUnicode_AsUnicode(PyObject *);+PyObject *hscpython_PyUnicode_FromUnicode(Py_UNICODE *, Py_ssize_t);+PyObject *hscpython_PyUnicode_FromEncodedObject(PyObject *, const char *, const char *);+PyObject *hscpython_PyUnicode_AsEncodedString(PyObject *, const char *, const char *);+PyObject *hscpython_PyUnicode_FromObject(PyObject *);+PyObject *hscpython_PyUnicode_Decode(const char *, Py_ssize_t, const char *, const char *);+PyObject *hscpython_PyUnicode_Concat(PyObject *, PyObject *);+PyObject *hscpython_PyUnicode_Split(PyObject *, PyObject *, Py_ssize_t);+PyObject *hscpython_PyUnicode_Splitlines(PyObject *, int);+PyObject *hscpython_PyUnicode_Translate(PyObject *, PyObject *, const char *);+PyObject *hscpython_PyUnicode_Join(PyObject *, PyObject *);+int hscpython_PyUnicode_Tailmatch(PyObject *, PyObject *, Py_ssize_t, Py_ssize_t, int);+Py_ssize_t hscpython_PyUnicode_Find(PyObject *, PyObject *, Py_ssize_t, Py_ssize_t, int);+Py_ssize_t hscpython_PyUnicode_Count(PyObject *, PyObject *, Py_ssize_t, Py_ssize_t);+PyObject *hscpython_PyUnicode_Replace(PyObject *, PyObject *, PyObject *, Py_ssize_t);+PyObject *hscpython_PyUnicode_Format(PyObject *, PyObject *);+int hscpython_PyUnicode_Contains(PyObject *, PyObject *); /* Lists */-void hscpython_peek_list (PyObject *, Py_ssize_t, PyObject **);-PyObject *hscpython_poke_list (size_t, PyObject **);+void hscpython_peek_list(PyObject *, Py_ssize_t, PyObject **);+PyObject *hscpython_poke_list(size_t, PyObject **); /* Tuples */-void hscpython_peek_tuple (PyObject *, Py_ssize_t, PyObject **);-PyObject *hscpython_poke_tuple (size_t, PyObject **);+void hscpython_peek_tuple(PyObject *, Py_ssize_t, PyObject **);+PyObject *hscpython_poke_tuple(size_t, PyObject **); #endif
cpython.cabal view
@@ -1,5 +1,5 @@ name: cpython-version: 3.1.3+version: 3.1.4 license: GPL-3 license-file: license.txt author: John Millikin <jmillikin@gmail.com>@@ -14,11 +14,6 @@ These bindings allow Haskell code to call CPython code. It is not currently possible to call Haskell code from CPython, but this feature is planned.- .- Please note that this library uses a somewhat abnormal versioning scheme;- the first two version numbers are the CPython version, the third is the- package's version. For example, the package version 3.1.1 binds to CPython- 3.1, and has a package version of 1. extra-source-files: cbits/hscpython-shim.h@@ -30,10 +25,11 @@ source-repository this type: bazaar location: https://john-millikin.com/branches/haskell-cpython/3.1/- tag: haskell-cpython_3.1.3+ tag: haskell-cpython_3.1.4 library- ghc-options: -Wall -O2+ ghc-options: -Wall -O2 -fno-warn-orphans+ cc-options: -fPIC hs-source-dirs: lib build-depends:
lib/CPython.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython ( initialize , isInitialized@@ -35,11 +36,13 @@ , getPythonHome , setPythonHome ) where-import Data.Text (Text)-import CPython.Internal #include <hscpython-shim.h> +import Data.Text (Text)++import CPython.Internal+ -- | Initialize the Python interpreter. In an application embedding Python, -- this should be called before using any other Python/C API computations; -- with the exception of 'setProgramName', 'initThreads',@@ -50,14 +53,12 @@ -- is a no-op when called for a second time (without calling 'finalize' -- first). There is no return value; it is a fatal error if the initialization -- fails.--- {# fun Py_Initialize as initialize {} -> `()' id #} -- | Return 'True' when the Python interpreter has been initialized, 'False' -- if not. After 'finalize' is called, this returns 'False' until -- 'initialize' is called again.--- {# fun Py_IsInitialized as isInitialized {} -> `Bool' #} @@ -68,7 +69,7 @@ -- Python interpreter. This is a no-op when called for a second time (without -- calling 'initialize' again first). There is no return value; errors during -- finalization are ignored.-+-- -- This computation is provided for a number of reasons. An embedding -- application might want to restart Python without having to restart the -- application itself. An application that has loaded the Python interpreter@@ -76,7 +77,7 @@ -- allocated by Python before unloading the DLL. During a hunt for memory -- leaks in an application a developer might want to free all memory -- allocated by Python before exiting from the application.-+-- -- /Bugs and caveats/: The destruction of modules and objects in modules is -- done in arbitrary order; this may cause destructors (@__del__()@ methods) -- to fail when they depend on other objects (even functions) or modules.@@ -87,7 +88,6 @@ -- modules may not be freed. Some extensions may not work properly if their -- initialization routine is called more than once; this can happen if an -- application calls 'initialize' and 'finalize' more than once.--- {# fun Py_Finalize as finalize {} -> `()' id #} @@ -102,7 +102,7 @@ -- variable. It has new standard I/O stream file objects @sys.stdin@, -- @sys.stdout@ and @sys.stderr@ (however these refer to the same underlying -- @FILE@ structures in the C library).--- +-- -- The return value points to the first thread state created in the new -- sub-interpreter. This thread state is made in the current thread state. -- Note that no actual thread is created; see the discussion of thread states@@ -113,7 +113,7 @@ -- held before calling this computation and is still held when it returns; -- however, unlike most other Python/C API computations, there -- needn’t be a current thread state on entry.)--- +-- -- Extension modules are shared between (sub-)interpreters as follows: the -- first time a particular extension is imported, it is initialized normally, -- and a (shallow) copy of its module’s dictionary is squirreled away.@@ -124,7 +124,7 @@ -- interpreter has been completely re-initialized by calling 'finalize' and -- 'initialize'; in that case, the extension’s @init/module/@ -- procedure is called again.--- +-- -- /Bugs and caveats/: Because sub-interpreters (and the main interpreter) -- are part of the same process, the insulation between them isn’t -- perfect — for example, using low-level file operations like@@ -140,12 +140,11 @@ -- sub-interpreters, since import operations executed by such objects may -- affect the wrong (sub-)interpreter’s dictionary of loaded modules. -- (XXX This is a hard-to-fix bug that will be addressed in a future release.)--- +-- -- Also note that the use of this functionality is incompatible with -- extension modules such as PyObjC and ctypes that use the @PyGILState_*()@ -- APIs (and this is inherent in the way the @PyGILState_*()@ procedures -- work). Simple things may work, but confusing behavior will always be near.--- newInterpreter :: IO (Maybe ThreadState) newInterpreter = do ptr <- {# call Py_NewInterpreter as ^ #}@@ -161,13 +160,11 @@ -- before calling this computation and is still held when it returns.) -- 'finalize' will destroy all sub-interpreters that haven’t been -- explicitly destroyed at that point.--- endInterpreter :: ThreadState -> IO () endInterpreter (ThreadState ptr) = {# call Py_EndInterpreter as ^ #} $ castPtr ptr -- | Return the program name set with 'setProgramName', or the default.--- getProgramName :: IO Text getProgramName = pyGetProgramName >>= peekTextW @@ -181,7 +178,6 @@ -- run-time libraries relative to the interpreter executable. The default -- value is @\"python\"@. No code in the Python interpreter will change the -- program name.--- setProgramName :: Text -> IO () setProgramName name = withTextW name cSetProgramName @@ -196,7 +192,6 @@ -- top-level Makefile and the /--prefix/ argument to the @configure@ script -- at build time. The value is available to Python code as @sys.prefix@. It -- is only useful on UNIX. See also 'getExecPrefix'.--- getPrefix :: IO Text getPrefix = pyGetPrefix >>= peekTextW @@ -211,13 +206,13 @@ -- top-level Makefile and the /--exec-prefix/ argument to the @configure@ -- script at build time. The value is available to Python code as -- @sys.exec_prefix@. It is only useful on UNIX.--- +-- -- Background: The exec-prefix differs from the prefix when platform -- dependent files (such as executables and shared libraries) are installed -- in a different directory tree. In a typical installation, platform -- dependent files may be installed in the @\/usr\/local\/plat@ subtree while -- platform independent may be installed in @\/usr\/local@.--- +-- -- Generally speaking, a platform is a combination of hardware and software -- families, e.g. Sparc machines running the Solaris 2.x operating system -- are considered the same platform, but Intel machines running Solaris@@ -229,11 +224,10 @@ -- empty string. Note that compiled Python bytecode files are platform -- independent (but not independent from the Python version by which they -- were compiled!).--- +-- -- System administrators will know how to configure the @mount@ or @automount@ -- programs to share @\/usr\/local@ between platforms while having -- @\/usr\/local\/plat@ be a different filesystem for each platform.--- getExecPrefix :: IO Text getExecPrefix = pyGetExecPrefix >>= peekTextW @@ -244,7 +238,6 @@ -- as a side-effect of deriving the default module search path from the -- program name (set by 'setProgramName' above). The value is available to -- Python code as @sys.executable@.--- getProgramFullPath :: IO Text getProgramFullPath = pyGetProgramFullPath >>= peekTextW @@ -258,7 +251,6 @@ -- character is @\':\'@ on Unix and Mac OS X, @\';\'@ on Windows. The value -- is available to Python code as the list @sys.path@, which may be modified -- to change the future search path for loaded modules.--- getPath :: IO Text getPath = pyGetPath >>= peekTextW @@ -267,16 +259,15 @@ -- | Return the version of this Python interpreter. This is a string that -- looks something like--- +-- -- @ -- \"3.0a5+ (py3k:63103M, May 12 2008, 00:53:55) \\n[GCC 4.2.3]\" -- @--- +-- -- The first word (up to the first space character) is the current Python -- version; the first three characters are the major and minor version -- separated by a period. The value is available to Python code as -- @sys.version@.--- {# fun Py_GetVersion as getVersion {} -> `Text' peekText* #} @@ -286,45 +277,41 @@ -- Solaris 2.x, which is also known as SunOS 5.x, the value is @\"sunos5\"@. -- On Mac OS X, it is @\"darwin\"@. On Windows, it is @\"win\"@. The value -- is available to Python code as @sys.platform@.--- {# fun Py_GetPlatform as getPlatform {} -> `Text' peekText* #} -- | Return the official copyright string for the current Python version, -- for example--- +-- -- @ -- \"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam\" -- @--- +-- -- The value is available to Python code as @sys.copyright@.--- {# fun Py_GetCopyright as getCopyright {} -> `Text' peekText* #} -- | Return an indication of the compiler used to build the current Python -- version, in square brackets, for example:--- +-- -- @ -- \"[GCC 2.7.2.2]\" -- @--- +-- -- The value is available to Python code as part of the variable -- @sys.version@.--- {# fun Py_GetCompiler as getCompiler {} -> `Text' peekText* #} -- | Return information about the sequence number and build date and time of -- the current Python interpreter instance, for example--- +-- -- @ -- \"#67, Aug 1 1997, 22:34:28\" -- @--- +-- -- The value is available to Python code as part of the variable -- @sys.version@.--- {# fun Py_GetBuildInfo as getBuildInfo {} -> `Text' peekText* #} @@ -334,11 +321,10 @@ -- interpreter. If there isn’t a script that will be run, the first -- parameter can be an empty string. If this function fails to initialize -- @sys.argv@, a fatal condition is signalled using @Py_FatalError()@.--- +-- -- This function also prepends the executed script’s path to -- @sys.path@. If no script is executed (in the case of calling @python -c@ -- or just the interactive interpreter), the empty string is used instead.--- setArgv :: Text -> [Text] -> IO () setArgv argv0 argv = mapWith withTextW (argv0 : argv) $ \textPtrs ->@@ -351,9 +337,8 @@ -- | Return the default “home”, that is, the value set by a -- previous call to 'setPythonHome', or the value of the @PYTHONHOME@ -- environment variable if it is set.--- -getPythonHome :: IO Text-getPythonHome = pyGetPythonHome >>= peekTextW+getPythonHome :: IO (Maybe Text)+getPythonHome = pyGetPythonHome >>= peekMaybeTextW foreign import ccall safe "hscpython-shim.h Py_GetPythonHome" pyGetPythonHome :: IO CWString@@ -362,9 +347,8 @@ -- of the standard Python libraries. The libraries are searched in -- @/home/\/lib\//python version/@ and @/home/\/lib\//python version/@. No -- code in the Python interpreter will change the Python home.--- -setPythonHome :: Text -> IO ()-setPythonHome name = withTextW name cSetPythonHome+setPythonHome :: Maybe Text -> IO ()+setPythonHome name = withMaybeTextW name cSetPythonHome foreign import ccall safe "hscpython-shim.h hscpython_SetPythonHome" cSetPythonHome :: CWString -> IO ()
lib/CPython/Constants.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Constants ( none , true@@ -22,32 +23,30 @@ , isTrue , isFalse ) where-import CPython.Internal #include <hscpython-shim.h> +import CPython.Internal+ -- | The Python @None@ object, denoting lack of value.--- -{# fun hscpython_Py_None as none+{# fun unsafe hscpython_Py_None as none {} -> `SomeObject' peekObject* #} -- | The Python @True@ object.--- -{# fun hscpython_Py_True as true+{# fun unsafe hscpython_Py_True as true {} -> `SomeObject' peekObject* #} -- | The Python @False@ object.--- -{# fun hscpython_Py_False as false+{# fun unsafe hscpython_Py_False as false {} -> `SomeObject' peekObject* #} -{# fun pure hscpython_Py_None as rawNone+{# fun pure unsafe hscpython_Py_None as rawNone {} -> `Ptr ()' id #} -{# fun pure hscpython_Py_True as rawTrue+{# fun pure unsafe hscpython_Py_True as rawTrue {} -> `Ptr ()' id #} -{# fun pure hscpython_Py_False as rawFalse+{# fun pure unsafe hscpython_Py_False as rawFalse {} -> `Ptr ()' id #} isNone :: SomeObject -> IO Bool
lib/CPython/Internal.chs view
@@ -1,21 +1,22 @@+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE ExistentialQuantification #-}+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}-{-# LANGUAGE ExistentialQuantification #-}-{-# LANGUAGE DeriveDataTypeable #-}+ module CPython.Internal ( -- * FFI support@@ -25,9 +26,12 @@ , cFromBool , peekText , peekTextW+ , peekMaybeTextW , withText , withTextW+ , withMaybeTextW , mapWith+ , unsafePerformIO -- * Fundamental types , SomeObject (..)@@ -71,15 +75,17 @@ , SomeIterator (..) , unsafeCastToIterator ) where-import Control.Applicative ((<$>))-import qualified Control.Exception as E-import qualified Data.Text as T-import Data.Typeable (Typeable)-import Foreign-import Foreign.C #include <hscpython-shim.h> +import Control.Applicative ((<$>))+import qualified Control.Exception as E+import qualified Data.Text as T+import Data.Typeable (Typeable)+import Foreign hiding (unsafePerformIO)+import Foreign.C+import System.IO.Unsafe (unsafePerformIO)+ cToBool :: CInt -> Bool cToBool = (/= 0) @@ -92,11 +98,17 @@ peekTextW :: CWString -> IO T.Text peekTextW = fmap T.pack . peekCWString +peekMaybeTextW :: CWString -> IO (Maybe T.Text)+peekMaybeTextW = maybePeek peekTextW+ withText :: T.Text -> (CString -> IO a) -> IO a withText = withCString . T.unpack withTextW :: T.Text -> (CWString -> IO a) -> IO a withTextW = withCWString . T.unpack++withMaybeTextW :: Maybe T.Text -> (CWString -> IO a) -> IO a+withMaybeTextW = maybeWith withTextW mapWith :: (a -> (b -> IO c) -> IO c) -> [a] -> ([b] -> IO c) -> IO c mapWith with' = step [] where
lib/CPython/Protocols/Iterator.chs view
@@ -1,32 +1,33 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Protocols.Iterator ( Iterator (..) , SomeIterator , castToIterator , next ) where-import CPython.Internal #include <hscpython-shim.h> +import CPython.Internal+ -- | Attempt to convert an object to a generic 'Iterator'. If the object does -- not implement the iterator protocol, returns 'Nothing'.--- castToIterator :: Object a => a -> IO (Maybe SomeIterator) castToIterator obj = withObject obj $ \objPtr -> do@@ -37,7 +38,6 @@ -- | Return the next value from the iteration, or 'Nothing' if there are no -- remaining items.--- next :: Iterator iter => iter -> IO (Maybe SomeObject) next iter = withObject iter $ \iterPtr -> do
lib/CPython/Protocols/Mapping.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Protocols.Mapping ( Mapping (..) , SomeMapping@@ -27,9 +28,10 @@ , values , items ) where-import CPython.Internal #include <hscpython-shim.h>++import CPython.Internal instance Mapping Dictionary where toMapping = unsafeCastToMapping
lib/CPython/Protocols/Number.chs view
@@ -1,20 +1,21 @@+{-# LANGUAGE ForeignFunctionInterface #-}+{-# LANGUAGE ExistentialQuantification #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}-{-# LANGUAGE ExistentialQuantification #-}+ module CPython.Protocols.Number ( Number (..) , SomeNumber@@ -52,17 +53,19 @@ , toFloat , toBase ) where-import Prelude hiding (Integer, Float, subtract, and, or, toInteger)-import qualified Prelude as Prelude-import CPython.Internal hiding (xor, shiftR, shiftL)-import CPython.Constants (none)-import CPython.Types.Complex (Complex)-import CPython.Types.Float (Float)-import CPython.Types.Integer (Integer)-import CPython.Types.Unicode (Unicode)-import CPython.Types.Set (Set, FrozenSet) #include <hscpython-shim.h>++import Prelude hiding (Integer, Float, subtract, and, or, toInteger)+import qualified Prelude as Prelude++import CPython.Constants (none)+import CPython.Internal hiding (xor, shiftR, shiftL)+import CPython.Types.Complex (Complex)+import CPython.Types.Float (Float)+import CPython.Types.Integer (Integer)+import CPython.Types.Set (Set, FrozenSet)+import CPython.Types.Unicode (Unicode) data SomeNumber = forall a. (Number a) => SomeNumber (ForeignPtr a)
lib/CPython/Protocols/Object.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Protocols.Object ( Object , Concrete@@ -54,21 +55,22 @@ , dir , getIterator ) where-import Prelude hiding (Ordering (..), print)++#include <hscpython-shim.h>++import Prelude hiding (Ordering (..), print) import qualified Data.Text as T-import System.IO (Handle, hPutStrLn)-import CPython.Internal hiding (toBool)+import System.IO (Handle, hPutStrLn)++import CPython.Internal hiding (toBool) import qualified CPython.Types.Bytes as B import qualified CPython.Types.Dictionary as D import qualified CPython.Types.Tuple as Tuple import qualified CPython.Types.Unicode as U -#include <hscpython-shim.h>- -- | Returns a 'Type' object corresponding to the object type of /self/. On -- failure, throws @SystemError@. This is equivalent to the Python expression -- @type(o)@.--- {# fun PyObject_Type as getType `Object self' => { withObject* `self'@@ -84,7 +86,7 @@ -- class object, nor a tuple, /inst/ must have a @__class__@ attribute ߞ -- the class relationship of the value of that attribute with /cls/ will be -- used to determine the result of this function.--- +-- -- Subclass determination is done in a fairly straightforward way, but -- includes a wrinkle that implementors of extensions to the class system -- may want to be aware of. If A and B are class objects, B is a subclass of@@ -95,7 +97,6 @@ -- Bߢs @__bases__@ attribute is searched in a depth-first fashion for -- A ߞ the presence of the @__bases__@ attribute is considered -- sufficient for this determination.--- {# fun PyObject_IsInstance as isInstance `(Object self, Object cls)' => { withObject* `self'@@ -109,7 +110,6 @@ -- returns 'True', otherwise it will be 'False'. If either /derived/ or /cls/ -- is not an actual class object (or tuple), this function uses the generic -- algorithm described above.--- {# fun PyObject_IsSubclass as isSubclass `(Object derived, Object cls)' => { withObject* `derived'@@ -118,7 +118,6 @@ -- | Attempt to cast an object to some concrete class. If the object -- isn't an instance of the class or subclass, returns 'Nothing'.--- cast :: (Object a, Concrete b) => a -> IO (Maybe b) cast obj = do let castObj = case toObject obj of@@ -131,7 +130,6 @@ -- | Returns 'True' if /self/ has an attribute with the given name, and -- 'False' otherwise. This is equivalent to the Python expression -- @hasattr(self, name)@--- {# fun PyObject_HasAttr as hasAttribute `Object self' => { withObject* `self'@@ -141,7 +139,6 @@ -- | Retrieve an attribute with the given name from object /self/. Returns -- the attribute value on success, and throws an exception on failure. This -- is the equivalent of the Python expression @self.name@.--- {# fun PyObject_GetAttr as getAttribute `Object self' => { withObject* `self'@@ -151,7 +148,6 @@ -- | Set the value of the attribute with the given name, for object /self/, -- to the value /v/. THrows an exception on failure. This is the equivalent -- of the Python statement @self.name = v@.--- {# fun PyObject_SetAttr as setAttribute `(Object self, Object v)' => { withObject* `self'@@ -162,7 +158,6 @@ -- | Delete an attribute with the given name, for object /self/. Throws an -- exception on failure. This is the equivalent of the Python statement -- @del self.name@.--- {# fun hscpython_PyObject_DelAttr as deleteAttribute `Object self' => { withObject* `self'@@ -170,13 +165,11 @@ } -> `()' checkStatusCode* #} -- | Print @repr(self)@ to a handle.--- print :: Object self => self -> Handle -> IO () print obj h = repr obj >>= U.fromUnicode >>= (hPutStrLn h . T.unpack) -- | Compute a string representation of object /self/, or throw an exception -- on failure. This is the equivalent of the Python expression @repr(self)@.--- {# fun PyObject_Repr as repr `Object self' => { withObject* `self'@@ -186,7 +179,6 @@ -- the non-ASCII characters in the string returned by 'repr' with @\x@, @\u@ -- or @\U@ escapes. This generates a string similar to that returned by -- 'repr' in Python 2.--- {# fun PyObject_ASCII as ascii `Object self' => { withObject* `self'@@ -194,7 +186,6 @@ -- | Compute a string representation of object /self/, or throw an exception -- on failure. This is the equivalent of the Python expression @str(self)@.--- {# fun PyObject_Str as string `Object self' => { withObject* `self'@@ -202,14 +193,12 @@ -- | Compute a bytes representation of object /self/, or throw an exception -- on failure. This is equivalent to the Python expression @bytes(self)@.--- {# fun PyObject_Bytes as bytes `Object self' => { withObject* `self' } -> `B.Bytes' stealObject* #} -- | Determine if the object /self/ is callable.--- {# fun PyCallable_Check as callable `Object self' => { withObject* `self'@@ -219,7 +208,6 @@ -- tuple and named arguments given by the dictionary. Returns the result of -- the call on success, or throws an exception on failure. This is the -- equivalent of the Python expression @self(*args, **kw)@.--- call :: Object self => self -> Tuple -> Dictionary -> IO SomeObject call self args kwargs = withObject self $ \selfPtr ->@@ -229,7 +217,6 @@ >>= stealObject -- | Call a callable Python object /self/, with arguments given by the list.--- callArgs :: Object self => self -> [SomeObject] -> IO SomeObject callArgs self args = do args' <- Tuple.toTuple args@@ -239,7 +226,6 @@ -- tuple and named arguments given by the dictionary. Returns the result of -- the call on success, or throws an exception on failure. This is the -- equivalent of the Python expression @self.method(args)@.--- callMethod :: Object self => self -> T.Text -> Tuple -> Dictionary -> IO SomeObject callMethod self name args kwargs = do method <- getAttribute self =<< U.toUnicode name@@ -249,7 +235,6 @@ -- list. Returns the result of the call on success, or throws an exception -- on failure. This is the equivalent of the Python expression -- @self.method(args)@.--- callMethodArgs :: Object self => self -> T.Text -> [SomeObject] -> IO SomeObject callMethodArgs self name args = do args' <- Tuple.toTuple args@@ -271,7 +256,6 @@ -- | Compare the values of /a/ and /b/ using the specified comparison. -- If an exception is raised, throws an exception.--- {# fun PyObject_RichCompareBool as richCompare `(Object a, Object b)' => { withObject* `a'@@ -282,7 +266,6 @@ -- | Returns 'True' if the object /self/ is considered to be true, and 'False' -- otherwise. This is equivalent to the Python expression @not not self@. On -- failure, throws an exception.--- {# fun PyObject_IsTrue as toBool `Object self' => { withObject* `self'@@ -291,7 +274,6 @@ -- | Compute and return the hash value of an object /self/. On failure, -- throws an exception. This is the equivalent of the Python expression -- @hash(self)@.--- {# fun PyObject_Hash as hash `Object self' => { withObject* `self'@@ -300,7 +282,6 @@ -- | This is equivalent to the Python expression @dir(self)@, returning a -- (possibly empty) list of strings appropriate for the object argument, -- or throws an exception if there was an error.--- {# fun PyObject_Dir as dir `Object self' => { withObject* `self'@@ -310,7 +291,6 @@ -- new iterator for the object argument, or the object itself if the object -- is already an iterator. Throws @TypeError@ if the object cannot be -- iterated.--- {# fun PyObject_GetIter as getIterator `Object self' => { withObject* `self'
lib/CPython/Protocols/Sequence.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Protocols.Sequence ( Sequence (..) , SomeSequence@@ -36,15 +37,17 @@ , toTuple , fast ) where-import Prelude hiding (repeat, length)-import Data.Text (Text)-import CPython.Internal-import CPython.Types.ByteArray (ByteArray)-import CPython.Types.Bytes (Bytes)-import CPython.Types.Unicode (Unicode) #include <hscpython-shim.h> +import Prelude hiding (repeat, length)+import Data.Text (Text)++import CPython.Internal+import CPython.Types.ByteArray (ByteArray)+import CPython.Types.Bytes (Bytes)+import CPython.Types.Unicode (Unicode)+ instance Sequence ByteArray where toSequence = unsafeCastToSequence @@ -62,7 +65,6 @@ -- | Attempt to convert an object to a generic 'Sequence'. If the object does -- not implement the sequence protocol, returns 'Nothing'.--- castToSequence :: Object a => a -> IO (Maybe SomeSequence) castToSequence obj = withObject obj $ \objPtr -> do@@ -155,7 +157,6 @@ -- | Return the first index /i/ for which @self[i] == v@. This is equivalent -- to the Python expression @self.index(v)@.--- {# fun PySequence_Index as index `(Sequence self, Object v)' => { withObject* `self'@@ -164,7 +165,6 @@ -- | Return a list object with the same contents as the arbitrary sequence -- /seq/. The returned list is guaranteed to be new.--- {# fun PySequence_List as toList `Sequence seq' => { withObject* `seq'@@ -172,7 +172,6 @@ -- | Return a tuple object with the same contents as the arbitrary sequence -- /seq/. If /seq/ is already a tuple, it is re-used rather than copied.--- {# fun PySequence_Tuple as toTuple `Sequence seq' => { withObject* `seq'@@ -181,7 +180,6 @@ -- | Returns the sequence /seq/ as a tuple, unless it is already a tuple or -- list, in which case /seq/ is returned. If an error occurs, throws -- @TypeError@ with the given text as the exception text.--- {# fun PySequence_Fast as fast `Sequence seq' => { withObject* `seq'
lib/CPython/Reflection.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+-- module CPython.Reflection ( getBuiltins , getLocals@@ -22,38 +23,35 @@ , getFunctionName , getFunctionDescription ) where-import Data.Text (Text)-import CPython.Internal #include <hscpython-shim.h> +import Data.Text (Text)++import CPython.Internal+ -- | Return a 'Dictionary' of the builtins in the current execution frame, -- or the interpreter of the thread state if no frame is currently executing.--- {# fun PyEval_GetBuiltins as getBuiltins {} -> `Dictionary' peekObject* #} -- | Return a 'Dictionary' of the local variables in the current execution -- frame, or 'Nothing' if no frame is currently executing.--- getLocals :: IO (Maybe Dictionary) getLocals = {# call PyEval_GetLocals as ^#} >>= maybePeek peekObject -- | Return a 'Dictionary' of the global variables in the current execution -- frame, or 'Nothing' if no frame is currently executing.--- getGlobals :: IO (Maybe Dictionary) getGlobals = {# call PyEval_GetGlobals as ^#} >>= maybePeek peekObject -- | Return the current thread state's frame, which is 'Nothing' if no frame -- is currently executing.--- getFrame :: IO (Maybe SomeObject) getFrame = {# call PyEval_GetFrame as ^#} >>= maybePeek peekObject -- | Return the name of /func/ if it is a function, class or instance object, -- else the name of /func/'s type.--- {# fun PyEval_GetFuncName as getFunctionName `Object func' => { withObject* `func'@@ -63,7 +61,6 @@ -- values include @\"()\"@ for functions and methods, @\"constructor\"@, -- @\"instance\"@, and @\"object\"@. Concatenated with the result of -- 'getFunctionName', the result will be a description of /func/.--- {# fun PyEval_GetFuncDesc as getFunctionDescription `Object func' => { withObject* `func'
lib/CPython/System.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.System ( getObject , setObject@@ -22,14 +23,15 @@ , addWarnOption , setPath ) where-import Data.Text (Text)-import CPython.Internal #include <hscpython-shim.h> +import Data.Text (Text)++import CPython.Internal+ -- | Return the object /name/ from the @sys@ module, or 'Nothing' if it does -- not exist.--- getObject :: Text -> IO (Maybe SomeObject) getObject name = withText name $ \cstr -> do@@ -39,7 +41,6 @@ -- getFile -- | Set /name/ in the @sys@ module to a value.--- setObject :: Object a => Text -> a -> IO () setObject name v = withText name $ \cstr ->@@ -48,7 +49,6 @@ >>= checkStatusCode -- | Delete /name/ from the @sys@ module.--- deleteObject :: Text -> IO () deleteObject name = withText name $ \cstr ->@@ -56,12 +56,10 @@ >>= checkStatusCode -- | Reset @sys.warnoptions@ to an empty list.--- {# fun PySys_ResetWarnOptions as resetWarnOptions {} -> `()' id #} -- | Add an entry to @sys.warnoptions@.--- addWarnOption :: Text -> IO () addWarnOption str = withTextW str pySysAddWarnOption @@ -71,7 +69,6 @@ -- | Set @sys.path@ to a list object of paths found in the parameter, which -- should be a list of paths separated with the platform's search path -- delimiter (@\':\'@ on Unix, @\';\'@ on Windows).--- setPath :: Text -> IO () setPath path = withTextW path pySysSetPath
lib/CPython/Types.hs view
@@ -1,18 +1,18 @@ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- + module CPython.Types ( -- * Types and classes@@ -92,25 +92,26 @@ , toUnicode , fromUnicode ) where-import CPython.Types.ByteArray-import CPython.Types.Bytes-import CPython.Types.Capsule-import CPython.Types.Cell-import CPython.Types.Code-import CPython.Types.Complex-import CPython.Types.Dictionary-import CPython.Types.Exception-import CPython.Types.Float-import CPython.Types.Function-import CPython.Types.InstanceMethod-import CPython.Types.Integer-import CPython.Types.Iterator-import CPython.Types.List-import CPython.Types.Method-import CPython.Types.Module-import CPython.Types.Set-import CPython.Types.Slice-import CPython.Types.Tuple-import CPython.Types.Type-import CPython.Types.Unicode-import CPython.Types.WeakReference++import CPython.Types.ByteArray+import CPython.Types.Bytes+import CPython.Types.Capsule+import CPython.Types.Cell+import CPython.Types.Code+import CPython.Types.Complex+import CPython.Types.Dictionary+import CPython.Types.Exception+import CPython.Types.Float+import CPython.Types.Function+import CPython.Types.InstanceMethod+import CPython.Types.Integer+import CPython.Types.Iterator+import CPython.Types.List+import CPython.Types.Method+import CPython.Types.Module+import CPython.Types.Set+import CPython.Types.Slice+import CPython.Types.Tuple+import CPython.Types.Type+import CPython.Types.Unicode+import CPython.Types.WeakReference
lib/CPython/Types/ByteArray.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.ByteArray ( ByteArray , byteArrayType@@ -24,12 +25,14 @@ , length , resize ) where-import Prelude hiding (length)++#include <hscpython-shim.h>++import Prelude hiding (length) import qualified Data.ByteString as B import qualified Data.ByteString.Unsafe as B-import CPython.Internal -#include <hscpython-shim.h>+import CPython.Internal newtype ByteArray = ByteArray (ForeignPtr ByteArray) @@ -40,7 +43,7 @@ instance Concrete ByteArray where concreteType _ = byteArrayType -{# fun pure hscpython_PyByteArray_Type as byteArrayType+{# fun pure unsafe hscpython_PyByteArray_Type as byteArrayType {} -> `Type' peekStaticObject* #} toByteArray :: B.ByteString -> IO ByteArray@@ -58,7 +61,6 @@ -- | Create a new byte array from any object which implements the buffer -- protocol.--- {# fun PyByteArray_FromObject as fromObject `Object self ' => { withObject* `self'
lib/CPython/Types/Bytes.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Bytes ( Bytes , bytesType@@ -23,12 +24,14 @@ , length , append ) where-import Prelude hiding (length)++#include <hscpython-shim.h>++import Prelude hiding (length) import qualified Data.ByteString as B import qualified Data.ByteString.Unsafe as B-import CPython.Internal -#include <hscpython-shim.h>+import CPython.Internal newtype Bytes = Bytes (ForeignPtr Bytes) @@ -39,7 +42,7 @@ instance Concrete Bytes where concreteType _ = bytesType -{# fun pure hscpython_PyBytes_Type as bytesType+{# fun pure unsafe hscpython_PyBytes_Type as bytesType {} -> `Type' peekStaticObject* #} toBytes :: B.ByteString -> IO Bytes@@ -61,7 +64,6 @@ -- | Create a new byte string from any object which implements the buffer -- protocol.--- {# fun PyBytes_FromObject as fromObject `Object self ' => { withObject* `self'@@ -81,4 +83,3 @@ {# call PyBytes_Concat as ^ #} tempPtr nextPtr newSelf <- peek tempPtr stealObject newSelf-
lib/CPython/Types/Capsule.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Capsule ( Capsule , capsuleType@@ -29,11 +30,13 @@ , setContext --, setName ) where-import Data.Text (Text)-import CPython.Internal hiding (new) #include <hscpython-shim.h> +import Data.Text (Text)++import CPython.Internal hiding (new)+ -- type Destructor = Ptr () -> IO () newtype Capsule = Capsule (ForeignPtr Capsule) @@ -44,7 +47,7 @@ instance Concrete Capsule where concreteType _ = capsuleType -{# fun pure hscpython_PyCapsule_Type as capsuleType+{# fun pure unsafe hscpython_PyCapsule_Type as capsuleType {} -> `Type' peekStaticObject* #} -- new :: Ptr () -> Maybe Text -> Destructor -> IO Capsule@@ -52,12 +55,11 @@ -- | Retrieve the pointer stored in the capsule. On failure, throws an -- exception.--- +-- -- The name parameter must compare exactly to the name stored in the capsule. -- If the name stored in the capsule is 'Nothing', the name passed in must -- also be 'Nothing'. Python uses the C function strcmp() to compare capsule -- names.--- getPointer :: Capsule -> Maybe Text -> IO (Ptr ()) getPointer py name = withObject py $ \pyPtr ->@@ -68,7 +70,6 @@ -- getDestructor = undefined -- | Return the current context stored in the capsule, which might be @NULL@.--- getContext :: Capsule -> IO (Ptr ()) getContext py = withObject py $ \pyPtr -> do@@ -82,7 +83,6 @@ return ptr -- | Return the current name stored in the capsule, which might be 'Nothing'.--- getName :: Capsule -> IO (Maybe Text) getName py = withObject py $ \pyPtr -> do@@ -101,11 +101,10 @@ -- string exactly. If the second parameter is 'False', import the module -- without blocking (using @PyImport_ImportModuleNoBlock()@). Otherwise, -- imports the module conventionally (using @PyImport_ImportModule()@).--- +-- -- Return the capsule’s internal pointer on success. On failure, throw -- an exception. If the module could not be imported, and if importing in -- non-blocking mode, returns 'Nothing'.--- importNamed :: Text -> Bool -> IO (Maybe (Ptr ())) importNamed name block = withText name $ \namePtr ->@@ -123,10 +122,9 @@ -- 'capsuleType', has a non-NULL pointer stored in it, and its internal name -- matches the name parameter. (See 'getPointer' for information on how -- capsule names are compared.)--- +-- -- In other words, if 'isValid' returns 'True', calls to any of the -- accessors (any function starting with @get@) are guaranteed to succeed.--- isValid :: Capsule -> Maybe Text -> IO Bool isValid py name = withObject py $ \pyPtr ->@@ -135,7 +133,6 @@ >>= checkBoolReturn -- | Set the void pointer inside the capsule. The pointer may not be @NULL@.--- {# fun PyCapsule_SetPointer as setPointer { withObject* `Capsule' , id `Ptr ()'@@ -145,7 +142,6 @@ -- setDestructor = undefined -- | Set the context pointer inside the capsule.--- {# fun PyCapsule_SetContext as setContext { withObject* `Capsule' , id `Ptr ()'
lib/CPython/Types/Cell.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Cell ( Cell , cellType@@ -21,10 +22,11 @@ , get , set ) where-import CPython.Internal hiding (new) #include <hscpython-shim.h> +import CPython.Internal hiding (new)+ newtype Cell = Cell (ForeignPtr Cell) instance Object Cell where@@ -34,11 +36,10 @@ instance Concrete Cell where concreteType _ = cellType -{# fun pure hscpython_PyCell_Type as cellType+{# fun pure unsafe hscpython_PyCell_Type as cellType {} -> `Type' peekStaticObject* #} -- | Create and return a new cell containing the value /obj/.--- new :: Object obj => Maybe obj -> IO Cell new obj = maybeWith withObject obj $ \objPtr ->@@ -46,7 +47,6 @@ >>= stealObject -- | Return the contents of a cell.--- get :: Cell -> IO (Maybe SomeObject) get cell = withObject cell $ \cellPtr ->@@ -55,7 +55,6 @@ -- | Set the contents of a cell to /obj/. This releases the reference to any -- current content of the cell.--- set :: Object obj => Cell -> Maybe obj -> IO () set cell obj = withObject cell $ \cellPtr ->
lib/CPython/Types/Code.chs view
@@ -1,27 +1,29 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Code ( Code , codeType ) where-import CPython.Internal #include <hscpython-shim.h> +import CPython.Internal+ newtype Code = Code (ForeignPtr Code) instance Object Code where@@ -31,5 +33,5 @@ instance Concrete Code where concreteType _ = codeType -{# fun pure hscpython_PyCode_Type as codeType+{# fun pure unsafe hscpython_PyCode_Type as codeType {} -> `Type' peekStaticObject* #}
lib/CPython/Types/Complex.chs view
@@ -1,30 +1,33 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Complex ( Complex , complexType , toComplex , fromComplex ) where-import qualified Data.Complex as C-import CPython.Internal #include <hscpython-shim.h> +import qualified Data.Complex as C++import CPython.Internal+ newtype Complex = Complex (ForeignPtr Complex) instance Object Complex where@@ -34,7 +37,7 @@ instance Concrete Complex where concreteType _ = complexType -{# fun pure hscpython_PyComplex_Type as complexType+{# fun pure unsafe hscpython_PyComplex_Type as complexType {} -> `Type' peekStaticObject* #} toComplex :: C.Complex Double -> IO Complex
lib/CPython/Types/Dictionary.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Dictionary ( Dictionary , dictionaryType@@ -32,14 +33,15 @@ , update , mergeFromSeq2 ) where-import CPython.Internal hiding (new) #include <hscpython-shim.h> +import CPython.Internal hiding (new)+ instance Concrete Dictionary where concreteType _ = dictionaryType -{# fun pure hscpython_PyDict_Type as dictionaryType+{# fun pure unsafe hscpython_PyDict_Type as dictionaryType {} -> `Type' peekStaticObject* #} {# fun PyDict_New as new@@ -48,7 +50,6 @@ -- newProxy -- | Empty an existing dictionary of all key-value pairs.--- {# fun PyDict_Clear as clear { withObject* `Dictionary' } -> `()' id #}@@ -56,7 +57,6 @@ -- | Determine if a dictionary contains /key/. If an item in the dictionary -- matches /key/, return 'True', otherwise return 'False'. On error, throws -- an exception. This is equivalent to the Python expression @key in d@.--- {# fun PyDict_Contains as contains `Object key' => { withObject* `Dictionary'@@ -65,14 +65,12 @@ -- | Return a new dictionary that contains the same key-value pairs as the -- old dictionary.--- {# fun PyDict_Copy as copy { withObject* `Dictionary' } -> `Dictionary' stealObject* #} -- | Return the object from a dictionary which has a key /key/. Return -- 'Nothing' if the key is not present.--- getItem :: Object key => Dictionary -> key -> IO (Maybe SomeObject) getItem dict key = withObject dict $ \dict' ->@@ -90,7 +88,6 @@ -- | Inserts /value/ into a dictionary with a key of /key/. /key/ must be -- hashable; if it isn’t, throws @TypeError@.--- {# fun PyDict_SetItem as setItem `(Object key, Object value)' => { withObject* `Dictionary'@@ -102,7 +99,6 @@ -- | Remove the entry in a dictionary with key /key/. /key/ must be hashable; -- if it isn’t, throws @TypeError@.--- {# fun PyDict_DelItem as deleteItem `Object key' => { withObject* `Dictionary'@@ -113,21 +109,18 @@ -- | Return a 'List' containing all the items in the dictionary, as in -- the Python method @dict.items()@.--- {# fun PyDict_Items as items { withObject* `Dictionary' } -> `List' stealObject* #} -- | Return a 'List' containing all the keys in the dictionary, as in -- the Python method @dict.keys()@.--- {# fun PyDict_Keys as keys { withObject* `Dictionary' } -> `List' stealObject* #} -- | Return a 'List' containing all the values in the dictionary, as in -- the Python method @dict.values()@.--- {# fun PyDict_Values as values { withObject* `Dictionary' } -> `List' stealObject* #}@@ -145,7 +138,6 @@ -- If the third parameter is 'True', existing pairs in will be replaced if a -- matching key is found in /b/, otherwise pairs will only be added if there -- is not already a matching key.--- {# fun PyDict_Merge as merge `Mapping b' => { withObject* `Dictionary'@@ -155,7 +147,6 @@ -- | This is the same as @(\\a b -> 'merge' a b True)@ in Haskell, or -- @a.update(b)@ in Python.--- {# fun PyDict_Update as update `Mapping b' => { withObject* `Dictionary'@@ -167,14 +158,13 @@ -- viewed as key-value pairs. In case of duplicate keys, the last wins if -- the third parameter is 'True', otherwise the first wins. Equivalent -- Python:--- +-- -- @ -- def mergeFromSeq2(a, seq2, override): -- for key, value in seq2: -- if override or key not in a: -- a[key] = value -- @--- {# fun PyDict_MergeFromSeq2 as mergeFromSeq2 `Object seq2' => { withObject* `Dictionary'
lib/CPython/Types/Exception.chs view
@@ -1,25 +1,25 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Exception ( Exception , exceptionType , exceptionValue , exceptionTraceback ) where-import CPython.Internal -#include <hscpython-shim.h>+import CPython.Internal
lib/CPython/Types/Float.chs view
@@ -1,30 +1,33 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Float ( Float , floatType , toFloat , fromFloat ) where-import Prelude hiding (Float)-import CPython.Internal #include <hscpython-shim.h> +import Prelude hiding (Float)++import CPython.Internal+ newtype Float = Float (ForeignPtr Float) instance Object Float where@@ -34,7 +37,7 @@ instance Concrete Float where concreteType _ = floatType -{# fun pure hscpython_PyFloat_Type as floatType+{# fun pure unsafe hscpython_PyFloat_Type as floatType {} -> `Type' peekStaticObject* #} {# fun PyFloat_FromDouble as toFloat
lib/CPython/Types/Function.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Function ( Function , functionType@@ -28,12 +29,13 @@ , getAnnotations , setAnnotations ) where-import CPython.Internal hiding (new)-import CPython.Types.Code (Code)-import qualified CPython.Constants as Const #include <hscpython-shim.h> +import qualified CPython.Constants as Const+import CPython.Internal hiding (new)+import CPython.Types.Code (Code)+ newtype Function = Function (ForeignPtr Function) instance Object Function where@@ -43,28 +45,25 @@ instance Concrete Function where concreteType _ = functionType -{# fun pure hscpython_PyFunction_Type as functionType+{# fun pure unsafe hscpython_PyFunction_Type as functionType {} -> `Type' peekStaticObject* #} -- | Return a new function associated with the given code object. The second -- parameter will be used as the globals accessible to the function.--- +-- -- The function's docstring, name, and @__module__@ are retrieved from the -- code object. The parameter defaults and closure are set to 'Nothing'.--- {# fun PyFunction_New as new { withObject* `Code' , withObject* `Dictionary' } -> `Function' stealObject* #} -- | Return the code object associated with a function.--- {# fun PyFunction_GetCode as getCode { withObject* `Function' } -> `Code' peekObject* #} -- | Return the globals dictionary associated with a function.--- {# fun PyFunction_GetGlobals as getGlobals { withObject* `Function' } -> `Dictionary' peekObject* #}@@ -72,7 +71,6 @@ -- | Return the @__module__@ attribute of a function. This is normally -- a 'Unicode' containing the module name, but can be set to any other -- object by Python code.--- {# fun PyFunction_GetModule as getModule { withObject* `Function' } -> `SomeObject' peekObject* #}@@ -88,13 +86,11 @@ -- | Return the default parameter values for a function. This can be a tuple -- or 'Nothing'.--- {# fun PyFunction_GetDefaults as getDefaults { withObject* `Function' } -> `Maybe Tuple' peekNullableObject* #} -- | Set the default values for a function.--- {# fun PyFunction_SetDefaults as setDefaults { withObject* `Function' , withNullableObject* `Maybe Tuple'@@ -102,14 +98,12 @@ -- | Return the closure associated with a function. This can be 'Nothing', -- or a tuple of 'Cell's.--- {# fun PyFunction_GetClosure as getClosure { withObject* `Function' } -> `Maybe Tuple' peekNullableObject* #} -- | Set the closure associated with a function. The tuple should contain -- 'Cell's.--- {# fun PyFunction_SetClosure as setClosure { withObject* `Function' , withNullableObject* `Maybe Tuple'@@ -117,13 +111,11 @@ -- | Return the annotations for a function. This can be a mutable dictionary, -- or 'Nothing'.--- {# fun PyFunction_GetAnnotations as getAnnotations { withObject* `Function' } -> `Maybe Dictionary' peekNullableObject* #} -- | Set the annotations for a function object.--- {# fun PyFunction_SetAnnotations as setAnnotations { withObject* `Function' , withNullableObject* `Maybe Dictionary'
lib/CPython/Types/InstanceMethod.chs view
@@ -1,29 +1,31 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.InstanceMethod ( InstanceMethod , instanceMethodType , new , function ) where-import CPython.Internal hiding (new) #include <hscpython-shim.h> +import CPython.Internal hiding (new)+ newtype InstanceMethod = InstanceMethod (ForeignPtr InstanceMethod) instance Object InstanceMethod where@@ -33,7 +35,7 @@ instance Concrete InstanceMethod where concreteType _ = instanceMethodType -{# fun pure hscpython_PyInstanceMethod_Type as instanceMethodType+{# fun pure unsafe hscpython_PyInstanceMethod_Type as instanceMethodType {} -> `Type' peekStaticObject* #} {# fun PyInstanceMethod_New as new
lib/CPython/Types/Integer.chs view
@@ -1,33 +1,36 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Integer ( Integer , integerType , toInteger , fromInteger ) where-import Prelude hiding (Integer, toInteger, fromInteger)++#include <hscpython-shim.h>++import Prelude hiding (Integer, toInteger, fromInteger) import qualified Prelude as Prelude import qualified Data.Text as T-import CPython.Internal-import qualified CPython.Types.Unicode as U-import qualified CPython.Protocols.Object as O -#include <hscpython-shim.h>+import CPython.Internal+import qualified CPython.Protocols.Object as O+import qualified CPython.Types.Unicode as U newtype Integer = Integer (ForeignPtr Integer) @@ -38,7 +41,7 @@ instance Concrete Integer where concreteType _ = integerType -{# fun pure hscpython_PyLong_Type as integerType+{# fun pure unsafe hscpython_PyLong_Type as integerType {} -> `Type' peekStaticObject* #} toInteger :: Prelude.Integer -> IO Integer
lib/CPython/Types/Iterator.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Iterator ( SequenceIterator , sequenceIteratorType@@ -23,10 +24,11 @@ , callableIteratorType , callableIteratorNew ) where-import CPython.Internal #include <hscpython-shim.h> +import CPython.Internal+ newtype SequenceIterator = SequenceIterator (ForeignPtr SequenceIterator) instance Iterator SequenceIterator where@@ -51,16 +53,15 @@ instance Concrete CallableIterator where concreteType _ = callableIteratorType -{# fun pure hscpython_PySeqIter_Type as sequenceIteratorType+{# fun pure unsafe hscpython_PySeqIter_Type as sequenceIteratorType {} -> `Type' peekStaticObject* #} -{# fun pure hscpython_PyCallIter_Type as callableIteratorType+{# fun pure unsafe hscpython_PyCallIter_Type as callableIteratorType {} -> `Type' peekStaticObject* #} -- | Return an 'Iterator' that works with a general sequence object, /seq/. -- The iteration ends when the sequence raises @IndexError@ for the -- subscripting operation.--- {# fun PySeqIter_New as sequenceIteratorNew `Sequence seq' => { withObject* `seq'@@ -70,7 +71,6 @@ -- Python callable object that can be called with no parameters; each call -- to it should return the next item in the iteration. When /callable/ -- returns a value equal to /sentinel/, the iteration will be terminated.--- {# fun PyCallIter_New as callableIteratorNew `(Object callable, Object sentinel)' => { withObject* `callable'
lib/CPython/Types/List.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.List ( List , listType@@ -31,16 +32,18 @@ , reverse , toTuple ) where-import Prelude hiding (reverse, length)-import CPython.Internal hiding (new)-import qualified CPython.Types.Tuple as T #include <hscpython-shim.h> +import Prelude hiding (reverse, length)++import CPython.Internal hiding (new)+import qualified CPython.Types.Tuple as T+ instance Concrete List where concreteType _ = listType -{# fun pure hscpython_PyList_Type as listType+{# fun pure unsafe hscpython_PyList_Type as listType {} -> `Type' peekStaticObject* #} toList :: [SomeObject] -> IO List@@ -51,7 +54,6 @@ >>= stealObject -- | Convert any object implementing the iterator protocol to a 'List'.--- iterableToList :: Object iter => iter -> IO List iterableToList iter = do raw <- callObjectRaw listType =<< T.toTuple [toObject iter]@@ -73,14 +75,12 @@ -- | Returns the object at a given position in the list. The position must be -- positive; indexing from the end of the list is not supported. If the -- position is out of bounds, throws an @IndexError@ exception.--- {# fun PyList_GetItem as getItem { withObject* `List' , fromIntegral `Integer' } -> `SomeObject' peekObject* #} -- | Set the item at a given index.--- setItem :: Object o => List -> Integer -> o -> IO () setItem self index x = withObject self $ \selfPtr ->@@ -91,7 +91,6 @@ -- | Inserts /item/ into the list in front of the given index. Throws an -- exception if unsuccessful. Analogous to @list.insert(index, item)@.--- {# fun PyList_Insert as insert `Object item' => { withObject* `List'@@ -101,7 +100,6 @@ -- | Append /item/ to the end of th list. Throws an exception if unsuccessful. -- Analogous to @list.append(item)@.--- {# fun PyList_Append as append `Object item' => { withObject* `List'@@ -112,7 +110,6 @@ -- the given indexes. Throws an exception if unsuccessful. Analogous to -- @list[low:high]@. Negative indices, as when slicing from Python, are not -- supported.--- {# fun PyList_GetSlice as getSlice { withObject* `List' , fromIntegral `Integer'@@ -124,7 +121,6 @@ -- replacement may be 'Nothing', indicating the assignment of an empty list -- (slice deletion). Negative indices, as when slicing from Python, are not -- supported.--- setSlice :: List -> Integer -- ^ Low@@ -140,21 +136,18 @@ >>= checkStatusCode -- | Sort the items of a list in place. This is equivalent to @list.sort()@.--- {# fun PyList_Sort as sort { withObject* `List' } -> `()' checkStatusCode* #} -- | Reverses the items of a list in place. This is equivalent to -- @list.reverse()@.--- {# fun PyList_Reverse as reverse { withObject* `List' } -> `()' checkStatusCode* #} -- | Return a new 'Tuple' containing the contents of a list; equivalent to -- @tuple(list)@.--- {# fun PyList_AsTuple as toTuple { withObject* `List' } -> `Tuple' stealObject* #}
lib/CPython/Types/Method.chs view
@@ -1,19 +1,21 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+--+ module CPython.Types.Method ( Method , methodType@@ -21,10 +23,11 @@ , function , self ) where-import CPython.Internal hiding (new) #include <hscpython-shim.h> +import CPython.Internal hiding (new)+ newtype Method = Method (ForeignPtr Method) instance Object Method where@@ -34,7 +37,7 @@ instance Concrete Method where concreteType _ = methodType -{# fun pure hscpython_PyMethod_Type as methodType+{# fun pure unsafe hscpython_PyMethod_Type as methodType {} -> `Type' peekStaticObject* #} {# fun PyMethod_New as new
lib/CPython/Types/Module.chs view
@@ -1,19 +1,21 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+--+ module CPython.Types.Module ( Module , moduleType@@ -27,14 +29,16 @@ , importModule , reload ) where-import Prelude hiding (toInteger)-import Data.Text (Text)-import CPython.Internal hiding (new)-import CPython.Types.Integer (toInteger)-import CPython.Types.Unicode (toUnicode) #include <hscpython-shim.h> +import Prelude hiding (toInteger)+import Data.Text (Text)++import CPython.Internal hiding (new)+import CPython.Types.Integer (toInteger)+import CPython.Types.Unicode (toUnicode)+ newtype Module = Module (ForeignPtr Module) instance Object Module where@@ -44,13 +48,12 @@ instance Concrete Module where concreteType _ = moduleType -{# fun pure hscpython_PyModule_Type as moduleType+{# fun pure unsafe hscpython_PyModule_Type as moduleType {} -> `Type' peekStaticObject* #} -- | Return a new module object with the @__name__@ attribute set. Only the -- module’s @__doc__@ and @__name__@ attributes are filled in; the -- caller is responsible for providing a @__file__@ attribute.--- {# fun PyModule_New as new { withText* `Text' } -> `Module' stealObject* #}@@ -59,14 +62,12 @@ -- this object is the same as the @__dict__@ attribute of the module. This -- computation never fails. It is recommended extensions use other -- computations rather than directly manipulate a module’s @__dict__@.--- {# fun PyModule_GetDict as getDictionary { withObject* `Module' } -> `Dictionary' peekObject* #} -- | Returns a module’s @__name__@ value. If the module does not -- provide one, or if it is not a string, throws @SystemError@.--- getName :: Module -> IO Text getName py = withObject py $ \py' -> do@@ -77,7 +78,6 @@ -- | Returns the name of the file from which a module was loaded using the -- module’s @__file__@ attribute. If this is not defined, or if it is -- not a string, throws @SystemError@.--- getFilename :: Module -> IO Text getFilename py = withObject py $ \py' -> do@@ -88,7 +88,6 @@ -- | Add an object to a module with the given name. This is a convenience -- computation which can be used from the module’s initialization -- computation.--- addObject :: Object value => Module -> Text -> value -> IO () addObject py name val = withObject py $ \py' ->@@ -100,13 +99,11 @@ -- | Add an integer constant to a module. This convenience computation can be -- used from the module’s initialization computation.--- addIntegerConstant :: Module -> Text -> Integer -> IO () addIntegerConstant m name value = toInteger value >>= addObject m name -- | Add a string constant to a module. This convenience computation can be -- used from the module’s initialization computation.--- addTextConstant :: Module -> Text -> Text -> IO () addTextConstant m name value = toUnicode value >>= addObject m name @@ -115,9 +112,8 @@ -- invokes the @__import__()@ computation from the @__builtins__@ of the -- current globals. This means that the import is done using whatever import -- hooks are installed in the current environment.--- +-- -- This computation always uses absolute imports.--- importModule :: Text -> IO Module importModule name = do pyName <- toUnicode name@@ -127,7 +123,6 @@ -- | Reload a module. If an error occurs, an exception is thrown and the old -- module still exists.--- {# fun PyImport_ReloadModule as reload { withObject* `Module' } -> `Module' stealObject* #}
lib/CPython/Types/Set.chs view
@@ -1,26 +1,25 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-} -- | Any functionality not listed below is best accessed using the either -- the 'Object' protocol (including 'callMethod', 'richCompare', 'hash', -- 'repr', 'isTrue', and 'getIter') or the 'Number' protocol (including 'and', -- 'subtract', 'or', 'xor', 'inPlaceAnd', 'inPlaceSubtract', 'inPlaceOr', -- and 'inPlaceXor').--- module CPython.Types.Set ( AnySet , Set@@ -39,11 +38,12 @@ , pop , clear ) where-import CPython.Internal-import CPython.Types.Tuple (toTuple, iterableToTuple, fromTuple) #include <hscpython-shim.h> +import CPython.Internal+import CPython.Types.Tuple (toTuple, iterableToTuple, fromTuple)+ class Object a => AnySet a newtype Set = Set (ForeignPtr Set)@@ -67,10 +67,10 @@ instance AnySet Set instance AnySet FrozenSet -{# fun pure hscpython_PySet_Type as setType+{# fun pure unsafe hscpython_PySet_Type as setType {} -> `Type' peekStaticObject* #} -{# fun pure hscpython_PyFrozenSet_Type as frozenSetType+{# fun pure unsafe hscpython_PyFrozenSet_Type as frozenSetType {} -> `Type' peekStaticObject* #} toSet :: [SomeObject] -> IO Set@@ -82,7 +82,6 @@ -- | Return a new 'Set' from the contents of an iterable 'Object'. The object -- may be 'Nothing' to create an empty set. Throws a @TypeError@ if the object -- is not iterable.--- {# fun PySet_New as iterableToSet `Object obj' => { withObject* `obj'@@ -91,7 +90,6 @@ -- | Return a new 'FrozenSet' from the contents of an iterable 'Object'. The -- object may be 'Nothing' to create an empty frozen set. Throws a @TypeError@ -- if the object is not iterable.--- {# fun PyFrozenSet_New as iterableToFrozenSet `Object obj' => { withObject* `obj'@@ -101,7 +99,6 @@ fromSet set = iterableToTuple set >>= fromTuple -- | Return the size of a 'Set' or 'FrozenSet'.--- {# fun PySet_Size as size `AnySet set' => { withObject* `set'@@ -111,7 +108,6 @@ -- @__contains__()@ method, this computation does not automatically convert -- unhashable 'Set's into temporary 'FrozenSet's. Throws a @TypeError@ if the -- key is unhashable.--- {# fun PySet_Contains as contains `(AnySet set, Object key)' => { withObject* `set'@@ -123,7 +119,6 @@ -- brand new 'FrozenSet's before they are exposed to other code). Throws a -- @TypeError@ if the key is unhashable. Throws a @MemoryError@ if there is -- no room to grow.--- {# fun PySet_Add as add `(AnySet set, Object key)' => { withObject* `set'@@ -135,7 +130,6 @@ -- if /key/ is unhashable. Unlike the Python @discard()@ method, this -- computation does not automatically convert unhashable sets into temporary -- 'FrozenSet's.--- {# fun PySet_Discard as discard `Object key' => { withObject* `Set'@@ -144,13 +138,11 @@ -- | Return an arbitrary object in the set, and removes the object from the -- set. Throws @KeyError@ if the set is empty.--- {# fun PySet_Pop as pop { withObject* `Set' } -> `SomeObject' stealObject* #} -- | Remove all elements from a set.--- {# fun PySet_Clear as clear { withObject* `Set' } -> `()' checkStatusCode* #}
lib/CPython/Types/Slice.chs view
@@ -1,30 +1,33 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Slice ( Slice , sliceType , new , getIndices ) where-import Prelude hiding (length)-import CPython.Internal hiding (new) #include <hscpython-shim.h> +import Prelude hiding (length)++import CPython.Internal hiding (new)+ newtype Slice = Slice (ForeignPtr Slice) instance Object Slice where@@ -34,14 +37,13 @@ instance Concrete Slice where concreteType _ = sliceType -{# fun pure hscpython_PySlice_Type as sliceType+{# fun pure unsafe hscpython_PySlice_Type as sliceType {} -> `Type' peekStaticObject* #} -- | Return a new slice object with the given values. The /start/, /stop/, -- and /step/ parameters are used as the values of the slice object -- attributes of the same names. Any of the values may be 'Nothing', in which -- case @None@ will be used for the corresponding attribute.--- new :: (Object start, Object stop, Object step) => Maybe start -> Maybe stop -> Maybe step -> IO Slice new start stop step = maybeWith withObject start $ \startPtr ->@@ -52,7 +54,6 @@ -- | Retrieve the start, stop, step, and slice length from a 'Slice', -- assuming a sequence of the given length.--- getIndices :: Slice -> Integer -- ^ Sequence length -> IO (Integer, Integer, Integer, Integer)
lib/CPython/Types/Tuple.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Tuple ( Tuple , tupleType@@ -25,15 +26,17 @@ , getSlice , setItem ) where-import Prelude hiding (length)-import CPython.Internal hiding (new) #include <hscpython-shim.h> +import Prelude hiding (length)++import CPython.Internal hiding (new)+ instance Concrete Tuple where concreteType _ = tupleType -{# fun pure hscpython_PyTuple_Type as tupleType+{# fun pure unsafe hscpython_PyTuple_Type as tupleType {} -> `Type' peekStaticObject* #} toTuple :: [SomeObject] -> IO Tuple@@ -44,7 +47,6 @@ >>= stealObject -- | Convert any object implementing the iterator protocol to a 'Tuple'.--- iterableToTuple :: Object iter => iter -> IO Tuple iterableToTuple iter = do raw <- callObjectRaw tupleType =<< toTuple [toObject iter]@@ -65,7 +67,6 @@ -- | Return the object at a given index from a tuple, or throws @IndexError@ -- if the index is out of bounds.--- {# fun PyTuple_GetItem as getItem { withObject* `Tuple' , fromIntegral `Integer'@@ -73,7 +74,6 @@ -- | Take a slice of a tuple from /low/ to /high/, and return it as a new -- tuple.--- {# fun PyTuple_GetSlice as getSlice { withObject* `Tuple' , fromIntegral `Integer'
lib/CPython/Types/Type.chs view
@@ -1,37 +1,38 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Type ( Type , typeType , isSubtype ) where-import CPython.Internal #include <hscpython-shim.h> +import CPython.Internal+ instance Concrete Type where concreteType _ = typeType -{# fun pure hscpython_PyType_Type as typeType+{# fun pure unsafe hscpython_PyType_Type as typeType {} -> `Type' peekStaticObject* #} -- | Returns 'True' if the first parameter is a subtype of the second -- parameter.--- {# fun PyType_IsSubtype as isSubtype { withObject* `Type' , withObject* `Type'
lib/CPython/Types/Unicode.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.Unicode ( -- * Unicode objects@@ -44,19 +45,22 @@ , format , contains ) where+ #include <hscpython-shim.h> -import Prelude hiding (length)-import Control.Exception (ErrorCall (..), throwIO)+import Prelude hiding (length)+import Control.Exception (ErrorCall (..), throwIO) import qualified Data.Text as T+ #ifdef Py_UNICODE_WIDE-import Data.Char (chr, ord)+import Data.Char (chr, ord) #else import qualified Data.Text.Foreign as TF #endif-import CPython.Internal-import CPython.Types.Bytes (Bytes) +import CPython.Internal+import CPython.Types.Bytes (Bytes)+ newtype Unicode = Unicode (ForeignPtr Unicode) instance Object Unicode where@@ -79,7 +83,7 @@ Replace -> "replace" Ignore -> "ignore" -{# fun pure hscpython_PyUnicode_Type as unicodeType+{# fun pure unsafe hscpython_PyUnicode_Type as unicodeType {} -> `Type' peekStaticObject* #} toUnicode :: T.Text -> IO Unicode@@ -111,13 +115,12 @@ } -> `Integer' checkIntReturn* #} -- | Coerce an encoded object /obj/ to an Unicode object.--- +-- -- 'Bytes' and other char buffer compatible objects are decoded according to -- the given encoding and error handling mode.--- +-- -- All other objects, including 'Unicode' objects, cause a @TypeError@ to be -- thrown.--- {# fun hscpython_PyUnicode_FromEncodedObject as fromEncodedObject `Object obj' => { withObject* `obj'@@ -126,7 +129,6 @@ } -> `Unicode' stealObject* #} -- | Shortcut for @'fromEncodedObject' \"utf-8\" 'Strict'@--- fromObject :: Object obj => obj -> IO Unicode fromObject obj = fromEncodedObject obj (T.pack "utf-8") Strict @@ -134,7 +136,6 @@ -- The encoding and error mode have the same meaning as the parameters of -- the the @str.encode()@ method. The codec to be used is looked up using -- the Python codec registry.--- {# fun hscpython_PyUnicode_AsEncodedString as encode { withObject* `Unicode' , withText* `Encoding'@@ -145,7 +146,6 @@ -- error mode have the same meaning as the parameters of the the -- @str.encode()@ method. The codec to be used is looked up using the Python -- codec registry.--- decode :: Bytes -> Encoding -> ErrorHandling -> IO Unicode decode bytes enc errors = withObject bytes $ \bytesPtr ->@@ -169,7 +169,6 @@ -- 'Nothing', splitting will be done at all whitespace substrings. Otherwise, -- splits occur at the given separator. Separators are not included in the -- resulting list.--- split :: Unicode -> Maybe Unicode -- ^ Separator@@ -186,23 +185,21 @@ -- strings. CRLF is considered to be one line break. If the second parameter -- is 'False', the line break characters are not included in the resulting -- strings.--- {# fun hscpython_PyUnicode_Splitlines as splitLines { withObject* `Unicode' , `Bool' } -> `List' stealObject* #} -- | Translate a string by applying a character mapping table to it.--- +-- -- The mapping table must map Unicode ordinal integers to Unicode ordinal -- integers or @None@ (causing deletion of the character).--- +-- -- Mapping tables need only provide the @__getitem__()@ interface; -- dictionaries and sequences work well. Unmapped character ordinals (ones -- which cause a @LookupError@) are left untouched and are copied as-is.--- +-- -- The error mode has the usual meaning for codecs.--- {# fun hscpython_PyUnicode_Translate as translate `Object table' => { withObject* `Unicode'@@ -211,7 +208,6 @@ } -> `Unicode' stealObject* #} -- | Join a sequence of strings using the given separator.--- {# fun hscpython_PyUnicode_Join as join `Sequence seq' => { withObject* `Unicode'@@ -223,7 +219,6 @@ -- | Return 'True' if the substring matches @string*[*start:end]@ at the -- given tail end (either a 'Prefix' or 'Suffix' match), 'False' otherwise.--- tailMatch :: Unicode -- ^ String -> Unicode -- ^ Substring@@ -248,7 +243,6 @@ -- | Return the first position of the substring in @string*[*start:end]@ -- using the given direction. The return value is the index of the first -- match; a value of 'Nothing' indicates that no match was found.--- find :: Unicode -- ^ String -> Unicode -- ^ Substring@@ -273,7 +267,6 @@ -- | Return the number of non-overlapping occurrences of the substring in -- @string[start:end]@.--- count :: Unicode -- ^ String -> Unicode -- ^ Substring@@ -290,7 +283,6 @@ -- | Replace occurrences of the substring with a given replacement. If the -- maximum count is 'Nothing', replace all occurences.--- replace :: Unicode -- ^ String -> Unicode -- ^ Substring@@ -309,16 +301,14 @@ -- | Return a new 'Unicode' object from the given format and args; this is -- analogous to @format % args@.--- {# fun hscpython_PyUnicode_Format as format { withObject* `Unicode' , withObject* `Tuple' } -> `Unicode' stealObject* #} -- | Check whether /element/ is contained in a string.--- +-- -- /element/ has to coerce to a one element string.--- {# fun hscpython_PyUnicode_Contains as contains `Object element' => { withObject* `Unicode'
lib/CPython/Types/WeakReference.chs view
@@ -1,19 +1,20 @@+{-# LANGUAGE ForeignFunctionInterface #-}+ -- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>--- +-- -- 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 3 of the License, or -- 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, see <http://www.gnu.org/licenses/>.--- -{-# LANGUAGE ForeignFunctionInterface #-}+ module CPython.Types.WeakReference ( Reference , Proxy@@ -21,10 +22,11 @@ , newProxy , getObject ) where-import CPython.Internal #include <hscpython-shim.h> +import CPython.Internal+ newtype Reference = Reference (ForeignPtr Reference) instance Object Reference where toObject (Reference x) = SomeObject x@@ -42,7 +44,6 @@ -- collected; it should accept a single parameter, which will be the weak -- reference object itself. If ob is not a weakly-referencable object, or if -- /callback/ is not callable, this will throw a @TypeError@.--- newReference :: (Object obj, Object callback) => obj -> Maybe callback -> IO Reference newReference obj cb = withObject obj $ \objPtr ->@@ -57,7 +58,6 @@ -- should accept a single parameter, which will be the weak reference object -- itself. If ob is not a weakly-referencable object, or if /callback/ is not -- callable, this will throw a @TypeError@.--- newProxy :: (Object obj, Object callback) => obj -> Maybe callback -> IO Proxy newProxy obj cb = withObject obj $ \objPtr ->@@ -67,7 +67,6 @@ -- | Return the referenced object from a weak reference. If the referent is -- no longer live, returns @None@.--- {# fun PyWeakref_GetObject as getObject { withObject* `Reference' } -> `SomeObject' peekObject* #}