AeroEng43 / shedskin

Automatically exported from code.google.com/p/shedskin
0 stars 0 forks source link

operator module #50

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I noticed a lack of an operator module, so I decided to write a basic 
one...

def add(a, b):
    return a + b

def sub(a, b):
    return a - b

def mul(a, b):
    return a * b

def div(a, b):
    return a / b

def floordiv(a, b):
    return int(a / b)

def truediv(a, b):
    # no easy way to implement truediv in 2.6.
    return a.__truediv__(b)

def and_(a, b):
    return a and b

def or_(a, b):
    return a or b

def not_(a):
    return not a

def inv_(a, b):
    return ~a

__add__ = add
__sub__ = sub
__mul__ = mul
__div__ = div
__floordiv__ = floordiv
__truediv__ = truediv
__and__ = and_
__or__ = or_
__not__ = not_
__inv__ = inv_

I got an error while trying to compile it though.

thomas@kamikaze:~/shedskin/lib$ python ../ss.py operator.py 
*** SHED SKIN Python-to-C++ Compiler 0.2 ***
Copyright 2005-2009 Mark Dufour; License GNU GPL version 3 (See LICENSE)

Traceback (most recent call last):
  File "../ss.py", line 433, in <module>
    main()
  File "../ss.py", line 428, in main
    analysis(name)
  File "../ss.py", line 51, in analysis
    getgx().main_module = parse_module(getgx().main_mod, ast)
  File "/home/thomas/shedskin/graph.py", line 1428, in parse_module
    mv.dispatch(mod.ast)
  File "/home/thomas/shedskin/graph.py", line 54, in dispatch
    ASTVisitor.dispatch(self, node, *args)
  File "/usr/lib/python2.6/compiler/visitor.py", line 57, in dispatch
    return meth(node, *args)
  File "/home/thomas/shedskin/graph.py", line 248, in visitModule
    self.visit(n)
  File "/home/thomas/shedskin/graph.py", line 54, in dispatch
    ASTVisitor.dispatch(self, node, *args)
  File "/usr/lib/python2.6/compiler/visitor.py", line 57, in dispatch
    return meth(node, *args)
  File "/home/thomas/shedskin/graph.py", line 384, in visitFrom
    mod = self.importmodules(node.modname, node, True)
  File "/home/thomas/shedskin/graph.py", line 360, in importmodules
    mod = self.importmodule(subname, subname, node, fake)
  File "/home/thomas/shedskin/graph.py", line 367, in importmodule
    mod = self.analyzeModule(name, pseudonym, node, fake)
  File "/home/thomas/shedskin/graph.py", line 421, in analyzeModule
    mod = parse_module(name, None, getmv().module, node)
  File "/home/thomas/shedskin/graph.py", line 1428, in parse_module
    mv.dispatch(mod.ast)
  File "/home/thomas/shedskin/graph.py", line 54, in dispatch
    ASTVisitor.dispatch(self, node, *args)
  File "/usr/lib/python2.6/compiler/visitor.py", line 57, in dispatch
    return meth(node, *args)
  File "/home/thomas/shedskin/graph.py", line 242, in visitModule
    self.visit(Class(dummy, [], None, Pass()))
  File "/home/thomas/shedskin/graph.py", line 54, in dispatch
    ASTVisitor.dispatch(self, node, *args)
  File "/usr/lib/python2.6/compiler/visitor.py", line 57, in dispatch
    return meth(node, *args)
  File "/home/thomas/shedskin/graph.py", line 1166, in visitClass
    if not getmv().module.builtin and not node in getmv().classnodes:
AttributeError: moduleVisitor instance has no attribute 'classnodes'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/apport_python_hook.py", line 39, 
in apport_excepthook
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python2.6/dist-packages/apport/__init__.py", line 1, in 
<module>
    from apport.report import Report
  File "/usr/lib/python2.6/dist-packages/apport/report.py", line 14, in 
<module>
    import subprocess, tempfile, os.path, urllib, re, pwd, grp, os, sys
  File "/usr/lib/python2.6/subprocess.py", line 430, in <module>
    import pickle
  File "/usr/lib/python2.6/pickle.py", line 827, in <module>
    class Unpickler:
  File "/usr/lib/python2.6/pickle.py", line 957, in Unpickler
    def load_binfloat(self, unpack=struct.unpack):
AttributeError: 'module' object has no attribute 'unpack'

Original exception was:
Traceback (most recent call last):
  File "../ss.py", line 433, in <module>
    main()
  File "../ss.py", line 428, in main
    analysis(name)
  File "../ss.py", line 51, in analysis
    getgx().main_module = parse_module(getgx().main_mod, ast)
  File "/home/thomas/shedskin/graph.py", line 1428, in parse_module
    mv.dispatch(mod.ast)
  File "/home/thomas/shedskin/graph.py", line 54, in dispatch
    ASTVisitor.dispatch(self, node, *args)
  File "/usr/lib/python2.6/compiler/visitor.py", line 57, in dispatch
    return meth(node, *args)
  File "/home/thomas/shedskin/graph.py", line 248, in visitModule
    self.visit(n)
  File "/home/thomas/shedskin/graph.py", line 54, in dispatch
    ASTVisitor.dispatch(self, node, *args)
  File "/usr/lib/python2.6/compiler/visitor.py", line 57, in dispatch
    return meth(node, *args)
  File "/home/thomas/shedskin/graph.py", line 384, in visitFrom
    mod = self.importmodules(node.modname, node, True)
  File "/home/thomas/shedskin/graph.py", line 360, in importmodules
    mod = self.importmodule(subname, subname, node, fake)
  File "/home/thomas/shedskin/graph.py", line 367, in importmodule
    mod = self.analyzeModule(name, pseudonym, node, fake)
  File "/home/thomas/shedskin/graph.py", line 421, in analyzeModule
    mod = parse_module(name, None, getmv().module, node)
  File "/home/thomas/shedskin/graph.py", line 1428, in parse_module
    mv.dispatch(mod.ast)
  File "/home/thomas/shedskin/graph.py", line 54, in dispatch
    ASTVisitor.dispatch(self, node, *args)
  File "/usr/lib/python2.6/compiler/visitor.py", line 57, in dispatch
    return meth(node, *args)
  File "/home/thomas/shedskin/graph.py", line 242, in visitModule
    self.visit(Class(dummy, [], None, Pass()))
  File "/home/thomas/shedskin/graph.py", line 54, in dispatch
    ASTVisitor.dispatch(self, node, *args)
  File "/usr/lib/python2.6/compiler/visitor.py", line 57, in dispatch
    return meth(node, *args)
  File "/home/thomas/shedskin/graph.py", line 1166, in visitClass
    if not getmv().module.builtin and not node in getmv().classnodes:
AttributeError: moduleVisitor instance has no attribute 'classnodes'

Original issue reported on code.google.com by toldb...@gmail.com on 8 Jan 2010 at 7:06

GoogleCodeExporter commented 8 years ago
Hi,

I've no problem running shedskin.py on your code with either 
CPython2.{5,6,7a1+} (but
then, the C++ code does not compile).

What version of shedskin do you use? Did you try the svn version?

Best regards,

Original comment by arkanosis on 8 Jan 2010 at 9:11

GoogleCodeExporter commented 8 years ago
thanks for giving the operator module a try. the code compiles to C++ fine 
here, too,
using SVN.

unfortunately, it's not just a matter of compiling a pure-python version. the 
reason
is that when we pass around functions from this module, we're actually passing 
around
something dynamic, in the sense that these functions work on objects of 
different
types. this is not straightforward to translate to C++.. 

there are some hacks in place to make this work for builtins such as 'int' 
(which
also operate on different types). the operator module could probably be 
supported by
using similar hacks (adding a static wrapper for each place where a function is
directly referenced). 

Original comment by mark.duf...@gmail.com on 9 Jan 2010 at 10:05

GoogleCodeExporter commented 8 years ago

Original comment by mark.duf...@gmail.com on 22 Jan 2010 at 7:55

GoogleCodeExporter commented 8 years ago

Original comment by mark.duf...@gmail.com on 18 Mar 2010 at 7:59

GoogleCodeExporter commented 8 years ago
I merged all standard library issues into issue 106.

Original comment by mark.duf...@gmail.com on 27 Oct 2010 at 1:02

GoogleCodeExporter commented 8 years ago
I merged all standard library issues into issue 106.

Original comment by mark.duf...@gmail.com on 27 Oct 2010 at 1:02

GoogleCodeExporter commented 8 years ago
I'd like to solve a side-problem here. The original error was actually 
"AttributeError: moduleVisitor instance has no attribute 'classnodes'" and 
that's because he went into the shedskin lib folder and tried to run "python 
../ss.py operator.py".

When you're in that lib folder, a lot of imports go wrong and cause that issue. 
That's the destination folder for libs, not where they should be compiled.

Original comment by fahh...@gmail.com on 28 Feb 2011 at 4:23