FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add null checks before binding by filmor · Pull Request #2406 · pythonnet/pythonnet · GitHub

Repository navigation

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 6 additions & 8 deletions src/runtime/ClassManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,13 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
Runtime.PyType_Modified(pyType.Reference);
}

internal static bool ShouldBindMethod(MethodBase mb)
{
return (mb.IsPublic || mb.IsFamily || mb.IsFamilyOrAssembly);
}
internal static bool ShouldBindMethod(MethodBase mb) =>
mb != null &&
(mb.IsPublic || mb.IsFamily || mb.IsFamilyOrAssembly);

internal static bool ShouldBindField(FieldInfo fi)
{
return (fi.IsPublic || fi.IsFamily || fi.IsFamilyOrAssembly);
}
internal static bool ShouldBindField(FieldInfo fi) =>
fi != null &&
(fi.IsPublic || fi.IsFamily || fi.IsFamilyOrAssembly);
Comment on lines -291 to +297

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Doesn't this just mask issues? I don't remember if we have nullability annotations on, but I think if you want to add a check here, it should throw ArgumentNullException, and instead null should be handled at the call site.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It does mask issues, but in a non-failing way which is a lot better than raising during startup (which effectively renders this library useless). This is an adaptation of what fixes #2405. Throwing an exception here is (apart from having a slightly better stacktrace) effectively the same as leaving the code unchanged.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I mean I suggest to check for null at the call site which is what #2405 (comment) does, and optionally add an argument check in ShouldBindMethod to throw something better than NullReferenceException.

Simply returning false for null inputs is error-prone.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't agree, but I will also not put any more work into this. If you want to propose a different solution (in code), be my guest.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@filmor opened #2409


internal static bool ShouldBindProperty(PropertyInfo pi)
{
Expand Down

Back | FazBrowse Home | New Git URL