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

Add import directive #46

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Add import directive #46

wants to merge 4 commits into from

Conversation

meteospike
Copy link

Since it is an independent extra feature, it may not be necessary to add a new command line flag.
The principal change is probably moving ahead the sequence evaluating the list of modules directories.

Here is an example of the new directive :

program pm_dble
#:import pm, foo
  integer :: i=${int(pm.pm_two+foo.foovar)}$
  write(6,*) 'Begin DBLE'
  write(6,*) 'i =', i
  write(6,*) 'End DBLE'
end program pm_dble

Copy link
Owner

@aradi aradi left a comment

Choose a reason for hiding this comment

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

Thanks for the changes. In general I could imagine to merge them. See the comments further down. Also, please add 1-2 unit tests for checking the functionality of the new features.

bin/fypp Outdated
@@ -119,6 +119,9 @@ _SET_PARAM_REGEXP = re.compile(
_DEL_PARAM_REGEXP = re.compile(
r'^(?:[(]\s*)?[a-zA-Z_]\w*(?:\s*,\s*[a-zA-Z_]\w*)*(?:\s*[)])?$')

_IMPORT_PARAM_REGEXP = re.compile(
r'^(?:[(]\s*)?[a-zA-Z_]\w*(?:\s*,\s*[a-zA-Z_]\w*)*(?:\s*[)])?$')
Copy link
Owner

Choose a reason for hiding this comment

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

This won't allow to package.module type imports (e.g. import os.path)

Copy link
Author

Choose a reason for hiding this comment

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

Ouch ! Sorry about that !
I've made a new commit with a more suitable regex, allowing also the "as" alias. It implies some change in the import mechanism to get the actual module and not the root module in the local namespace in that case. So far, this kind of import is working fine now:

#:import pm
#:import os.path
#:import numpy as np
#:import pmore.foo as pfoo

bin/fypp Outdated
'''
modnames = self._get_variable_names(name)
for modname in modnames:
self._check_variable_name(modname)
Copy link
Owner

Choose a reason for hiding this comment

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

We should have a different check, as module names might differ from variable names (e.g. "." in the name)

Copy link
Author

Choose a reason for hiding this comment

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

Ok, the check is on prefix or reserved names, but a new commit add a _check_module_name anyway, called with module name or alias name if defined.

Comment on lines +2566 to +2572
syspath = self._get_syspath_without_scriptdir()
lookuppath = []
if options.moduledirs is not None:
lookuppath += [os.path.abspath(moddir) for moddir in options.moduledirs]
lookuppath.append(os.path.abspath('.'))
lookuppath += syspath
self._adjust_syspath(lookuppath)
Copy link
Owner

Choose a reason for hiding this comment

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

This is somewhat problematic, because it would change the sys.path permanently for the rest of the code. Originally, the syspath manipulation was in effect during the import of the user specified modules only. Is there any reason, why you want to make the changes permanent instead?

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