Commit Graph

59 Commits

Author SHA1 Message Date
Hugh Bellamy
9c953ab8cb Merge pull request #8384 from hughbe/gyb-pyc-fixes
Make sure to lint the interpreted, not compiled python file
2017-03-28 10:08:53 +07:00
Hugh Bellamy
c4d2ff24df Make sure to lint the interpreted, not compiled python file 2017-03-28 09:14:07 +07:00
Hugh Bellamy
0b2e5bf468 Fix GYB tests on Windows
SR-4371
2017-03-27 14:20:21 +07:00
Dave Abrahams
b52cf92609 Merge pull request #8292 from hughbe/gyb-line-direcrtive-cleanup
Cleanup recent line-directive and gyb test fixes
2017-03-26 23:54:40 -07:00
Dave Abrahams
5109788c95 Merge pull request #8360 from hughbe/python-more-fixes
Couple of fixes to python-linting
2017-03-26 23:03:56 -07:00
Hugh Bellamy
abfca72357 Run python linting on the file if running doctests 2017-03-27 12:31:56 +07:00
Dave Abrahams
895f90f997 Delete trailing whitespace
pyhon-lint is complaining and we need to fix that but I want to get this in first.
2017-03-26 12:35:51 -07:00
Dave Abrahams
bc287359f4 [gyb] Fix line-directive mapping of multiline substitutions
fixes https://bugs.swift.org/browse/SR-4362
2017-03-25 14:49:20 -07:00
Dave Abrahams
7019b84676 [gyb] expand files found relative to template 2017-03-25 14:48:16 -07:00
Hugh Bellamy
da9035cbb4 Cleanup recent line-directive and gyb test fixes 2017-03-25 15:45:27 +07:00
Hugh Bellamy
8671854674 Properly python lint remaining files 2017-03-23 14:06:46 +07:00
Dave Abrahams
a174c634c6 [gyb] Add, effectively, an #include directive 2017-03-22 07:47:56 -07:00
Dave Abrahams
621293fbbe [gyb] use line directives absent explicit request
The few places that are explicitly using execute_template could benefit
2017-03-22 00:47:10 -07:00
Hugh Bellamy
54e2c6893a Fix GYB doctests on Windows 2017-03-20 15:48:37 +07:00
Hugh Bellamy
fa3543d3c5 Fix some pylint errors for double whitespace after class/function 2017-02-17 15:37:19 +07:00
Jason Toffaletti
b9118b02f8 [gyb] check isinstance(result, basestring) before string comparison
Guard against types that override __eq__ and do things like
raise TypeError when the types being compared don't match.
2017-02-09 23:41:06 -08:00
practicalswift
ee871c7938 [python] Bring code base in line with PEP-8 again 2016-09-16 19:33:04 +02:00
Michael Gottesman
751e8c51ac [python] Make python lint pass (#4296) 2016-08-14 22:49:14 -04:00
Chris Williams
8edc5cb86d Avoid truncating floats in gyb 2016-08-01 14:27:51 -07:00
rintaro ishizaki
9e10ce049f [gyb] Add __file__ binding 2016-06-15 11:49:43 +09:00
Rintaro Ishizaki
4bae3d4068 [gyb] Make utils/gyb --test to really perform doctest
Because utils/gyb is just a launcher, `doctest.testmod()` without
explicit module doesn't perform doctest of `gyb` module.
It used to perform doctest of `util/gyb`, the `__main__` module.
2016-06-10 18:04:57 +09:00
Rintaro Ishizaki
0006a54c34 [gyb] Get doctest to pass
Mainly, because of `from __future__ import print_function`, doctest was
broken.
2016-06-10 17:22:35 +09:00
practicalswift
c760f6dfbf [gardening] Add whitespace: "foo,bar" → "foo, bar" 2016-04-12 22:31:46 +02:00
practicalswift
1edb62dc38 [Python] Make flake8 linting pass without errors/warning (w/ default rules) 2016-03-13 20:19:51 +01:00
Chris Lattner
4992474168 Add support for #sourceLocation in its ratified forms. Switch gyb to produce
the new form.  This keeps accepting #setline for now, but we should rip it out
at some point.
2016-03-11 22:21:42 -08:00
practicalswift
0796eaad1f [Python] Fix 80-column violations 2016-03-09 23:52:11 +01:00
practicalswift
c69ffe4eb6 [Python] Use new style class definitions: "class A" → "class A(object)" 2016-03-09 09:42:42 +01:00
Brian Gesiak
c9000af795 Merge pull request #1526 from practicalswift/fix-pep8-violations-ii
[Python] Fix five classes of PEP-8 violations (E101/E111/E128/E302/W191)
2016-03-08 23:55:46 -05:00
practicalswift
e8c0600727 [Python] NotImplemented → NotImplementedError
From the Python documentation:
* NotImplemented: "Special value which can be returned by the 'rich comparison'
  special methods (__eq__(), __lt__(), and friends), to indicate that the
  comparison is not implemented with respect to the other type."
* NotImplementedError: "This exception is derived from RuntimeError. In user
  defined base classes, abstract methods should raise this exception when they
  require derived classes to override the method."
2016-03-08 13:56:01 +01:00
practicalswift
265835fdfc [Python] Use consistent import ordering for Python code
Ordering used:
1.) standard library imports
2.) third party imports
3.) local package imports

Each group is individually alphabetized.
2016-03-07 23:25:16 +01:00
practicalswift
183da818df [Python] Fix five classes of PEP-8 violations (E101/E111/E128/E302/W191)
* E101: indentation contains mixed spaces and tabs
* E111: indentation is not a multiple of four
* E128: continuation line under-indented for visual indent
* E302: expected 2 blank lines, found 1
* W191: indentation contains tabs
2016-03-07 22:36:23 +01:00
practicalswift
e33f1747a9 [Python] Improve Python consistency: "variable in function should be lowercase" (N806)
The repo contains roughly 80 Python scripts. "snake_case" naming is used for
local variables in all those scripts. This is the form recommended by the PEP 8
naming recommendations (Python Software Foundation) and typically associated
with idiomatic Python code.

However, in nine of the 80 scripts there were at least one instance of
"camelCase" naming prior to this commit.

This commit improves consistency in the Python code base by making sure that
these nine remaining files follow the variable naming convention used for
Python code in the project.

References:
* PEP 8: https://www.python.org/dev/peps/pep-0008/
* pep8-naming: https://pypi.python.org/pypi/pep8-naming
2016-03-02 18:56:47 +01:00
Daniel Duan
742c2cf1f3 [gyb] replace #line with #setline in default arguments and examples 2016-03-01 19:12:31 -08:00
practicalswift
f6d6585ee0 [Python] Improve Python consistency: Use function_name(…) throughout (PEP8) 2016-02-29 22:49:19 +01:00
practicalswift
d8a7c4b447 [Python] Argument names should be lowercase by convention 2016-02-20 23:29:41 +01:00
practicalswift
e395ea0f0e [Python] Follow standard Python docstrings conventions (PEP-0257).
https://www.python.org/dev/peps/pep-0257/
2016-02-20 17:11:57 +01:00
practicalswift
970216434d [Python] Fix "too many leading '#' for block comment"
See discussion with @gribozavr in https://github.com/apple/swift/pull/1287#issuecomment-183413319
2016-02-12 23:02:10 +01:00
practicalswift
679f1853d2 PEP8: Fix all violations of type "at least two spaces before inline comment" (E261) 2016-01-24 10:10:55 +01:00
practicalswift
d021aedbdc PEP8: Fix all violations of type "too many blank lines" (E303) 2016-01-24 10:10:50 +01:00
practicalswift
4fbc28c704 PEP8: Fix all violations of type "continuation line with same indent as next logical line" (E125) 2016-01-24 09:30:48 +01:00
practicalswift
1cd4d4e9c9 [gardening] Fix violations of non-controversial PEP8 rules
Fixes:
* multiple statements on one line (colon) (E701)
* missing whitespace around arithmetic operator (E226)
* missing whitespace around operator (E225)
* closing bracket does not match visual indentation (E124)
* blank line contains whitespace (W293)
* continuation line missing indentation or outdented (E122)
* continuation line over-indented for hanging indent (E126)
* missing expected blank line (E301)
* trailing whitespace (W291)
* unexpected spaces around keyword / parameter equals (E251)
* whitespace after '(', '[' or '{' (E201)
* whitespace before ')', ']' or '}' (E202)
* whitespace before ',' or ':' (E203)
2016-01-23 09:23:33 +01:00
Dmitri Gribenko
1b44809413 Revert "[gyb] Force UTF-8 encoding when parsing templates on Linux" 2016-01-13 01:07:53 -08:00
Ryan Lovelett
c9e32b8be9 [gyb] Force UTF-8 encoding when parsing templates on Linux
Python 3 on Linux reads the system locale information to determine what
it should use as the default encoding for strings read from files (this
is different from OS X which is always UTF-8 by default [1]).

Since all the Swift gyb templates are UTF-8 encoded there is effectively
no reason to parse them as anything else. This patch forces the gyb
template parser to read the template using UTF-8 encoding. It accounts
for both reading and writing to a file as well as reading from stdin and
writing to stdout.

Two changes of note are that it now includes a __future__ import that
should make Python 2 behave a little closer to Python 3 in terms of
unicode support. Additionally Python 2 can no longer use cStringIO
because it does not support unicode [2].

To test this patch I ran these commands before and after the patch.
Note: that before the patch if the locale was set to something other
than UTF-8, ASCII for instance, the Python 3 runs would fail. See [3]
for example failure message.

Without stdin/stdout:

$ python2 utils/gyb -o Arrays.2.7.swift stdlib/public/core/Arrays.swift.gyb
$ python3 utils/gyb -o Arrays.3.5.swift stdlib/public/core/Arrays.swift.gyb
$ diff -u Arrays.2.7.swift Arrays.3.5.swift

With stdin/stdout:

$ cat stdlib/public/core/Arrays.swift.gyb | python2 utils/gyb > Arrays.2.7.stdin.stdout.swift
$ cat stdlib/public/core/Arrays.swift.gyb | python3 utils/gyb > Arrays.3.5.stdin.stdout.swift
$ diff -u Arrays.2.7.stdin.stdout.swift Arrays.3.5.stdin.stdout.swift

[1] https://docs.python.org/3/howto/unicode.html#unicode-filenames
[2] https://docs.python.org/2/library/stringio.html#cStringIO.StringIO
[3] https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20160111/000780.html
2016-01-12 20:56:33 -05:00
Zach Panzarino
0b70e6e62b Fix exit bugs of some python scripts 2016-01-07 03:27:46 +00:00
Ryan Lovelett
c677844ba4 [gyb] Provide Python 2 and 3 compatable exception syntax
In Python 2 the syntax to catch exceptions was:

except (Exception1, Exception2), target:

In Python 3 the syntax to catch exceptions is:

except (Exception1, Exception2) as target:

This newer Python 3 syntax is also available in 2.6 and 2.7. Therefore,
it is preferred for compatibility reasons. Additionally, the target no
longer can be a tuple. This patch refactors the exception handeling code
to be the newer Python 3 exception catch syntax.
2015-12-31 16:51:47 -05:00
Ryan Lovelett
86650bbb6d [gyb] Python 2 and 3 compatible StringIO import
The `StringIO` and `cStringIO` modules are gone in Python 3. The
recommendation is to import the `io` module on Python 3. Therefore, this
patch first attempts to import the Python 2 module, `cStringIO`, and if
that fails then attempts to import the Python 3 module, `io`.

**NOTE**: There are still other Python 3.x fixes necessary to make `gyb`
run on a Python 3.x interpreter. This is just one small incremental
patch on the way there.
2015-12-31 16:51:47 -05:00
Ryan Lovelett
7700b3b4e3 [gyb] Use lambda function to work around PEP 3114
[PEP 3114](https://www.python.org/dev/peps/pep-3114/) renamed
`iterator.next()` (Python 2) to `iterator.__next__()` (Python 3). The
recommended solution to make the code work in both Python 2 and 3 is to
call the global `next` function.

To use this recommended global `next` function this patch uses a lambda
function to supply `tokenize.generate_tokens` a callable function as it was previously.

This should be functionally equivalent to the old code with the added
benefit of working on both Python 2 and 3.
2015-12-31 16:51:47 -05:00
Alex Chan
1f2a39c5f3 Tidy up Python file handlers
Rather than using `f = open(path).read()`, which leaves the file open
for an indeterminate period of time, switch to the `with open(path) as f`
idiom, which ensures the file is always closed correctly.
2015-12-22 22:33:18 +00:00
JHoloboski
7fb230a8f8 replace asserts with nicer ValueErrors in gyb 2015-12-22 12:45:31 -05:00
practicalswift
335270e9f2 Remove local variables that are assigned but never used. 2015-12-21 10:24:08 +01:00