diff --git a/batavia/builtins/bytes.js b/batavia/builtins/bytes.js index 81fc3eab9..43adfb41d 100755 --- a/batavia/builtins/bytes.js +++ b/batavia/builtins/bytes.js @@ -3,7 +3,7 @@ var Buffer = require('buffer').Buffer var exceptions = require('../core').exceptions var callables = require('../core').callables var type_name = require('../core').type_name -var constants = require('../core').constants +var version = require('../core').version var types = require('../types') var iter = require('./iter') @@ -27,18 +27,14 @@ function bytes(args, kwargs) { } else if (args.length === 1) { var arg = args[0] if (arg === null) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - "'NoneType' object is not iterable" - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "cannot convert 'NoneType' object to bytes" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + "'NoneType' object is not iterable" + ) + } else { + throw new exceptions.TypeError.$pyclass( + "cannot convert 'NoneType' object to bytes" + ) } } else if (types.isinstance(arg, types.Int)) { // bytes(int) -> bytes array of size given by the parameter initialized with null bytes @@ -111,18 +107,14 @@ function bytes(args, kwargs) { return new types.Bytes(Buffer.from(buffer_args)) } else { // the argument is not one of the special cases, and not an iterable, so... - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - "'" + type_name(arg) + "' object is not iterable" - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "cannot convert '" + type_name(arg) + "' object to bytes" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + "'" + type_name(arg) + "' object is not iterable" + ) + } else { + throw new exceptions.TypeError.$pyclass( + "cannot convert '" + type_name(arg) + "' object to bytes" + ) } } } else if (args.length >= 2 && args.length <= 3) { diff --git a/batavia/builtins/chr.js b/batavia/builtins/chr.js index 82727be12..ba88dfdd9 100755 --- a/batavia/builtins/chr.js +++ b/batavia/builtins/chr.js @@ -1,4 +1,4 @@ -var constants = require('../core').constants +var version = require('../core').version var exceptions = require('../core').exceptions var types = require('../types') @@ -10,18 +10,14 @@ function chr(args, kwargs) { throw new exceptions.TypeError.$pyclass('chr() takes no keyword arguments') } if (!args || args.length !== 1) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - throw new exceptions.TypeError.$pyclass('chr() takes exactly 1 argument (' + args.length + ' given)') - - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass('chr() takes exactly one argument (' + args.length + ' given)') - - default: - throw new exceptions.BataviaError.$pyclass('Unsupported BATAVIA_MAGIC. Possibly using unsupported Python version (supported: 3.4, 3.5)') + if (version.later('3.4')) { + throw new exceptions.TypeError.$pyclass( + 'chr() takes exactly one argument (' + args.length + ' given)' + ) + } else { + throw new exceptions.TypeError.$pyclass( + 'chr() takes exactly 1 argument (' + args.length + ' given)' + ) } } if (args[0].__name__ === 'NoneType') { diff --git a/batavia/core/types/NoneType.js b/batavia/core/types/NoneType.js index 2d530f2e0..c9f4e1f4b 100644 --- a/batavia/core/types/NoneType.js +++ b/batavia/core/types/NoneType.js @@ -4,7 +4,7 @@ var PyObject = require('./Object') var basic_types = require('./Type') var exceptions = require('../exceptions') -var constants = require('../constants') +var version = require('../version') function NoneType() { PyObject.call(this) @@ -49,36 +49,28 @@ NoneType.prototype.__setattr__ = function(attr, value) { **************************************************/ NoneType.prototype.__lt__ = function(other) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - 'unorderable types: NoneType() < ' + basic_types.type_name(other) + '()' - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "'<' not supported between instances of 'NoneType' and '" + - basic_types.type_name(other) + "'" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + 'unorderable types: NoneType() < ' + basic_types.type_name(other) + '()' + ) + } else { + throw new exceptions.TypeError.$pyclass( + "'<' not supported between instances of 'NoneType' and '" + + basic_types.type_name(other) + "'" + ) } } NoneType.prototype.__le__ = function(other) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - 'unorderable types: NoneType() <= ' + basic_types.type_name(other) + '()' - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "'<=' not supported between instances of 'NoneType' and '" + - basic_types.type_name(other) + "'" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + 'unorderable types: NoneType() <= ' + basic_types.type_name(other) + '()' + ) + } else { + throw new exceptions.TypeError.$pyclass( + "'<=' not supported between instances of 'NoneType' and '" + + basic_types.type_name(other) + "'" + ) } } @@ -91,36 +83,28 @@ NoneType.prototype.__ne__ = function(other) { } NoneType.prototype.__gt__ = function(other) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - 'unorderable types: NoneType() > ' + basic_types.type_name(other) + '()' - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "'>' not supported between instances of 'NoneType' and '" + - basic_types.type_name(other) + "'" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + 'unorderable types: NoneType() > ' + basic_types.type_name(other) + '()' + ) + } else { + throw new exceptions.TypeError.$pyclass( + "'>' not supported between instances of 'NoneType' and '" + + basic_types.type_name(other) + "'" + ) } } NoneType.prototype.__ge__ = function(other) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - 'unorderable types: NoneType() >= ' + basic_types.type_name(other) + '()' - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "'>=' not supported between instances of 'NoneType' and '" + - basic_types.type_name(other) + "'" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + 'unorderable types: NoneType() >= ' + basic_types.type_name(other) + '()' + ) + } else { + throw new exceptions.TypeError.$pyclass( + "'>=' not supported between instances of 'NoneType' and '" + + basic_types.type_name(other) + "'" + ) } } diff --git a/batavia/types/Bool.js b/batavia/types/Bool.js index 0a723eced..33d11c6b4 100644 --- a/batavia/types/Bool.js +++ b/batavia/types/Bool.js @@ -1,6 +1,6 @@ var create_pyclass = require('../core').create_pyclass var exceptions = require('../core').exceptions -var constants = require('../core').constants +var version = require('../core').version var type_name = require('../core').type_name var utils = require('./utils') /************************************************************************* @@ -100,19 +100,14 @@ Bool.prototype.__ge__ = function(other) { } return new Bool(this_bool >= other_bool) } else if (types.isbataviainstance(other)) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - 'unorderable types: bool() >= ' + type_name(other) + '()' - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "'>=' not supported between instances of 'bool' and '" + - type_name(other) + "'" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + 'unorderable types: bool() >= ' + type_name(other) + '()' + ) + } else { + throw new exceptions.TypeError.$pyclass( + "'>=' not supported between instances of 'bool' and '" + type_name(other) + "'" + ) } } else { throw new exceptions.TypeError.$pyclass("unsupported operand type(s) for >=: 'bool' and '" + type_name(other) + "'") @@ -145,19 +140,15 @@ Bool.prototype.__gt__ = function(other) { } return new Bool(this_bool > other_bool) } else if (types.isbataviainstance(other)) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - 'unorderable types: bool() > ' + type_name(other) + '()' - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "'>' not supported between instances of 'bool' and '" + - type_name(other) + "'" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + 'unorderable types: bool() > ' + type_name(other) + '()' + ) + } else { + throw new exceptions.TypeError.$pyclass( + "'>' not supported between instances of 'bool' and '" + + type_name(other) + "'" + ) } } else { throw new exceptions.TypeError.$pyclass("unsupported operand type(s) for >: 'bool' and '" + type_name(other) + "'") @@ -190,19 +181,15 @@ Bool.prototype.__le__ = function(other) { } return new Bool(this_bool <= other_bool) } else if (types.isbataviainstance(other)) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - 'unorderable types: bool() <= ' + type_name(other) + '()' - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "'<=' not supported between instances of 'bool' and '" + - type_name(other) + "'" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + 'unorderable types: bool() <= ' + type_name(other) + '()' + ) + } else { + throw new exceptions.TypeError.$pyclass( + "'<=' not supported between instances of 'bool' and '" + + type_name(other) + "'" + ) } } else { throw new exceptions.TypeError.$pyclass("unsupported operand type(s) for <=: 'bool' and '" + type_name(other) + "'") @@ -235,19 +222,15 @@ Bool.prototype.__lt__ = function(other) { } return new Bool(this_bool < other_bool) } else if (types.isbataviainstance(other)) { - switch (constants.BATAVIA_MAGIC) { - case constants.BATAVIA_MAGIC_34: - case constants.BATAVIA_MAGIC_35a0: - case constants.BATAVIA_MAGIC_35: - case constants.BATAVIA_MAGIC_353: - throw new exceptions.TypeError.$pyclass( - 'unorderable types: bool() < ' + type_name(other) + '()' - ) - case constants.BATAVIA_MAGIC_36: - throw new exceptions.TypeError.$pyclass( - "'<' not supported between instances of 'bool' and '" + - type_name(other) + "'" - ) + if (version.earlier('3.6')) { + throw new exceptions.TypeError.$pyclass( + 'unorderable types: bool() < ' + type_name(other) + '()' + ) + } else { + throw new exceptions.TypeError.$pyclass( + "'<' not supported between instances of 'bool' and '" + + type_name(other) + "'" + ) } } else { throw new exceptions.TypeError.$pyclass("unsupported operand type(s) for <: 'bool' and '" + type_name(other) + "'")