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: 5 additions & 0 deletions callable.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ fn PyCallable::create_by_ref_unchecked(
PyCallable::{ obj: PyObject::create(obj) }
}

///|
test {
ignore(PyCallable::create_by_ref_unchecked)
}

///|
pub fn PyCallable::dump(self : PyCallable) -> Unit {
self.obj.dump()
Expand Down
6 changes: 3 additions & 3 deletions cpython/context.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ pub extern "C" fn py_context_var_new(

///|
pub extern "C" fn py_context_var_get(
var : PyObjectRef,
pyvar : PyObjectRef,
default_value : PyObjectRef,
value : ArrayPyObjectRef
) -> Int = "PyContextVar_Get"

///|
pub extern "C" fn py_context_var_set(
var : PyObjectRef,
pyvar : PyObjectRef,
value : PyObjectRef
) -> PyObjectRef = "PyContextVar_Set"

///|
pub extern "C" fn py_context_var_reset(
var : PyObjectRef,
pyvar : PyObjectRef,
token : PyObjectRef
) -> Int = "PyContextVar_Reset"

Expand Down
12 changes: 12 additions & 0 deletions cpython/core.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ fn CStr::to_string_with_length(self : CStr, len : UInt) -> String {
c_str_to_moonbit_str_with_length(self, len)
}

///|
test {
ignore(CStr::to_string_with_length)
ignore(CWStr::from)
}

///|
fn CWStr::from(s : String) -> CWStr {
moonbit_str_to_cw_str(s)
Expand All @@ -70,6 +76,12 @@ fn CWStr::to_string_with_length(self : CWStr, len : UInt) -> String {
cw_str_to_moonbit_str_with_length(self, len)
}

///|
test {
ignore(CWStr::to_string_with_length)
ignore(CWStr::to_string)
}

///|
extern "C" fn py_object_is_null(obj : PyObjectRef) -> Int = "Moonbit_PyObjectRef_is_null"

Expand Down
5 changes: 5 additions & 0 deletions float.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ fn PyFloat::create_by_ref_unchecked(obj : @cpython.PyObjectRef) -> PyFloat {
PyFloat::{ obj: PyObject::create(obj) }
}

///|
test {
ignore(PyFloat::create_by_ref_unchecked)
}

///| Create a PyFloat from a Double value.
///
/// ## Example
Expand Down
5 changes: 5 additions & 0 deletions integer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ fn PyInteger::create_by_ref_unchecked(obj : @cpython.PyObjectRef) -> PyInteger {
PyInteger::{ obj: PyObject::create(obj) }
}

///|
test {
ignore(PyInteger::create_by_ref_unchecked)
}

///| Create a PyInteger from an integer.
///
/// ## Example
Expand Down
4 changes: 3 additions & 1 deletion list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ pub fn[T : IsPyObject] PyList::op_set(
idx : Int,
item : T
) -> Unit {
self.set?(idx, item).unwrap()
self.set(idx, item) catch {
_ => panic()
}
}

///| Let python interpreter print the list directly.
Expand Down
2 changes: 1 addition & 1 deletion main/main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main {
args..set(0, py_nums)

// It's equivalent to `cnt = Counter(nums)`
guard counter.invoke?(args~) is Ok(Some(cnt))
guard (try? counter.invoke(args~)) is Ok(Some(cnt))
guard cnt is PyDict(cnt)

// `print(cnt)`
Expand Down
4 changes: 4 additions & 0 deletions module.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ fn PyModule::create_by_ref_unchecked(
PyModule::{ obj: PyObject::create(obj_ref), name: None, attrs: Map::new() }
}

///|
test {
ignore(PyModule::create_by_ref_unchecked)
}
// REVIEW: what if user call pyimport twice or more?

///| Import a python module
Expand Down
5 changes: 5 additions & 0 deletions str.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ fn PyString::create_by_ref_unchecked(
PyString::{ obj: PyObject::create(obj_ref) }
}

///|
test {
ignore(PyString::create_by_ref_unchecked)
}

///| Create a PyString from a string
///
/// ## Example
Expand Down
2 changes: 1 addition & 1 deletion test/object_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ test "PyDict Test" {
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))
let e = try? dict.setByObj(list, PyInteger::from(42))
inspect(e, content="Err(KeyIsUnHashableError)")
}
27 changes: 13 additions & 14 deletions time/lib.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ let singleton : () -> TimeModule = get_lib()
///|
fn get_lib() -> () -> TimeModule {
@python.init_py()
let lib = match new?() {
Ok(lib) => lib
Err(e) => {
let lib = new() catch {
e => {
println(e)
panic()
}
Expand All @@ -41,7 +40,7 @@ pub fn sleep(seconds : Double) -> Unit {
guard lib.time.get_attr("sleep") is Some(PyCallable(f))
let args = PyTuple::new(1)
args..set(0, PyFloat::from(seconds))
let _ = f.invoke?(args~)
let _ = try? f.invoke(args~)
()
}

Expand All @@ -54,7 +53,7 @@ pub fn time() -> Double {
println("Error: Could not get time.time function") // Placeholder error value
return 0.0
}
guard f.invoke?().unwrap() is Some(PyFloat(t)) else {
guard (try? f.invoke()).unwrap() is Some(PyFloat(t)) else {
println("Error: time.time did not return a float") // Placeholder error value
return 0.0
}
Expand All @@ -69,7 +68,7 @@ pub fn time_ns() -> Int64 {
println("Error: Could not get time.time_ns function") // Placeholder error value
return 0L
}
guard f.invoke?().unwrap() is Some(PyInteger(t)) else {
guard (try? f.invoke()).unwrap() is Some(PyInteger(t)) else {
println("Error: time.time_ns did not return an integer") // Placeholder error value
return 0L
}
Expand All @@ -84,7 +83,7 @@ pub fn monotonic() -> Double {
println("Error: Could not get time.monotonic function")
return 0.0
}
guard f.invoke?().unwrap() is Some(PyFloat(t)) else {
guard (try? f.invoke()).unwrap() is Some(PyFloat(t)) else {
println("Error: time.monotonic did not return a float")
return 0.0
}
Expand All @@ -99,7 +98,7 @@ pub fn monotonic_ns() -> Int64 {
println("Error: Could not get time.monotonic_ns function")
return 0L
}
guard f.invoke?().unwrap() is Some(PyInteger(t)) else {
guard (try? f.invoke()).unwrap() is Some(PyInteger(t)) else {
println("Error: time.monotonic_ns did not return an integer")
return 0L
}
Expand All @@ -114,7 +113,7 @@ pub fn perf_counter() -> Double {
println("Error: Could not get time.perf_counter function")
return 0.0
}
guard f.invoke?().unwrap() is Some(PyFloat(t)) else {
guard (try? f.invoke()).unwrap() is Some(PyFloat(t)) else {
println("Error: time.perf_counter did not return a float")
return 0.0
}
Expand All @@ -129,7 +128,7 @@ pub fn perf_counter_ns() -> Int64 {
println("Error: Could not get time.perf_counter_ns function")
return 0L
}
guard f.invoke?().unwrap() is Some(PyInteger(t)) else {
guard (try? f.invoke()).unwrap() is Some(PyInteger(t)) else {
println("Error: time.perf_counter_ns did not return an integer")
return 0L
}
Expand All @@ -144,7 +143,7 @@ pub fn process_time() -> Double {
println("Error: Could not get time.process_time function")
return 0.0
}
guard f.invoke?().unwrap() is Some(PyFloat(t)) else {
guard (try? f.invoke()).unwrap() is Some(PyFloat(t)) else {
println("Error: time.process_time did not return a float")
return 0.0
}
Expand All @@ -159,7 +158,7 @@ pub fn process_time_ns() -> Int64 {
println("Error: Could not get time.process_time_ns function")
return 0L
}
guard f.invoke?().unwrap() is Some(PyInteger(t)) else {
guard (try? f.invoke()).unwrap() is Some(PyInteger(t)) else {
println("Error: time.process_time_ns did not return an integer")
return 0L
}
Expand All @@ -179,9 +178,9 @@ pub fn ctime(secs_option : Double?) -> String {
Some(secs) => {
let args = PyTuple::new(1)
args..set(0, PyFloat::from(secs))
f.invoke?(args~).unwrap()
(try? f.invoke(args~)).unwrap()
}
None => f.invoke?().unwrap() // Call without arguments
None => (try? f.invoke()).unwrap() // Call without arguments
}
guard result is Some(PyString(s)) else {
println("Error: time.ctime did not return a string") // Placeholder error value
Expand Down
16 changes: 8 additions & 8 deletions tkinter/tkinter.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ let singleton : () -> Tkinter = get_lib()
///|
fn get_lib() -> () -> Tkinter {
@python.init_py()
let lib = match new?() {
Ok(lib) => lib
Err(e) => {
let lib = new() catch {
e => {
println(e)
panic()
}
Expand All @@ -63,7 +62,7 @@ fn get_lib() -> () -> Tkinter {
pub fn Window::new() -> Window {
let lib = singleton()
guard lib.tkinter.get_attr("Tk") is Some(PyCallable(createWindow))
guard createWindow.invoke?().unwrap() is Some(PyClass(window))
guard (try? createWindow.invoke()).unwrap() is Some(PyClass(window))
Window::{ window, }
}

Expand All @@ -72,14 +71,14 @@ pub fn Window::set_title(self : Window, title : String) -> Unit {
guard self.window.get_attr("title") is Some(PyCallable(setTitle))
let args = PyTuple::new(1)
args..set(0, PyString::from(title))
let _ = setTitle.invoke?(args~)
let _ = try? setTitle.invoke(args~)

}

///|
pub fn Window::mainloop(self : Window) -> Unit {
guard self.window.get_attr("mainloop") is Some(PyCallable(mainloop))
let _ = mainloop.invoke?()
let _ = try? mainloop.invoke()

}

Expand All @@ -91,13 +90,14 @@ pub fn Label::new(window : Window, text : String) -> Label {
args..set(0, window.window)
let kwargs = PyDict::new()
kwargs..set("text", PyString::from(text))
guard createLabel.invoke?(args~, kwargs~).unwrap() is Some(PyClass(label))
guard (try? createLabel.invoke(args~, kwargs~)).unwrap()
is Some(PyClass(label))
Label::{ label, }
}

///|
pub fn Label::pack(self : Label) -> Unit {
guard self.label.get_attr("pack") is Some(PyCallable(pack))
let _ = pack.invoke?()
let _ = try? pack.invoke()

}
5 changes: 5 additions & 0 deletions tuple.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ fn PyTuple::create_by_ref_unchecked(obj_ref : @cpython.PyObjectRef) -> PyTuple {
PyTuple::{ obj: PyObject::create(obj_ref) }
}

///|
test {
ignore(PyTuple::create_by_ref_unchecked)
}

///| Return the size of the tuple.
///
/// ## Example:
Expand Down
7 changes: 3 additions & 4 deletions turtle/lib.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ let singleton : () -> Turtle = get_lib()
///|
fn get_lib() -> () -> Turtle {
@python.init_py()
let lib = match new?() {
Ok(lib) => lib
Err(e) => {
let lib = new() catch {
e => {
println(e)
panic()
}
Expand All @@ -50,6 +49,6 @@ pub fn bgcolor(color : Color) -> Unit {
guard lib.turtle.get_attr("bgcolor") is Some(PyCallable(bgcolor))
let args = PyTuple::new(1)
args..set(0, PyString::from(color.to_string()))
let _ = bgcolor.invoke?(args~)
let _ = try? bgcolor.invoke(args~)
()
}
Loading
Loading