From 4928443b6b28e2f6ca1a294221d909550097c810 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Thu, 19 Jun 2025 11:40:44 +0800 Subject: [PATCH 1/2] add preferred target --- moon.mod.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/moon.mod.json b/moon.mod.json index 970a636..3a0a928 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -9,5 +9,6 @@ "scripts": { "postadd": "curl -O https://raw.githubusercontent.com/Kaida-Amethyst/python.mbt/blob/master/env.sh" }, - "--moonbit-unstable-prebuild": "build.js" + "--moonbit-unstable-prebuild": "build.js", + "preferred-target" : "native" } From 597df344044d9a7de1428051d0ebeff9074f8864 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Thu, 19 Jun 2025 11:43:34 +0800 Subject: [PATCH 2/2] fmt, upgrade to beta --- bool.mbt | 5 +- callable.mbt | 7 +-- cpython/abstract.mbt | 3 ++ cpython/cell.mbt | 2 + cpython/ceval.mbt | 3 ++ cpython/class.mbt | 1 + cpython/code.mbt | 1 + cpython/codecs.mbt | 1 + cpython/context.mbt | 1 + cpython/dict.mbt | 1 + cpython/file.mbt | 1 + cpython/import.mbt | 2 + cpython/iter.mbt | 1 + cpython/list.mbt | 1 + cpython/number.mbt | 4 ++ cpython/object.mbt | 1 + cpython/odict.mbt | 1 + cpython/set.mbt | 1 + cpython/tuple.mbt | 1 + cpython/unicode.mbt | 2 + dict.mbt | 7 +-- errors.mbt | 2 +- float.mbt | 4 +- integer.mbt | 5 +- list.mbt | 8 +-- module.mbt | 6 ++- str.mbt | 4 +- test/module_test.mbt | 14 +++--- test/object_test.mbt | 116 +++++++++++++++++++++---------------------- time/lib.mbt | 4 +- tkinter/tkinter.mbt | 4 +- tuple.mbt | 5 +- turtle/lib.mbt | 4 +- 33 files changed, 130 insertions(+), 93 deletions(-) diff --git a/bool.mbt b/bool.mbt index ac8393f..8b7cbb0 100644 --- a/bool.mbt +++ b/bool.mbt @@ -1,6 +1,7 @@ // ======================================== // PyBool // ======================================== + ///| pub struct PyBool { priv obj : PyObject @@ -8,7 +9,7 @@ pub struct PyBool { ///| 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, } } @@ -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) } } diff --git a/callable.mbt b/callable.mbt index e2febca..2d7157b 100644 --- a/callable.mbt +++ b/callable.mbt @@ -1,6 +1,7 @@ // ======================================== // Function // ======================================== + ///| pub struct PyCallable { priv obj : PyObject @@ -8,7 +9,7 @@ pub struct PyCallable { ///| 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, } } @@ -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) } } @@ -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 { diff --git a/cpython/abstract.mbt b/cpython/abstract.mbt index 0dde6ec..e84ccd1 100644 --- a/cpython/abstract.mbt +++ b/cpython/abstract.mbt @@ -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'. /// @@ -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, @@ -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, diff --git a/cpython/cell.mbt b/cpython/cell.mbt index b183b25..9772ba5 100644 --- a/cpython/cell.mbt +++ b/cpython/cell.mbt @@ -1,4 +1,5 @@ // cell + ///| pub extern "C" fn py_cell_new(a : PyObjectRef) -> PyObjectRef = "PyCell_New" @@ -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, diff --git a/cpython/ceval.mbt b/cpython/ceval.mbt index b0abf9b..0573a09 100644 --- a/cpython/ceval.mbt +++ b/cpython/ceval.mbt @@ -1,4 +1,5 @@ // ceval + ///| pub extern "C" fn py_eval_call_object_with_keywords( callable : PyObjectRef, @@ -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" @@ -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" diff --git a/cpython/class.mbt b/cpython/class.mbt index f226035..8e1a387 100644 --- a/cpython/class.mbt +++ b/cpython/class.mbt @@ -1,4 +1,5 @@ // class + ///| pub extern "C" fn py_method_new( a : PyObjectRef, diff --git a/cpython/code.mbt b/cpython/code.mbt index 05cdda9..7e4e9e4 100644 --- a/cpython/code.mbt +++ b/cpython/code.mbt @@ -1,4 +1,5 @@ // code + ///| pub extern "C" fn py_code_new( a : Int, diff --git a/cpython/codecs.mbt b/cpython/codecs.mbt index a1fcced..ed526d1 100644 --- a/cpython/codecs.mbt +++ b/cpython/codecs.mbt @@ -1,4 +1,5 @@ // code cs + ///| pub extern "C" fn py_codec_register(search_function : PyObjectRef) -> Int = "PyCodec_Register" diff --git a/cpython/context.mbt b/cpython/context.mbt index 7323220..bed934d 100644 --- a/cpython/context.mbt +++ b/cpython/context.mbt @@ -1,4 +1,5 @@ // context + ///| pub extern "C" fn py_context_new() -> PyObjectRef = "PyContext_New" diff --git a/cpython/dict.mbt b/cpython/dict.mbt index f659163..a89d285 100644 --- a/cpython/dict.mbt +++ b/cpython/dict.mbt @@ -1,4 +1,5 @@ // dict + ///| pub extern "C" fn py_dict_new() -> PyObjectRef = "PyDict_New" diff --git a/cpython/file.mbt b/cpython/file.mbt index ce81068..42c4c34 100644 --- a/cpython/file.mbt +++ b/cpython/file.mbt @@ -1,4 +1,5 @@ // File + ///| pub extern "C" fn py_file_from_fd( fd : Int, diff --git a/cpython/import.mbt b/cpython/import.mbt index 97c86c5..b15a4c6 100644 --- a/cpython/import.mbt +++ b/cpython/import.mbt @@ -1,4 +1,5 @@ // Import + ///| pub extern "C" fn py_import_get_magic_number() -> Int = "PyImport_GetMagicNumber" @@ -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) diff --git a/cpython/iter.mbt b/cpython/iter.mbt index 697c20b..5cfbc70 100644 --- a/cpython/iter.mbt +++ b/cpython/iter.mbt @@ -1,4 +1,5 @@ // Iter + ///| pub extern "C" fn py_seq_iter_new(a : PyObjectRef) -> PyObjectRef = "PySeqIter_New" diff --git a/cpython/list.mbt b/cpython/list.mbt index 5b3b89c..c02340b 100644 --- a/cpython/list.mbt +++ b/cpython/list.mbt @@ -1,4 +1,5 @@ // List + ///| pub extern "C" fn py_list_new(sz : Int64) -> PyObjectRef = "PyList_New" diff --git a/cpython/number.mbt b/cpython/number.mbt index 111a487..fe2a90f 100644 --- a/cpython/number.mbt +++ b/cpython/number.mbt @@ -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" @@ -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, @@ -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" diff --git a/cpython/object.mbt b/cpython/object.mbt index 41906b0..75147d0 100644 --- a/cpython/object.mbt +++ b/cpython/object.mbt @@ -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" diff --git a/cpython/odict.mbt b/cpython/odict.mbt index c852ad7..0ba5270 100644 --- a/cpython/odict.mbt +++ b/cpython/odict.mbt @@ -1,4 +1,5 @@ // ODict + ///| pub extern "C" fn py_odict_new() -> PyObjectRef = "PyODict_New" diff --git a/cpython/set.mbt b/cpython/set.mbt index 5d74eaf..e6743e1 100644 --- a/cpython/set.mbt +++ b/cpython/set.mbt @@ -1,4 +1,5 @@ // Set + ///| pub extern "C" fn py_set_new(a : PyObjectRef) -> PyObjectRef = "PySet_New" diff --git a/cpython/tuple.mbt b/cpython/tuple.mbt index 59f3684..eee0839 100644 --- a/cpython/tuple.mbt +++ b/cpython/tuple.mbt @@ -1,4 +1,5 @@ // Tuple + ///| pub extern "C" fn pytuple_new(sz : UInt64) -> PyObjectRef = "PyTuple_New" diff --git a/cpython/unicode.mbt b/cpython/unicode.mbt index 03b18fa..438d236 100644 --- a/cpython/unicode.mbt +++ b/cpython/unicode.mbt @@ -1,4 +1,5 @@ // Unicode + ///| pub extern "C" fn py_unicode_from_unicode( u : CWStr, @@ -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" diff --git a/dict.mbt b/dict.mbt index 0763b6c..20707a3 100644 --- a/dict.mbt +++ b/dict.mbt @@ -1,6 +1,7 @@ // ======================================== // Dict // ======================================== + ///| pub struct PyDict { priv obj : PyObject @@ -8,7 +9,7 @@ pub struct PyDict { ///| 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, } } @@ -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) } } @@ -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()) diff --git a/errors.mbt b/errors.mbt index b04abb0..b59613c 100644 --- a/errors.mbt +++ b/errors.mbt @@ -1,5 +1,5 @@ ///| -pub type! PyRuntimeError { +pub suberror PyRuntimeError { TypeMisMatchError IndexOutOfBoundsError KeyIsUnHashableError diff --git a/float.mbt b/float.mbt index 4833bd2..3bbec35 100644 --- a/float.mbt +++ b/float.mbt @@ -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, } } @@ -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) } } diff --git a/integer.mbt b/integer.mbt index e1a5ad3..704a8ba 100644 --- a/integer.mbt +++ b/integer.mbt @@ -1,6 +1,7 @@ // ======================================== // PyInteger // ======================================== + ///| pub struct PyInteger { priv obj : PyObject @@ -8,7 +9,7 @@ pub struct PyInteger { ///| 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, } } @@ -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) } } diff --git a/list.mbt b/list.mbt index b191786..bd57d60 100644 --- a/list.mbt +++ b/list.mbt @@ -1,6 +1,7 @@ // ======================================== // py_list // ======================================== + ///| pub struct PyList { priv obj : PyObject @@ -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, } } @@ -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) } } @@ -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()) @@ -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(), diff --git a/module.mbt b/module.mbt index 789dc7d..f1165ae 100644 --- a/module.mbt +++ b/module.mbt @@ -1,6 +1,7 @@ // ======================================== // Import // ======================================== + ///| pub struct PyModule { priv obj : PyObject @@ -9,7 +10,7 @@ pub struct PyModule { } ///| -pub fn PyModule::create(obj : PyObject) -> PyModule!PyRuntimeError { +pub fn PyModule::create(obj : PyObject) -> PyModule raise PyRuntimeError { guard obj.is_module() else { raise TypeMisMatchError } PyModule::{ obj, name: None, attrs: Map::new() } } @@ -22,7 +23,7 @@ fn PyModule::create_unchecked(obj : PyObject) -> PyModule { ///| pub fn PyModule::create_by_ref( obj_ref : @cpython.PyObjectRef -) -> PyModule!PyRuntimeError { +) -> PyModule raise PyRuntimeError { guard @cpython.py_module_check(obj_ref) else { raise TypeMisMatchError } PyModule::{ obj: PyObject::create(obj_ref), name: None, attrs: Map::new() } } @@ -35,6 +36,7 @@ fn PyModule::create_by_ref_unchecked( } // REVIEW: what if user call pyimport twice or more? + ///| Import a python module /// /// ## Example diff --git a/str.mbt b/str.mbt index 112ba35..c735c91 100644 --- a/str.mbt +++ b/str.mbt @@ -9,7 +9,7 @@ pub struct PyString { ///| Create a python string from a python object. /// If the python object is not a string, it will raise a TypeMisMatchError. -pub fn PyString::create(obj : PyObject) -> PyString!PyRuntimeError { +pub fn PyString::create(obj : PyObject) -> PyString raise PyRuntimeError { guard obj.is_string() else { raise TypeMisMatchError } PyString::{ obj, } } @@ -23,7 +23,7 @@ fn PyString::create_unchecked(obj : PyObject) -> PyString { ///If the python object is not a string, it will raise a TypeMisMatchError. pub fn PyString::create_by_ref( obj_ref : @cpython.PyObjectRef -) -> PyString!PyRuntimeError { +) -> PyString raise PyRuntimeError { guard @cpython.py_string_check(obj_ref) else { raise TypeMisMatchError } PyString::{ obj: PyObject::create(obj_ref) } } diff --git a/test/module_test.mbt b/test/module_test.mbt index 078e27c..06faf54 100644 --- a/test/module_test.mbt +++ b/test/module_test.mbt @@ -1,18 +1,18 @@ ///| test "Module Test" { let os = @python.pyimport("os", print_err=true) - assert_true!(os is Some(_)) + assert_true(os is Some(_)) let os = os.unwrap() - inspect!(os.get_name(), content="os") + inspect(os.get_name(), content="os") guard os.get_attr("name").unwrap() is PyString(s) - assert_true!(["posix", "nt", "java"].contains(s.to_string())) - assert_true!(os.get_attr("listdir") is Some(PyCallable(_))) + assert_true(["posix", "nt", "java"].contains(s.to_string())) + assert_true(os.get_attr("listdir") is Some(PyCallable(_))) /// ```python /// import collections /// ``` let collections = @python.pyimport("collections", print_err=true) - assert_true!(collections is Some(_)) + assert_true(collections is Some(_)) let collections = collections.unwrap() /// ```python @@ -29,12 +29,12 @@ test "Module Test" { let args = PyTuple::new(1) args..set(0, list) guard counter.invoke(args~) is Some(PyDict(cnt)) - inspect!(cnt, content="Counter({3: 3, 2: 2, 1: 1})") + inspect(cnt, content="Counter({3: 3, 2: 2, 1: 1})") // ```python // print(cnt.total()) # Output: 6 // ``` guard cnt.obj().get_attr("total") is Some(PyCallable(total)) let args = PyTuple::new(0) - inspect!(total.invoke(args~).unwrap(), content="PyInteger(6)") + inspect(total.invoke(args~).unwrap(), content="PyInteger(6)") } diff --git a/test/object_test.mbt b/test/object_test.mbt index 397e362..2d09691 100644 --- a/test/object_test.mbt +++ b/test/object_test.mbt @@ -12,18 +12,18 @@ typealias @python.( ///| test "PyInteger Test" { let i = PyInteger::from(42) - inspect!(i, content="42") - inspect!(i.type_name(), content="int") - assert_eq!(i.to_int64(), 42) - assert_eq!(i.to_double(), 42.0) + inspect(i, content="42") + inspect(i.type_name(), content="int") + assert_eq(i.to_int64(), 42) + assert_eq(i.to_double(), 42.0) } ///| test "PyFloat Test" { let f = PyFloat::from(3.5) - inspect!(f, content="3.5") - inspect!(f.type_name(), content="float") - assert_eq!(f.to_double(), 3.5) + inspect(f, content="3.5") + inspect(f.type_name(), content="float") + assert_eq(f.to_double(), 3.5) } ///| @@ -31,52 +31,52 @@ test "PyBool Test" { let t = PyBool::from(true) let f = PyBool::from(false) let f2 = t.not() - inspect!(t, content="True") - inspect!(f, content="False") - inspect!(t.type_name(), content="bool") - assert_true!(t.is_true()) - assert_true!(f.is_false()) - assert_true!(f2.is_false()) + inspect(t, content="True") + inspect(f, content="False") + inspect(t.type_name(), content="bool") + assert_true(t.is_true()) + assert_true(f.is_false()) + assert_true(f2.is_false()) } ///| test "PyString Test" { let s = PyString::from("hello") - inspect!(s, content="hello") - inspect!(s.type_name(), content="str") - inspect!(PyString::from("one"), content="one") - inspect!(PyString::from("two"), content="two") + inspect(s, content="hello") + inspect(s.type_name(), content="str") + inspect(PyString::from("one"), content="one") + inspect(PyString::from("two"), content="two") let hw = PyString::from("你好,世界") - inspect!(hw, content="你好,世界") + inspect(hw, content="你好,世界") } ///| test "PyList Test" { let list = PyList::new() - inspect!(list, content="[]") - assert_eq!(list.len(), 0) - inspect!(list.type_name(), content="list") + inspect(list, content="[]") + assert_eq(list.len(), 0) + inspect(list.type_name(), content="list") let one = PyInteger::from(1) let two = PyFloat::from(2.0) let three = PyString::from("three") list.append(one) list.append(two) list.append(three) - assert_eq!(list.len(), 3) - inspect!(list, content="[1, 2.0, \'three\']") - inspect!(list.get(0).unwrap(), content="PyInteger(1)") - inspect!(list.get(1).unwrap(), content="PyFloat(2.0)") - inspect!(list.get(2), content="Some(PyString(three))") - inspect!(list.get(3), content="None") - inspect!(list.get(-1), content="None") - inspect!(list[0], content="PyInteger(1)") - inspect!(list[1], content="PyFloat(2.0)") - inspect!(list[2], content="PyString(three)") + assert_eq(list.len(), 3) + inspect(list, content="[1, 2.0, \'three\']") + inspect(list.get(0).unwrap(), content="PyInteger(1)") + inspect(list.get(1).unwrap(), content="PyFloat(2.0)") + inspect(list.get(2), content="Some(PyString(three))") + inspect(list.get(3), content="None") + inspect(list.get(-1), content="None") + inspect(list[0], content="PyInteger(1)") + inspect(list[1], content="PyFloat(2.0)") + inspect(list[2], content="PyString(three)") let forty_two = PyInteger::from(42) - list.set!(0, forty_two) - inspect!(list, content="[42, 2.0, \'three\']") + list.set(0, forty_two) + inspect(list, content="[42, 2.0, \'three\']") list[1] = forty_two - inspect!(list, content="[42, 42, \'three\']") + inspect(list, content="[42, 42, \'three\']") } ///| @@ -86,15 +86,15 @@ test "PyTuple Test" { ..set(0, PyInteger::from(1)) ..set(1, PyFloat::from(2.0)) ..set(2, PyString::from("three")) - inspect!(tuple.type_name(), content="tuple") - assert_eq!(tuple.len(), 3) - inspect!(tuple, content="(1, 2.0, \'three\')") - inspect!(tuple.get(0).unwrap(), content="PyInteger(1)") - inspect!(tuple.get(1).unwrap(), content="PyFloat(2.0)") - inspect!(tuple.get(2).unwrap(), content="PyString(three)") - inspect!(tuple.get(3), content="None") + inspect(tuple.type_name(), content="tuple") + assert_eq(tuple.len(), 3) + inspect(tuple, content="(1, 2.0, \'three\')") + inspect(tuple.get(0).unwrap(), content="PyInteger(1)") + inspect(tuple.get(1).unwrap(), content="PyFloat(2.0)") + inspect(tuple.get(2).unwrap(), content="PyString(three)") + inspect(tuple.get(3), content="None") tuple[0] = PyInteger::from(42) - inspect!(tuple, content="(42, 2.0, \'three\')") + inspect(tuple, content="(42, 2.0, \'three\')") } ///| @@ -104,25 +104,25 @@ test "PyDict Test" { ..set("one", PyInteger::from(1)) ..set("two", PyFloat::from(2.0)) ..set("three", PyBool::from(true)) - inspect!(dict.type_name(), content="dict") - assert_eq!(dict.len(), 3) - inspect!(dict, content="{\'one\': 1, \'two\': 2.0, \'three\': True}") - inspect!(dict.get("one").unwrap(), content="PyInteger(1)") - inspect!(dict.get("two"), content="Some(PyFloat(2.0))") - inspect!(dict.get("four"), content="None") - inspect!(dict["one"], content="PyInteger(1)") - inspect!(dict["two"], content="PyFloat(2.0)") - inspect!(dict["three"], content="PyBool(True)") - inspect!(dict.keys(), content="[\'one\', \'two\', \'three\']") - inspect!(dict.values(), content="[1, 2.0, True]") + inspect(dict.type_name(), content="dict") + assert_eq(dict.len(), 3) + inspect(dict, content="{\'one\': 1, \'two\': 2.0, \'three\': True}") + inspect(dict.get("one").unwrap(), content="PyInteger(1)") + inspect(dict.get("two"), content="Some(PyFloat(2.0))") + inspect(dict.get("four"), content="None") + inspect(dict["one"], content="PyInteger(1)") + inspect(dict["two"], content="PyFloat(2.0)") + inspect(dict["three"], content="PyBool(True)") + inspect(dict.keys(), content="[\'one\', \'two\', \'three\']") + inspect(dict.values(), content="[1, 2.0, True]") let dict = PyDict::new() dict - ..setByObj!(PyInteger::from(1), PyInteger::from(1)) - ..setByObj!(PyInteger::from(2), PyInteger::from(4)) - ..setByObj!(PyInteger::from(3), PyInteger::from(9)) - inspect!(dict, content="{1: 1, 2: 4, 3: 9}") + ..setByObj(PyInteger::from(1), PyInteger::from(1)) + ..setByObj(PyInteger::from(2), PyInteger::from(4)) + ..setByObj(PyInteger::from(3), PyInteger::from(9)) + inspect(dict, content="{1: 1, 2: 4, 3: 9}") let list = PyList::new() list..append(PyInteger::from(3))..append(PyInteger::from(2)) let e = dict.setByObj?(list, PyInteger::from(42)) - inspect!(e, content="Err(KeyIsUnHashableError)") + inspect(e, content="Err(KeyIsUnHashableError)") } diff --git a/time/lib.mbt b/time/lib.mbt index 63fed4e..84cd714 100644 --- a/time/lib.mbt +++ b/time/lib.mbt @@ -7,12 +7,12 @@ pub struct TimeModule { } ///| -pub type! TurtleError { +pub suberror TurtleError { LoadTimeModuleError } derive(Show) ///| -fn new() -> TimeModule!Error { +fn new() -> TimeModule raise Error { guard @python.pyimport("time") is Some(time) else { raise LoadTimeModuleError } diff --git a/tkinter/tkinter.mbt b/tkinter/tkinter.mbt index 2852ada..6e6f6f0 100644 --- a/tkinter/tkinter.mbt +++ b/tkinter/tkinter.mbt @@ -16,7 +16,7 @@ typealias @python.(PyModule, PyObject, PyTuple, PyDict, PyString) ///| -pub type! TkinterError { +pub suberror TkinterError { LoadTkinterError } derive(Show) @@ -36,7 +36,7 @@ pub struct Label { } ///| -fn new() -> Tkinter!Error { +fn new() -> Tkinter raise Error { guard @python.pyimport("tkinter") is Some(tkinter) else { raise LoadTkinterError } diff --git a/tuple.mbt b/tuple.mbt index 4e13252..21dc263 100644 --- a/tuple.mbt +++ b/tuple.mbt @@ -1,6 +1,7 @@ // ======================================== // Tuple // ======================================== + ///| pub struct PyTuple { priv obj : PyObject @@ -29,7 +30,7 @@ pub fn PyTuple::new(size : UInt64) -> PyTuple { ///| Create a PyTuple object from a python object. /// If the python object is not a tuple, it will raise a TypeMisMatchError. -pub fn PyTuple::create(obj : PyObject) -> PyTuple!PyRuntimeError { +pub fn PyTuple::create(obj : PyObject) -> PyTuple raise PyRuntimeError { guard obj.is_tuple() else { raise TypeMisMatchError } PyTuple::{ obj, } } @@ -43,7 +44,7 @@ fn PyTuple::create_unchecked(obj : PyObject) -> PyTuple { /// If the python object is not a tuple, it will raise a TypeMisMatchError. pub fn PyTuple::create_by_ref( obj_ref : @cpython.PyObjectRef -) -> PyTuple!PyRuntimeError { +) -> PyTuple raise PyRuntimeError { guard @cpython.py_tuple_check(obj_ref) else { raise TypeMisMatchError } PyTuple::{ obj: PyObject::create(obj_ref) } } diff --git a/turtle/lib.mbt b/turtle/lib.mbt index 50c0900..c3d5483 100644 --- a/turtle/lib.mbt +++ b/turtle/lib.mbt @@ -11,7 +11,7 @@ typealias @python.( ) ///| -pub type! TurtleError { +pub suberror TurtleError { LoadTurtleError } derive(Show) @@ -21,7 +21,7 @@ pub struct Turtle { } ///| -fn new() -> Turtle!Error { +fn new() -> Turtle raise Error { guard @python.pyimport("turtle") is Some(turtle) else { raise LoadTurtleError }