-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description <!-- Is this a user-visible change? Remember to update RELEASE_NOTES.md --> <!-- Is this a bug fix for an issue visible in the latest release? Mention this in the PR details and ensure a patch release is considered --> #### 1 Add a `--python-module-name` option to the (new) Dafny CLI. This option takes in a string that will be prefixed to translated module imports. ex. If a module has a generated Python import statement `import MyModule`, then translating with `--python-module-name=my_python_module` will result in an import statement `import my_python_module.MyModule as MyModule`. I intend that Smithy-Dafny will use this with `--python-module-name=[module_name].internaldafny.generated`. #### 2 Do not generate `import module_` for `System_.py` in the DafnyRuntime Python. Before this change `System_.py` in the DafnyRuntimePython generates this import. This removes that import. Two problems with this import: 1. (Correctness problem, i.e. "why this change is necessary") The DafnyRuntimePython does not include a `module_.py`. Having `import module_` prevents the DafnyRuntimeModule from being used on its own. 2. (My problem, i.e. "why I'm making this change now") `import module_` is an invalid import statement with module mode. `module_` will be located at `[python_module_name].module_`, and isn't able to be imported without qualifiers. An alternative to removing the import is to generate a `module_.py` in the DafnyRuntimePython. I don't know why DafnyRuntimePython doesn't have this file, so this was easier for me. I don't have an opinion on which approach happens; I'll defer to the Dafny team on the approach. #### 3 Python: Do not generate import statements for empty modules. "Empty modules" might come from prefixes of nested modules. This is a follow-up to 4ce2f7e, which stopped generating empty modules. ### How has this been tested? <!-- Tests can be added to `Source/IntegrationTests/TestFiles/LitTests/LitTest/` or to `Source/*.Test/…` and run with `dotnet test` --> Some tests in the directory above: * singlemodule (validating compiling and running with 1 module) * multimodule (validating compiling and running with 2 modules with a dependency relationship) * nestedmodule (validating depending on a (Python) module with a nested (Dafny) module name) I've tested this with Smithy-Dafny-Python and the (WIP) Python MPL, and this behavior works as I'd expect. <small>By submitting this pull request, I confirm that my contribution is made under the terms of the [MIT license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small> --------- Co-authored-by: Shubham Chaturvedi <[email protected]> Co-authored-by: Robin Salkeld <[email protected]>
- Loading branch information
1 parent
552a4f9
commit a5d600c
Showing
26 changed files
with
329 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
13 changes: 13 additions & 0 deletions
13
...ce/IntegrationTests/TestFiles/LitTests/LitTest/pythonmodule/multimodule/DerivedModule.dfy
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// RUN: %baredafny translate py --python-module-name=PythonModule2 --library="%S/PythonModule1.doo" --translation-record "%S/PythonModule1-py.dtr" --output "%S/PythonModule2" "%s" | ||
// RUN: rm -rf "%S/PythonModule2" | ||
// RUN: mv "%S/PythonModule2-py" "%S/PythonModule2" | ||
// RUN: pip3 install "%S" | ||
// RUN: python3 %S/PythonModule2/ > %t | ||
// RUN: %diff "%s.expect" "%t" | ||
module DafnyModule3 { | ||
import DafnyModule1 | ||
|
||
method Main() { | ||
DafnyModule1.HelloWorld(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...grationTests/TestFiles/LitTests/LitTest/pythonmodule/multimodule/DerivedModule.dfy.expect
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello World |
4 changes: 4 additions & 0 deletions
4
...IntegrationTests/TestFiles/LitTests/LitTest/pythonmodule/multimodule/PythonModule1-py.dtr
This file contains 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
file_format_version = "1.0" | ||
dafny_version = "4.6.0.0" | ||
[options_by_module.DafnyModule1] | ||
python-module-name = "PythonModule1" |
Binary file added
BIN
+661 Bytes
...ce/IntegrationTests/TestFiles/LitTests/LitTest/pythonmodule/multimodule/PythonModule1.doo
Binary file not shown.
19 changes: 19 additions & 0 deletions
19
...onTests/TestFiles/LitTests/LitTest/pythonmodule/multimodule/PythonModule1/DafnyModule1.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import sys | ||
from typing import Callable, Any, TypeVar, NamedTuple | ||
from math import floor | ||
from itertools import count | ||
|
||
import PythonModule1.module_ as module_ | ||
import _dafny as _dafny | ||
import System_ as System_ | ||
|
||
# Module: PythonModule1.DafnyModule1 | ||
|
||
class default__: | ||
def __init__(self): | ||
pass | ||
|
||
@staticmethod | ||
def HelloWorld(): | ||
_dafny.print((_dafny.SeqWithoutIsStrInference(map(_dafny.CodePoint, "Hello World"))).VerbatimString(False)) | ||
|
9 changes: 9 additions & 0 deletions
9
...rationTests/TestFiles/LitTests/LitTest/pythonmodule/multimodule/PythonModule1/__main__.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Dafny program helloworld.dfy compiled into Python | ||
import sys | ||
from typing import Callable, Any, TypeVar, NamedTuple | ||
from math import floor | ||
from itertools import count | ||
|
||
import PythonModule1.module_ as module_ | ||
import _dafny as _dafny | ||
|
12 changes: 12 additions & 0 deletions
12
...grationTests/TestFiles/LitTests/LitTest/pythonmodule/multimodule/PythonModule1/module_.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import sys | ||
from typing import Callable, Any, TypeVar, NamedTuple | ||
from math import floor | ||
from itertools import count | ||
|
||
import PythonModule1.module_ as module_ | ||
import _dafny as _dafny | ||
import System_ as System_ | ||
import PythonModule1.DafnyModule1 as DafnyModule1 | ||
|
||
# Module: PythonModule1.module_ | ||
|
13 changes: 13 additions & 0 deletions
13
Source/IntegrationTests/TestFiles/LitTests/LitTest/pythonmodule/multimodule/setup.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# setup.py | ||
# installs the committed PythonModule1 and generated PythonModule2 as a Python module | ||
from setuptools import setup | ||
|
||
setup( | ||
name="testpythonmodulemultimodule", | ||
version="0.1.0", | ||
packages=["PythonModule1", "PythonModule2"], | ||
python_requires='>=3.6', | ||
install_requires=[ | ||
'DafnyRuntimePython', | ||
], | ||
) |
Binary file added
BIN
+665 Bytes
...ntegrationTests/TestFiles/LitTests/LitTest/pythonmodule/nestedmodule/SomeNestedModule.doo
Binary file not shown.
13 changes: 13 additions & 0 deletions
13
...rationTests/TestFiles/LitTests/LitTest/pythonmodule/nestedmodule/SomeNestedModule/Some.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import sys | ||
from typing import Callable, Any, TypeVar, NamedTuple | ||
from math import floor | ||
from itertools import count | ||
|
||
import SomeNestedModule.module_ as module_ | ||
import _dafny as _dafny | ||
import System_ as System_ | ||
import SomeNestedModule.Some_Nested_Module as Some_Nested_Module | ||
import SomeNestedModule.Some_Nested as Some_Nested | ||
|
||
# Module: Some | ||
|
4 changes: 4 additions & 0 deletions
4
...Files/LitTests/LitTest/pythonmodule/nestedmodule/SomeNestedModule/SomeNestedModule-py.dtr
This file contains 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
file_format_version = "1.0" | ||
dafny_version = "4.6.0.0" | ||
[options_by_module."Some.Nested.Module"] | ||
python-module-name = "SomeNestedModule" |
12 changes: 12 additions & 0 deletions
12
...ests/TestFiles/LitTests/LitTest/pythonmodule/nestedmodule/SomeNestedModule/Some_Nested.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import sys | ||
from typing import Callable, Any, TypeVar, NamedTuple | ||
from math import floor | ||
from itertools import count | ||
|
||
import SomeNestedModule.module_ as module_ | ||
import _dafny as _dafny | ||
import System_ as System_ | ||
import SomeNestedModule.Some_Nested_Module as Some_Nested_Module | ||
|
||
# Module: Some_Nested | ||
|
19 changes: 19 additions & 0 deletions
19
...stFiles/LitTests/LitTest/pythonmodule/nestedmodule/SomeNestedModule/Some_Nested_Module.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import sys | ||
from typing import Callable, Any, TypeVar, NamedTuple | ||
from math import floor | ||
from itertools import count | ||
|
||
import SomeNestedModule.module_ as module_ | ||
import _dafny as _dafny | ||
import System_ as System_ | ||
|
||
# Module: Some_Nested_Module | ||
|
||
class default__: | ||
def __init__(self): | ||
pass | ||
|
||
@staticmethod | ||
def HelloWorld(): | ||
_dafny.print((_dafny.SeqWithoutIsStrInference(map(_dafny.CodePoint, "Hello World"))).VerbatimString(False)) | ||
|
9 changes: 9 additions & 0 deletions
9
...onTests/TestFiles/LitTests/LitTest/pythonmodule/nestedmodule/SomeNestedModule/__main__.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Dafny program SomeNestedModule.dfy compiled into Python | ||
import sys | ||
from typing import Callable, Any, TypeVar, NamedTuple | ||
from math import floor | ||
from itertools import count | ||
|
||
import SomeNestedModule.module_ as module_ | ||
import _dafny as _dafny | ||
|
Oops, something went wrong.