Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
WIP use version module for runtime version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
abonie committed Sep 7, 2017
1 parent 7b43de2 commit d8e41b8
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 144 deletions.
42 changes: 17 additions & 25 deletions batavia/builtins/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 9 additions & 13 deletions batavia/builtins/chr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var constants = require('../core').constants
var version = require('../core').version
var exceptions = require('../core').exceptions
var types = require('../types')

Expand All @@ -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') {
Expand Down
90 changes: 37 additions & 53 deletions batavia/core/types/NoneType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) + "'"
)
}
}

Expand All @@ -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) + "'"
)
}
}

Expand Down
89 changes: 36 additions & 53 deletions batavia/types/Bool.js
Original file line number Diff line number Diff line change
@@ -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')
/*************************************************************************
Expand Down Expand Up @@ -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) + "'")
Expand Down Expand Up @@ -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) + "'")
Expand Down Expand Up @@ -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) + "'")
Expand Down Expand Up @@ -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) + "'")
Expand Down

0 comments on commit d8e41b8

Please sign in to comment.