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

Version dependent error messages #643

Merged
merged 12 commits into from
Aug 30, 2017
31 changes: 27 additions & 4 deletions batavia/builtins/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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 types = require('../types')
var iter = require('./iter')

Expand All @@ -26,7 +27,19 @@ function bytes(args, kwargs) {
} else if (args.length === 1) {
var arg = args[0]
if (arg === null) {
throw new exceptions.TypeError.$pyclass("'NoneType' object is not iterable")
switch (constants.BATAVIA_MAGIC) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we could condense these cases down to a helper function, but I'm fine with that being a cleanup PR later.

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"
)
}
} else if (types.isinstance(arg, types.Int)) {
// bytes(int) -> bytes array of size given by the parameter initialized with null bytes
// Batavia ints are BigNumbers, so we need to unpack the value from the BigNumber Array.
Expand Down Expand Up @@ -98,9 +111,19 @@ 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...
throw new exceptions.TypeError.$pyclass(
// "'" + type_name(val) + "' object is not iterable");
"'" + type_name(arg) + "' object is not iterable")
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"
)
}
}
} else if (args.length >= 2 && args.length <= 3) {
// bytes(string, encoding[, errors]) -> bytes
Expand Down
1 change: 1 addition & 0 deletions batavia/builtins/chr.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function chr(args, kwargs) {
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:
Expand Down
9 changes: 9 additions & 0 deletions batavia/core/exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,13 @@ ZeroDivisionError.prototype.__class__.$pyclass = ZeroDivisionError

exceptions.ZeroDivisionError = ZeroDivisionError.prototype.__class__

var JSONDecodeError = function(msg) {
Exception.call(this, 'JSONDecodeError', msg)
}
JSONDecodeError.prototype = Object.create(Exception.prototype)
JSONDecodeError.prototype.__class__ = new Type('JSONDecodeError', [Exception.prototype.__class__])
JSONDecodeError.prototype.__class__.$pyclass = JSONDecodeError

exceptions.JSONDecodeError = JSONDecodeError.prototype.__class__

module.exports = exceptions
61 changes: 57 additions & 4 deletions batavia/core/types/NoneType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var PyObject = require('./Object')
var basic_types = require('./Type')
var exceptions = require('../exceptions')
var constants = require('../constants')

function NoneType() {
PyObject.call(this)
Expand Down Expand Up @@ -48,11 +49,37 @@ NoneType.prototype.__setattr__ = function(attr, value) {
**************************************************/

NoneType.prototype.__lt__ = function(other) {
throw new exceptions.TypeError.$pyclass('unorderable types: NoneType() < ' + basic_types.type_name(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) + "'"
)
}
}

NoneType.prototype.__le__ = function(other) {
throw new exceptions.TypeError.$pyclass('unorderable types: NoneType() <= ' + basic_types.type_name(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) + "'"
)
}
}

NoneType.prototype.__eq__ = function(other) {
Expand All @@ -64,11 +91,37 @@ NoneType.prototype.__ne__ = function(other) {
}

NoneType.prototype.__gt__ = function(other) {
throw new exceptions.TypeError.$pyclass('unorderable types: NoneType() > ' + basic_types.type_name(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) + "'"
)
}
}

NoneType.prototype.__ge__ = function(other) {
throw new exceptions.TypeError.$pyclass('unorderable types: NoneType() >= ' + basic_types.type_name(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) + "'"
)
}
}

NoneType.prototype.__contains__ = function(other) {
Expand Down
61 changes: 57 additions & 4 deletions batavia/core/types/NotImplementedType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var PyObject = require('./Object')
var basic_types = require('./Type')
var exceptions = require('../exceptions')
var constants = require('../constants')

function NotImplementedType() {
PyObject.call(this)
Expand Down Expand Up @@ -42,11 +43,37 @@ NotImplementedType.prototype.__str__ = function() {
**************************************************/

NotImplementedType.prototype.__lt__ = function(other) {
throw new exceptions.TypeError.$pyclass('unorderable types: NotImplementedType() < ' + basic_types.type_name(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: NotImplementedType() < ' + basic_types.type_name(other) + '()'
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"'<' not supported between instances of 'NotImplementedType' and '" +
basic_types.type_name(other) + "'"
)
}
}

NotImplementedType.prototype.__le__ = function(other) {
throw new exceptions.TypeError.$pyclass('unorderable types: NotImplementedType() <= ' + basic_types.type_name(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: NotImplementedType() <= ' + basic_types.type_name(other) + '()'
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"'<=' not supported between instances of 'NotImplementedType' and '" +
basic_types.type_name(other) + "'"
)
}
}

NotImplementedType.prototype.__eq__ = function(other) {
Expand All @@ -58,11 +85,37 @@ NotImplementedType.prototype.__ne__ = function(other) {
}

NotImplementedType.prototype.__gt__ = function(other) {
throw new exceptions.TypeError.$pyclass('unorderable types: NotImplementedType() > ' + basic_types.type_name(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: NotImplementedType() > ' + basic_types.type_name(other) + '()'
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"'>' not supported between instances of 'NotImplementedType' and '" +
basic_types.type_name(other) + "'"
)
}
}

NotImplementedType.prototype.__ge__ = function(other) {
throw new exceptions.TypeError.$pyclass('unorderable types: NotImplementedType() >= ' + basic_types.type_name(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: NotImplementedType() >= ' + basic_types.type_name(other) + '()'
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"'>=' not supported between instances of 'NotImplementedType' and '" +
basic_types.type_name(other) + "'"
)
}
}

NotImplementedType.prototype.__contains__ = function(other) {
Expand Down
9 changes: 7 additions & 2 deletions batavia/modules/json/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var core = require('../../core')
var PyObject = core.Object
var exceptions = core.exceptions
var callables = core.callables
var constants = core.constants
var validateParams = require('./utils').validateParams

function JSONDecoder() {
Expand Down Expand Up @@ -63,7 +64,11 @@ JSONDecoder.prototype.decode = function(s) {
try {
ret = JSON.parse(s, reviver)
} catch (e) {
throw new exceptions.ValueError.$pyclass(e.message)
if (constants.BATAVIA_MAGIC === constants.BATAVIA_MAGIC_34) {
throw new exceptions.ValueError.$pyclass(e.message)
} else {
throw new exceptions.JSONDecodeError.$pyclass(e.message)
}
}
return ret
}
Expand Down Expand Up @@ -94,7 +99,6 @@ var loads = function(args, kwargs) {
delete params['encoding']
params['strict'] = true

// TODO(abonie): possible bug here? see test_cls in tests
var dec = callables.call_function(cls, [], params)
return callables.call_method(dec, 'decode', [s])
}
Expand Down Expand Up @@ -132,5 +136,6 @@ load.$pyargs = true
module.exports = {
'loads': loads,
'load': load,
'JSONDecodeError': exceptions.JSONDecodeError,
'JSONDecoder': _JSONDecoder
}
18 changes: 15 additions & 3 deletions batavia/modules/json/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var PyObject = require('../../core').Object
var create_pyclass = require('../../core').create_pyclass
var callables = require('../../core').callables
var exceptions = require('../../core').exceptions
var constants = require('../../core').constants
var type_name = require('../../core').type_name
var types = require('../../types')
var builtins = require('../../builtins')
var validateParams = require('./utils').validateParams
Expand Down Expand Up @@ -176,9 +178,19 @@ var make_encode = function(
} else if (default_) {
ret = encode(callables.call_function(default_, [obj]), indent_level)
} else {
throw new exceptions.TypeError.$pyclass(
obj.toString() + ' is not JSON serializable'
)
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(
obj.toString() + ' is not JSON serializable'
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"Object of type '" + type_name(obj) + "' is not JSON serializable"
)
}
}

if (seen !== undefined) {
Expand Down
Loading