SlideShare a Scribd company logo
RustPython Sprint
How to find & fix a bug?
Example
➜ ~ cd ~/Projects/RustPython
(RustPython) ➜ RustPython git:(master) ✗ RUSTPYTHONPATH=Lib cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.10s
Running `target/debug/rustpython`
Welcome to the magnificent Rust Python 0.1.0 interpreter 😱 🖖
>>>>> import datetime
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Lib/datetime.py", line 2100, in <module>
File "Lib/datetime.py", line 2194, in timezone
_maxoffset = timedelta(hours=23, minutes=59)
File "Lib/datetime.py", line 654, in __neg__
-self._microseconds)
File "Lib/datetime.py", line 564, in __new__
assert isinstance(s, int) and 0 <= s < 24*3600
AssertionError
>>>>>
•
Debug
timedelta.__new__ 0 -86340 0 0 0 0 0
divmod in: -86340 86400
divmod out: 0 -86340
<class 'int'> -86340
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Lib/datetime.py", line 2100, in <module>
File "Lib/datetime.py", line 2194, in timezone
_maxoffset = timedelta(hours=23, minutes=59)
File "Lib/datetime.py", line 654, in __neg__
-self._microseconds)
File "Lib/datetime.py", line 564, in __new__
assert isinstance(s, int) and 0 <= s < 24*3600
AssertionError
>>>>>
•
Add a test case
diff --git a/tests/snippets/builtin_divmod.py b/tests/snippets/
builtin_divmod.py
index 939e1d2d..ce9bdfba 100644
--- a/tests/snippets/builtin_divmod.py
+++ b/tests/snippets/builtin_divmod.py
@@ -3,6 +3,7 @@ from testutils import assert_raises
assert divmod(11, 3) == (3, 2)
assert divmod(8,11) == (0, 8)
assert divmod(0.873, 0.252) == (3.0, 0.11699999999999999)
+assert divmod(-86340, 86400) == (-1, 60)
assert_raises(ZeroDivisionError, lambda: divmod(5, 0), 'divmod
by zero')
assert_raises(ZeroDivisionError, lambda: divmod(5.0, 0.0),
'divmod by zero')
Run test
(RustPython) ➜ RustPython git:(master) ✗ python tests/snippets/
builtin_divmod.py
(RustPython) ➜ RustPython git:(master) ✗ cargo run tests/snippets/
builtin_divmod.py
Finished dev [unoptimized + debuginfo] target(s) in 0.11s
Running `target/debug/rustpython tests/snippets/
builtin_divmod.py`
Traceback (most recent call last):
File "tests/snippets/builtin_divmod.py", line 6, in <module>
assert divmod(-86340, 86400) == (-1, 60)
AssertionError
Watch code
(RustPython) ➜ RustPython git:(master) ✗ vi ./vm/src/obj/objint.rs
…
#[pymethod(name = "__divmod__")]
fn divmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
if objtype::isinstance(&other, &vm.ctx.int_type()) {
let v2 = get_value(&other);
if *v2 != BigInt::zero() {
let (r1, r2) = self.value.div_rem(v2);
Ok(vm
.ctx
.new_tuple(vec![vm.ctx.new_int(r1), vm.ctx.new_int(r2)]))
} else {
Err(vm.new_zero_division_error("integer divmod by zero".to_str
ing()))
}
} else {
Ok(vm.ctx.not_implemented())
}
}
…
Fix - Add divmod()
@@ -739,6 +735,17 @@ fn truediv_ints(vm: &VirtualMachine, i1: &BigInt, i2: &BigInt) -> PyResult
{
}
}
+#[inline]
+fn divmod(vm: &VirtualMachine, i1: &BigInt, i2: &BigInt) -> PyResult<(BigInt, BigInt)> {
+ if i2.is_zero() {
+ Err(vm.new_zero_division_error("integer division by zero".to_string()))
+ } else {
+ let modulo = (i1 % i2 + i2) % i2;
+ let division = (i1 - &modulo) / i2;
+ Ok((division, modulo))
+ }
+}
+
pub fn init(context: &PyContext) {
PyInt::extend_class(context, &context.int_type);
extend_class!(context, &context.int_type, {
Fix - bugfix
diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs
index 000dcafd..f90d069a 100644
--- a/vm/src/obj/objint.rs
+++ b/vm/src/obj/objint.rs
@@ -405,14 +405,10 @@ impl PyInt {
fn divmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
if objtype::isinstance(&other, &vm.ctx.int_type()) {
let v2 = get_value(&other);
- if *v2 != BigInt::zero() {
- let (r1, r2) = self.value.div_rem(v2);
- Ok(vm
- .ctx
- .new_tuple(vec![vm.ctx.new_int(r1), vm.ctx.new_int(r2)]))
- } else {
- Err(vm.new_zero_division_error("integer divmod by zero".to_string()))
- }
+ let (div, modulo) = divmod(vm, &self.value, v2)?;
+ Ok(vm
+ .ctx
+ .new_tuple(vec![vm.ctx.new_int(div), vm.ctx.new_int(modulo)]))
} else {
Ok(vm.ctx.not_implemented())
}
Pull request
(RustPython) ➜ RustPython git:(master) ✗ git push origin master:fix-divmod
Enumerating objects: 36, done.
Counting objects: 100% (36/36), done.
Delta compression using up to 12 threads.
Compressing objects: 100% (27/27), done.
Writing objects: 100% (27/27), 2.50 KiB | 2.50 MiB/s, done.
Total 27 (delta 24), reused 0 (delta 0)
remote: Resolving deltas: 100% (24/24), completed with 9 local objects.
remote:
remote: Create a pull request for 'fix-divmod' on GitHub by visiting:
remote: https://github.com/youknowone/RustPython/pull/new/fix-divmod
remote:
To https://github.com/youknowone/RustPython.git
* [new branch] master -> fix-divmod
Pull request
(RustPython) ➜ RustPython git:(master) ✗ git push origin master:fix-divmod
Enumerating objects: 36, done.
Counting objects: 100% (36/36), done.
Delta compression using up to 12 threads.
Compressing objects: 100% (27/27), done.
Writing objects: 100% (27/27), 2.50 KiB | 2.50 MiB/s, done.
Total 27 (delta 24), reused 0 (delta 0)
remote: Resolving deltas: 100% (24/24), completed with 9 local objects.
remote:
remote: Create a pull request for 'fix-divmod' on GitHub by visiting:
remote: https://github.com/youknowone/RustPython/pull/new/fix-divmod
remote:
To https://github.com/youknowone/RustPython.git
* [new branch] master -> fix-divmod
Pull request
Original issue
(RustPython) ➜ RustPython git:(master) ✗ RUSTPYTHONPATH=Lib cargo run
Compiling rustpython-vm v0.1.0 (/Users/youknowone/Projects/
RustPython/vm)
Compiling rustpython v0.1.0 (/Users/youknowone/Projects/RustPython)
Finished dev [unoptimized + debuginfo] target(s) in 15.14s
Running `target/debug/rustpython`
Welcome to the magnificent Rust Python 0.1.0 interpreter 😱 🖖
>>>>> import datetime
divmod in: 0 86400
divmod out: 0 0
divmod in: 86399 86400
divmod out: 0 86399
divmod in: 86340 86400
divmod out: 0 86340
divmod in: -86340 86400
divmod out: -1 60
divmod in: 0 86400
divmod out: 0 0
>>>>>
😱 해결!
보통 이런 일은 잘 없으니

기대하지 맙시다 ㅋㅋㅋㅋ

More Related Content

What's hot (20)

PDF
Go memory
jgrahamc
 
PDF
Cluj Big Data Meetup - Big Data in Practice
Steffen Wenz
 
PDF
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
PDF
Go debugging and troubleshooting tips - from real life lessons at SignalFx
SignalFx
 
PDF
Go Profiling - John Graham-Cumming
Cloudflare
 
PDF
"Metrics: Where and How", Vsevolod Polyakov
Yulia Shcherbachova
 
PDF
RestMQ - HTTP/Redis based Message Queue
Gleicon Moraes
 
PDF
Building a DSL with GraalVM (VoxxedDays Luxembourg)
Maarten Mulders
 
PPTX
Why learn Internals?
Shaul Rosenzwieg
 
PPTX
Deep dumpster diving 2010
RonnBlack
 
PPTX
Developing High Performance Application with Aerospike & Go
Chris Stivers
 
PPT
NS2: Binding C++ and OTcl variables
Teerawat Issariyakul
 
PDF
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
OdessaJS Conf
 
PDF
Ntp cheat sheet
csystemltd
 
PDF
Nginx-lua
Дэв Тим Афс
 
PDF
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
OdessaJS Conf
 
PDF
C c++-meetup-1nov2017-autofdo
Kim Phillips
 
PDF
Virtual machine and javascript engine
Duoyi Wu
 
PDF
Go Concurrency
jgrahamc
 
PDF
Full Stack Clojure
Michiel Borkent
 
Go memory
jgrahamc
 
Cluj Big Data Meetup - Big Data in Practice
Steffen Wenz
 
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
Go debugging and troubleshooting tips - from real life lessons at SignalFx
SignalFx
 
Go Profiling - John Graham-Cumming
Cloudflare
 
"Metrics: Where and How", Vsevolod Polyakov
Yulia Shcherbachova
 
RestMQ - HTTP/Redis based Message Queue
Gleicon Moraes
 
Building a DSL with GraalVM (VoxxedDays Luxembourg)
Maarten Mulders
 
Why learn Internals?
Shaul Rosenzwieg
 
Deep dumpster diving 2010
RonnBlack
 
Developing High Performance Application with Aerospike & Go
Chris Stivers
 
NS2: Binding C++ and OTcl variables
Teerawat Issariyakul
 
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
OdessaJS Conf
 
Ntp cheat sheet
csystemltd
 
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
OdessaJS Conf
 
C c++-meetup-1nov2017-autofdo
Kim Phillips
 
Virtual machine and javascript engine
Duoyi Wu
 
Go Concurrency
jgrahamc
 
Full Stack Clojure
Michiel Borkent
 

Recently uploaded (20)

PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
smart lot access control system with eye
rasabzahra
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
smart lot access control system with eye
rasabzahra
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Ad

PyCon KR 2019 sprint - RustPython by example

  • 1. RustPython Sprint How to find & fix a bug?
  • 2. Example ➜ ~ cd ~/Projects/RustPython (RustPython) ➜ RustPython git:(master) ✗ RUSTPYTHONPATH=Lib cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.10s Running `target/debug/rustpython` Welcome to the magnificent Rust Python 0.1.0 interpreter 😱 🖖 >>>>> import datetime Traceback (most recent call last): File "<stdin>", line 1, in <module> File "Lib/datetime.py", line 2100, in <module> File "Lib/datetime.py", line 2194, in timezone _maxoffset = timedelta(hours=23, minutes=59) File "Lib/datetime.py", line 654, in __neg__ -self._microseconds) File "Lib/datetime.py", line 564, in __new__ assert isinstance(s, int) and 0 <= s < 24*3600 AssertionError >>>>> •
  • 3. Debug timedelta.__new__ 0 -86340 0 0 0 0 0 divmod in: -86340 86400 divmod out: 0 -86340 <class 'int'> -86340 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "Lib/datetime.py", line 2100, in <module> File "Lib/datetime.py", line 2194, in timezone _maxoffset = timedelta(hours=23, minutes=59) File "Lib/datetime.py", line 654, in __neg__ -self._microseconds) File "Lib/datetime.py", line 564, in __new__ assert isinstance(s, int) and 0 <= s < 24*3600 AssertionError >>>>> •
  • 4. Add a test case diff --git a/tests/snippets/builtin_divmod.py b/tests/snippets/ builtin_divmod.py index 939e1d2d..ce9bdfba 100644 --- a/tests/snippets/builtin_divmod.py +++ b/tests/snippets/builtin_divmod.py @@ -3,6 +3,7 @@ from testutils import assert_raises assert divmod(11, 3) == (3, 2) assert divmod(8,11) == (0, 8) assert divmod(0.873, 0.252) == (3.0, 0.11699999999999999) +assert divmod(-86340, 86400) == (-1, 60) assert_raises(ZeroDivisionError, lambda: divmod(5, 0), 'divmod by zero') assert_raises(ZeroDivisionError, lambda: divmod(5.0, 0.0), 'divmod by zero')
  • 5. Run test (RustPython) ➜ RustPython git:(master) ✗ python tests/snippets/ builtin_divmod.py (RustPython) ➜ RustPython git:(master) ✗ cargo run tests/snippets/ builtin_divmod.py Finished dev [unoptimized + debuginfo] target(s) in 0.11s Running `target/debug/rustpython tests/snippets/ builtin_divmod.py` Traceback (most recent call last): File "tests/snippets/builtin_divmod.py", line 6, in <module> assert divmod(-86340, 86400) == (-1, 60) AssertionError
  • 6. Watch code (RustPython) ➜ RustPython git:(master) ✗ vi ./vm/src/obj/objint.rs … #[pymethod(name = "__divmod__")] fn divmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { if objtype::isinstance(&other, &vm.ctx.int_type()) { let v2 = get_value(&other); if *v2 != BigInt::zero() { let (r1, r2) = self.value.div_rem(v2); Ok(vm .ctx .new_tuple(vec![vm.ctx.new_int(r1), vm.ctx.new_int(r2)])) } else { Err(vm.new_zero_division_error("integer divmod by zero".to_str ing())) } } else { Ok(vm.ctx.not_implemented()) } } …
  • 7. Fix - Add divmod() @@ -739,6 +735,17 @@ fn truediv_ints(vm: &VirtualMachine, i1: &BigInt, i2: &BigInt) -> PyResult { } } +#[inline] +fn divmod(vm: &VirtualMachine, i1: &BigInt, i2: &BigInt) -> PyResult<(BigInt, BigInt)> { + if i2.is_zero() { + Err(vm.new_zero_division_error("integer division by zero".to_string())) + } else { + let modulo = (i1 % i2 + i2) % i2; + let division = (i1 - &modulo) / i2; + Ok((division, modulo)) + } +} + pub fn init(context: &PyContext) { PyInt::extend_class(context, &context.int_type); extend_class!(context, &context.int_type, {
  • 8. Fix - bugfix diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 000dcafd..f90d069a 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -405,14 +405,10 @@ impl PyInt { fn divmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { if objtype::isinstance(&other, &vm.ctx.int_type()) { let v2 = get_value(&other); - if *v2 != BigInt::zero() { - let (r1, r2) = self.value.div_rem(v2); - Ok(vm - .ctx - .new_tuple(vec![vm.ctx.new_int(r1), vm.ctx.new_int(r2)])) - } else { - Err(vm.new_zero_division_error("integer divmod by zero".to_string())) - } + let (div, modulo) = divmod(vm, &self.value, v2)?; + Ok(vm + .ctx + .new_tuple(vec![vm.ctx.new_int(div), vm.ctx.new_int(modulo)])) } else { Ok(vm.ctx.not_implemented()) }
  • 9. Pull request (RustPython) ➜ RustPython git:(master) ✗ git push origin master:fix-divmod Enumerating objects: 36, done. Counting objects: 100% (36/36), done. Delta compression using up to 12 threads. Compressing objects: 100% (27/27), done. Writing objects: 100% (27/27), 2.50 KiB | 2.50 MiB/s, done. Total 27 (delta 24), reused 0 (delta 0) remote: Resolving deltas: 100% (24/24), completed with 9 local objects. remote: remote: Create a pull request for 'fix-divmod' on GitHub by visiting: remote: https://github.com/youknowone/RustPython/pull/new/fix-divmod remote: To https://github.com/youknowone/RustPython.git * [new branch] master -> fix-divmod
  • 10. Pull request (RustPython) ➜ RustPython git:(master) ✗ git push origin master:fix-divmod Enumerating objects: 36, done. Counting objects: 100% (36/36), done. Delta compression using up to 12 threads. Compressing objects: 100% (27/27), done. Writing objects: 100% (27/27), 2.50 KiB | 2.50 MiB/s, done. Total 27 (delta 24), reused 0 (delta 0) remote: Resolving deltas: 100% (24/24), completed with 9 local objects. remote: remote: Create a pull request for 'fix-divmod' on GitHub by visiting: remote: https://github.com/youknowone/RustPython/pull/new/fix-divmod remote: To https://github.com/youknowone/RustPython.git * [new branch] master -> fix-divmod
  • 12. Original issue (RustPython) ➜ RustPython git:(master) ✗ RUSTPYTHONPATH=Lib cargo run Compiling rustpython-vm v0.1.0 (/Users/youknowone/Projects/ RustPython/vm) Compiling rustpython v0.1.0 (/Users/youknowone/Projects/RustPython) Finished dev [unoptimized + debuginfo] target(s) in 15.14s Running `target/debug/rustpython` Welcome to the magnificent Rust Python 0.1.0 interpreter 😱 🖖 >>>>> import datetime divmod in: 0 86400 divmod out: 0 0 divmod in: 86399 86400 divmod out: 0 86399 divmod in: 86340 86400 divmod out: 0 86340 divmod in: -86340 86400 divmod out: -1 60 divmod in: 0 86400 divmod out: 0 0 >>>>> 😱 해결! 보통 이런 일은 잘 없으니
 기대하지 맙시다 ㅋㅋㅋㅋ