voc icon indicating copy to clipboard operation
voc copied to clipboard

Complete implementation of Python builtins

Open freakboy3742 opened this issue 9 years ago • 16 comments

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.

freakboy3742 avatar Jan 21 '16 07:01 freakboy3742

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.

freakboy3742 avatar Jan 30 '16 14:01 freakboy3742

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.

cflee avatar Mar 24 '16 08:03 cflee

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.

freakboy3742 avatar Mar 24 '16 15:03 freakboy3742

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.)

cflee avatar Mar 24 '16 16:03 cflee

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.

freakboy3742 avatar Mar 24 '16 16:03 freakboy3742

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.

cflee avatar Mar 26 '16 08:03 cflee

I'd be interested to take a look at sorted() and hasattr()

raphaelm avatar Nov 05 '16 12:11 raphaelm

Is this issue done now?

Looks like all the builtins have been implemented as I see none with NotImplementedError

SanketDG avatar Nov 06 '17 07:11 SanketDG

I would be interested in taking a look at iter().

AmirYalamov avatar Jun 17 '18 00:06 AmirYalamov

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)

raraizan avatar Jun 18 '18 09:06 raraizan

I am currently taking a look at ascii().

AmirYalamov avatar Jun 27 '18 02:06 AmirYalamov

Is the issue still open? If so, I'd like to help.

AbhishekSinghDhadwal avatar Mar 15 '19 10:03 AbhishekSinghDhadwal

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?

lyfofvipin avatar Jun 26 '19 12:06 lyfofvipin

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?

SubhamPaul21 avatar Oct 04 '19 06:10 SubhamPaul21

Is the issue still open?

Jyotikarajpal avatar Oct 17 '19 17:10 Jyotikarajpal

Hi. I would be interested in taking a look at open()

brunoccalmeida avatar Oct 25 '19 21:10 brunoccalmeida