unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Persisting GOOPS objects
@ 2013-01-18 10:25 Andrew Gaylard
  2013-01-20 20:15 ` Andy Wingo
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Gaylard @ 2013-01-18 10:25 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1470 bytes --]

Hi,

I'm trying to persist a GOOPS object.

I've found oop/goops/save.scm, which defines save-object.
That looks like what I need.

However, I can't get it to work.  Below is an example of what I'm seeing.
I'm sure I'm doing something dumb here -- but what?

Many thanks for your help,
- Andrew

$ /usr/bin/guile
GNU Guile 2.0.5-deb+1-1
Copyright (C) 1995-2012 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)> (use-modules (oop goops))
scheme@(guile-user)> (use-modules (oop goops describe))
scheme@(guile-user)> (use-modules (oop goops save))
scheme@(guile-user)> (define-class <ag-complex> (<number>) r i)
scheme@(guile-user)> (define c (make <ag-complex>))
scheme@(guile-user)> (slot-set! c 'r 3)
scheme@(guile-user)> (slot-set! c 'i 4)
scheme@(guile-user)> (describe c)
#<<ag-complex> 91a35b0> is an instance of class <ag-complex>
Slots are:
     r = 3
     i = 4
scheme@(guile-user)> (define l '((c . c)))
scheme@(guile-user)> (assoc 'c l)
$1 = (c . c)
scheme@(guile-user)> (save-objects l (current-output-port))
(define c 'c)
ERROR: In procedure hashq-get-handle:
ERROR: In procedure hashq-get-handle: Handle access not permitted on weak
table

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]>

[-- Attachment #2: Type: text/html, Size: 1913 bytes --]

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

* Re: Persisting GOOPS objects
  2013-01-18 10:25 Persisting GOOPS objects Andrew Gaylard
@ 2013-01-20 20:15 ` Andy Wingo
  2013-01-21 12:48   ` Andrew Gaylard
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Wingo @ 2013-01-20 20:15 UTC (permalink / raw)
  To: Andrew Gaylard; +Cc: guile-user

Hi,

On Fri 18 Jan 2013 11:25, Andrew Gaylard <ag@computer.org> writes:

> I'm trying to persist a GOOPS object.
>
> I've found oop/goops/save.scm, which defines save-object.
> That looks like what I need.

Cool.  I don't know of anyone that has used this code in many years, so
be prepared for some bugs.  OTOH if it works fine, documentation is
appreciated :)

> scheme@(guile-user)> (save-objects l (current-output-port))
> (define c 'c)
> ERROR: In procedure hashq-get-handle:
> ERROR: In procedure hashq-get-handle: Handle access not permitted on
> weak table

This is an implementation bug, introduced May 2011 by a change to the
weak table interface.  See commit
1d9c2e6271105ee0f728127d9b544432b7cc0f4f for full details.

I have fixed this in stable-2.0.  You can fix in in your copy by:

  (set! (@@ (oop goops save) readable?)
        (lambda (x) (hashq-ref (@@ (oop goops save) readables) x)))

Regards,

Andy
-- 
http://wingolog.org/



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

* Re: Persisting GOOPS objects
  2013-01-20 20:15 ` Andy Wingo
@ 2013-01-21 12:48   ` Andrew Gaylard
  2013-01-22  9:47     ` Andy Wingo
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Gaylard @ 2013-01-21 12:48 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 2804 bytes --]

Thanks, Andy, for your reply -- with your patch, save-objects now completes.

However, it still appears not to create output with enough information
to re-create the objects.  If I do this...:

$ cat persistence-test.scm
(use-modules (oop goops))
(use-modules (oop goops describe))
(use-modules (oop goops save))

;; Apply Andy's fix to the readable? function
(set! (@@ (oop goops save) readable?)
        (lambda (x) (hashq-ref (@@ (oop goops save) readables) x)))

;; make a new class, make an instance of it, and show it
(define-class <ag-complex> (<number>) r i)
(define c (make <ag-complex>))
(slot-set! c 'r 3)
(slot-set! c 'i 4)
(describe c)

;; persist the instance
(define a '())
(set! a (acons 'c c a))
(save-objects a (current-output-port))

$ guile ./persistence-test.scm
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;       or pass the --no-auto-compile argument to disable.
;;; compiling /home/apg/t/./persistence-test.scm
;;; compiled
/home/apg/.cache/guile/ccache/2.0-LE-4-2.0/home/apg/t/persistence-test.scm.go
#<<ag-complex> 932ace0> is an instance of class <ag-complex>
Slots are:
     r = 3
     i = 4
(define c '#<<ag-complex> 932ace0>)

... then that last line, which is the output of save-objects,  doesn't look
sufficient
(to my untrained eye, at any rate) to re-create the object in a
freshly-launched
guile in a new address space.  I'd expect it to contain the values for the
slots
as shown in the output from 'describe', as well as the class definition.

My assumptions derive from these comments in oop/goops/save.scm:

*;;; Save OBJECT ... to PORT so that when the data is read and evaluated
;;; OBJECT ... are re-created under names NAME ... .

*
What am I missing?

- Andrew


On Sun, Jan 20, 2013 at 10:15 PM, Andy Wingo <wingo@pobox.com> wrote:

> Hi,
>
> On Fri 18 Jan 2013 11:25, Andrew Gaylard <ag@computer.org> writes:
>
> > I'm trying to persist a GOOPS object.
> >
> > I've found oop/goops/save.scm, which defines save-object.
> > That looks like what I need.
>
> Cool.  I don't know of anyone that has used this code in many years, so
> be prepared for some bugs.  OTOH if it works fine, documentation is
> appreciated :)
>
> > scheme@(guile-user)> (save-objects l (current-output-port))
> > (define c 'c)
> > ERROR: In procedure hashq-get-handle:
> > ERROR: In procedure hashq-get-handle: Handle access not permitted on
> > weak table
>
> This is an implementation bug, introduced May 2011 by a change to the
> weak table interface.  See commit
> 1d9c2e6271105ee0f728127d9b544432b7cc0f4f for full details.
>
> I have fixed this in stable-2.0.  You can fix in in your copy by:
>
>   (set! (@@ (oop goops save) readable?)
>         (lambda (x) (hashq-ref (@@ (oop goops save) readables) x)))
>
> Regards,
>
> Andy
> --
> http://wingolog.org/
>

[-- Attachment #2: Type: text/html, Size: 3824 bytes --]

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

* Re: Persisting GOOPS objects
  2013-01-21 12:48   ` Andrew Gaylard
@ 2013-01-22  9:47     ` Andy Wingo
  2013-01-23 13:46       ` Andrew Gaylard
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Wingo @ 2013-01-22  9:47 UTC (permalink / raw)
  To: Andrew Gaylard; +Cc: guile-user

On Mon 21 Jan 2013 13:48, Andrew Gaylard <ag@computer.org> writes:

> ;; make a new class, make an instance of it, and show it
> (define-class <ag-complex> (<number>) r i)

I did a ,trace (save-objects a (current-output-port)), and it shows:

     (enumerate-component! #<<ag-complex> 16b9a20> #<<environment> 1d51770>)
     |  (#<procedure 1a6c600 at oop/goops/dispatch.scm:204:4 args> #<<ag-complex> 16b9a20>)
     |  (cache-dispatch #<<generic> immediate? (7)> (#<<ag-complex> 16b9a20>))
     |  [...]
     |  (#<procedure 14dbca0 at oop/goops/save.scm:92:0 (o)> #<<ag-complex> 16b9a20>)
     |  #t

Which means that (immediate? c) ended up dispatching to the procedure on
line 92 of save.scm, which is:

  (define-method (immediate? (o <number>)) #t)

Which returns true because your <ag-complex> is a <number>.

So there seems to be an assumption somewhere that numbers are readable
and writable as-is.  It might suffice to define an (immediate? (o
<ag-complex>)) method.  Dunno, I've never worked with this code
before...  Test cases and documentation are welcome :)

Andy
-- 
http://wingolog.org/



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

* Re: Persisting GOOPS objects
  2013-01-22  9:47     ` Andy Wingo
@ 2013-01-23 13:46       ` Andrew Gaylard
  2013-01-23 15:55         ` Andy Wingo
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Gaylard @ 2013-01-23 13:46 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 1188 bytes --]

I made another attempt using strings instead of numbers, to get an idea of
whether save.scm is usable:

$ cat persistence-test.scm
(use-modules (oop goops))
(use-modules (oop goops describe))
(use-modules (oop goops save))

;; Apply Andy's fix to the readable? function
(set! (@@ (oop goops save) readable?)
      (lambda (x) (hashq-ref (@@ (oop goops save) readables) x)))

;; make a new class, make an instance of it, and show it
(define-class <ag-record> (<object>) name addr)
(define r (make <ag-record>))
(slot-set! r 'name "Fred")
(slot-set! r 'addr "101 Elm Street")
(describe r)

;; persist the instance
(define a '())
(set! a (acons 'r r a))
(save-objects a (current-output-port))

$ guile
scheme@(guile-user)> (load "persistence-test.scm")
#<<ag-record> 9f2b2a0> is an instance of class <ag-record>
Slots are:
     name = "Fred"
     addr = "101 Elm Street"
(define r (restore <ag-record> (name addr) oop/goops/save.scm:423:32: In
procedure #<procedure 9f64f48 at oop/goops/save.scm:414:21 (name aname get
set)>:
oop/goops/save.scm:423:32: Wrong type to apply: #<syntax-transformer
write-component>

... So I guess the answer is, "not yet".

Many thanks for your help,
- Andrew

[-- Attachment #2: Type: text/html, Size: 1524 bytes --]

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

* Re: Persisting GOOPS objects
  2013-01-23 13:46       ` Andrew Gaylard
@ 2013-01-23 15:55         ` Andy Wingo
  2013-01-25 18:06           ` Andrew Gaylard
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Wingo @ 2013-01-23 15:55 UTC (permalink / raw)
  To: Andrew Gaylard; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 1698 bytes --]

Hi,

On Wed 23 Jan 2013 14:46, Andrew Gaylard <ag@computer.org> writes:

> I made another attempt using strings instead of numbers, to get an idea
> of whether save.scm is usable:
>
> $ cat persistence-test.scm
> (use-modules (oop goops))
> (use-modules (oop goops describe))
> (use-modules (oop goops save))
>
> ;; Apply Andy's fix to the readable? function
> (set! (@@ (oop goops save) readable?)
>       (lambda (x) (hashq-ref (@@ (oop goops save) readables) x)))
>
> ;; make a new class, make an instance of it, and show it
> (define-class <ag-record> (<object>) name addr)
> (define r (make <ag-record>))
> (slot-set! r 'name "Fred")
> (slot-set! r 'addr "101 Elm Street")
> (describe r)
>
> ;; persist the instance
> (define a '())
> (set! a (acons 'r r a))
> (save-objects a (current-output-port))
>
> $ guile
> scheme@(guile-user)> (load "persistence-test.scm")
> #<<ag-record> 9f2b2a0> is an instance of class <ag-record>
> Slots are: 
>      name = "Fred"
>      addr = "101 Elm Street"
> (define r (restore <ag-record> (name addr) oop/goops/save.scm:423:32: In
> procedure #<procedure 9f64f48 at oop/goops/save.scm:414:21 (name aname
> get set)>:
> oop/goops/save.scm:423:32: Wrong type to apply: #<syntax-transformer
> write-component>

This tells me that this module has not worked in Guile 2.0.
Write-component is a macro, and all of its uses were before its
definition.

There is no way to patch this easily; you have to edit the file.  The
attached patch is applied to git.

Now:

  (save-objects a (current-output-port))
  =| (define r (restore <ag-record> (name addr) "Fred" "101 Elm Street"))

Regards,

Andy


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-oop-goops-save-fix-compile-time-availability-of-writ.patch --]
[-- Type: text/x-diff, Size: 2354 bytes --]

From a3df9ad9e6be7d5fbc566a10bc9ba035a2e38f31 Mon Sep 17 00:00:00 2001
From: Andy Wingo <wingo@pobox.com>
Date: Wed, 23 Jan 2013 16:53:54 +0100
Subject: [PATCH] oop goops save: fix compile-time availability of
 write-component

* module/oop/goops/save.scm (write-component)
  (write-component-procedure): Move definitions up so that syntax
  definition is available when compiling the rest of the file.
---
 module/oop/goops/save.scm |   41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/module/oop/goops/save.scm b/module/oop/goops/save.scm
index 05362e0..dda2aea 100644
--- a/module/oop/goops/save.scm
+++ b/module/oop/goops/save.scm
@@ -128,6 +128,29 @@
   (hashq-ref readables obj))
 
 ;;;
+;;; Writer helpers
+;;;
+
+(define (write-component-procedure o file env)
+  "Return #f if circular reference"
+  (cond ((immediate? o) (write o file) #t)
+	((readable? o) (write (readable-expression o) file) #t)
+	((excluded? o env) (display #f file) #t)
+	(else
+	 (let ((info (object-info o env)))
+	   (cond ((not (binding? info)) (write-readably o file env) #t)
+		 ((not (eq? (visiting info) #:defined)) #f) ;forward reference
+		 (else (display (binding info) file) #t))))))
+
+;;; write-component OBJECT PATCHER FILE ENV
+;;;
+(define-macro (write-component object patcher file env)
+  `(or (write-component-procedure ,object ,file ,env)
+       (begin
+         (display #f ,file)
+         (add-patcher! ,patcher ,env))))
+
+;;;
 ;;; Strings
 ;;;
 
@@ -603,24 +626,6 @@
 	   (pop-ref! env)
 	   (set! (objects env) (cons o (objects env)))))))
 
-(define (write-component-procedure o file env)
-  "Return #f if circular reference"
-  (cond ((immediate? o) (write o file) #t)
-	((readable? o) (write (readable-expression o) file) #t)
-	((excluded? o env) (display #f file) #t)
-	(else
-	 (let ((info (object-info o env)))
-	   (cond ((not (binding? info)) (write-readably o file env) #t)
-		 ((not (eq? (visiting info) #:defined)) #f) ;forward reference
-		 (else (display (binding info) file) #t))))))
-
-;;; write-component OBJECT PATCHER FILE ENV
-;;;
-(define-macro (write-component object patcher file env)
-  `(or (write-component-procedure ,object ,file ,env)
-       (begin
-         (display #f ,file)
-         (add-patcher! ,patcher ,env))))
 
 ;;;
 ;;; Main engine
-- 
1.7.10.4


[-- Attachment #3: Type: text/plain, Size: 26 bytes --]


-- 
http://wingolog.org/

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

* Re: Persisting GOOPS objects
  2013-01-23 15:55         ` Andy Wingo
@ 2013-01-25 18:06           ` Andrew Gaylard
  0 siblings, 0 replies; 7+ messages in thread
From: Andrew Gaylard @ 2013-01-25 18:06 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 281 bytes --]

Hi Andy,

That did the trick!  Many thanks for your help with this.

I'm now happily persisting and re-materialising objects,
with the proviso that they aren't derived from <number>.
(That was a red herring in the whole exercise; my classes
derive from <object> anyway.)

- Andrew

[-- Attachment #2: Type: text/html, Size: 422 bytes --]

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

end of thread, other threads:[~2013-01-25 18:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-18 10:25 Persisting GOOPS objects Andrew Gaylard
2013-01-20 20:15 ` Andy Wingo
2013-01-21 12:48   ` Andrew Gaylard
2013-01-22  9:47     ` Andy Wingo
2013-01-23 13:46       ` Andrew Gaylard
2013-01-23 15:55         ` Andy Wingo
2013-01-25 18:06           ` Andrew Gaylard

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).