Complete implementation of Python builtins
org.Python is a Java implementation of all the Python builtins - methods like print(), input(), list(), abs(), min() and so on.
An implementation of these builtins is required.
See also Ouroboros, a related project to develop a standalone, pure python version of the standard library. Ideally, it should be possible to use Ouroboros to generate the builtins and standard library.
Where should the tests for these go? I was looking at adding tests for abs(x), bool(x) for Int but I'm not sure if that should go into test_int.IntTests, or go into a new type of TestCase in utils.py.
I think an entirely new test module is called for - something to parallel datatypes, stdlib and structures. There's probably enough tests for each method that test_abs.py containing AbsTests, test_bool.py containing BoolTests etc wouldn't be excessive.
I'm trying to figure out how comprehensive the tests are meant to be, since that affects how they should be defined. I quite like how the current UnaryOperationTestCase, etc are 'generating' all the possible combinations in a compact format, without cluttering up the individual test files. It means that we humans won't accidentally miss something out when implementing, particularly a strange edge/error case.
However, I'm not sure whether to do that for functions/methods - I can imagine the combinations exploding quite quickly, especially if each argument is tested with all data types, and with all possible combinations of arguments. It seems like the fraction of tests that would result in exercising some kind of error or another might be quite high.
(I've hacked up a BuiltinFunctionTestCase to just run all the builtin functions against the test values of each type, but that doesn't take into account 2+ arguments yet.)
For functions that take a single argument, I can see this being a good idea; however, it's going to get out of control pretty quickly once you move into functions with multiple arguments - if only because the multi-argument functions all have different combinations of arguments.
If you can put together a variant of UnaryOperationTestCase that can validate the simple, single argument methods (the ones like abs, bool, and so on), I think that would be worthwhile; however, for the more complex methods, I think aiming for branch coverage is more than enough.
I've implemented bool(), but am having some issues getting the correct type names to generate the TypeError message in org.Python.abs(). Looking through the codebase, it usually is obtained via x.typeName() or org.python.types.Type.pythonType(x.getClass()) where x is an org.python.Object.
This works perfectly fine for all the basic types like dict but it breaks for defined python classes as in this test:
self.assertCodeExecution("""
class NotAbsLike:
pass
x = NotAbsLike()
print(abs(x))
""")
Output:
### EXCEPTION ###
- TypeError: bad operand type for abs(): 'test.NotAbsLike'
? -----
+ TypeError: bad operand type for abs(): 'NotAbsLike'
test.py:4
I guess this would affect all the other methods in org.Python that are throwing TypeErrors as well.
I'd be interested to take a look at sorted() and hasattr()
Is this issue done now?
Looks like all the builtins have been implemented as I see none with NotImplementedError
I would be interested in taking a look at iter().
Many built in's are not implemented yet, reading through org.Python I've found
not implemented but method for telling so exists as:
@org.python.Method(
__doc__ = "ascii(object) -> string" +
"\n" +
"As repr(), return a string containing a printable representation of an\n" +
"object, but escape the non-ASCII characters in the string returned by\n" +
"repr() using \\x, \\u or \\U escapes. This generates a string similar\n" +
"to that returned by repr() in Python 2.\n",
args = {"object"}
)
public static org.python.types.Str ascii(org.python.Object object) {
throw new org.python.exceptions.NotImplementedError("Builtin function 'ascii' not implemented");
}
ascii() classmethod() compile() eval() exec() help() open() staticmethod() __import__()
using the following functions on voc globals() trows a different output than python locals() trows a different output than python
using memoryview() fails tuple() fails
they trow
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at org.python.types.Type.invoke(Type.java:446)
at python.example.module$import(example.py:1)
at python.example.main(example.py)
I am currently taking a look at ascii().
Is the issue still open? If so, I'd like to help.
Hello, After reading all the comments and checking the ouroboros/__builtins__.py i got to know that it don't have min and max functions can i implement those?
I want to take up this issue as my first challenge... But Can anyone guide me as I am totally new to open source contribution?
Is the issue still open?
Hi. I would be interested in taking a look at open()