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

Commit

Permalink
Replace old runtime checks with version module
Browse files Browse the repository at this point in the history
  • Loading branch information
abonie committed Sep 7, 2017
1 parent d8e41b8 commit e1a15c2
Show file tree
Hide file tree
Showing 15 changed files with 702 additions and 1,033 deletions.
90 changes: 37 additions & 53 deletions batavia/core/types/NotImplementedType.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 NotImplementedType() {
PyObject.call(this)
Expand Down Expand Up @@ -43,36 +43,28 @@ NotImplementedType.prototype.__str__ = function() {
**************************************************/

NotImplementedType.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: 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) + "'"
)
if (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
'unorderable types: NotImplementedType() < ' + basic_types.type_name(other) + '()'
)
} else {
throw new exceptions.TypeError.$pyclass(
"'<' not supported between instances of 'NotImplementedType' and '" +
basic_types.type_name(other) + "'"
)
}
}

NotImplementedType.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: 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) + "'"
)
if (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
'unorderable types: NotImplementedType() <= ' + basic_types.type_name(other) + '()'
)
} else {
throw new exceptions.TypeError.$pyclass(
"'<=' not supported between instances of 'NotImplementedType' and '" +
basic_types.type_name(other) + "'"
)
}
}

Expand All @@ -85,36 +77,28 @@ NotImplementedType.prototype.__ne__ = function(other) {
}

NotImplementedType.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: 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) + "'"
)
if (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
'unorderable types: NotImplementedType() > ' + basic_types.type_name(other) + '()'
)
} else {
throw new exceptions.TypeError.$pyclass(
"'>' not supported between instances of 'NotImplementedType' and '" +
basic_types.type_name(other) + "'"
)
}
}

NotImplementedType.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: 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) + "'"
)
if (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
'unorderable types: NotImplementedType() >= ' + basic_types.type_name(other) + '()'
)
} else {
throw new exceptions.TypeError.$pyclass(
"'>=' not supported between instances of 'NotImplementedType' and '" +
basic_types.type_name(other) + "'"
)
}
}

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

function JSONDecoder() {
Expand Down Expand Up @@ -64,7 +64,7 @@ JSONDecoder.prototype.decode = function(s) {
try {
ret = JSON.parse(s, reviver)
} catch (e) {
if (constants.BATAVIA_MAGIC === constants.BATAVIA_MAGIC_34) {
if (version.earlier('3.5')) {
throw new exceptions.ValueError.$pyclass(e.message)
} else {
throw new exceptions.JSONDecodeError.$pyclass(e.message)
Expand Down
22 changes: 9 additions & 13 deletions batavia/modules/json/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 version = require('../../core').version
var type_name = require('../../core').type_name
var types = require('../../types')
var builtins = require('../../builtins')
Expand Down Expand Up @@ -178,18 +178,14 @@ var make_encode = function(
} else if (default_) {
ret = encode(callables.call_function(default_, [obj]), indent_level)
} else {
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 (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
obj.toString() + ' is not JSON serializable'
)
} else {
throw new exceptions.TypeError.$pyclass(
"Object of type '" + type_name(obj) + "' is not JSON serializable"
)
}
}

Expand Down
109 changes: 45 additions & 64 deletions batavia/types/Bytes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Buffer = require('buffer').Buffer

var constants = require('../core').constants
var version = require('../core').version
var PyObject = require('../core').Object
var create_pyclass = require('../core').create_pyclass
var exceptions = require('../core').exceptions
Expand Down Expand Up @@ -89,19 +90,15 @@ Bytes.prototype.__lt__ = function(other) {
if (types.isinstance(other, Bytes)) {
return this.val < other.val
} else {
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: bytes() < ' + type_name(other) + '()'
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"'<' not supported between instances of 'bytes' and '" +
type_name(other) + "'"
)
if (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
'unorderable types: bytes() < ' + type_name(other) + '()'
)
} else {
throw new exceptions.TypeError.$pyclass(
"'<' not supported between instances of 'bytes' and '" +
type_name(other) + "'"
)
}
}
}
Expand All @@ -112,19 +109,15 @@ Bytes.prototype.__le__ = function(other) {
if (types.isinstance(other, Bytes)) {
return this.val <= other.val
} else {
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: bytes() <= ' + type_name(other) + '()'
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"'<=' not supported between instances of 'bytes' and '" +
type_name(other) + "'"
)
if (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
'unorderable types: bytes() <= ' + type_name(other) + '()'
)
} else {
throw new exceptions.TypeError.$pyclass(
"'<=' not supported between instances of 'bytes' and '" +
type_name(other) + "'"
)
}
}
}
Expand Down Expand Up @@ -153,19 +146,15 @@ Bytes.prototype.__gt__ = function(other) {
if (types.isinstance(other, Bytes)) {
return this.val > other.val
} else {
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: bytes() > ' + type_name(other) + '()'
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"'>' not supported between instances of 'bytes' and '" +
type_name(other) + "'"
)
if (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
'unorderable types: bytes() > ' + type_name(other) + '()'
)
} else {
throw new exceptions.TypeError.$pyclass(
"'>' not supported between instances of 'bytes' and '" +
type_name(other) + "'"
)
}
}
}
Expand All @@ -176,19 +165,15 @@ Bytes.prototype.__ge__ = function(other) {
if (types.isinstance(other, Bytes)) {
return this.val >= other.val
} else {
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: bytes() >= ' + type_name(other) + '()'
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"'>=' not supported between instances of 'bytes' and '" +
type_name(other) + "'"
)
if (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
'unorderable types: bytes() >= ' + type_name(other) + '()'
)
} else {
throw new exceptions.TypeError.$pyclass(
"'>=' not supported between instances of 'bytes' and '" +
type_name(other) + "'"
)
}
}
}
Expand Down Expand Up @@ -332,18 +317,14 @@ Bytes.prototype.__add__ = function(other) {
types.Str,
types.Tuple ])) {
// does not concat with all these
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(
"can't concat bytes to " + type_name(other)
)
case constants.BATAVIA_MAGIC_36:
throw new exceptions.TypeError.$pyclass(
"can't concat " + type_name(other) + ' to bytes'
)
if (version.earlier('3.6')) {
throw new exceptions.TypeError.$pyclass(
"can't concat bytes to " + type_name(other)
)
} else {
throw new exceptions.TypeError.$pyclass(
"can't concat " + type_name(other) + ' to bytes'
)
}
} else {
throw new exceptions.NotImplementedError.$pyclass('Bytes.__add__ has not been implemented')
Expand Down
Loading

0 comments on commit e1a15c2

Please sign in to comment.