#12868: Attaching a pyx file in the presence of __init__.py results in wrong 
module
names
-----------------------------+----------------------------------------------
       Reporter:  SimonKing  |         Owner:  jason   
           Type:  defect     |        Status:  new     
       Priority:  major      |     Milestone:  sage-5.1
      Component:  misc       |    Resolution:          
       Keywords:             |   Work issues:          
Report Upstream:  N/A        |     Reviewers:          
        Authors:             |     Merged in:          
   Dependencies:             |      Stopgaps:          
-----------------------------+----------------------------------------------

Comment (by leif):

 Replying to [comment:13 leif]:
 > Replying to [comment:6 SimonKing]:
 > > Replying to [comment:5 leif]:
 > > > ... the following does '''not''' solve your problem (if it really is
 one):
 > >
 > > We have a module containing a class bla, but
 `sys.modules[bla.__module__]` gives a key error. I am surprised that you
 question whether that is a problem.
 >
 > That should IMHO be solvable by importing / loading the module
 differently, although I think you run into the same if for example you
 create links to extension modules and import from these rather than their
 original location.

 I think that's actually a cleaner (and probably also safer) solution.

 {{{
 #!diff
 diff --git a/sage/misc/cython.py b/sage/misc/cython.py
 --- a/sage/misc/cython.py
 +++ b/sage/misc/cython.py
 @@ -382,6 +382,10 @@
          os.system(cmd)
          if os.path.exists("%s/setup.py" % build_dir):
              os.unlink("%s/setup.py" % build_dir)
 +        if os.path.exists("%s/__init__.py" % build_dir):
 +            sys.stderr.write("Note: Not ignoring %s/__init__.py ...\n" %
 abs_base)
 +            sys.stderr.flush()
 +            # os.unlink("%s/__init__.py" % build_dir)

      if compile_message:
          print "Compiling %s..."%filename
 @@ -527,7 +531,13 @@
          if os.system(cmd):
              raise RuntimeError, "Error making local copy of shared object
 library for %s"%filename

 -    return name, build_dir
 +    if os.path.exists("%s/__init__.py" % build_dir):
 +        # In that case, the module name Cython creates contains the
 package
 +        # name, i.e., the name of the directory the file is contained in.
 +        assert not build_dir.endswith(os.path.sep)
 +        return "%s.%s" % (base, name), os.path.dirname(build_dir)
 +    else:
 +        return name, build_dir



 }}}

 Note that ''this'' change has the side effect that now `__init__.py` also
 gets executed, which IMHO is the desired behaviour:
 {{{
 sage: attach("/tmp/foo/foo.pyx")
 Note: Not ignoring /tmp/foo/__init__.py ...
 Compiling /tmp/foo/foo.pyx...
 Hello, I'm '/tmp/foo/__init__.py'.
 sage: bar.__module__
 '_tmp_foo_foo_pyx._tmp_foo_foo_pyx_0'
 sage: sys.modules[bar.__module__]
 <module '_tmp_foo_foo_pyx._tmp_foo_foo_pyx_0' from
 
'/home/leif/.sage//temp/sleepless/21086//spyx/_tmp_foo_foo_pyx/_tmp_foo_foo_pyx_0.so'>
 sage: bar?
 Type:           classobj
 String Form:    _tmp_foo_foo_pyx._tmp_foo_foo_pyx_0.bar
 Namespace:      Interactive
 Loaded File:
 
/home/leif/.sage/temp/sleepless/21086/spyx/_tmp_foo_foo_pyx/_tmp_foo_foo_pyx_0.so
 Source File:
 
/home/leif/.sage/temp/sleepless/21086/spyx/_tmp_foo_foo_pyx/_tmp_foo_foo_pyx_0.so

 sage:
 }}}

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/12868#comment:14>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to