unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* mkstemp strange behavior
@ 2005-08-10 17:25 José Roberto B. de A. Monteiro
  2005-08-10 17:54 ` Alan Grover
  0 siblings, 1 reply; 7+ messages in thread
From: José Roberto B. de A. Monteiro @ 2005-08-10 17:25 UTC (permalink / raw)


Hello all!

I was trying to use mkstemp! in my program, instead of tmpnam, but I have no
success... I mkstemp! is getting me confused. It alters original string, so
we can easely have the name of temporary file.

But, with following code, I suposed it would work, but it is reusing an old
string:

#!/usr/bin/guile \
-s
!#

(define (test)
  (let ((filename "/tmp/XXXXXX")
        (tmp #f))
    (format #t "before: filename=~A\n" filename)
    (set! tmp (mkstemp! filename))
    (format #t "after : filename=~A\n" filename)
    (close tmp)))
	  
(format #t "First call...\n")
(test)
(format #t "Second call...\n")
(test)
			  
And the result of this code is:

First call...
before: filename=/tmp/XXXXXX
after : filename=/tmp/HGLPtZ
Second call...
before: filename=/tmp/HGLPtZ
ERROR: In procedure mkstemp!:
ERROR: Invalid argument

I can not figure out why the string is not set to /tmp/XXXXXX in the second
call...

Some help!?

Regards
Betoes


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: mkstemp strange behavior
  2005-08-10 17:25 mkstemp strange behavior José Roberto B. de A. Monteiro
@ 2005-08-10 17:54 ` Alan Grover
  2005-08-10 18:18   ` R. Mattes
  2005-08-10 19:24   ` José Roberto B. de A. Monteiro
  0 siblings, 2 replies; 7+ messages in thread
From: Alan Grover @ 2005-08-10 17:54 UTC (permalink / raw)



[-- Attachment #1.1: Type: text/plain, Size: 2011 bytes --]

I ran into this too. My reasoning was something like:

The string, "/tmp/XXXXXX" is constructed at reader time. So, there is
exactly one string. The "let" creates a new variable each time you call
"test", but the variable is bound to the same string.

Mkstemp modifies the (one and only) string the first time.

If you force the value to be computed at exec time, the problem should
go away: (string-append "" "/tmp/XXXXXX").

You could test my theory by something like:

(define (test) (let ((filename "/tmp/XXXXXX")) filename))

(display (eq? (test) (test)))

Which yields #t for me. And, (eq? "/tmp/XXXXXX" "/tmp/XXXXXX") yields
#f, for comparison.

José Roberto B. de A. Monteiro wrote:
> Hello all!
> 
> I was trying to use mkstemp! in my program, instead of tmpnam, but I have no
> success... I mkstemp! is getting me confused. It alters original string, so
> we can easely have the name of temporary file.
> 
> But, with following code, I suposed it would work, but it is reusing an old
> string:
> 
> #!/usr/bin/guile \
> -s
> !#
> 
> (define (test)
>   (let ((filename "/tmp/XXXXXX")
>         (tmp #f))
>     (format #t "before: filename=~A\n" filename)
>     (set! tmp (mkstemp! filename))
>     (format #t "after : filename=~A\n" filename)
>     (close tmp)))
> 	  
> (format #t "First call...\n")
> (test)
> (format #t "Second call...\n")
> (test)
> 			  
> And the result of this code is:
> 
> First call...
> before: filename=/tmp/XXXXXX
> after : filename=/tmp/HGLPtZ
> Second call...
> before: filename=/tmp/HGLPtZ
> ERROR: In procedure mkstemp!:
> ERROR: Invalid argument
> 
> I can not figure out why the string is not set to /tmp/XXXXXX in the second
> call...
> 
> Some help!?
> 
> Regards
> Betoes
> 
> 
> _______________________________________________
> Guile-user mailing list
> Guile-user@gnu.org
> http://lists.gnu.org/mailman/listinfo/guile-user
> 

-- 
Alan Grover
awgrover@mail.msen.com
+1.734.476.0969


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 256 bytes --]

[-- Attachment #2: Type: text/plain, Size: 140 bytes --]

_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: mkstemp strange behavior
  2005-08-10 17:54 ` Alan Grover
@ 2005-08-10 18:18   ` R. Mattes
  2005-08-11 19:37     ` Marius Vollmer
  2005-08-10 19:24   ` José Roberto B. de A. Monteiro
  1 sibling, 1 reply; 7+ messages in thread
From: R. Mattes @ 2005-08-10 18:18 UTC (permalink / raw)
  Cc: guile-user

On Wed, 2005-08-10 at 13:54 -0400, Alan Grover wrote:
> I ran into this too. My reasoning was something like:
> 
> The string, "/tmp/XXXXXX" is constructed at reader time. So, there is
> exactly one string. The "let" creates a new variable each time you call
> "test", but the variable is bound to the same string.

Besides: string constants (i.e. "xxx" stuff) are _inmutable_ in scheme.

> Mkstemp modifies the (one and only) string the first time.

Yes, modifying a constant is not a good idea (and guile should croack 
allready the first time).

 Just my 0.02$
  
 Ralf Mattes

> If you force the value to be computed at exec time, the problem should
> go away: (string-append "" "/tmp/XXXXXX").
> 
> You could test my theory by something like:
> 
> (define (test) (let ((filename "/tmp/XXXXXX")) filename))
> 
> (display (eq? (test) (test)))
> 
> Which yields #t for me. And, (eq? "/tmp/XXXXXX" "/tmp/XXXXXX") yields
> #f, for comparison.
> 
> José Roberto B. de A. Monteiro wrote:
> > Hello all!
> > 
> > I was trying to use mkstemp! in my program, instead of tmpnam, but I have no
> > success... I mkstemp! is getting me confused. It alters original string, so
> > we can easely have the name of temporary file.
> > 
> > But, with following code, I suposed it would work, but it is reusing an old
> > string:
> > 
> > #!/usr/bin/guile \
> > -s
> > !#
> > 
> > (define (test)
> >   (let ((filename "/tmp/XXXXXX")
> >         (tmp #f))
> >     (format #t "before: filename=~A\n" filename)
> >     (set! tmp (mkstemp! filename))
> >     (format #t "after : filename=~A\n" filename)
> >     (close tmp)))
> > 	  
> > (format #t "First call...\n")
> > (test)
> > (format #t "Second call...\n")
> > (test)
> > 			  
> > And the result of this code is:
> > 
> > First call...
> > before: filename=/tmp/XXXXXX
> > after : filename=/tmp/HGLPtZ
> > Second call...
> > before: filename=/tmp/HGLPtZ
> > ERROR: In procedure mkstemp!:
> > ERROR: Invalid argument
> > 
> > I can not figure out why the string is not set to /tmp/XXXXXX in the second
> > call...
> > 
> > Some help!?
> > 
> > Regards
> > Betoes
> > 
> > 
> > _______________________________________________
> > Guile-user mailing list
> > Guile-user@gnu.org
> > http://lists.gnu.org/mailman/listinfo/guile-user
> > 
> 
> _______________________________________________
> Guile-user mailing list
> Guile-user@gnu.org
> http://lists.gnu.org/mailman/listinfo/guile-user



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: mkstemp strange behavior
  2005-08-10 17:54 ` Alan Grover
  2005-08-10 18:18   ` R. Mattes
@ 2005-08-10 19:24   ` José Roberto B. de A. Monteiro
  2005-08-12  8:03     ` Tomas Zerolo
  1 sibling, 1 reply; 7+ messages in thread
From: José Roberto B. de A. Monteiro @ 2005-08-10 19:24 UTC (permalink / raw)


On Wed, Aug 10, 2005 at 01:54:31PM -0400, Alan Grover wrote:
> I ran into this too. My reasoning was something like:
> 
> The string, "/tmp/XXXXXX" is constructed at reader time. So, there is
> exactly one string. The "let" creates a new variable each time you call
> "test", but the variable is bound to the same string.
> 
> Mkstemp modifies the (one and only) string the first time.
> ...

Thanks Alan, I think it is the reason, so it worked using:

(let ((filename (string-copy "/tmp/XXXXXX"))
      (f (mkstemp! filename)))
      ...)

Regards

Betoes


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: mkstemp strange behavior
  2005-08-10 18:18   ` R. Mattes
@ 2005-08-11 19:37     ` Marius Vollmer
  0 siblings, 0 replies; 7+ messages in thread
From: Marius Vollmer @ 2005-08-11 19:37 UTC (permalink / raw)
  Cc: guile-user

"R. Mattes" <rm@seid-online.de> writes:

> Yes, modifying a constant is not a good idea (and guile should croack 
> allready the first time).

Yeah, we have the mechanism for that in 1.7, but it is not being used
yet since the old C API didn't distinguish between reading and writing
a string...

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: mkstemp strange behavior
  2005-08-10 19:24   ` José Roberto B. de A. Monteiro
@ 2005-08-12  8:03     ` Tomas Zerolo
  2005-08-12 20:47       ` José Roberto B. de A. Monteiro
  0 siblings, 1 reply; 7+ messages in thread
From: Tomas Zerolo @ 2005-08-12  8:03 UTC (permalink / raw)
  Cc: guile-user


[-- Attachment #1.1: Type: text/plain, Size: 591 bytes --]

On Wed, Aug 10, 2005 at 04:24:47PM -0300, Jos? Roberto B. de A. Monteiro wrote:
> On Wed, Aug 10, 2005 at 01:54:31PM -0400, Alan Grover wrote:
> > I ran into this too. My reasoning was something like:
> > 
> > The string, "/tmp/XXXXXX" is constructed at reader time. [...]
[...]
> Thanks Alan, I think it is the reason, so it worked using:
> 
> (let ((filename (string-copy "/tmp/XXXXXX"))
>       (f (mkstemp! filename)))
>       ...)

Shouldn't that be (let*...)?

(Reminder: with let* later bindings can `see' earlier ones, and you want
this with f).

Regards
-- tomás

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 140 bytes --]

_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: mkstemp strange behavior
  2005-08-12  8:03     ` Tomas Zerolo
@ 2005-08-12 20:47       ` José Roberto B. de A. Monteiro
  0 siblings, 0 replies; 7+ messages in thread
From: José Roberto B. de A. Monteiro @ 2005-08-12 20:47 UTC (permalink / raw)


On Fri, Aug 12, 2005 at 10:03:42AM +0200, Tomas Zerolo wrote:
> On Wed, Aug 10, 2005 at 04:24:47PM -0300, Jos? Roberto B. de A. Monteiro wrote:
> > On Wed, Aug 10, 2005 at 01:54:31PM -0400, Alan Grover wrote:
> > > I ran into this too. My reasoning was something like:
> > > 
> > > The string, "/tmp/XXXXXX" is constructed at reader time. [...]
> [...]
> > Thanks Alan, I think it is the reason, so it worked using:
> > 
> > (let ((filename (string-copy "/tmp/XXXXXX"))
> >       (f (mkstemp! filename)))
> >       ...)
> 
> Shouldn't that be (let*...)?

Yes, sorry... typing error...

> 
> (Reminder: with let* later bindings can `see' earlier ones, and you want
> this with f).
> 
> Regards
> -- tomás



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-08-12 20:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-08-10 17:25 mkstemp strange behavior José Roberto B. de A. Monteiro
2005-08-10 17:54 ` Alan Grover
2005-08-10 18:18   ` R. Mattes
2005-08-11 19:37     ` Marius Vollmer
2005-08-10 19:24   ` José Roberto B. de A. Monteiro
2005-08-12  8:03     ` Tomas Zerolo
2005-08-12 20:47       ` José Roberto B. de A. Monteiro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).