jbaiter / chdkptp.py

Python bindings for chdkptp
GNU General Public License v3.0
13 stars 14 forks source link

Python3.6 compatibility #6

Closed 5up3rD4n1 closed 6 years ago

5up3rD4n1 commented 6 years ago

Hi Baiter, I'm trying to import chdkptp with python 3.6 (I know it is not supported yet) but maybe you can help me out wit this.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sd/.pyenv/versions/3.6.0/lib/python3.6/site-packages/chdkptp/__init__.py", line 1, in <module>
    from chdkptp.device import ChdkDevice, list_devices, DeviceInfo
  File "/home/sd/.pyenv/versions/3.6.0/lib/python3.6/site-packages/chdkptp/device.py", line 3, in <module>
    import StringIO
ModuleNotFoundError: No module named 'StringIO'

So, StringIO is deprecated on Python3.6 so I try to 'hack' the system modules with

>>> import sys, io
>>> sys.modules['StringIO'] = io

and I'm getting now the following Traceback when I import chdkptp

>>> import sys, io
>>> sys.modules['StringIO'] = io
>>> import chdkptp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sd/.pyenv/versions/3.6.0/lib/python3.6/site-packages/chdkptp/__init__.py", line 1, in <module>
    from chdkptp.device import ChdkDevice, list_devices, DeviceInfo
  File "/home/sd/.pyenv/versions/3.6.0/lib/python3.6/site-packages/chdkptp/device.py", line 8, in <module>
    from chdkptp.lua import LuaContext, global_lua, parse_table
  File "/home/sd/.pyenv/versions/3.6.0/lib/python3.6/site-packages/chdkptp/lua.py", line 141, in <module>
    global_lua = LuaContext()
  File "/home/sd/.pyenv/versions/3.6.0/lib/python3.6/site-packages/chdkptp/lua.py", line 92, in __init__
    self._setup_runtime()
  File "/home/sd/.pyenv/versions/3.6.0/lib/python3.6/site-packages/chdkptp/lua.py", line 100, in _setup_runtime
    """)
  File "lupa/_lupa.pyx", line 262, in lupa._lupa.LuaRuntime.execute
  File "lupa/_lupa.pyx", line 1279, in lupa._lupa.run_lua
  File "lupa/_lupa.pyx", line 1288, in lupa._lupa.call_lua
  File "lupa/_lupa.pyx", line 1314, in lupa._lupa.execute_lua_call
  File "lupa/_lupa.pyx", line 1250, in lupa._lupa.raise_lua_error
lupa._lupa.LuaError: [string "<python>"]:3: attempt to concatenate global 'CHDKPTP_PATH' (a userdata value)
stack traceback:
    [string "<python>"]:3: in main chunk

Do you have an idea how can I solve this?

5up3rD4n1 commented 6 years ago

I found that the encode=None at the lua runtime was the cause of the issue, but now I can't install the package.

lupa._lupa.LuaError: [string "<python>"]:2: module 'chdkptp' not found:
    no field package.preload['chdkptp']
    no file '/home/sd/Documents/HACKPLACE/chdkptp.py/chdkptp/vendor/chdkptp/lua/chdkptp.lua'
    no file '/usr/local/share/lua/5.2/chdkptp.lua'
    no file '/usr/local/share/lua/5.2/chdkptp/init.lua'
    no file '/usr/local/lib/lua/5.2/chdkptp.lua'
    no file '/usr/local/lib/lua/5.2/chdkptp/init.lua'
    no file '/usr/share/lua/5.2/chdkptp.lua'
    no file '/usr/share/lua/5.2/chdkptp/init.lua'
    no file './chdkptp.lua'
    no file '/home/sd/Documents/HACKPLACE/chdkptp.py/chdkptp/vendor/chdkptp/chdkptp.so'
    no file '/home/sd/Documents/HACKPLACE/chdkptp.py/chdkptp/vendor/chdkptp/lua/chdkptp.lua'
    no file '/usr/local/share/lua/5.2/chdkptp.lua'
    no file '/usr/local/share/lua/5.2/chdkptp/init.lua'
    no file '/usr/local/lib/lua/5.2/chdkptp.lua'
    no file '/usr/local/lib/lua/5.2/chdkptp/init.lua'
    no file '/usr/share/lua/5.2/chdkptp.lua'
    no file '/usr/share/lua/5.2/chdkptp/init.lua'
    no file './chdkptp.lua'
stack traceback:
    [C]: in function 'require'
    [string "<python>"]:2: in main chunk

Looks like the package is looking for the lua files at my relative path not inside the chdkptp installation path.

5up3rD4n1 commented 6 years ago

So... I forked the repo and made it compatible with python3

Just if someone find it useful, here is the repo:

https://github.com/5up3rD4n1/chdkptp.py

I'm closing the issue. Thanks! :) :+1:

jbaiter commented 6 years ago

Could you submit a pull request with your changes? I'd love to add Python 3 support, but I haven't used the library in a a long time and no longer have access to a camera that can run chdk :/

5up3rD4n1 commented 6 years ago

Sure, no problem!

emanuelelaface commented 5 years ago

Hi, I am using the Python3 fork of @5up3rD4n1 but after the installation I have the error dynamic libraries not enabled; check your Lua installation I see on some other group that you had the same issue, did you find a way to fix it?

R-ay commented 5 years ago

Hello, I used the python3.7.4 fork @5up3rD4n1 also and did the installation. Also I have lupa 1.8. Everything worked except when I launch the test.py file.

I have a "Segmentation fault (coredumped)" So I tried to debug this with gdb :

0 0x00007ffff69b1421 in ?? () from /usr/lib/liblua5.2.so.5.2

1 0x00007ffff69aea89 in lua_error () from /usr/lib/liblua5.2.so.5.2

2 0x00007ffff69bfc88 in luaL_error () from /usr/lib/liblua5.2.so.5.2

3 0x00007ffff69c11ed in luaLcheckversion () from /usr/lib/liblua5.2.so.5.2

4 0x00007ffff69c12b4 in luaL_setfuncs () from /usr/lib/liblua5.2.so.5.2

5 0x00007ffff6ac1466 in luaopen_lfs (L=0x5555556a3c68) at lfs.c:886

6 0x00007ffff6acbc80 in luaopen_chdkptp (L=0x5555556a3c68) at chdkptp.c:2202

7 0x00007ffff6a71ac7 in ?? () from /usr/lib/liblua.so.5.3

8 0x00007ffff6a71cf3 in ?? () from /usr/lib/liblua.so.5.3

9 0x00007ffff6a71d51 in ?? () from /usr/lib/liblua.so.5.3

10 0x00007ffff6a6d7bc in lua_callk () from /usr/lib/liblua.so.5.3

I don't think it's relevant, so I used pycharm to debug test.py. Seems like the segfault come when I import chdkptp, so I went deep, and found that this line is causing the segfault :

 self._rt.execute("""
            require('chdkptp')
            util = require('util')
            util:import()
            varsubst = require('varsubst')
            chdku = require('chdku')
            exposure = require('exposure')
            dng = require('dng')
            prefs = require('prefs')
            fsutil = require('fsutil')
        """.format(CHDKPTP_PATH))

Specially the require('chdkptp'), because when I remove it (I know it's a must have line), I have this error (no more segfault):

lupa._lupa.LuaError: ../chdkptp.py/chdkptp/vendor/chdkptp/lua/chdku.lua:23: attempt to index a nil value (global 'sys')

Next solution I tried, I removed the modification done by @5up3rD4n1, which was : self._rt = LuaRuntime(unpack_returned_tuples=True, encoding='latin-1') So I put encoding = None self._rt = LuaRuntime(unpack_returned_tuples=True, encoding=None) But I have this issue :

lupa._lupa.LuaError: [string ""]:3: attempt to concatenate a POBJECT value (global 'CHDKPTP_PATH')

I tried the chdkptp.py python2.7, it works.

To sump up, the segfault is caused by "require('chdkptp')" when the encoding is set to 'latin-1', but when there is no encoding we have the problem with the POBJECT.

I would like to make it fully compatible with 3.7 and with the last chdkptp, but this segfault issue is really annoying, do you have any idea?

Thank you! :)