py2cr
py2cr copied to clipboard
Exception: Unknown klass <class 'ast.Constant'> in CrystalTypes
When running via python 3.9.9, I tested the app on https://github.com/viraptor/arpy/blob/master/arpy.py
The result is: (with the end of verbose output)
...
ClassDef class_name[ArchiveFileData] base_classes: ['io.IOBase'] base_rclasses: ['io.IOBase']
ClassDef class_name[ArchiveFileData] bases: ['Io.IOBase']
self._self_functions : ['__init__', 'read', 'tell', 'seek', 'seekable', '__enter__', '__exit__']
self._classes_self_functions : {'ArchiveFormatError': [], 'ArchiveAccessError': [], 'ArchiveFileHeader': ['__init__', '__repr__']}
Traceback (most recent call last):
File "/home/viraptor/Projects/py2cr/py2cr.py", line 5, in <module>
py2cr.main()
File "/home/viraptor/Projects/py2cr/py2cr/__init__.py", line 3189, in main
rtn = convert_py2rb_write(py_path, options.base_path_count, subfilenames,
File "/home/viraptor/Projects/py2cr/py2cr/__init__.py", line 2966, in convert_py2rb_write
rtn, header, data = convert_py2rb(s, dir_path, name_path, base_path_count, mods, mod_paths, no_stop=no_stop, verbose=verbose)
File "/home/viraptor/Projects/py2cr/py2cr/__init__.py", line 2912, in convert_py2rb
v.visit(t)
File "/home/viraptor/Projects/py2cr/py2cr/__init__.py", line 425, in visit
return visitor(node)
File "/home/viraptor/Projects/py2cr/py2cr/__init__.py", line 451, in visit_Module
self.visit(stmt)
File "/home/viraptor/Projects/py2cr/py2cr/__init__.py", line 425, in visit
return visitor(node)
File "/home/viraptor/Projects/py2cr/py2cr/__init__.py", line 795, in visit_ClassDef
self.visit(stmt)
File "/home/viraptor/Projects/py2cr/py2cr/__init__.py", line 425, in visit
return visitor(node)
File "/home/viraptor/Projects/py2cr/py2cr/__init__.py", line 532, in visit_FunctionDef
argxlist.append(anno.visit())
File "/home/viraptor/Projects/py2cr/py2cr/types.py", line 87, in visit
raise Exception("Unknown klass %s in CrystalTypes" % type(node))
Exception: Unknown klass <class 'ast.Constant'> in CrystalTypes
Hi @viraptor I've been working on some feature branches with improved types support, num.cr support and more tests. Per your report I've made updates just now that should allow you to translate the arpy.py code you provided (thanks for the testcase), though the post-translated Crystal code has features that prevent it from compiling cleanly. The changes have all been merged into the main branch of py2cr. Let me know if you still have crashes or suggestions for better output.
That's awesome, thanks. I just did a driveby check out of curiosity.
I haven't looked into this deeply, but some obvious things that seem to be needed for this file to compile are:
-
struct
implementation/renaming - io.seekable - from python doesn't seem to have a match in crystal (apart from checking if
seek(0, Seek::Current)
raises?)
Also I noticed that foo is None
is translated into foo.nil?
, but:
while self.read_next_header() is not None:
became
while !read_next_header().equal?(nil)
instead of
while !read_next_header().nil?
@viraptor, I updated handing of is None
and is not None
with some added tests just a moment ago, which should take care of the last concern.
As for the other two concerns: struct
renaming and io.seekable
- It's not clear to me at this point how that may be workable in py2cr, as the differences between Python/Crystal here may not be 1:1, and i'm not sure how what the appropriate mapping is.
I only briefly glanced at your arpy code (and haven't used struct in python) but if it involves packing/unpacking binary, maybe something in here could be of use? https://therubyist.org/2020/05/07/rewriting-rubys-pack-and-unpack-methods-in-crystal/
Please feel free to submit a PR for these if you wish.