Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pybee/voc into bspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Dayof committed Oct 11, 2017
2 parents f0137f0 + 0fab7de commit 9865674
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 98 deletions.
8 changes: 4 additions & 4 deletions python/common/org/python/types/ByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,8 @@ public org.python.Object isalnum(java.util.List<org.python.Object> args, java.ut
@org.python.Method(
__doc__ = "B.isalpha() -> bool\n\nReturn True if all characters in B are alphabetic\nand there is at least one character in B, False otherwise."
)
public org.python.Object isalpha(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs, java.util.List<org.python.Object> default_args, java.util.Map<java.lang.String, org.python.Object> default_kwargs) {
throw new org.python.exceptions.NotImplementedError("bytearray.isalpha has not been implemented.");
public org.python.Object isalpha() {
return new Bool(Bytes._isalpha(this.value));
}

@org.python.Method(
Expand Down Expand Up @@ -930,8 +930,8 @@ public org.python.Object translate(java.util.List<org.python.Object> args, java.
@org.python.Method(
__doc__ = "B.upper() -> copy of B\n\nReturn a copy of B with all ASCII characters converted to uppercase."
)
public org.python.Object upper(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs, java.util.List<org.python.Object> default_args, java.util.Map<java.lang.String, org.python.Object> default_kwargs) {
throw new org.python.exceptions.NotImplementedError("bytearray.upper has not been implemented.");
public org.python.Object upper() {
return new ByteArray(Bytes._upper(this.value));
}

@org.python.Method(
Expand Down
63 changes: 38 additions & 25 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -682,21 +682,17 @@ public org.python.Object center(org.python.Object width, org.python.Object byteT
if (((org.python.types.Bytes) byteToFill).value.length != 1) {
if (org.Python.VERSION < 0x030502F0) {
throw new org.python.exceptions.TypeError("must be a byte string of length 1, not bytes");
}
else {
} else {
throw new org.python.exceptions.TypeError("center() argument 2 must be a byte string of length 1, not bytes");
}
}
fillByte = ((org.python.types.Bytes) byteToFill).value;
}
else if (byteToFill == null) {
} else if (byteToFill == null) {
fillByte = " ".getBytes();
}
else {
} else {
if (org.Python.VERSION < 0x030502F0) {
throw new org.python.exceptions.TypeError("must be a byte string of length 1, not " + byteToFill.typeName());
}
else{
} else {
throw new org.python.exceptions.TypeError("center() argument 2 must be a byte string of length 1, not " + byteToFill.typeName());
}
}
Expand All @@ -717,7 +713,7 @@ else if (byteToFill == null) {
returnBytes[i] = fillByte[0];
}
for (int i = lenfirst; i < (iwidth - lensecond); i++) {
returnBytes[i] = this.value[i-lenfirst];
returnBytes[i] = this.value[i - lenfirst];
}
for (int i = (iwidth - lensecond); i < iwidth; i++) {
returnBytes[i] = fillByte[0];
Expand Down Expand Up @@ -914,25 +910,38 @@ public org.python.types.Int index(org.python.Object sub, org.python.Object start
@org.python.Method(
__doc__ = "B.isalnum() -> bool\n\nReturn True if all characters in B are alphanumeric\nand there is at least one character in B, False otherwise."
)
public org.python.Object isalnum(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs, java.util.List<org.python.Object> default_args, java.util.Map<java.lang.String, org.python.Object> default_kwargs) {
throw new org.python.exceptions.NotImplementedError("bytes.isalnum has not been implemented.");
}

@org.python.Method(
__doc__ = "B.isalpha() -> bool\n\nReturn True if all characters in B are alphabetic\nand there is at least one character in B, False otherwise."
)
public org.python.Object isalpha() {
public org.python.Object isalnum() {
if (this.value.length == 0) {
return new org.python.types.Bool(false);
}
for (byte ch : this.value) {
if (!(ch >= 'A' && ch <= 'Z') && !(ch >= 'a' && ch <= 'z')) {
for (byte ch: this.value) {
if (!(ch >= 'A' && ch <= 'Z') && !(ch >= 'a' && ch <= 'z') &&
!(ch >= '0' && ch <= '9')) {
return new org.python.types.Bool(false);
}
}
return new org.python.types.Bool(true);
}

public static boolean _isalpha(byte[] input) {
if (input.length == 0) {
return false;
}
for (byte ch : input) {
if (!(ch >= 'A' && ch <= 'Z') && !(ch >= 'a' && ch <= 'z')) {
return false;
}
}
return true;
}

@org.python.Method(
__doc__ = "B.isalpha() -> bool\n\nReturn True if all characters in B are alphabetic\nand there is at least one character in B, False otherwise."
)
public org.python.Object isalpha() {
return new Bool(_isalpha(this.value));
}

@org.python.Method(
__doc__ = "B.isdigit() -> bool\n\nReturn True if all characters in B are digits\nand there is at least one character in B, False otherwise."
)
Expand Down Expand Up @@ -1143,16 +1152,20 @@ public org.python.Object translate(java.util.List<org.python.Object> args, java.
throw new org.python.exceptions.NotImplementedError("bytes.translate has not been implemented.");
}

public static byte[] _upper(byte[] input) {
byte[] result = new byte[input.length];
for (int idx = 0; idx < input.length; ++idx) {
char lc = (char) input[idx];
result[idx] = (byte) Character.toUpperCase(lc);
}
return result;
}

@org.python.Method(
__doc__ = "B.upper() -> copy of B\n\nReturn a copy of B with all ASCII characters converted to uppercase."
)
public org.python.Object upper() {
byte[] result = new byte[this.value.length];
for (int idx = 0; idx < this.value.length; ++idx) {
char lc = (char) this.value[idx];
result[idx] = (byte) Character.toUpperCase(lc);
}
return new Bytes(result);
return new Bytes(_upper(this.value));
}

@org.python.Method(
Expand Down
15 changes: 5 additions & 10 deletions python/common/org/python/types/Object.java
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,7 @@ public org.python.Object __isub__(org.python.Object other) {
)
public org.python.Object __imul__(org.python.Object other) {
try {
this.setValue(this.__mul__(other));
return this;
return this.__mul__(other);
} catch (org.python.exceptions.TypeError e) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for *=: '" + this.typeName() + "' and '" + other.typeName() + "'");
}
Expand All @@ -826,8 +825,7 @@ public org.python.Object __imul__(org.python.Object other) {
)
public org.python.Object __itruediv__(org.python.Object other) {
try {
this.setValue(this.__truediv__(other));
return this;
return this.__truediv__(other);
} catch (org.python.exceptions.TypeError e) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for /=: '" + this.typeName() + "' and '" + other.typeName() + "'");
}
Expand All @@ -839,8 +837,7 @@ public org.python.Object __itruediv__(org.python.Object other) {
)
public org.python.Object __ifloordiv__(org.python.Object other) {
try {
this.setValue(this.__floordiv__(other));
return this;
return this.__floordiv__(other);
} catch (org.python.exceptions.TypeError e) {
if (other instanceof org.python.types.Complex) {
throw new org.python.exceptions.TypeError("can't take floor of complex number.");
Expand Down Expand Up @@ -884,8 +881,7 @@ public org.python.Object __idivmod__(org.python.Object other) {
args = {"other"}
)
public org.python.Object __ipow__(org.python.Object other) {
this.setValue(this.__pow__(other, null));
return this;
return this.__pow__(other, null);
}

@org.python.Method(
Expand Down Expand Up @@ -932,8 +928,7 @@ public org.python.Object __iand__(org.python.Object other) {
)
public org.python.Object __ixor__(org.python.Object other) {
try {
this.setValue(this.__xor__(other));
return this;
return this.__xor__(other);
} catch (org.python.exceptions.TypeError e) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for ^=: '" + this.typeName() + "' and '" + other.typeName() + "'");
}
Expand Down
58 changes: 53 additions & 5 deletions python/common/org/python/types/Slice.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,43 +158,91 @@ public org.python.Object __repr__() {
this.step.__repr__()));
}

private org.python.types.Tuple convertSliceToTuple(org.python.types.Slice slice) {
org.python.types.Tuple tupleFromSlice = new org.python.types.Tuple();
tupleFromSlice.value.add(slice.start);
tupleFromSlice.value.add(slice.stop);
tupleFromSlice.value.add(slice.step);
return tupleFromSlice;
}

@org.python.Method(
__doc__ = "Return self>=value.",
args = {"other"}
)
public org.python.Object __ge__(org.python.Object other) {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;

if (other instanceof org.python.types.Slice) {
org.python.types.Tuple otherSliceAsTuple = convertSliceToTuple((org.python.types.Slice) other);
org.python.types.Tuple thisSliceAsTuple = convertSliceToTuple(this);
return thisSliceAsTuple.__ge__(otherSliceAsTuple);
}
else {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
}

@org.python.Method(
__doc__ = "Return self>value.",
args = {"other"}
)
public org.python.Object __gt__(org.python.Object other) {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;

if (other instanceof org.python.types.Slice) {
org.python.types.Tuple otherSliceAsTuple = convertSliceToTuple((org.python.types.Slice) other);
org.python.types.Tuple thisSliceAsTuple = convertSliceToTuple(this);
return thisSliceAsTuple.__gt__(otherSliceAsTuple);
}
else {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
}

@org.python.Method(
__doc__ = "Return self==value.",
args = {"other"}
)
public org.python.Object __eq__(org.python.Object other) {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;

if (other instanceof org.python.types.Slice) {
org.python.types.Tuple otherSliceAsTuple = convertSliceToTuple((org.python.types.Slice) other);
org.python.types.Tuple thisSliceAsTuple = convertSliceToTuple(this);
return thisSliceAsTuple.__eq__(otherSliceAsTuple);
}
else {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
}

@org.python.Method(
__doc__ = "Return self<value.",
args = {"other"}
)
public org.python.Object __lt__(org.python.Object other) {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;

if (other instanceof org.python.types.Slice) {
org.python.types.Tuple otherSliceAsTuple = convertSliceToTuple((org.python.types.Slice) other);
org.python.types.Tuple thisSliceAsTuple = convertSliceToTuple(this);
return thisSliceAsTuple.__lt__(otherSliceAsTuple);
}
else {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
}

@org.python.Method(
__doc__ = "Return self<=value.",
args = {"other"}
)
public org.python.Object __le__(org.python.Object other) {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;

if (other instanceof org.python.types.Slice) {
org.python.types.Tuple otherSliceAsTuple = convertSliceToTuple((org.python.types.Slice) other);
org.python.types.Tuple thisSliceAsTuple = convertSliceToTuple(this);
return thisSliceAsTuple.__le__(otherSliceAsTuple);
}
else {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
}
}
27 changes: 1 addition & 26 deletions tests/datatypes/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,4 @@ class InplaceBoolOperationTests(InplaceOperationTestCase, TranspileTestCase):
data_type = 'bool'

not_implemented = [
'test_floor_divide_bool',
'test_floor_divide_float',
'test_floor_divide_int',

'test_multiply_bool',
'test_multiply_bytearray',
'test_multiply_bytes',
'test_multiply_complex',
'test_multiply_float',
'test_multiply_int',
'test_multiply_list',
'test_multiply_str',
'test_multiply_tuple',

'test_power_bool',
'test_power_complex',
'test_power_float',
'test_power_int',

'test_true_divide_bool',
'test_true_divide_complex',
'test_true_divide_float',
'test_true_divide_int',

'test_xor_int',
]
]
29 changes: 28 additions & 1 deletion tests/datatypes/test_bytearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def test_islower(self):
print(bytearray(b'hello world').islower())
print(bytearray(b'hello world ').islower())
""")
# self.assertCodeExecution("""""")

def test_isspace(self):
self.assertCodeExecution("""
Expand All @@ -55,6 +54,34 @@ def test_isspace(self):
print(bytearray(b' \\r').isspace())
""")

def test_upper(self):
# TODO: add this test when adding support for literal hex bytes
# print(bytearray(b'\xf0').upper())

self.assertCodeExecution("""
print(bytearray(b'abc').upper())
print(bytearray(b'').upper())
print(bytearray(b'Abccc').upper())
print(bytearray(b'HELLO WORD').upper())
print(bytearray(b'@#$%!').upper())
print(bytearray(b'hello world').upper())
print(bytearray(b'hello world ').upper())
""")

def test_isalpha(self):
# TODO: add this test when adding support for literal hex bytes
# print(bytearray(b'\xf0').isalpha())

self.assertCodeExecution("""
print(bytearray(b'abc').isalpha())
print(bytearray(b'').isalpha())
print(bytearray(b'Abccc').isalpha())
print(bytearray(b'HELLO WORD').isalpha())
print(bytearray(b'@#$%!').isalpha())
print(bytearray(b'hello world').isalpha())
print(bytearray(b'hello world ').isalpha())
""")


class UnaryBytearrayOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'bytearray'
Expand Down
13 changes: 13 additions & 0 deletions tests/datatypes/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,19 @@ def test_contains(self):
print(['b', 'e'] in b'pybee') #Test TypeError invalid byte array
""", exits_early=True)

def test_isalnum(self):
self.assertCodeExecution("""
print(b'w1thnumb3r2'.isalnum())
print(b'withoutnumber'.isalnum())
print(b'with spaces'.isalnum())
print(b'666'.isalnum())
print(b'66.6'.isalnum())
print(b' '.isalnum())
print(b''.isalnum())
print(b'/@. test'.isalnum())
print(b'\x46\x55\x43\x4B'.isalnum())
""")

def test_isalpha(self):
self.assertCodeExecution("""
print(b'testalpha'.isalpha())
Expand Down
Loading

0 comments on commit 9865674

Please sign in to comment.