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

Continued working on builtins. #697

Merged
merged 4 commits into from Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions batavia/builtins/compile.js
Expand Up @@ -3,6 +3,11 @@ var exceptions = require('../core').exceptions
function compile(args, kwargs) {
var _compile = require('../modules/_compile/_compile')

if (args.length < 3) {
var argument_names = ['source', 'filename', 'mode']
throw new exceptions.TypeError.$pyclass('Required argument \'' + argument_names[args.length] + '\' (pos ' + (args.length + 1) + ') not found')
}

var source = args[0]
var filename = args[1]
var mode = args[2]
Expand Down
14 changes: 11 additions & 3 deletions batavia/builtins/dict.js
Expand Up @@ -12,14 +12,22 @@ function dict(args, kwargs) {
if (types.isinstance(args[0], [types.Int, types.Bool])) {
throw new exceptions.TypeError.$pyclass("'" + type_name(args[0]) + "' object is not iterable")
}
if (types.isinstance(args[0], types.Str)) {
throw new exceptions.ValueError.$pyclass('dictionary update sequence element #0 has length 1; 2 is required')
if (types.isinstance(args[0], types.Bytearray) || (types.isinstance(args[0], types.Bytes) && args[0].val.length > 0) || (types.isinstance(args[0], types.Range) && args[0].length > 0) || (types.isinstance(args[0], types.FrozenSet) && args[0].data.size > 0)) {
throw new exceptions.TypeError.$pyclass('cannot convert dictionary update sequence element #0 to a sequence')
}
var i
if (types.isinstance(args[0], types.Set)) {
for (i = 0; i < args[0].data.keys().length; i++) {
var current_item = args[0].data.keys()[i]
if (!types.isinstance(current_item, types.Tuple) || current_item.length !== 2) {
throw new exceptions.TypeError.$pyclass('cannot convert dictionary update sequence element #0 to a sequence')
}
}
}
// if single bool case

// if multiple bool case

var i
// handling keyword arguments and no arguments
if (args.length === 0 || args[0].length === 0) {
if (kwargs) {
Expand Down
8 changes: 8 additions & 0 deletions batavia/core/types/Type.js
Expand Up @@ -120,6 +120,14 @@ Type.prototype.__delattr__ = function(name) {
var exceptions = require('../exceptions')
var native = require('../native')

if (this.dict) {
throw new exceptions.AttributeError.$pyclass(name)
}

if (['int', 'str'].indexOf(this.__name__) > -1) {
throw new exceptions.TypeError.$pyclass("can't set attributes of built-in/extension type '" + this.__name__ + "'")
}

var attr = native.getattr_raw(this.$pyclass.prototype, name)
if (attr === undefined) {
throw new exceptions.AttributeError.$pyclass(
Expand Down
1 change: 0 additions & 1 deletion batavia/types/Int.js
Expand Up @@ -600,7 +600,6 @@ Int.prototype.__getitem__ = function(index) {
Int.prototype.__setattr__ = function(other) {
throw new exceptions.AttributeError.$pyclass("'int' object has no attribute '" + other + "'")
}

/**************************************************
* Bitshift and logical ops
**************************************************/
Expand Down
4 changes: 4 additions & 0 deletions batavia/types/List.js
Expand Up @@ -445,6 +445,10 @@ List.prototype.__sub__ = function(other) {
throw new exceptions.TypeError.$pyclass("unsupported operand type(s) for -: 'list' and '" + type_name(other) + "'")
}

List.prototype.__delattr__ = function(attr) {
throw new exceptions.AttributeError.$pyclass("'list' object has no attribute '" + attr + "'")
}

List.prototype.__getitem__ = function(index) {
var types = require('../types')

Expand Down
4 changes: 4 additions & 0 deletions batavia/types/Str.js
Expand Up @@ -75,6 +75,10 @@ Str.prototype.__setattr__ = function(attr, value) {
}
}

Str.prototype.__delattr__ = function(attr) {
throw new exceptions.AttributeError.$pyclass("'str' object has no attribute '" + attr + "'")
}

/**************************************************
* Comparison operators
**************************************************/
Expand Down
4 changes: 4 additions & 0 deletions batavia/types/Tuple.js
Expand Up @@ -338,6 +338,10 @@ Tuple.prototype.__sub__ = function(other) {
throw new exceptions.TypeError.$pyclass("unsupported operand type(s) for -: 'tuple' and '" + type_name(other) + "'")
}

Tuple.prototype.__delattr__ = function(attr) {
throw new exceptions.AttributeError.$pyclass("'tuple' object has no attribute '" + attr + "'")
}

Tuple.prototype.__getitem__ = function(index) {
var types = require('../types')

Expand Down
21 changes: 0 additions & 21 deletions tests/builtins/test_compile.py
Expand Up @@ -7,24 +7,3 @@ class CompileTests(TranspileTestCase):

class BuiltinCompileFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
function = "compile"

not_implemented = [
'test_noargs',
'test_bool',
'test_bytearray',
'test_bytes',
'test_class',
'test_complex',
'test_dict',
'test_float',
'test_frozenset',
'test_int',
'test_list',
'test_None',
'test_NotImplemented',
'test_range',
'test_set',
'test_slice',
'test_str',
'test_tuple',
]
7 changes: 0 additions & 7 deletions tests/builtins/test_delattr.py
Expand Up @@ -58,10 +58,3 @@ def __init__(self, val):

class BuiltinDelattrFunctionTests(BuiltinTwoargFunctionTestCase, TranspileTestCase):
function = "delattr"

not_implemented = [
'test_class_str',
'test_list_str',
'test_str_str',
'test_tuple_str',
]
9 changes: 0 additions & 9 deletions tests/builtins/test_dict.py
Expand Up @@ -8,15 +8,6 @@ class DictTests(TranspileTestCase):
class BuiltinDictFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
function = "dict"

not_implemented = [
'test_bytearray',
'test_bytes',
'test_frozenset',
'test_range',
'test_set',
'test_str',
]

def test_well_formatted_set(self):
self.assertCodeExecution("""
good_set = {(1, 2), (2, 3)}
Expand Down