omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Importing from dynamic library written in C

    Pythonista
    3
    5
    2485
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • sulcud
      sulcud last edited by sulcud

      Hello everyone, I have this stupid question that has a high chance of receive a NO as answer.

      First the background.
      Recently I have testing the python feature of importing code written in C and compiled into dynamic libraries .so files on my PC, In my tests I could notice that I can import files with only the read and write permissions, so base on that I conclude that I don't need the execution permission in that kind of file to import them to python.

      All of those tests where made approaching

      import ctypes
      
      
      my_c_library = ctypes.cdll.LoadLibrary("/path/to/library")
      my_c_library.my_c_function(arg_1, arg_2)
      

      Now that I have access to a Mac I was trying to approach the Xcode tools to compile a dynamic library and import it to Pythonista, the problem is that it is extremely hard to setup the environment for the compilation to iOS, I mean, I could compile to arm64 but looks like iOS has other libraries that I have no access to (or at least I don't know where to find them)

      If anyone have a solution or have an alternative way to approach something similar I really will appreciate it

      JonB 1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        You would need compile it into the app. Which is not be possible without the source code to Pythonista. You would be able to do it with Pyto which is open source but it would be complicated.

        1 Reply Last reply Reply Quote 0
        • sulcud
          sulcud last edited by ccc

          @ccc said:

          You would need compile it into the app. Which is not be possible without the source code to Pythonista. You would be able to do it with Pyto which is open source but it would be complicated.

          You are right. With the AppStore version of both apps is not possible, I need to compile my own, at least can test it with Pyto.

          In my last approach I ended by compiling a test C library for arm64 and then I signed it with codesign, Pythonista apparently accept the dynamic library but stops importing it as the identity used to signed it is not the same of the identity the owner of the app used (or at least that is what I think, really don’t know which other thing is preventing to load it)

          If you are interested in reading the traceback of Pythonista:

          Traceback (most recent call last):
            File "/private/var/mobile/Containers/Shared/AppGroup/HIDDEN/Pythonista3/Documents/hw.py", line 4, in <module>
              test_lib = ctypes.cdll.LoadLibrary("./test-lib_1.so")
            File "/var/containers/Bundle/Application/HIDDEN/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/ctypes/__init__.py", line 427, in LoadLibrary
              return self._dlltype(name)
            File "/var/containers/Bundle/Application/HIDDEN/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/ctypes/__init__.py", line 349, in __init__
              self._handle = _dlopen(self._name, mode)
          OSError: dlopen(/private/var/mobile/Containers/Shared/AppGroup/HIDDEN/Pythonista3/Documents/test-lib_1.so, 0x0006): code signature invalid (errno=1) sliceOffset=0x00000000, codeBlobOffset=0x00008050, codeBlobSize=0x00004900 for '/private/var/mobile/Containers/Shared/AppGroup/HIDDEN/Pythonista3/Documents/test-lib_1.so'
          

          How apple encapsulates the apps is very interesting and I still believe that someone super skilled can archive this, anyway, thanks again @ccc for your quick answer

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            Yes, apple sandbox prevents you from loading dynamic libraries not signed by either your own app, or signed by apple.

            1 Reply Last reply Reply Quote 0
            • JonB
              JonB @sulcud last edited by

              @sulcud on second thought, I'm not sure what prevents one from re-signing the ipa file with their own provisioning profile, and reinstalling. See also here which has a lot more info, and a script to do the work for you.

              It appears that this allows you to update the entitlements plist, and update code signature to match whatever c libraries you are making.

              Presumably this means the "new" app gets a different name, and you then have to sideload it.

              It would be super interesting to hear if this works -- it might raise the possibility for people with XCode to integrate their own custom compiled dynamic libraries and call them from pythonista (scipy/pandas?)

              1 Reply Last reply Reply Quote 2
              • First post
                Last post
              Powered by NodeBB Forums | Contributors