Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions bool.mbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// ========================================
// PyBool
// ========================================

///|
pub struct PyBool {
priv obj : PyObject
}

///| Create a python boolean object from a python object.
/// If the object is not a boolean, it will raise a TypeMisMatchError.
pub fn PyBool::create(obj : PyObject) -> PyBool!PyRuntimeError {
pub fn PyBool::create(obj : PyObject) -> PyBool raise PyRuntimeError {
guard obj.is_bool() else { raise TypeMisMatchError }
PyBool::{ obj, }
}
Expand All @@ -22,7 +23,7 @@ fn PyBool::create_unchecked(obj : PyObject) -> PyBool {
/// If the object is not a boolean, it will raise a TypeMisMatchError.
pub fn PyBool::create_by_ref(
obj_ref : @cpython.PyObjectRef
) -> PyBool!PyRuntimeError {
) -> PyBool raise PyRuntimeError {
guard @cpython.py_bool_check(obj_ref) else { raise TypeMisMatchError }
PyBool::{ obj: PyObject::create(obj_ref) }
}
Expand Down
7 changes: 4 additions & 3 deletions callable.mbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// ========================================
// Function
// ========================================

///|
pub struct PyCallable {
priv obj : PyObject
}

///| Create a python callable object from a PyObject.
/// If the object is not callable, a TypeMisMatchError is raised.
pub fn PyCallable::create(obj : PyObject) -> PyCallable!PyRuntimeError {
pub fn PyCallable::create(obj : PyObject) -> PyCallable raise PyRuntimeError {
guard obj.is_callable() else { raise TypeMisMatchError }
PyCallable::{ obj, }
}
Expand All @@ -22,7 +23,7 @@ fn PyCallable::create_unchecked(obj : PyObject) -> PyCallable {
/// If the object is not callable, a TypeMisMatchError is raised.
pub fn PyCallable::create_by_ref(
obj : @cpython.PyObjectRef
) -> PyCallable!PyRuntimeError {
) -> PyCallable raise PyRuntimeError {
guard @cpython.py_callable_check(obj) else { raise TypeMisMatchError }
PyCallable::{ obj: PyObject::create(obj) }
}
Expand All @@ -45,7 +46,7 @@ pub fn PyCallable::invoke(
args~ : PyTuple = PyTuple::new(0),
kwargs~ : PyDict = PyDict::new(),
print_err~ : Bool = false
) -> PyObjectEnum?!PyRuntimeError {
) -> PyObjectEnum? raise PyRuntimeError {
let obj_ref = if kwargs.len() > 0 {
@cpython.py_object_call(self.obj_ref(), args.obj_ref(), kwargs.obj_ref())
} else {
Expand Down
3 changes: 3 additions & 0 deletions cpython/abstract.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Abstract

///| Call a callable Python object 'callable' with arguments given by the
/// tuple 'args' and keywords arguments given by the dictionary 'kwargs'.
///
Expand Down Expand Up @@ -488,6 +489,7 @@ pub extern "C" fn pysequence_list(obj : PyObjectRef) -> PyObjectRef = "PySequenc
pub extern "C" fn pysequence_fast(obj : PyObjectRef, msg : CStr) -> PyObjectRef = "PySequence_Fast"

// PyAPI_FUNC(Py_ssize_t) PySequence_Count(py_object *o, PyObject *value);

///|
pub extern "C" fn pysequence_count(
obj : PyObjectRef,
Expand All @@ -504,6 +506,7 @@ pub extern "C" fn pysequence_contains(
) -> Int = "PySequence_Contains"

// PyAPI_FUNC(Py_ssize_t) PySequence_Index(py_object *o, PyObject *value);

///|
pub extern "C" fn pysequence_index(
obj : PyObjectRef,
Expand Down
2 changes: 2 additions & 0 deletions cpython/cell.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// cell

///|
pub extern "C" fn py_cell_new(a : PyObjectRef) -> PyObjectRef = "PyCell_New"

Expand All @@ -9,6 +10,7 @@ pub extern "C" fn py_cell_get(a : PyObjectRef) -> PyObjectRef = "PyCell_Get"
pub extern "C" fn py_cell_set(a : PyObjectRef, b : PyObjectRef) -> Int = "PyCell_Set"

// Eval

///|
pub extern "C" fn py_eval_eval_code(
a : PyObjectRef,
Expand Down
3 changes: 3 additions & 0 deletions cpython/ceval.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ceval

///|
pub extern "C" fn py_eval_call_object_with_keywords(
callable : PyObjectRef,
Expand All @@ -10,6 +11,7 @@ pub extern "C" fn py_eval_call_object_with_keywords(
// pub extern "C" fn py_eval_call_method(obj: PyObjectRef, name: CStr, format: CStr, ...) -> PyObjectRef = "PyEval_CallMethod" // 可变参数 ...
// pub extern "C" fn py_eval_set_profile(a: Py_tracefunc, b: PyObjectRef) = "PyEval_SetProfile"
// pub extern "C" fn py_eval_set_trace(a: Py_tracefunc, b: PyObjectRef) = "PyEval_SetTrace"

///|
pub extern "C" fn py_eval_get_builtins() -> PyObjectRef = "PyEval_GetBuiltins"

Expand All @@ -21,6 +23,7 @@ pub extern "C" fn py_eval_get_locals() -> PyObjectRef = "PyEval_GetLocals"

// pub extern "C" fn py_eval_merge_compiler_flags(cf: *PyCompilerFlags) -> Int = "PyEval_MergeCompilerFlags" // 指针类型 *
// pub extern "C" fn py_add_pending_call(func: *fn(a: *Void) -> Int, arg: *Void) -> Int = "Py_AddPendingCall" // 函数指针, void 指针

///|
pub extern "C" fn py_make_pending_calls() -> Int = "Py_MakePendingCalls"

Expand Down
1 change: 1 addition & 0 deletions cpython/class.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// class

///|
pub extern "C" fn py_method_new(
a : PyObjectRef,
Expand Down
1 change: 1 addition & 0 deletions cpython/code.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// code

///|
pub extern "C" fn py_code_new(
a : Int,
Expand Down
1 change: 1 addition & 0 deletions cpython/codecs.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// code cs

///|
pub extern "C" fn py_codec_register(search_function : PyObjectRef) -> Int = "PyCodec_Register"

Expand Down
1 change: 1 addition & 0 deletions cpython/context.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// context

///|
pub extern "C" fn py_context_new() -> PyObjectRef = "PyContext_New"

Expand Down
1 change: 1 addition & 0 deletions cpython/dict.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// dict

///|
pub extern "C" fn py_dict_new() -> PyObjectRef = "PyDict_New"

Expand Down
1 change: 1 addition & 0 deletions cpython/file.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// File

///|
pub extern "C" fn py_file_from_fd(
fd : Int,
Expand Down
2 changes: 2 additions & 0 deletions cpython/import.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Import

///|
pub extern "C" fn py_import_get_magic_number() -> Int = "PyImport_GetMagicNumber"

Expand Down Expand Up @@ -52,6 +53,7 @@ pub extern "C" fn py_import_add_module(name : CStr) -> PyObjectRef = "PyImport_A
extern "C" fn __py_import_import_module(name : Bytes) -> PyObjectRef = "PyImport_ImportModule"

//extern "C" fn __py_import_import_module(name: Bytes) -> PyObjectRef = "py_import_import_module"

///|
pub fn py_import_import_module(name : String) -> PyObjectRef {
let cstr = ToCStr::to_cstr(name)
Expand Down
1 change: 1 addition & 0 deletions cpython/iter.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Iter

///|
pub extern "C" fn py_seq_iter_new(a : PyObjectRef) -> PyObjectRef = "PySeqIter_New"

Expand Down
1 change: 1 addition & 0 deletions cpython/list.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// List

///|
pub extern "C" fn py_list_new(sz : Int64) -> PyObjectRef = "PyList_New"

Expand Down
4 changes: 4 additions & 0 deletions cpython/number.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
pub extern "C" fn py_long_from_long(value : Int64) -> PyObjectRef = "PyLong_FromLong"

// As Long

///|
pub extern "C" fn py_long_as_long(obj : PyObjectRef) -> Int64 = "PyLong_AsLong"

// As Double

///|
pub extern "C" fn py_long_as_double(obj : PyObjectRef) -> Double = "PyLong_AsDouble"

Expand All @@ -32,6 +34,7 @@ pub extern "C" fn py_float_get_info() -> PyObjectRef = "PyFloat_GetInfo"
pub extern "C" fn py_float_from_string(obj : PyObjectRef) -> PyObjectRef = "PyFloat_FromString"

// Complex

///|
pub extern "C" fn py_complex_from_doubles(
real : Double,
Expand All @@ -45,5 +48,6 @@ pub extern "C" fn py_complex_real_as_double(op : PyObjectRef) -> Double = "PyCom
pub extern "C" fn py_complex_imag_as_double(op : PyObjectRef) -> Double = "PyComplex_ImagAsDouble"

// Bool

///|
pub extern "C" fn py_bool_from_long(value : Int64) -> PyObjectRef = "PyBool_FromLong"
1 change: 1 addition & 0 deletions cpython/object.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub extern "C" fn py_object_generic_set_attr(

// pub extern "C" fn py_object_hash(a: PyObjectRef) -> Py_hash_t = "PyObject_Hash"
// pub extern "C" fn py_object_hash_not_implemented(a: PyObjectRef) -> Py_hash_t = "PyObject_HashNotImplemented"

///|
extern "C" fn __py_object_is_true(a : PyObjectRef) -> Int = "PyObject_IsTrue"

Expand Down
1 change: 1 addition & 0 deletions cpython/odict.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ODict

///|
pub extern "C" fn py_odict_new() -> PyObjectRef = "PyODict_New"

Expand Down
1 change: 1 addition & 0 deletions cpython/set.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Set

///|
pub extern "C" fn py_set_new(a : PyObjectRef) -> PyObjectRef = "PySet_New"

Expand Down
1 change: 1 addition & 0 deletions cpython/tuple.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Tuple

///|
pub extern "C" fn pytuple_new(sz : UInt64) -> PyObjectRef = "PyTuple_New"

Expand Down
2 changes: 2 additions & 0 deletions cpython/unicode.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Unicode

///|
pub extern "C" fn py_unicode_from_unicode(
u : CWStr,
Expand All @@ -21,6 +22,7 @@ pub fn py_unicode_from_string(s : String) -> PyObjectRef {
}

// PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);

///|
extern "C" fn __py_unicode_as_utf8(u : PyObjectRef) -> CStr = "PyUnicode_AsUTF8"

Expand Down
7 changes: 4 additions & 3 deletions dict.mbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// ========================================
// Dict
// ========================================

///|
pub struct PyDict {
priv obj : PyObject
}

///| Create a python dict object.
/// If the python object is not a dict, it will raise a TypeMisMatchError.
pub fn PyDict::create(obj : PyObject) -> PyDict!PyRuntimeError {
pub fn PyDict::create(obj : PyObject) -> PyDict raise PyRuntimeError {
guard obj.is_dict() else { raise TypeMisMatchError }
PyDict::{ obj, }
}
Expand All @@ -22,7 +23,7 @@ fn PyDict::create_unchecked(obj : PyObject) -> PyDict {
/// If the python object is not a dict, it will raise a TypeMisMatchError.
pub fn PyDict::create_by_ref(
obj : @cpython.PyObjectRef
) -> PyDict!PyRuntimeError {
) -> PyDict raise PyRuntimeError {
guard @cpython.py_dict_check(obj) else { raise TypeMisMatchError }
PyDict::{ obj: PyObject::create(obj) }
}
Expand Down Expand Up @@ -197,7 +198,7 @@ pub fn[K : IsPyObject, V : IsPyObject] PyDict::setByObj(
self : PyDict,
key : K,
val : V
) -> Unit!PyRuntimeError {
) -> Unit raise PyRuntimeError {
let dict = self.obj_ref()
@cpython.py_err_clear()
let _ = @cpython.py_dict_set_item(dict, key.obj_ref(), val.obj_ref())
Expand Down
2 changes: 1 addition & 1 deletion errors.mbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///|
pub type! PyRuntimeError {
pub suberror PyRuntimeError {
TypeMisMatchError
IndexOutOfBoundsError
KeyIsUnHashableError
Expand Down
4 changes: 2 additions & 2 deletions float.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct PyFloat {

///| Create a python float object from a python object.
/// If the python object is not a float, it will raise a TypeMisMatchError.
pub fn PyFloat::create(obj : PyObject) -> PyFloat!PyRuntimeError {
pub fn PyFloat::create(obj : PyObject) -> PyFloat raise PyRuntimeError {
guard obj.is_float() else { raise TypeMisMatchError }
PyFloat::{ obj, }
}
Expand All @@ -19,7 +19,7 @@ fn PyFloat::create_unchecked(obj : PyObject) -> PyFloat {
/// If the python object is not a float, it will raise a TypeMisMatchError.
pub fn PyFloat::create_by_ref(
obj : @cpython.PyObjectRef
) -> PyFloat!PyRuntimeError {
) -> PyFloat raise PyRuntimeError {
guard @cpython.py_float_check(obj) else { raise TypeMisMatchError }
PyFloat::{ obj: PyObject::create(obj) }
}
Expand Down
5 changes: 3 additions & 2 deletions integer.mbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// ========================================
// PyInteger
// ========================================

///|
pub struct PyInteger {
priv obj : PyObject
}

///| Create a python integer object from a python object.
/// If
pub fn PyInteger::create(obj : PyObject) -> PyInteger!PyRuntimeError {
pub fn PyInteger::create(obj : PyObject) -> PyInteger raise PyRuntimeError {
guard obj.is_int() else { raise TypeMisMatchError }
PyInteger::{ obj, }
}
Expand All @@ -21,7 +22,7 @@ pub fn PyInteger::create_unchecked(obj : PyObject) -> PyInteger {
///|
pub fn PyInteger::create_by_ref(
obj : @cpython.PyObjectRef
) -> PyInteger!PyRuntimeError {
) -> PyInteger raise PyRuntimeError {
guard @cpython.py_int_check(obj) else { raise TypeMisMatchError }
PyInteger::{ obj: PyObject::create(obj) }
}
Expand Down
8 changes: 5 additions & 3 deletions list.mbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ========================================
// py_list
// ========================================

///|
pub struct PyList {
priv obj : PyObject
Expand All @@ -23,7 +24,7 @@ pub fn PyList::new() -> PyList {

///| Create a python list object from a pyobject.
/// If the pyobject is not a list, it will raise a TypeMisMatchError.
pub fn PyList::create(obj : PyObject) -> PyList!PyRuntimeError {
pub fn PyList::create(obj : PyObject) -> PyList raise PyRuntimeError {
guard obj.is_list() else { raise TypeMisMatchError }
PyList::{ obj, }
}
Expand All @@ -37,7 +38,7 @@ fn PyList::create_unchecked(obj : PyObject) -> PyList {
/// If the pyobject is not a list, it will raise a TypeMisMatchError.
pub fn PyList::create_by_ref(
obj : @cpython.PyObjectRef
) -> PyList!PyRuntimeError {
) -> PyList raise PyRuntimeError {
guard @cpython.py_list_check(obj) else { raise TypeMisMatchError }
PyList::{ obj: PyObject::create(obj) }
}
Expand Down Expand Up @@ -106,6 +107,7 @@ pub fn[T : IsPyObject] PyList::append(self : PyList, item : T) -> Unit {
}

// Review: The following code is available, but I don't think it is necessary.

///| Sort the elements inside the list, using python's built-in sort.
//pub fn PyList::sort(self: PyList) -> Unit {
// let _ = @cpython.py_list_sort(self.obj_ref())
Expand Down Expand Up @@ -215,7 +217,7 @@ pub fn[T : IsPyObject] PyList::set(
self : PyList,
idx : Int,
item : T
) -> Unit!PyRuntimeError {
) -> Unit raise PyRuntimeError {
guard idx >= 0 && idx < self.len() else { raise IndexOutOfBoundsError }
let _ = @cpython.py_list_set_item(
self.obj_ref(),
Expand Down
Loading
Loading