Skip to content

Commit

Permalink
CHANGE: deprecated try/except. Using try/with instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Jun 23, 2023
1 parent 519fd4d commit 6ed8da9
Show file tree
Hide file tree
Showing 23 changed files with 61 additions and 39 deletions.
6 changes: 4 additions & 2 deletions src/boot/natives.reb
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,11 @@ trace: native [
]

try: native [
{Tries to DO a block and returns its value or an error.}
{Tries to DO a block and returns its value or an error!.}
block [block! paren!]
/except "On exception, evaluate this code block"
/with "On error, evaluate the handler and return its result"
handler [block! any-function!]
/except "** DEPRERCATED **"
code [block! any-function!]
]

Expand Down
17 changes: 13 additions & 4 deletions src/core/n-control.c
Original file line number Diff line number Diff line change
Expand Up @@ -886,15 +886,24 @@ enum {
/*
***********************************************************************/
{
REBFLG except = D_REF(2);
REBVAL handler = *D_ARG(3); // TRY exception will trim the stack
REBVAL *last_error = Get_System(SYS_STATE, STATE_LAST_ERROR);
REBFLG with = D_REF(2);
REBVAL handler;
REBVAL *last_error = Get_System(SYS_STATE, STATE_LAST_ERROR);
SET_NONE(last_error);

// If not used the new /with refine, try to use the deprecated /except
if (with) {
handler = *D_ARG(3);
} else {
with = D_REF(4);
handler = *D_ARG(5);
}
// TRY exception will trim the stack
if (Try_Block(VAL_SERIES(D_ARG(1)), VAL_INDEX(D_ARG(1)))) {
// save the error as a system/state/last-error value
*last_error = *DS_NEXT;
if (except) {

if (with) {
if (IS_BLOCK(&handler)) {
DO_BLK(&handler);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mezz/codec-ar.reb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ register-codec [
real: none
forall info [
; it may be an empty string in Windows' lib file!
try/except [info/1: to integer! info/1][info/1: 0]
info/1: try/with [to integer! info/1][0]
]
; convert timestamp to Rebol value (it may be -1 in *.lib files)
info/1: either info/1 <= 0 [none][to date! info/1]
Expand Down
2 changes: 1 addition & 1 deletion src/mezz/codec-image-ext.reb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ if find codecs 'png [
bin: binary data
out: make block! 12
; in cloud builds the console width is not resolved!
num: try/except [-40 + query/mode console:// 'window-cols][40]
num: try/with [-40 + query/mode console:// 'window-cols][40]
while [8 < length? bin/buffer][
len: binary/read bin 'ui32be
tag: copy/part bin/buffer 4
Expand Down
2 changes: 1 addition & 1 deletion src/mezz/codec-json.reb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ decode-unicode-char: func [
"Convert \uxxxx format (NOT simple JSON backslash escapes) to a Unicode char"
ch [string!] "4 hex digits"
][
try/except [to char! to integer! debase ch 16][ return none ]
try/with [to char! to integer! debase ch 16][ none ]
]

replace-unicode-escapes: func [
Expand Down
4 changes: 2 additions & 2 deletions src/mezz/codec-pdf.reb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ rl_reference: [
*stack: copy []

rl_value: [
rl_name ;(try/except [value: to word! value][insert value #"/"])
rl_name ;(try/with [value: to word! value][insert value #"/"])
| rl_reference ;must be before number!
| rl_number
| rl_boolean
Expand Down Expand Up @@ -322,7 +322,7 @@ decompress-obj: func[obj [object!] /local p][

import-objstm: function[obj [object!]][
decompress-obj obj
try/except [
try/with [
offsets: load to-string copy/part obj/data obj/spec/First
;probe to-string obj/data
obj-id: 0x0
Expand Down
4 changes: 2 additions & 2 deletions src/mezz/codec-ppk.reb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ register-codec [
sp: charset " ^-^/^M"
!sp: complement sp
!crlf: complement charset "^M^/"
try/except [
try/with [
parse data [
"PuTTY-User-Key-File-" ["1:" (vers: 1) | "2:" (vers: 2)]
any sp copy type some !sp some sp
Expand Down Expand Up @@ -63,7 +63,7 @@ register-codec [
pri: debase pri 64

if encr = "aes256-cbc" [
try/except [
try/with [
pass: either password [copy pass][
ask/hide ajoin ["Key password for " mold comm ": "]
]
Expand Down
9 changes: 7 additions & 2 deletions src/mezz/codec-ssh-key.reb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ wrap [
]
if "4,ENCRYPTED" = select pkix/header "Proc-Type" [
sysl/log/info 'REBOL "ENCRYPTED key!"
try/except [
try/with [
dek-info: select pkix/header "DEK-Info"
sysl/log/info 'REBOL ["Using:" dek-info]
parse dek-info [
Expand All @@ -102,7 +102,7 @@ wrap [
]
]
; decode DER structure from decoded PKIX binary
try/except [
try/with [
data: codecs/der/decode pkix/binary
][
sys/log/error 'REBOL "Failed to decode DER day for RSA key!"
Expand Down Expand Up @@ -134,6 +134,11 @@ wrap [
init-rsa-public-key data
][ init-rsa-private-key data ]
]
#{2A8648CE3D0201} [ ;= ecPublicKey
return either pkix/label = "PUBLIC KEY" [
init-rsa-public-key data
][ init-rsa-private-key data ]
]
][
sys/log/error 'REBOL ["Unknown key type:" codecs/der/decode-OID oid]
none
Expand Down
2 changes: 1 addition & 1 deletion src/mezz/codec-zip.reb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ register-codec [
compressed-size: size: crc: entries: filename-length: offset: 0

add-file: func[file [file!] /local dir spec][
try/except [
try/with [
spec: query/mode file [type: date:]
either spec [
file-name: find/tail file root
Expand Down
2 changes: 2 additions & 0 deletions src/mezz/mezz-banner.reb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ append sys/boot-banner
* Sandbox and security are not fully available.
* Direct access to TCP HTTP required (no proxies).
* Use at your own risk.
* ^[[1;32mTry/except^[[1;31m is deprecated and will be removed!^[[0m Use ^[[1;32mtry/with^[[0m instead.
^[[1;33mSpecial functions^[[0m:
Expand Down
2 changes: 1 addition & 1 deletion src/mezz/mezz-files.reb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ dir-tree: func [
word! path! [to-file path]
]
if #"/" <> first directory [insert directory what-dir]
value: contents: try/except [read directory][
value: contents: try/with [read directory][
print ["Not found:" :directory]
exit
]
Expand Down
2 changes: 1 addition & 1 deletion src/mezz/mezz-secure.reb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ secure: function/with [

; Set each policy target separately:
foreach [target pol] policy [
try/except [
try/with [
assert/type [target [word! file! url!] pol [block! word! integer!]]
][ cause-error 'access 'security-error reduce [target pol] ]
set-policy target make-policy target pol pol-obj
Expand Down
2 changes: 1 addition & 1 deletion src/mezz/prot-http.reb
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ decode-result: func[
][
if encoding: select result/2 'Content-Encoding [
either find ["gzip" "deflate"] encoding [
try/except [
try/with [
result/3: switch encoding [
"gzip" [ decompress result/3 'gzip]
"deflate" [ decompress result/3 'deflate]
Expand Down
4 changes: 2 additions & 2 deletions src/mezz/prot-pop3.reb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ do-cmd: func[ctx cmd /with arg /local pop3][
ctx/argument: arg
pop3: ctx/connection/parent
clear pop3/data
try/except [
try/with [
write ctx/connection to binary! net-log/C ajoin either/only arg [cmd SP arg CRLF][cmd CRLF]
if pop3/state <> 'INIT [ pop3/state: 'WRITING ]
][
Expand All @@ -88,7 +88,7 @@ do-cmd-hide: func[ctx cmd arg msg /local pop3][
ctx/argument: arg
pop3: ctx/connection/parent
clear pop3/data
try/except [
try/with [
net-log/C ajoin [cmd SP msg]
write ctx/connection to binary! ajoin either/only arg [cmd SP arg CRLF][cmd CRLF]
if pop3/state <> 'INIT [ pop3/state: 'WRITING ]
Expand Down
6 changes: 3 additions & 3 deletions src/mezz/prot-tls.reb
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,7 @@ TLS-parse-handshake-message: function [
]
log-more ["Received" length? ctx/server-certs "server certificates."]
;? ctx/server-certs
try/except [
try/with [
;?? ctx/server-certs/1/public-key
;?? ctx/server-certs/1
key: ctx/server-certs/1/public-key
Expand Down Expand Up @@ -1761,7 +1761,7 @@ TLS-parse-handshake-message: function [
ECDHE_RSA
ECDHE_ECDSA [
;? msg/buffer
try/except [
try/with [
binary/read msg [
s: INDEX
ECCurveType: UI8
Expand Down Expand Up @@ -2231,7 +2231,7 @@ do-TLS-open: func [
unless block? certs [certs: to block! certs]
bin: binary 4000
foreach file certs [
try/except [
try/with [
der: select decode 'pkix read file 'binary
binary/write bin [UI24BYTES :der]
][
Expand Down
2 changes: 1 addition & 1 deletion src/mezz/sys-load.reb
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ download-extension: function[
file: select decode-url url 'target
]
opt: system/options/log
try/except [
try/with [
if exists? file [
; we don't want to overwrite existing files!
log/error 'REBOL ["File already exists:^[[m" file]
Expand Down
4 changes: 2 additions & 2 deletions src/mezz/sys-ports.reb
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,10 @@ init-schemes: func [
copy path [to slash | end] skip
copy speed to end
]
try/except [port/spec/path: to file! path][
try/with [port/spec/path: to file! path][
cause-error 'access 'invalid-spec :path
]
try/except [port/spec/speed: to integer! speed][
try/with [port/spec/speed: to integer! speed][
cause-error 'access 'invalid-spec :speed
]
]
Expand Down
4 changes: 2 additions & 2 deletions src/mezz/sys-start.reb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ start: func [
#"/" = first tmp ; only if we know absolute path
exists? tmp/rebol.reb
][
try/except [do tmp/rebol.reb][sys/log/error 'REBOL system/state/last-error]
try/with [do tmp/rebol.reb][sys/log/error 'REBOL system/state/last-error]
]
]

Expand All @@ -198,7 +198,7 @@ start: func [

sys/log/info 'REBOL ["Checking for user.reb file in" home]
if exists? home/user.reb [
try/except [do home/user.reb][sys/log/error 'REBOL system/state/last-error]
try/with [do home/user.reb][sys/log/error 'REBOL system/state/last-error]
]


Expand Down
3 changes: 2 additions & 1 deletion src/modules/port-scanner.reb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Rebol [
If you use anything other than `localhost` you had better have permission
for the host name you do use, or you will suddenly be an *internet bad guy*.
Don't.}
Needs: 3.11.0 ;; using try/with instead of deprecated try/except
]

; result:
Expand Down Expand Up @@ -51,7 +52,7 @@ scan-ports: function [

total: 1 + to - from

try/except [
try/with [
ip: either tuple? name [ name ][
read join dns:// any [name 'localhost]
]
Expand Down
5 changes: 3 additions & 2 deletions src/modules/spotify.reb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Rebol [
https://developer.spotify.com/documentation/general/guides/authorization-guide/
https://aaronparecki.com/oauth-2-simplified/
}
Needs: 3.11.0 ;; using try/with instead of deprecated try/except
]
unless system/modules/httpd [
print "Importing HTTPD module"
Expand Down Expand Up @@ -126,7 +127,7 @@ authorize: function [

;-- 2. Have your application request refresh and access tokens; Spotify returns access and refresh tokens

try/except [
try/with [
time: now
ctx/token: load-json write https://accounts.spotify.com/api/token probe compose [
POST [
Expand Down Expand Up @@ -179,7 +180,7 @@ request: func [
data [any-type!]
/local header
][
try/except [
try/with [
if now >= ctx/token/expires_in [ refresh ctx ]
header: compose [
Authorization: (join "Bearer " ctx/token/access_token)
Expand Down
3 changes: 2 additions & 1 deletion src/tests/test-udp-server.r3
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ Rebol [
Title: "Test UDP server"
File: %test-udp-server.r3
Note: https://github.com/Oldes/Rebol-issues/issues/1803
Needs: 3.11.0 ;; using try/with instead of deprecated try/except
]

print [as-red "Opening UDP server listening on port" as-yellow 1189]

udp-server: try/except [open udp://:1189][
udp-server: try/with [open udp://:1189][
print as-purple "Failed to listen on UDP port 1189!"
quit
]
Expand Down
2 changes: 1 addition & 1 deletion src/tests/units/crash-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Rebol [

--test-- "issue-1514"
;@@ https://github.com/Oldes/Rebol-issues/issues/1514
--assert error? try [try/except [1 / 0] :add] ;- no crash
--assert error? try [try/with [1 / 0] :add] ;- no crash

--test-- {enline "^/"}
--assert string? enline "^/" ;- no crash
Expand Down
11 changes: 6 additions & 5 deletions src/tests/units/evaluation-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -708,23 +708,24 @@ Rebol [
--assert error? try [catch/quit [1] do make error! "Hello"]
--assert error? try [try [catch/quit []] 1 / 0]

--test-- "try/except [1 / 0] block!"
;@@ https://github.com/Oldes/Rebol-issues/issues/822
--test-- "try/with [1 / 0] block!"
;@@ https://github.com/Oldes/Rebol-issues/issues/2419
system/state/last-error: none
try/except [1 / 0][
try/with [1 / 0][
--assert error? system/state/last-error
--assert system/state/last-error/id = 'zero-divide
]
; any TRY call resets system/state/last-error to none:
--assert not error? try [1 + 1]
--assert none? system/state/last-error
; the last-error is stored also when /except is not used:
; the last-error is stored also when /with is not used:
--assert error? try [this-is-error]
--assert error? system/state/last-error

--test-- "try/except [1 / 0] function!"
--test-- "try/with [1 / 0] function!"
system/state/last-error: none
--assert string? try/except [1 / 0] :mold
--assert string? try/with [1 / 0] :mold
--assert system/state/last-error/id = 'zero-divide

===end-group===
Expand Down

0 comments on commit 6ed8da9

Please sign in to comment.