[plt-scheme] modules, namespaces, and drscheme tools

From: Daniel Silva (dansilva at lynx.dac.neu.edu)
Date: Fri May 2 05:04:12 EDT 2003

Hello,

In order to implement the importing of python modules, I evaluate the
contents of the python file in a new namespace and return an object
representing the imported python module.  This new namespace needs to
have the basic runtime libraries, but dynamically requiring them takes
way too long.  To get around that, I save the namespace from when the
interpreter was first invoked as a cache namespace, and load the
necessary scheme modules there.  Then, when making a new namespace, I
namespace-attach-module and require-module the modules from the cache to
the new one.

This seems to work fine when I do this from the repl in the module
language, but it breaks when I try it as a DrScheme language tool.  The
error messages I get are:

exn:application:mismatch namespace-attach-module: a different module
with the same name is already in the destination namespace, for name:
|,/home/daniel/plt/collects/python/primitives

(when setting up the environment in language:on-execute), and

compile: bad syntax; reference to top-level identifiers is not allowed,
because no #%top syntax transformer is bound

(when running the code in language:front-end), as well as

require: broken compiled code (phase 0): cannot find module
|,/home/daniel/plt/collects/python/python-import| in: python-load-module

(when I hit check-syntax).


I've tried not caching until on-execute is called, at which point I set
the cache to be (current-namespace), but that doesn't seem to help.

I've also wrapped the namespace-attach-module with a with-handlers to
print out success/failure, the require spec, and the path returned by
the current-module-name-resolver when asked to find the spec in the
cache namespace.

The files I try to load at the handler for on-execute are base.ss,
runtime-support.ss, python-import.ss, and primitives.ss.
Upon starting DrScheme with the Python language, this pops out in the
repl:

Welcome to DrScheme, version 203.10-cvs27apr2003.
Language: Python.
> 
FAILURE. spec: (lib base.ss python) path:
,/home/daniel/plt/collects/python/base exn:
#(struct:exn:application:mismatch namespace-attach-module: a different
module with the same name is already in the destination namespace, for
name: |,/home/daniel/plt/collects/python/primitives|
#<continuation-mark-set> ,/home/daniel/plt/collects/python/primitives)
FAILURE. spec: (lib runtime-support.ss python) path:
,/home/daniel/plt/collects/python/runtime-support exn:
#(struct:exn:application:mismatch namespace-attach-module: a different
module with the same name is already in the destination namespace, for
name: |,/home/daniel/plt/collects/python/runtime-support|
#<continuation-mark-set>
,/home/daniel/plt/collects/python/runtime-support)
FAILURE. spec: (lib python-import.ss python) path:
,/home/daniel/plt/collects/python/python-import exn:
#(struct:exn:application:mismatch namespace-attach-module: a different
module with the same name is already in the destination namespace, for
name: |,/home/daniel/plt/collects/python/python-import|
#<continuation-mark-set>
,/home/daniel/plt/collects/python/python-import)
FAILURE. spec: (lib primitives.ss python) path:
,/home/daniel/plt/collects/python/primitives exn:
#(struct:exn:application:mismatch namespace-attach-module: a different
module with the same name is already in the destination namespace, for
name: |,/home/daniel/plt/collects/python/primitives|
#<continuation-mark-set> ,/home/daniel/plt/collects/python/primitives)



Then when I open and execute a python file with these two lines:
  import module_b as mb
  mb

For which the code becomes:
 (define mb
   (call-with-values
     (lambda () (python-load-module (list 'module_b)))
     namespace->py-module%)))
  mb

I get this:

Welcome to DrScheme, version 203.10-cvs27apr2003.
Language: Python.
FAILURE. spec: (lib base.ss python) path:
,/home/daniel/plt/collects/python/base exn:
#(struct:exn:application:mismatch namespace-attach-module: a different
module with the same name is already in the destination namespace, for
name: |,/home/daniel/plt/collects/python/base| #<continuation-mark-set>
,/home/daniel/plt/collects/python/base)
FAILURE. spec: (lib runtime-support.ss python) path:
,/home/daniel/plt/collects/python/runtime-support exn:
#(struct:exn:application:mismatch namespace-attach-module: a different
module with the same name is already in the destination namespace, for
name: |,/home/daniel/plt/collects/python/runtime-support|
#<continuation-mark-set>
,/home/daniel/plt/collects/python/runtime-support)
FAILURE. spec: (lib python-import.ss python) path:
,/home/daniel/plt/collects/python/python-import exn:
#(struct:exn:application:mismatch namespace-attach-module: a different
module with the same name is already in the destination namespace, for
name: |,/home/daniel/plt/collects/python/python-import|
#<continuation-mark-set>
,/home/daniel/plt/collects/python/python-import)
FAILURE. spec: (lib primitives.ss python) path:
,/home/daniel/plt/collects/python/primitives exn:
#(struct:exn:application:mismatch namespace-attach-module: a different
module with the same name is already in the destination namespace, for
name: |,/home/daniel/plt/collects/python/primitives|
#<continuation-mark-set> ,/home/daniel/plt/collects/python/primitives)
require: broken compiled code (phase 0): cannot find module
|,/home/daniel/plt/collects/python/python-import|


Am I doing something wrong and got lucky with the first case or did I
just miss something about the tool interface?

- Daniel




Posted on the users mailing list.