soegaard / pyffi

Use Python from Racket
25 stars 2 forks source link

Issues with `prefix-in` Requires #3

Open abhillman opened 7 months ago

abhillman commented 7 months ago

Observed:

If pyffi is qualified via (require (prefix-in pyffi/ pyffi)), attributes cannot be read off of bound python objects via dot . syntax.

Expected: Not sure. Maybe "just works" or some kind of mention of this in the documentation ideally with a workaround.

Repro:

Save this in py-point-qualified.rkt:

#lang racket

;; load `pyffi` qualified as `pyffi/`
(require (prefix-in pyffi/ pyffi))

;; initialize the python interpreter
(pyffi/initialize)
(pyffi/post-initialize)

;; define `Point` via `collections.namedtuple`
(pyffi/import collections)
(pyffi/run* "Point = collections.namedtuple('Point', 'x y')")

;; create a `Point` object and bind it
(define py-point (pyffi/run "Point(1, 2)"))

(print py-point.x)

Then run it:

$ racket py-point-qualified.rkt
py-point-qualified.rkt:20:7: py-point.x: unbound identifier
  in: py-point.x
  location...:
   py-point-qualified.rkt:20:7

Extra info:

This is not an issue if prefix-in is not used:

$ cat py-point-qualified.rkt
#lang racket

;; require pyffi
(require pyffi)

;; initialize the python interpreter
(initialize)
(post-initialize)

;; define `Point` via `collections.namedtuple`
(import collections)
(run* "Point = collections.namedtuple('Point', 'x y')")

;; create a `Point` object and bind it
(define py-point (run "Point(1, 2)"))

;; expose py-point
(provide py-point)

(print py-point.x)

$ racket py-point-qualified.rkt
1
abhillman commented 7 months ago

Workaround https://github.com/soegaard/pyffi/issues/2#issuecomment-1806661554