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

The major change being that we always use __getattr__ instead of __getattribute__ #55

Merged
merged 2 commits into from
Feb 18, 2021

Conversation

pelson
Copy link

@pelson pelson commented Feb 16, 2021

This makes a much nicer access pattern for self._instance / self._symbol, but performance should be checked to compare the two. This also fixes the issubclass check, which had the operands in the wrong order.

I also prototyped hooking it into JPype (because I wanted to play with how it would look to implement a library). To do so, I indented your tests.

Fixes the issubclass failing case (it was simply an order mismatch).

Note: This doesn't need to be merged (feel free to pick off the bits you like/want), I just wanted to play a little with it.

…tattribute__. This makes a much nicer access pattern for self._instance / self._symbol, but performance should be checked to compare the two. This also fixes the issubclass check, which had the operands in the wrong order.
def __eq__(self, other):
# We swap the order of the __eq__ here to allow other to influence the
# result further (e.g. it may be a _JResolved also)
return other == self._instance
Copy link
Author

Choose a reason for hiding this comment

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

This order swapping assumes equality is symmetric in Python (it isn't always, even though it should be)

instance = object.__getattribute__(self, '_instance')
return issubclass(v, instance)
def __subclasscheck__(self, other):
return issubclass(self._instance, other)
Copy link
Author

Choose a reason for hiding this comment

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

Notice the order swapped here - this fixes a bug in your implementation.

return value
def __getattr__(self, name):
forward = self._symbol

Copy link
Author

Choose a reason for hiding this comment

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

Caution - I removed the leading _ case, since it raises. Might be the wrong thing to do.

def post(v: java.lang.String, p=java.lang.String.CASE_INSENSITIVE_ORDER) -> java.lang.String:
return p
print(Integer(2) > java.lang.Integer(1)) # works
print(java.lang.Integer(2) > Integer(1)) # works
Copy link
Author

Choose a reason for hiding this comment

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

I wasn't confident that this would work. It does 👏

print(issubclass(String, Object)) # fail
# Annotations and defaults
print(pre(1) == post(1)) # works
print(post(1) == post(1)) # works
Copy link
Author

Choose a reason for hiding this comment

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

This wasn't passing until I swapped the order of the __eq__ implementation.

@Thrameos
Copy link
Owner

Thrameos commented Feb 16, 2021

So assuming we can port this in a C implementation with morphs for most field types (object, array, primitive) and leave class and exception as resolved would this satisfy what you would like to achieve? Does this prototype look reasonable?

Dont worry about the speed of instance and symbol as those will be dedicated C slots. The same with the getattr as in C we will have direct access.

The size of the forward object has to be two pointers anyway for the slots so the only types that dont fit are class and exceptions. Exceptions as static fields are very rare. Classes are common but they aren't customizable like the others so less likely to be have issues. Arrays will take some work to morph as we will have to use copy constructors on the internal bits. We will also need to properly dispose of the old guts so we dont leak the slot values on the morph.

@pelson
Copy link
Author

pelson commented Feb 17, 2021

I just added another commit to allow us to try the import machinery. There are one or two issues that need resolving in terms of identity pre-and-post JVM startup, which is covered by one of your True/False tests (now failing 😢), but otherwise it is showing some really nice behaviour.

I've moved the conversation thread back to jpype-project#933 (comment)

@Thrameos Thrameos merged commit 9eabe34 into Thrameos:forward Feb 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants