From luke at stat.uiowa.edu  Fri Aug 17 00:30:27 2007
From: luke at stat.uiowa.edu (Luke Tierney)
Date: Thu, 16 Aug 2007 17:30:27 -0500 (CDT)
Subject: [Rd] methods and try() [R-devel]
In-Reply-To: <7098abec0708161334q269d41b0ob4d2398d6660c1b3@mail.gmail.com>
References: <7098abec0708152232s6d9fbb2ewb76cfa2cd4d7b1b2@mail.gmail.com> 
	<Pine.LNX.4.64.0708160742020.2550@itasca2.wildberry.org> 
	<7098abec0708161253l4b437f37l380c529d1fd4c558@mail.gmail.com> 
	<Pine.LNX.4.64.0708161525100.2836@itasca2.wildberry.org>
	<7098abec0708161334q269d41b0ob4d2398d6660c1b3@mail.gmail.com>
Message-ID: <Pine.LNX.4.64.0708161654000.2836@itasca2.wildberry.org>

I think I understand the issue.  The methods code uses R_tryCatch
internally in a few places, hoping I think for a C level variant of
tryCatch.  It isn't meant to be used that way.  It is intended only
for use in embedded contexts where there is no proper top level, or
possibly in contexts where conceptually the evaluation is happening in
a seperate thread of execution.  In particular R_tryEval establishes
it's own top level (and that is the top level that is being jumped
to):

> { try(glue("A",B)); 2}
Error: no function to return from, jumping to top level
Error in glue("A", B) :
   error in evaluating the argument 'b' in selecting a method for function 'glue'
[1] 2
>

Because of the "internal" top level established by R_tryEval there is
no way to jump from inside the eval to an outer context, which is what
needs to happen for try() to work here.

It looks like the intent in methods_list_dispatch.c in all but one
case is to catch the error and report something a bit more meaningful;
in the one other case, in R_nextMethodCall, there is also a cleanup
action that occurs before resignaling the error.

To fix this will I think require designing a C level tryCatch.  That
will take a bit of time to get right.  Not sure if it will get done by
2.6.0.  There may be a quick temporary fix but I'm not seeing one at
this point.

Arguably R_tryEval in its present form should also ensure that the
handler stacks are empty in the call (not an issue for the intended
embedded usage).  Need to think about that a bit as well.

Best,

luke

On Thu, 16 Aug 2007, Byron Ellis wrote:

> On 8/16/07, Luke Tierney <luke at stat.uiowa.edu> wrote:
>>
>> There sae, at 2.5.0 I believe.  At that point try was reimplemented in
>> terms of tryCatch.  That may have uncovered a bug in our code or
>> yours, but without a reproducible example it's hard to say more.
>
> An indeed, I believe I've got one. Turns out it didn't solve my
> problem though. So, imagine we have a method (with a new generic).
> Say,
>
> setGeneric("glue",function(a,b) standardGeneric("glue"))
> setMethod("glue",signature("character","character"),function(a,b) {
> 	paste(a,b,sep="")
> })
>
> Trying the code:
>
>> glue("A","B")
> [1] "AB"
>> glue("A",B)
> Error: object "B" not found
> Error in glue("A", B) :
>  error in evaluating the argument 'b' in selecting a method for function 'glue'
>
> Good. Works as expected (there's no B in the environment). However,
> I'd expect try to work...
>
>>  try(glue("A",B))
> Error: no function to return from, jumping to top level
> Error in glue("A", B) :
>  error in evaluating the argument 'b' in selecting a method for function 'glue'
>
> with silent=TRUE the "jumping to top level" is not caught and causes
> scripts to bail out.
>
> Hopefully that helps?
>
>
>>
>> Best,
>>
>> luke
>>
>>
>>>
>>>>
>>>> Best,
>>>>
>>>> luke
>>>>
>>>>
>>>> --
>>>> Luke Tierney
>>>> Chair, Statistics and Actuarial Science
>>>> Ralph E. Wareham Professor of Mathematical Sciences
>>>> University of Iowa                  Phone:             319-335-3386
>>>> Department of Statistics and        Fax:               319-335-3017
>>>>     Actuarial Science
>>>> 241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
>>>> Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
>>>>
>>>
>>>
>>>
>>
>> --
>> Luke Tierney
>> Chair, Statistics and Actuarial Science
>> Ralph E. Wareham Professor of Mathematical Sciences
>> University of Iowa                  Phone:             319-335-3386
>> Department of Statistics and        Fax:               319-335-3017
>>     Actuarial Science
>> 241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
>> Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
>>
>
>
>

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



[Rd] methods and try() [R-devel]
Luke Tierney