@@ -734,7 +734,9 @@ export class PyObject {
734
734
735
735
const positionalCount = positional . length - namedCount ;
736
736
if ( positionalCount < 0 ) {
737
- throw new PythonError ( "Not enough arguments" ) ;
737
+ throw new TypeError (
738
+ `${ this . toString ( ) } requires at least ${ namedCount } arguments, but only ${ positional . length } were passed` ,
739
+ ) ;
738
740
}
739
741
740
742
const args = py . PyTuple_New ( positionalCount ) ;
@@ -787,8 +789,21 @@ export class PyObject {
787
789
export class PythonError extends Error {
788
790
name = "PythonError" ;
789
791
790
- constructor ( public message : string ) {
792
+ constructor (
793
+ public type : PyObject ,
794
+ public value : PyObject ,
795
+ public traceback : PyObject ,
796
+ ) {
797
+ let message = ( value ?? type ) . toString ( ) ?? "Unknown error" ;
798
+ let stack : string | undefined ;
799
+ if ( ! traceback . isNone ) {
800
+ const tb = python . import ( "traceback" ) ;
801
+ stack = ( tb . format_tb ( traceback ) . valueOf ( ) as string [ ] ) . join ( "" ) ;
802
+ message += stack ;
803
+ }
804
+
791
805
super ( mess
8000
age ) ;
806
+ this . stack = stack ;
792
807
}
793
808
}
794
809
@@ -812,13 +827,7 @@ export function maybeThrowError() {
812
827
value = new PyObject ( Deno . UnsafePointer . create ( pointers [ 1 ] ) ) ,
813
828
traceback = new PyObject ( Deno . UnsafePointer . create ( pointers [ 2 ] ) ) ;
814
829
815
- let errorMessage = ( value ?? type ) . toString ( ) ?? "Unknown error" ;
816
- if ( ! traceback . isNone ) {
817
- const tb = python . import ( "traceback" ) ;
818
- errorMessage += `\nTraceback:\n${ tb . format_tb ( traceback ) } ` ;
819
- }
820
-
821
- throw new PythonError ( errorMessage ) ;
830
+ throw new PythonError ( type , value , traceback ) ;
822
831
}
823
832
824
833
/**
@@ -884,7 +893,7 @@ export class Python {
884
893
*/
885
894
run ( code : string ) {
886
895
if ( py . PyRun_SimpleString ( cstr ( code ) ) !== 0 ) {
887
- throw new PythonError ( "Failed to run code" ) ;
896
+ throw new EvalError ( "Failed to run python code" ) ;
888
897
}
889
898
}
890
899
@@ -901,7 +910,7 @@ export class Python {
901
910
. handle ,
902
911
) ;
903
912
if ( module === null ) {
904
- throw new PythonError ( "Failed to run module" ) ;
913
+ throw new EvalError ( "Failed to run python module" ) ;
905
914
}
906
915
return new PyObject ( module ) ?. proxy ;
907
916
}
@@ -913,7 +922,7 @@ export class Python {
913
922
const mod = py . PyImport_ImportModule ( cstr ( name ) ) ;
914
923
if ( mod === null ) {
915
924
maybeThrowError ( ) ;
916
- throw new PythonError ( `Failed to import module ${ name } ` ) ;
925
+ throw new TypeError ( `Failed to import module ${ name } ` )
485E
;
917
926
}
918
927
return new PyObject ( mod ) ;
919
928
}
0 commit comments