Commit Graph

70 Commits

Author SHA1 Message Date
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
Alex Chan
ce7ce98a01 Update Python build scripts to use the print function
In Python 3, 'print' was changed from a statement to a function.  Using
the __future__ module allows scripts to use print function whether
running with Python 2.6+ or Python 3.x.  This commit changes as many
instances of print as I could find to use the print function and the
__future__ module.
2015-12-18 23:00:55 +00:00
Johan K. Jensen
fa76656c82 Remove instances of duplicated words 2015-12-03 20:00:29 +01:00
Dave Abrahams
be9acc08bf Fix another gyb parsing bug
Fixes <rdar://problem/15928178> gyb miscompiles nested if/elif

Swift SVN r32873
2015-10-24 16:43:58 +00:00
Dave Abrahams
b3ca7eed40 gyb parsing fixes
Fixes <rdar://problem/18900352> gyb miscompiles nested loops

Swift SVN r32863
2015-10-24 04:47:57 +00:00
Dave Abrahams
f2ab974977 Rename some things in gyb
We were still using 'at' as a prefix on things when the gyb directive
character had already been changed to %

Swift SVN r32862
2015-10-24 04:47:55 +00:00
Dave Abrahams
b4b8e6cca0 Fix a tiny gyb bug
Probably doesn't affect anything

Swift SVN r32861
2015-10-24 04:47:55 +00:00
Dave Abrahams
d8beed1394 [gyb] Fix line directive generation
Swift SVN r29959
2015-07-08 04:48:44 +00:00
Dave Abrahams
ef711586dd [gyb] Support importing local .py files
Allow the template to import .py files from its own directory.  This is
useful for reusing support code across .gyb files.

Swift SVN r19495
2014-07-03 00:01:42 +00:00
Dave Abrahams
af8421cac8 [utils] Move gyb into a .py file
This allows Python or .gyb files to do "import gyb" and use the basic
components of the system.  For example, we could create a Python
function to generate the boilerplate for a Mirror.  That function could
use gyb.parseTemplate() to load an inline template and
gyb.executeTemplate() to generate the code.  Then calls to that function
can be embedded in ${...} in a .gyb file.

Swift SVN r19490
2014-07-02 23:38:47 +00:00