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

patch #639

Open
wants to merge 10 commits into
base: create-system_builtin-once
Choose a base branch
from
Open

Conversation

mmatera
Copy link
Contributor

@mmatera mmatera commented Nov 21, 2022

This is a patch that makes that the test passes. However, I think in means that the scheme proposed is not working as it should.

@rocky
Copy link
Member

rocky commented Nov 21, 2022

This is a patch that makes that the test passes. However, I think in means that the scheme proposed is not working as it should.

Right now I am using copy.copy to initialize the definitions builtins field. We may need copy.deepcopy instead. And there are bugs in the code that prevent that. #636 fixes one of the bugs found in running deepcopy. But there is another one out there.

The other thing that could be going wrong is some sort of delayed initialization that gets kicked off by importing the mathics.format. The magicness of this was also observed by this before. And that might explain why something like this happens. It might now be that this needs to be done every time but just the first time.

@mmatera
Copy link
Contributor Author

mmatera commented Nov 21, 2022

This is a patch that makes that the test passes. However, I think in means that the scheme proposed is not working as it should.

Right now I am using copy.copy to initialize the definitions builtins field. We may need copy.deepcopy instead. And there are bugs in the code that prevent that. #636 fixes one of the bugs found in running deepcopy. But there is another one out there.

I added a comment in that direction.

This moves builtin-creation out of mathics.builtin and core.definitions
and into a new module.

I thing this is the last step before we actually move builtin creation
of the Definitions initialization and into some one-time code.
and add run pytest setting MATHICS_CHARACTER_ENCODING
This isn't working but this is the idea.

Another PR will contain some of the independent bugs found in this process
rocky and others added 5 commits November 22, 2022 10:21
This moves builtin-creation out of mathics.builtin and core.definitions
and into a new module.

I thing this is the last step before we actually move builtin creation
of the Definitions initialization and into some one-time code.
This isn't working but this is the idea.

Another PR will contain some of the independent bugs found in this process
* adding comments
* moving _initialize_system_definitions to a better place, and hide it.
* fixing contribute in order to make work the pymathics loading mechanism without overwrite builtins
@mmatera mmatera force-pushed the create-system_builtin-once-mm branch from 231b06f to 53300f5 Compare November 22, 2022 13:25
@mmatera
Copy link
Contributor Author

mmatera commented Nov 22, 2022

@rocky,
I guess this is ready now: the problem was in mathics.builtin.base.Builtin.contribute: when a pymathics module got loaded, system_definitions.builtin was overwriten. This caused that if one test (test/test_custom_boxexpression) contributes with rules to in System`MakeBoxes, the following tests can not get ride of the new definitions. Now it seems it is working properly.

@mmatera
Copy link
Contributor Author

mmatera commented Nov 22, 2022

Also, following #642, some imports in mathics.core from mathics.builtin were avoided.

@rocky
Copy link
Member

rocky commented Nov 22, 2022

@rocky, I guess this is ready now: the problem was in mathics.builtin.base.Builtin.contribute: when a pymathics module got loaded, system_definitions.builtin was overwriten. This caused that if one test (test/test_custom_boxexpression) contributes with rules to in System`MakeBoxes, the following tests can not get ride of the new definitions. Now it seems it is working properly.

Thanks for isolating what's wrong and working around/fixing this. I'd like to understand why when a pymathics module gets loaded, system_definitons.builtin is overwritten. That seems wrong, no?

All that code with a try block to work around this seems like a workaround for a bug or misfeature somewhere else.

@mmatera
Copy link
Contributor Author

mmatera commented Nov 22, 2022

Thanks for isolating what's wrong and working around/fixing this. I'd like to understand why when a pymathics module gets loaded system_definitons.builtin is overwritten. That seems wrong, no?

The problem comes with MakeBoxes rules. When a pymathics module "contributes" with a MakeBoxes rule, before this patch, a rule is added to definitions.builtin["System`MakeBoxes"]. Since we didn't make a deep copy of definitions.builtin, the added rule ends in system_definitions.builtin["System`MakeBoxes"].

With the workaround, if a pymathics module wants to add a rule to MakeBoxes, now it adds it to a definition in definitions.pymathics.

All that code with a try block to work around this seems like a workaround for a bug or misfeature somewhere else.

The try block is one way to handle the case in which pymathics does not have already a definition for System`MakeBoxes. I could also have implemented with something as

if "System`MakeBoxes" not in definitions.pymathics:
   ....

But let's say that this is something "exceptional" (it happens at most once in the life of a Definitions object). In any case, I hope to get rid of all the related to the MakeBoxes special case as soon as possible.

@rocky
Copy link
Member

rocky commented Nov 22, 2022

The problem comes with MakeBoxes rules. When a pymathics module "contributes" with a MakeBoxes rule, before this patch, a rule is added to definitions.builtin["System`MakeBoxes"]. Since we didn't make a deep copy of definitions.builtin, the added rule ends in system_definitions.builtin["System`MakeBoxes"].

Ok - so let see if we can get deepcopy to work or narrow the copy so something like this doesn't happen.

@mmatera
Copy link
Contributor Author

mmatera commented Nov 22, 2022

The question is, do we want wo modify Definitions.builtin? If not, even the copy is not necessary. On the other hand if what bothers you is the try block, it can be easily replaced with a conditional structure.

@rocky
Copy link
Member

rocky commented Nov 22, 2022

The question is, do we want [t]o modify Definitions.builtin? If not, even the copy is not necessary. On the other hand if what bothers you is the try block, it can be easily replaced with a conditional structure.

What bothers me is the layer upon layer of workaround, code and complexity because we aren't understanding the problem at the root cause or at a logical conceptual level.

Let me look at this over the weekend so I understand what's going on at a higher level.

You've given a lot of details as to what's up at an operational level, and that he helpful for understanding what is wrong at that level.

@mmatera
Copy link
Contributor Author

mmatera commented Nov 22, 2022

The question is, do we want [t]o modify Definitions.builtin? If not, even the copy is not necessary. On the other hand if what bothers you is the try block, it can be easily replaced with a conditional structure.

What bothers me is the layer upon layer of workaround, code and complexity because we aren't understanding the problem at the root cause or at a logical conceptual level.

At the higher level, what is wrong is how we process and store MakeBoxes rules.

Notice also that to copy (or even deep copy) an object that should be immutable, to have several immutable copies of the same thing is already a workaround.

Here there is an experimental branch where I show this: https://github.com/Mathics3/mathics-core/tree/create-system_builtin-once-mm-ro

I have a couple of ideas about how to fix the way in which we handle MakeBoxes, but that would imply a lot of other changes that would make this harder to review.

For this reason, the strategy that I am trying to propose since some time ago is to try to replace unlocalized workarounds (the ones that imply changing something in one module to fix something in another module) with workarounds localized inside a module, or even better, inside the function that should be fixed, which would be easier to document and correct
afterwards.

Let me look at this over the weekend so I understand what's going on at a higher level.

Sure.

You've given a lot of details as to what's up at an operational level, and that he helpful for understanding what is wrong at that level.

I give many details because I think they are needed to understand what is actually going on.

# both, which are not going to be in the cached version that
# Definitions.get_definition provides. To make the new rules
# accesible, we need to clear the definitions cache.
definitions.clear_cache("System`MakeBoxes")
Copy link
Member

Choose a reason for hiding this comment

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

If this is only needed when pymathics modules are loaded, then why isn't this only in the branch of the if is_pymathics_module branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But then, I should duplicate lines 414 and 415 in both branches.

@@ -191,7 +192,12 @@ def __new__(cls, *args, **kwargs):
# mathics.builtin.inout

if kwargs.get("expression", None) is not False:
return to_expression(cls.get_name(), *args)
try:
Copy link
Member

Choose a reason for hiding this comment

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

This was my bad - should be removed.

rocky added a commit that referenced this pull request Dec 4, 2022
Move system initialization module out of mathics.builtin.
(Similar to PR #639)

This also removes the need for Python to "partially initialize"
mathics.builtin since it doesn't import one of its children
rocky added a commit that referenced this pull request Dec 4, 2022
Move system initialization module out of mathics.builtin.
(Similar to PR #639)

This also removes the need for Python to "partially initialize"
mathics.builtin since it doesn't import one of its children
rocky added a commit that referenced this pull request Dec 4, 2022
Move system initialization module out of mathics.builtin.
(Similar to PR #639)

This also removes the need for Python to "partially initialize"
mathics.builtin since it doesn't import one of its children
rocky added a commit that referenced this pull request Dec 7, 2022
Move system initialization module out of mathics.builtin.
(Similar to PR #639)

This also removes the need for Python to "partially initialize"
mathics.builtin since it doesn't import one of its children

Merge mess
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.

None yet

2 participants