Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kjcole committed May 24, 2017
2 parents a199bbb + 2f22821 commit fc57aa6
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 15 deletions.
3 changes: 2 additions & 1 deletion batavia/builtins/iter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var exceptions = require('../core').exceptions
var callables = require('../core').callables
var type_name = require('../core').type_name
var types = require('../types')

function iter(args, kwargs) {
if (arguments.length !== 2) {
Expand All @@ -13,7 +14,7 @@ function iter(args, kwargs) {
throw new exceptions.TypeError.$pyclass('iter() expected at least 1 arguments, got 0')
}
if (args.length === 2) {
throw new exceptions.NotImplementedError.$pyclass("Builtin Batavia function 'iter' with callable/sentinel not implemented")
return new types.CallableIterator(args[0], args[1])
}
if (args.length > 2) {
throw new exceptions.TypeError.$pyclass('iter() expected at most 2 arguments, got 3')
Expand Down
5 changes: 5 additions & 0 deletions batavia/core/types/PYCFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ var constants = require('../constants')
*************************************************************************/

var PYCFile = function(data) {
if (!Uint8Array.prototype.slice) {
Object.defineProperty(Uint8Array.prototype, 'slice', { // eslint-disable-line no-extend-native
value: Array.prototype.slice
})
}
Object.call(this)
this.magic = data.slice(0, 4)
this.modtime = data.slice(4, 8)
Expand Down
2 changes: 2 additions & 0 deletions batavia/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ types['Generator'] = require('./types/Generator')
types['Range'] = require('./types/Range')
types['Slice'] = require('./types/Slice')

types['CallableIterator'] = require('./types/CallableIterator')

/*************************************************************************
* Type comparison defintions that match Python-like behavior.
*************************************************************************/
Expand Down
12 changes: 12 additions & 0 deletions batavia/types/Bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ Bool.prototype.__lshift__ = function(other) {
return new types.Int(0)
}
} else if (types.isinstance(other, types.Int)) {
if (other.valueOf() < 0) {
throw new exceptions.ValueError.$pyclass('negative shift count')
}
if (Number.MAX_SAFE_INTEGER < other.valueOf()) {
throw new exceptions.OverflowError.$pyclass('Python int too large to convert to C ssize_t')
}
if (this.valueOf()) {
this_bool = 1
} else {
Expand All @@ -540,6 +546,12 @@ Bool.prototype.__rshift__ = function(other) {
return new types.Int(0)
}
} else if (types.isinstance(other, types.Int)) {
if (other.valueOf() < 0) {
throw new exceptions.ValueError.$pyclass('negative shift count')
}
if (Number.MAX_SAFE_INTEGER < Math.abs(other.valueOf())) {
throw new exceptions.OverflowError.$pyclass('Python int too large to convert to C ssize_t')
}
if (this.valueOf()) {
this_bool = 1
} else {
Expand Down
43 changes: 43 additions & 0 deletions batavia/types/CallableIterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var PyObject = require('../core').Object
var exceptions = require('../core').exceptions
var create_pyclass = require('../core').create_pyclass

/**************************************************
* Callable Iterator
**************************************************/

function CallableIterator(callable, sentinel) {
PyObject.call(this)
this.callable = callable
this.sentinel = sentinel
this.exhausted = false
}

create_pyclass(CallableIterator, 'callable_iterator')

CallableIterator.prototype.__next__ = function() {
if (this.exhausted) {
throw new exceptions.StopIteration.$pyclass()
}

var item = this.callable.__call__([])
if (item.__eq__(this.sentinel)) {
this.exhausted = true
throw new exceptions.StopIteration.$pyclass()
}
return item
}

CallableIterator.prototype.__iter__ = function() {
return this
}

CallableIterator.prototype.__str__ = function() {
return '<callable_iterator object at 0x99999999>'
}

/**************************************************
* Module exports
**************************************************/

module.exports = CallableIterator
38 changes: 38 additions & 0 deletions tests/builtins/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,44 @@ def test_iter_bytes(self):
print(list(iter(b"abcdefgh")))
""")

def test_iter_sentinel_range(self):
self.assertCodeExecution("""
seq = iter(range(10))
callable = lambda: next(seq)
result = iter(callable, 3)
print(list(result))
""")

def test_iter_sentinel_repeated(self):
self.assertCodeExecution("""
seq = iter(range(10))
callable = lambda: next(seq)
iterator = iter(callable, 3)
print(next(iterator))
print(next(iterator))
print(next(iterator))
try:
print(next(iterator))
except StopIteration:
pass
try:
print(next(iterator))
except StopIteration:
pass
""")

def test_iter_sentinel_gen(self):
self.assertCodeExecution("""
def gen():
abc = 'abcdefghij'
for letter in abc:
yield letter
g = gen()
callable = lambda: next(g)
result = iter(callable, 'd')
print(list(result))
""")


class BuiltinIterFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
Expand Down
11 changes: 1 addition & 10 deletions tests/datatypes/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,13 @@ class BinaryBoolOperationTests(BinaryOperationTestCase, TranspileTestCase):
data_type = 'bool'

not_implemented = [
'test_lshift_int',

'test_rshift_int',

'test_true_divide_complex',
'test_true_divide_complex'
]


class InplaceBoolOperationTests(InplaceOperationTestCase, TranspileTestCase):
data_type = 'bool'

not_implemented = [

'test_lshift_int',

'test_rshift_int',

'test_true_divide_complex',
]
3 changes: 0 additions & 3 deletions tests/datatypes/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ class InplaceIntOperationTests(InplaceOperationTestCase, TranspileTestCase):
'test_power_complex',
'test_power_float',

'test_rshift_int', # this works, but some of the cases are too large
# until we replace bignumber.js

'test_subtract_complex',

'test_true_divide_complex',
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ def assertInplaceOperation(self, x_values, y_values, operation, format, substitu
'test_lshift_%s' % datatype, 'x <<= y', examples, small_ints=True
)
vars()['test_rshift_%s' % datatype] = _inplace_test(
'test_rshift_%s' % datatype, 'x >>= y', examples
'test_rshift_%s' % datatype, 'x >>= y', examples, small_ints=True
)
vars()['test_and_%s' % datatype] = _inplace_test(
'test_and_%s' % datatype, 'x &= y', examples
Expand Down

0 comments on commit fc57aa6

Please sign in to comment.