Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyobjc does not throw an error and instead returns None #518

Closed
yasoob opened this issue Dec 14, 2022 · 2 comments
Closed

pyobjc does not throw an error and instead returns None #518

yasoob opened this issue Dec 14, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@yasoob
Copy link

yasoob commented Dec 14, 2022

pyobjc does not throw an error and instead returns None when creating something with the wrong arguments. For instance, one proper way to create a CIImage is this:

import Quartz
from Foundation import NSURL

input_url = NSURL.fileURLWithPath_(img_path)
input_image = Quartz.CIImage.imageWithContentsOfURL_(input_url)

But if I pass in a string to imageWithContentsOfURL_ method instead of an NSURL, it doesn't throw an error. It simply returns None.

I am very new to pyobjc and even Objective C. I am trying to figure out if I am doing something wrong? Is there any way to make pyobjc throw an error instead of returning None? Or maybe returning None is how it works in Objective C too.

I even tried setting the verbose to True:

import objc
objc.options.verbose = True

But it didn't improve things. Can you kindly guide what I might be doing wrong? It is very hard to debug bugs without any indication that what I am doing is wrong.

@yasoob yasoob added the bug Something isn't working label Dec 14, 2022
@ronaldoussoren
Copy link
Owner

In this particular case the equivalent incorrect Objective-C code returns nil which maps to None in Python.

#import <CoreImage/CoreImage.h>
#import <Foundation/Foundation.h>

int main(void)
{
    NSLog(@"%@", [CIImage imageWithContentsOfURL:(NSURL*)@"path"]);
}

Sadly the Cocoa frameworks contain limited error checking, mostly relying on the compiler to find problems like this. If Objective-C code raises an exception that exception is automatically translated to a Python exception. The verbose option is primarily present to log exceptions that are later swallowed by a runloop to make it easier to debug code.

There will be type annotations for PyObjC in the future, but adding those is a fair bit of work and may require some work with the type community as well because PyObjC does some things that don't necessarily play nice with type checkers.

For now I generally keep Apple's documentation open when working with APIs that I haven't used enough.

@yasoob
Copy link
Author

yasoob commented Dec 14, 2022

Ah ok this makes sense. Thanks for the quick response. I will close this issue then. Either ways, thanks for all your work! I worked with pyobjc for the first time recently and it is pretty amazing to see what it is capable of :)

@yasoob yasoob closed this as completed Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants