Don't write parens around for loop value tuples

This commit is contained in:
Michael Hansen
2019-10-04 14:08:47 -07:00
parent fc1cb06926
commit 2e93d29233
4 changed files with 25 additions and 9 deletions

View File

@@ -301,16 +301,21 @@ private:
class ASTTuple : public ASTNode {
public:
typedef std::vector<PycRef<ASTNode> > value_t;
typedef std::vector<PycRef<ASTNode>> value_t;
ASTTuple(value_t values)
: ASTNode(NODE_TUPLE), m_values(values) { }
: ASTNode(NODE_TUPLE), m_values(std::move(values)),
m_requireParens(true) { }
const value_t& values() const { return m_values; }
void add(PycRef<ASTNode> name) { m_values.push_back(name); }
void setRequireParens(bool require) { m_requireParens = require; }
bool requireParens() const { return m_requireParens; }
private:
value_t m_values;
bool m_requireParens;
};

View File

@@ -1800,6 +1800,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (curblock->blktype() == ASTBlock::BLK_FOR
&& !curblock->inited()) {
PycRef<ASTTuple> tuple = tup.cast<ASTTuple>();
if (tuple != NULL)
tuple->setRequireParens(false);
curblock.cast<ASTIterBlock>()->setIndex(tup);
} else {
curblock->append(new ASTStore(seq, tup));
@@ -1859,6 +1862,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (curblock->blktype() == ASTBlock::BLK_FOR
&& !curblock->inited()) {
PycRef<ASTTuple> tuple = tup.cast<ASTTuple>();
if (tuple != NULL)
tuple->setRequireParens(false);
curblock.cast<ASTIterBlock>()->setIndex(tup);
} else {
curblock->append(new ASTStore(seq, tup));
@@ -1899,6 +1905,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (curblock->blktype() == ASTBlock::BLK_FOR
&& !curblock->inited()) {
PycRef<ASTTuple> tuple = tup.cast<ASTTuple>();
if (tuple != NULL)
tuple->setRequireParens(false);
curblock.cast<ASTIterBlock>()->setIndex(tup);
} else {
curblock->append(new ASTStore(seq, tup));
@@ -2769,8 +2778,10 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
break;
case ASTNode::NODE_TUPLE:
{
ASTTuple::value_t values = node.cast<ASTTuple>()->values();
fputs("(", pyc_output);
PycRef<ASTTuple> tuple = node.cast<ASTTuple>();
ASTTuple::value_t values = tuple->values();
if (tuple->requireParens())
fputc('(', pyc_output);
bool first = true;
for (ASTTuple::value_t::const_iterator b = values.begin(); b != values.end(); ++b) {
if (!first)
@@ -2779,9 +2790,9 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
first = false;
}
if (values.size() == 1)
fputs(",)", pyc_output);
else
fputs(")", pyc_output);
fputc(',', pyc_output);
if (tuple->requireParens())
fputc(')', pyc_output);
}
break;
default:

View File

@@ -5,6 +5,6 @@ import sys <EOL>
from rfc822 import Message as Msg822 <EOL>
from mimetools import Message as MimeMsg , decode , choose_boundary as MimeBoundary <EOL>
import test . test_StringIO as StringTest <EOL>
for ( k , v ) in globals ( ) . items ( ) : <EOL>
for k , v in globals ( ) . items ( ) : <EOL>
<INDENT>
print ` k ` , v <EOL>

View File

@@ -7,6 +7,6 @@ import test . test_MimeWriter as Mime_Writer <EOL>
from rfc822 import Message as MSG <EOL>
from mimetools import Message as mimeMsg , decode , choose_boundary as mimeBoundry <EOL>
print '---' * 20 <EOL>
for ( k , v ) in globals ( ) . items ( ) : <EOL>
for k , v in globals ( ) . items ( ) : <EOL>
<INDENT>
print k , repr ( v ) <EOL>