-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from scnerd/21-fix-unroll-issue
21 fix unroll issue
- Loading branch information
Showing
13 changed files
with
136 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[bumpversion] | ||
current_version = 0.2.5 | ||
commit = True | ||
tag = True | ||
|
||
[bumpversion:file:setup.py] | ||
|
||
[bumpversion:file:docs/conf.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 |
---|---|---|
|
@@ -4,6 +4,7 @@ python: | |
- "3.6" | ||
- "3.7" | ||
- "3.8" | ||
- "3.9" | ||
dist: xenial | ||
|
||
install: | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import ast | ||
import tempfile | ||
|
||
import astor | ||
|
||
|
||
def save_or_return_source(f_file, f_mod, glbls, return_source, save_source): | ||
if return_source or save_source: | ||
try: | ||
source = astor.to_source(f_mod) | ||
except Exception as ex: # pragma: nocover | ||
raise RuntimeError(astor.dump_tree(f_mod)) from ex | ||
else: | ||
source = None | ||
|
||
if return_source: | ||
return source | ||
|
||
f_mod = ast.fix_missing_locations(f_mod) | ||
if save_source: | ||
temp = tempfile.NamedTemporaryFile('w', delete=False) | ||
f_file = temp.name | ||
exec(compile(f_mod, f_file, 'exec'), glbls) | ||
func = glbls[f_mod.body[0].name] | ||
if save_source: | ||
func.__tempfile__ = temp | ||
# When there are other decorators, the co_firstlineno of *some* python distributions gets confused | ||
# and thinks they will be there even when they are not written to the file, causing readline overflow | ||
# So we put some empty lines to make them align | ||
temp.write(source) | ||
temp.write('\n' * func.__code__.co_firstlineno) | ||
temp.flush() | ||
temp.close() | ||
return func |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ coveralls | |
coverage | ||
nose | ||
ipython | ||
bump2version |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import pragma | ||
from tests.test_pragma import PragmaTest | ||
|
||
|
||
class TestIssue21(PragmaTest): | ||
def test_unroll_setitem(self): | ||
# This is expected to work properly | ||
biglist = list(range(20)) | ||
|
||
@pragma.unroll | ||
def hey(): | ||
for i in range(10): | ||
biglist.__setitem__(i, 0) | ||
|
||
result = ''' | ||
def hey(): | ||
biglist.__setitem__(0, 0) | ||
biglist.__setitem__(1, 0) | ||
biglist.__setitem__(2, 0) | ||
biglist.__setitem__(3, 0) | ||
biglist.__setitem__(4, 0) | ||
biglist.__setitem__(5, 0) | ||
biglist.__setitem__(6, 0) | ||
biglist.__setitem__(7, 0) | ||
biglist.__setitem__(8, 0) | ||
biglist.__setitem__(9, 0) | ||
''' | ||
|
||
self.assertSourceEqual(hey, result) | ||
hey() | ||
self.assertListEqual( | ||
biglist, | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + list(range(10, 20)) | ||
) | ||
|
||
def test_unroll_set_index(self): | ||
# This did not work as expected | ||
biglist = list(range(20)) | ||
|
||
@pragma.unroll | ||
def hey2(): | ||
for i in range(10): | ||
biglist[i] = 1 | ||
|
||
result = ''' | ||
def hey2(): | ||
biglist[0] = 1 | ||
biglist[1] = 1 | ||
biglist[2] = 1 | ||
biglist[3] = 1 | ||
biglist[4] = 1 | ||
biglist[5] = 1 | ||
biglist[6] = 1 | ||
biglist[7] = 1 | ||
biglist[8] = 1 | ||
biglist[9] = 1 | ||
''' | ||
|
||
self.assertSourceEqual(hey2, result) | ||
hey2() | ||
self.assertListEqual( | ||
biglist, | ||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] + list(range(10, 20)) | ||
) |