Skip to content

Commit

Permalink
Minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bksahu committed Sep 4, 2019
1 parent c7ff21d commit 31f3c04
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 27 deletions.
37 changes: 35 additions & 2 deletions Developer_Manual.rst
Expand Up @@ -2654,9 +2654,10 @@ Builtin ``zip`` for Python2
tmp_iter_3 = iter(tmp_arg3)
except TypeError:
raise TypeError("zip argument #3 must support iteration")
...
# could be more
...
tmp_result = []
try:
while 1:
Expand All @@ -2669,10 +2670,42 @@ Builtin ``zip`` for Python2
)
)
except StopIteration:
pass
break
return tmp_result
Builtin ``zip`` for Python3
+++++++++++++++++++++++++++


.. code-block:: python
for x, y, z in zip(a, b, c):
...
.. code-block:: python
def _zip_gen_object(a, b, c, ...):
...
# See Python2
...
# could be more
...
while 1:
yield (
next(tmp_iter_1),
next(tmp_iter_2),
next(tmp_iter_3),
...
)
except StopIteration:
break
for x, y, z in _zip_gen_object(a, b, c):
...
Builtin ``map`` for Python2
+++++++++++++++++++++++++++

Expand Down
3 changes: 3 additions & 0 deletions nuitka/build/include/nuitka/helpers.h
Expand Up @@ -263,6 +263,9 @@ extern PyObject *BUILTIN_SUPER(PyObject *type, PyObject *object);
// For built-in built-in all() functionality.
extern PyObject *BUILTIN_ALL(PyObject *value);

// For built-in built-in zip() functionality.
extern PyObject *BUILTIN_ZIP(PyObject *value);

// The patched isinstance() functionality used for the built-in.
extern int Nuitka_IsInstance(PyObject *inst, PyObject *cls);

Expand Down
8 changes: 7 additions & 1 deletion nuitka/build/static_src/HelpersBuiltin.c
Expand Up @@ -24,6 +24,12 @@
*
**/

// This file is included from another C file, help IDEs to still parse it on
// its own.
#ifdef __IDE_ONLY__
#include "nuitka/prelude.h"
#endif

static PyObject *CALL_BUILTIN_KW_ARGS(PyObject *callable, PyObject **args, char const **arg_names, int max_args) {
int i = 0;

Expand Down Expand Up @@ -262,7 +268,7 @@ NUITKA_DEFINE_BUILTIN(zip)
PyObject *BUILTIN_ZIP(PyObject *value) {
NUITKA_ASSIGN_BUILTIN(zip);

return CALL_FUNCTION_WITH_SINGLE_ARG(NUITKA_ACCESS_BUILTIN(zip), value);
return CALL_FUNCTION_WITH_POSARGS(NUITKA_ACCESS_BUILTIN(zip), value);
}


Expand Down
5 changes: 1 addition & 4 deletions nuitka/codegen/BuiltinCodes.py
Expand Up @@ -439,14 +439,11 @@ def generateBuiltinZipCode(to_name, expression, emit, context):

getTupleCreationCode(
to_name=tmp_zip_args_name,
elements=expression.getValue(),
elements=expression.getValues(),
emit=emit,
context=context,
)

# TODO: Create BUILTIN_ZIP that passes args to
# CALL_FUNCTION_WITH_POSARGS of builtin zip.

with withObjectCodeTemporaryAssignment(
to_name, "zip_value", expression, emit, context
) as value_name:
Expand Down
1 change: 1 addition & 0 deletions nuitka/codegen/Contexts.py
Expand Up @@ -637,6 +637,7 @@ def _getConstantDefaultPopulation():
# Meta path based loader.
"read",
"rb",
"zip",
]

if python_version >= 300:
Expand Down
2 changes: 2 additions & 0 deletions nuitka/nodes/BuiltinAllNodes.py
Expand Up @@ -54,6 +54,8 @@ def computeExpression(self, trace_collection):
value = self.getValue()
shape = value.getTypeShape()
if shape.hasShapeSlotIter() is False:
trace_collection.onExceptionRaiseExit(TypeError)

return makeRaiseTypeErrorExceptionReplacementFromTemplateAndValue(
template="'%s' object is not iterable",
operation="all",
Expand Down
2 changes: 2 additions & 0 deletions nuitka/nodes/BuiltinAnyNodes.py
Expand Up @@ -49,6 +49,8 @@ def computeExpression(self, trace_collection):
value = self.getValue()
shape = value.getTypeShape()
if shape.hasShapeSlotIter() is False:
trace_collection.onExceptionRaiseExit(TypeError)

return makeRaiseTypeErrorExceptionReplacementFromTemplateAndValue(
template="'%s' object is not iterable",
operation="any",
Expand Down
38 changes: 36 additions & 2 deletions nuitka/nodes/BuiltinZipNodes.py
Expand Up @@ -22,10 +22,14 @@
from nuitka.specs import BuiltinParameterSpecs

from .ExpressionBases import ExpressionChildHavingBase
from .FunctionNodes import ExpressionFunctionRef
from .GeneratorNodes import (
ExpressionGeneratorObjectBody,
ExpressionMakeGeneratorObject,
StatementGeneratorReturnNone,
)
from .NodeMakingHelpers import (
makeConstantReplacementNode,
makeRaiseTypeErrorExceptionReplacementFromTemplateAndValue,
wrapExpressionWithNodeSideEffects,
)


Expand Down Expand Up @@ -64,3 +68,33 @@ def computeExpression(self, trace_collection):
# TODO: We should have an iteration handle.
# Length and values might be possible to predict
# if every argument iteration handle is capable of it.

def computeExpressionIter1(self, iter_node, trace_collection):
statements = []
# TODO: Put re-formulation, ideally shared with Python2
# zip here, with yield instead of appending to a list
# needs no list temporary variable of course.

provider = self.getParentVariableProvider()

generator_body = ExpressionGeneratorObjectBody(
provider=provider,
name="builtin_zip",
code_object=provider.getCodeObject(),
flags=set(),
source_ref=self.source_ref,
)

# Code generation expects this to be there.
statements.append(StatementGeneratorReturnNone(source_ref=self.source_ref))

generator_body.setBody()

result = ExpressionMakeGeneratorObject(
generator_ref=ExpressionFunctionRef(
function_body=generator_body, source_ref=self.source_ref
),
source_ref=self.source_ref,
)

return result, "new_expression", "Lowered zip built-in to generator."
2 changes: 2 additions & 0 deletions nuitka/nodes/ExceptionNodes.py
Expand Up @@ -231,6 +231,8 @@ def willRaiseException(self, exception_type):
return exception_type is BaseException

def computeExpression(self, trace_collection):
trace_collection.onExceptionRaiseExit(BaseException)

return self, None, None

def computeExpressionDrop(self, statement, trace_collection):
Expand Down
3 changes: 3 additions & 0 deletions nuitka/nodes/ExpressionBases.py
Expand Up @@ -518,6 +518,9 @@ def computeExpressionIter1(self, iter_node, trace_collection):
shape = self.getTypeShape()

if shape.hasShapeSlotIter() is False:
# Any exception TypeError will be raised.
trace_collection.onExceptionRaiseExit(BaseException)

return makeRaiseTypeErrorExceptionReplacementFromTemplateAndValue(
template="'%s' object is not iterable",
operation="iter",
Expand Down
36 changes: 19 additions & 17 deletions nuitka/optimizations/OptimizeBuiltinCalls.py
Expand Up @@ -1046,26 +1046,25 @@ def wrapZipBuiltin(call_args, source_ref):
]

# Try creating iterators of all zip arguments.

# TODO: Use class derived from
# ExpressionBuiltinIter1 that takes "i" as an
# argument, and raises exceptions with the proper
# message directly.

statements += [
makeTryExceptSingleHandlerNode(
tried=makeStatementsSequence(
statements=[
StatementAssignmentVariable(
variable=zip_iter_variable,
source=ExpressionBuiltinIter1(
value=ExpressionTempVariableRef(
variable=zip_arg_variable, source_ref=source_ref
),
source_ref=source_ref,
tried=makeStatementsSequenceFromStatement(
StatementAssignmentVariable(
variable=zip_iter_variable,
source=ExpressionBuiltinIter1(
value=ExpressionTempVariableRef(
variable=zip_arg_variable, source_ref=source_ref
),
source_ref=source_ref,
)
for i, zip_iter_variable, zip_arg_variable in zip(
range(len(call_args)), zip_iter_variables, zip_arg_variables
)
],
allow_none=False,
source_ref=source_ref,
),
source_ref=source_ref,
)
),
exception_name="TypeError",
handler_body=StatementRaiseException(
Expand All @@ -1082,6 +1081,9 @@ def wrapZipBuiltin(call_args, source_ref):
),
source_ref=source_ref,
)
for i, zip_iter_variable, zip_arg_variable in zip(
range(len(call_args)), zip_iter_variables, zip_arg_variables
)
]

tmp_result = outline_body.allocateTempVariable(
Expand Down Expand Up @@ -1504,7 +1506,6 @@ def divmod_extractor(node):
"staticmethod": staticmethod_extractor,
"classmethod": classmethod_extractor,
"divmod": divmod_extractor,
"zip": zip3_extractor,
}

if python_version < 300:
Expand All @@ -1524,6 +1525,7 @@ def divmod_extractor(node):

# The Python3 range is really an xrange, use that.
_dispatch_dict["range"] = xrange_extractor
_dispatch_dict["zip"] = zip3_extractor


def check():
Expand Down
2 changes: 1 addition & 1 deletion tests/basics/BuiltinsTest.py
Expand Up @@ -628,4 +628,4 @@ def __complex__(self):
zip(1, "String")
except TypeError as e:
print(e)
# print(zip())
print(zip())

0 comments on commit 31f3c04

Please sign in to comment.