Skip to content

Commit 1178d0d

Browse files
committed
Simplify AI code
Signed-off-by: Tzanko Matev <[email protected]>
1 parent 9c550c3 commit 1178d0d

File tree

3 files changed

+18
-36
lines changed

3 files changed

+18
-36
lines changed

codetracer-python-recorder/src/code_object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ impl CodeObjectWrapper {
3838
}
3939

4040
/// Borrow the owned `Py<PyCode>` as a `Bound<'py, PyCode>`.
41-
pub fn as_bound<'py>(&'py self, py: Python<'py>) -> Bound<'py, PyCode> {
42-
self.obj.bind(py).clone()
41+
pub fn as_bound<'py>(&'py self, py: Python<'py>) -> &Bound<'py, PyCode> {
42+
self.obj.bind(py)
4343
}
4444

4545
/// Return the stable identity of the code object (equivalent to `id(code)`).

codetracer-python-recorder/src/tracer.rs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -466,33 +466,30 @@ pub fn uninstall_tracer(py: Python<'_>) -> PyResult<()> {
466466
#[pyfunction]
467467
fn callback_call(
468468
py: Python<'_>,
469-
code: Bound<'_, PyAny>,
469+
code: Bound<'_, PyCode>,
470470
offset: i32,
471471
callable: Bound<'_, PyAny>,
472472
arg0: Option<Bound<'_, PyAny>>,
473473
) -> PyResult<()> {
474474
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
475-
let code = code.downcast::<PyCode>()?;
476475
let wrapper = CodeObjectWrapper::new(py, &code);
477476
global.tracer.on_call(py, &wrapper, offset, &callable, arg0.as_ref());
478477
}
479478
Ok(())
480479
}
481480

482481
#[pyfunction]
483-
fn callback_line(py: Python<'_>, code: Bound<'_, PyAny>, lineno: u32) -> PyResult<()> {
482+
fn callback_line(py: Python<'_>, code: Bound<'_, PyCode>, lineno: u32) -> PyResult<()> {
484483
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
485-
let code = code.downcast::<PyCode>()?;
486484
let wrapper = CodeObjectWrapper::new(py, &code);
487485
global.tracer.on_line(py, &wrapper, lineno);
488486
}
489487
Ok(())
490488
}
491489

492490
#[pyfunction]
493-
fn callback_instruction(py: Python<'_>, code: Bound<'_, PyAny>, instruction_offset: i32) -> PyResult<()> {
491+
fn callback_instruction(py: Python<'_>, code: Bound<'_, PyCode>, instruction_offset: i32) -> PyResult<()> {
494492
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
495-
let code = code.downcast::<PyCode>()?;
496493
let wrapper = CodeObjectWrapper::new(py, &code);
497494
global.tracer.on_instruction(py, &wrapper, instruction_offset);
498495
}
@@ -502,12 +499,11 @@ fn callback_instruction(py: Python<'_>, code: Bound<'_, PyAny>, instruction_offs
502499
#[pyfunction]
503500
fn callback_jump(
504501
py: Python<'_>,
505-
code: Bound<'_, PyAny>,
502+
code: Bound<'_, PyCode>,
506503
instruction_offset: i32,
507504
destination_offset: i32,
508505
) -> PyResult<()> {
509506
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
510-
let code = code.downcast::<PyCode>()?;
511507
let wrapper = CodeObjectWrapper::new(py, &code);
512508
global
513509
.tracer
@@ -519,12 +515,11 @@ fn callback_jump(
519515
#[pyfunction]
520516
fn callback_branch(
521517
py: Python<'_>,
522-
code: Bound<'_, PyAny>,
518+
code: Bound<'_, PyCode>,
523519
instruction_offset: i32,
524520
destination_offset: i32,
525521
) -> PyResult<()> {
526522
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
527-
let code = code.downcast::<PyCode>()?;
528523
let wrapper = CodeObjectWrapper::new(py, &code);
529524
global
530525
.tracer
@@ -534,19 +529,17 @@ fn callback_branch(
534529
}
535530

536531
#[pyfunction]
537-
fn callback_py_start(py: Python<'_>, code: Bound<'_, PyAny>, instruction_offset: i32) -> PyResult<()> {
532+
fn callback_py_start(py: Python<'_>, code: Bound<'_, PyCode>, instruction_offset: i32) -> PyResult<()> {
538533
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
539-
let code = code.downcast::<PyCode>()?;
540534
let wrapper = CodeObjectWrapper::new(py, &code);
541535
global.tracer.on_py_start(py, &wrapper, instruction_offset);
542536
}
543537
Ok(())
544538
}
545539

546540
#[pyfunction]
547-
fn callback_py_resume(py: Python<'_>, code: Bound<'_, PyAny>, instruction_offset: i32) -> PyResult<()> {
541+
fn callback_py_resume(py: Python<'_>, code: Bound<'_, PyCode>, instruction_offset: i32) -> PyResult<()> {
548542
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
549-
let code = code.downcast::<PyCode>()?;
550543
let wrapper = CodeObjectWrapper::new(py, &code);
551544
global.tracer.on_py_resume(py, &wrapper, instruction_offset);
552545
}
@@ -556,12 +549,11 @@ fn callback_py_resume(py: Python<'_>, code: Bound<'_, PyAny>, instruction_offset
556549
#[pyfunction]
557550
fn callback_py_return(
558551
py: Python<'_>,
559-
code: Bound<'_, PyAny>,
552+
code: Bound<'_, PyCode>,
560553
instruction_offset: i32,
561554
retval: Bound<'_, PyAny>,
562555
) -> PyResult<()> {
563556
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
564-
let code = code.downcast::<PyCode>()?;
565557
let wrapper = CodeObjectWrapper::new(py, &code);
566558
global.tracer.on_py_return(py, &wrapper, instruction_offset, &retval);
567559
}
@@ -571,12 +563,11 @@ fn callback_py_return(
571563
#[pyfunction]
572564
fn callback_py_yield(
573565
py: Python<'_>,
574-
code: Bound<'_, PyAny>,
566+
code: Bound<'_, PyCode>,
575567
instruction_offset: i32,
576568
retval: Bound<'_, PyAny>,
577569
) -> PyResult<()> {
578570
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
579-
let code = code.downcast::<PyCode>()?;
580571
let wrapper = CodeObjectWrapper::new(py, &code);
581572
global.tracer.on_py_yield(py, &wrapper, instruction_offset, &retval);
582573
}
@@ -586,12 +577,11 @@ fn callback_py_yield(
586577
#[pyfunction]
587578
fn callback_py_throw(
588579
py: Python<'_>,
589-
code: Bound<'_, PyAny>,
580+
code: Bound<'_, PyCode>,
590581
instruction_offset: i32,
591582
exception: Bound<'_, PyAny>,
592583
) -> PyResult<()> {
593584
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
594-
let code = code.downcast::<PyCode>()?;
595585
let wrapper = CodeObjectWrapper::new(py, &code);
596586
global.tracer.on_py_throw(py, &wrapper, instruction_offset, &exception);
597587
}
@@ -601,12 +591,11 @@ fn callback_py_throw(
601591
#[pyfunction]
602592
fn callback_py_unwind(
603593
py: Python<'_>,
604-
code: Bound<'_, PyAny>,
594+
code: Bound<'_, PyCode>,
605595
instruction_offset: i32,
606596
exception: Bound<'_, PyAny>,
607597
) -> PyResult<()> {
608598
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
609-
let code = code.downcast::<PyCode>()?;
610599
let wrapper = CodeObjectWrapper::new(py, &code);
611600
global.tracer.on_py_unwind(py, &wrapper, instruction_offset, &exception);
612601
}
@@ -616,12 +605,11 @@ fn callback_py_unwind(
616605
#[pyfunction]
617606
fn callback_raise(
618607
py: Python<'_>,
619-
code: Bound<'_, PyAny>,
608+
code: Bound<'_, PyCode>,
620609
instruction_offset: i32,
621610
exception: Bound<'_, PyAny>,
622611
) -> PyResult<()> {
623612
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
624-
let code = code.downcast::<PyCode>()?;
625613
let wrapper = CodeObjectWrapper::new(py, &code);
626614
global.tracer.on_raise(py, &wrapper, instruction_offset, &exception);
627615
}
@@ -631,12 +619,11 @@ fn callback_raise(
631619
#[pyfunction]
632620
fn callback_reraise(
633621
py: Python<'_>,
634-
code: Bound<'_, PyAny>,
622+
code: Bound<'_, PyCode>,
635623
instruction_offset: i32,
636624
exception: Bound<'_, PyAny>,
637625
) -> PyResult<()> {
638626
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
639-
let code = code.downcast::<PyCode>()?;
640627
let wrapper = CodeObjectWrapper::new(py, &code);
641628
global.tracer.on_reraise(py, &wrapper, instruction_offset, &exception);
642629
}
@@ -646,12 +633,11 @@ fn callback_reraise(
646633
#[pyfunction]
647634
fn callback_exception_handled(
648635
py: Python<'_>,
649-
code: Bound<'_, PyAny>,
636+
code: Bound<'_, PyCode>,
650637
instruction_offset: i32,
651638
exception: Bound<'_, PyAny>,
652639
) -> PyResult<()> {
653640
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
654-
let code = code.downcast::<PyCode>()?;
655641
let wrapper = CodeObjectWrapper::new(py, &code);
656642
global
657643
.tracer
@@ -679,13 +665,12 @@ fn callback_exception_handled(
679665
#[pyfunction]
680666
fn callback_c_return(
681667
py: Python<'_>,
682-
code: Bound<'_, PyAny>,
668+
code: Bound<'_, PyCode>,
683669
offset: i32,
684670
callable: Bound<'_, PyAny>,
685671
arg0: Option<Bound<'_, PyAny>>,
686672
) -> PyResult<()> {
687673
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
688-
let code = code.downcast::<PyCode>()?;
689674
let wrapper = CodeObjectWrapper::new(py, &code);
690675
global
691676
.tracer
@@ -697,13 +682,12 @@ fn callback_c_return(
697682
#[pyfunction]
698683
fn callback_c_raise(
699684
py: Python<'_>,
700-
code: Bound<'_, PyAny>,
685+
code: Bound<'_, PyCode>,
701686
offset: i32,
702687
callable: Bound<'_, PyAny>,
703688
arg0: Option<Bound<'_, PyAny>>,
704689
) -> PyResult<()> {
705690
if let Some(global) = GLOBAL.lock().unwrap().as_mut() {
706-
let code = code.downcast::<PyCode>()?;
707691
let wrapper = CodeObjectWrapper::new(py, &code);
708692
global
709693
.tracer

codetracer-python-recorder/tests/code_object_wrapper.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ fn wrapper_basic_attributes() {
1414
let code: Bound<'_, PyCode> = func
1515
.getattr("__code__")
1616
.unwrap()
17-
.clone()
1817
.downcast_into()
1918
.unwrap();
2019
let wrapper = CodeObjectWrapper::new(py, &code);
@@ -36,7 +35,6 @@ fn wrapper_line_for_offset() {
3635
let code: Bound<'_, PyCode> = func
3736
.getattr("__code__")
3837
.unwrap()
39-
.clone()
4038
.downcast_into()
4139
.unwrap();
4240
let wrapper = CodeObjectWrapper::new(py, &code);

0 commit comments

Comments
 (0)