PythonKit throws exception

292 views
Skip to first unread message

shadi abu samra

unread,
Nov 5, 2020, 5:35:13 AM11/5/20
to Swift for TensorFlow
Hello,
When call python function with throw exception from swift the app crashed

Fatal error: 'try!' expression unexpectedly raised an error: Python exception: This is an example exception for GET

Swift code

do {

            let model = try Python.attemptImport("FinalTest")

            let value = try? model.get(true)

        } catch {

            print("==> error: \(error)")

        } 

Python function 

def swift_call_target():

    raise Exception("This is an example exception for GET")

Is there any way to call throw function? 

Please advice


Thanks 

Brennan Saeta

unread,
Nov 5, 2020, 11:14:20 AM11/5/20
to shadi abu samra, Swift for TensorFlow
Hi Shadi,

Unfortunately, I don't quite understand your code snippet. Did you leave some code out? 

All the best,
-Brennan


--
To unsubscribe from this group and stop receiving emails from it, send an email to swift+un...@tensorflow.org.

shadi abu samra

unread,
Nov 5, 2020, 11:25:05 AM11/5/20
to Brennan Saeta, Swift for TensorFlow
Hi Brennan,

The Swift code use Pythonkit to call swift_call_target in Python 

But the Python swift_call_target function throw exception and in this case the swift app crash 
I need way to use do catch in swift to catch  exception error
--
Regards Shadi

Ravindra Sonkar

unread,
Nov 16, 2020, 11:52:57 PM11/16/20
to Swift for TensorFlow, eng.sha...@gmail.com, Swift for TensorFlow, Brennan Saeta
I'm getting the same problem. Please help up out.

shadi abu samra

unread,
Nov 17, 2020, 1:34:41 AM11/17/20
to Ravindra Sonkar, Brennan Saeta, Swift for TensorFlow
For now I make python function code return tuple of (data,error)

This e-mail and any files transmitted with it may contain confidential and privileged information are for the sole use of the intended recipient(s). If you are not the intended recipient, please apprise the sender by reply e-mail and destroy all copies and the original message. Any unauthorised disclosure, dissemination, forwarding, printing or copying of this email or any action taken on this e-mail is strictly prohibited and may be unlawful. The recipient acknowledges that Jellyfish Technologies or its subsidiaries and associated companies, are unable to exercise control or ensure or guarantee the integrity of the contents of the information contained in e-mail transmissions and further acknowledges that any views expressed in this message are those of the individual senders and no binding nature of the message shall be implied or assumed unless the sender does so expressly with due authority of Jellyfish Technologies. Since Jellyfish Technologies cannot make a warranty that this email is free of virus/errors, kindly scan the email with antivirus software before opening the attachments/links.
--
Regards Shadi

Ravindra Sonkar

unread,
Nov 17, 2020, 3:46:03 AM11/17/20
to Swift for TensorFlow, eng.sha...@gmail.com, Brennan Saeta, Swift for TensorFlow, Ravindra Sonkar
can you share a sample of it?

shadi abu samra

unread,
Nov 18, 2020, 1:41:55 PM11/18/20
to Ravindra Sonkar, Swift for TensorFlow, Brennan Saeta
@Ravindra Sonkar 
I find a way to catch python exceptions please check below code and let me know if it works as expected

 func callFunction(name: String, withArguments args: [PythonConvertible]) -> Result<PythonObject, Error> {

        guard let functionObject = self.checking[dynamicMember: name] else {

            return .failure(CustomError.with(localizedDescription: "Could not access PythonObject member \(name)"))

        }

        do {

            let data = try functionObject.throwing.dynamicallyCall(withArguments: args)

            return .success(data)

        } catch {

            let pythonError = error as? PythonError

            let localizedDescription = pythonError?.description ?? error.localizedDescription

            return .failure(CustomError.with(localizedDescription: localizedDescription))

        }

    }


 

Regards,
------------------
Shadi Awwad.
Sr. Mobile developer. 

Ravindra Sonkar

unread,
Mar 12, 2021, 12:39:36 AM3/12/21
to Swift for TensorFlow, eng.sha...@gmail.com, Swift for TensorFlow, Brennan Saeta, Ravindra Sonkar
Hi @Shadi,
It didn't resolve my problem.
Here is what I'm doing 

  private let gp = Python.import("gphoto2")

  private lazy var context = gp.Context()

  private lazy var camera_config = camera.get_config(context)

  let result = callFunction(name: "capture", withArguments: [gp.GP_CAPTURE_IMAGE])

    switch result {

    case .success(let py):

      print(py)

    case .failure(let err) :

      print(err.localizedDescription)

      break

    }

Checcking[dynamicMember : name ] is getting nil

Here is what I do in my code.

        let file_path = camera.capture(gp.GP_CAPTURE_IMAGE)

Please can you look it I did what you have guided.

Thank you.

Ravindra Sonkar

unread,
Mar 12, 2021, 1:56:15 AM3/12/21
to Swift for TensorFlow, shadi abu samra
Reply all
Reply to author
Forward
0 new messages