Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3765274
D984.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D984.diff
View Options
diff --git a/Core.tcl b/Core.tcl
--- a/Core.tcl
+++ b/Core.tcl
@@ -505,8 +505,8 @@
#
# e.g. proc put_command_callback {fd line state} { put[lindex $state 0] [lindex $state 1] $line }
#
-# run_command "pwd" put_command_callback {dcc 12}
-# run_command "pwd" put_command_callback {quick "#foo"}
+# run_command "pwd" put_command_callback "" {dcc 12}
+# run_command "pwd" put_command_callback "" {quick "#foo"}
#
# (we provide a more generic print_command_callback procedure for this general use.)
@@ -543,24 +543,34 @@
}
}
+# Same to print a Python error without the traceback
+proc print_python_error_callback {fd output state} {
+ print_command_callback $fd [extractPythonError $output] $state
+}
+
# Runs a command, opens a file descriptor to communicate with the process
#
# @param $cmd the command to run
# @param $callbackProc a callback proc to handle the command output and send input
+# @param $errorCallbackProc a callback proc to handle the command error output
# @param $state a state object to send to the callback proc
-proc run_command {cmd callbackProc state} {
+proc run_command {cmd callbackProc {errorCallbackProc ""} state} {
set fd [open "| $cmd" r]
fconfigure $fd -blocking false
- fileevent $fd readable [list interactive_command_handler $fd $callbackProc $state]
+ fileevent $fd readable [list interactive_command_handler $fd $callbackProc $errorCallbackProc $state]
}
# Closes a command
#
# @param $fd File descriptor to the command process
-proc close_interactive_command {fd} {
+proc close_interactive_command {fd {errorCallbackProc ""} {state ""}} {
fconfigure $fd -blocking true
if {[catch {close $fd} err]} {
- putdebug $err
+ if {$errorCallbackProc == ""} {
+ putdebug $err
+ } {
+ $errorCallbackProc $fd $err $state
+ }
}
}
@@ -569,16 +579,17 @@
# @param $fd File descriptor to the command process
# @param $callbackProc a callback proc to handle the command output and send input
# @param $state a state object to send to the callback proc
-proc interactive_command_handler {fd callbackProc {state ""}} {
+proc interactive_command_handler {fd callbackProc errorCallbackProc {state ""}} {
set status [catch {gets $fd line} result]
if { $status != 0 } {
# unexpected error
- putdebug "Unexpected error running command: "$result
- close_interactive_command $fd
+ putdebug "Unexpected error running command: "
+ putdebug $result
+ close_interactive_command $fd $errorCallbackProc $state
} elseif {$result >= 0} {
$callbackProc $fd $line $state
} elseif {[eof $fd]} {
- close_interactive_command $fd
+ close_interactive_command $fd $errorCallbackProc $state
} elseif {[fblocked $f]} {
# Read blocked, so do nothing
}
@@ -604,6 +615,19 @@
append path $script
}
+# Extracts the error from Python
+proc extractPythonError {exception} {
+ # The exception is one of the line of the text (so the newline-sensitive matching)
+ # Before that, we have the full traceback we want to ignore
+ # e.g. of a line to match: pywikibot.data.api.APIError: modification-failed: Item …
+ if {[regexp -line {^([A-Za-z\.]+)\: (.*)$} $exception line type message]} {
+ return $line
+ } {
+ putdebug "Regexp doesn't match a Python error for this output in extractPythonError:"
+ putdebug $exception
+ }
+}
+
#
# IP and host string manipulations
#
diff --git a/Daeghrefn/Wikimedia.tcl b/Daeghrefn/Wikimedia.tcl
--- a/Daeghrefn/Wikimedia.tcl
+++ b/Daeghrefn/Wikimedia.tcl
@@ -61,7 +61,7 @@
# @param $title the item title
# @param $state the state to pass to the create command callback (here with a mode and a target to print result)
proc create_surname {title state} {
- run_command "[get_external_script create_surname] [posix_escape $title]" print_command_callback $state
+ run_command "[get_external_script create_surname] [posix_escape $title]" print_command_callback print_python_error_callback $state
}
# Handles .+givenname command
@@ -137,7 +137,7 @@
# @param $title the item title
# @param $state the state to pass to the create command callback (here with a mode and a target to print result)
proc create_givenname {title genre state} {
- run_command "[get_external_script create_given_name] [posix_escape $title] $genre" print_command_callback $state
+ run_command "[get_external_script create_given_name] [posix_escape $title] $genre" print_command_callback print_python_error_callback $state
}
# Determines if the specified title is suitable to pass as shell argument
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 23, 11:29 (18 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2257860
Default Alt Text
D984.diff (4 KB)
Attached To
Mode
D984: Extract Python error from an exception with traceback
Attached
Detach File
Event Timeline
Log In to Comment