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

Complete implementation of Python builtins #76

Open
freakboy3742 opened this issue Jan 21, 2016 · 16 comments
Open

Complete implementation of Python builtins #76

freakboy3742 opened this issue Jan 21, 2016 · 16 comments

Comments

@freakboy3742
Copy link
Member

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
Copy link
Member Author

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.

@cflee
Copy link
Member

cflee commented Mar 24, 2016

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.

@freakboy3742
Copy link
Member Author

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.

@cflee
Copy link
Member

cflee commented Mar 24, 2016

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

@freakboy3742
Copy link
Member Author

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.

@cflee
Copy link
Member

cflee commented Mar 26, 2016

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.

@raphaelm
Copy link
Contributor

raphaelm commented Nov 5, 2016

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

raphaelm added a commit to raphaelm/voc that referenced this issue Nov 5, 2016
freakboy3742 added a commit that referenced this issue Nov 5, 2016
Refs #76 -- Implement hasattr builtin
@SanketDG
Copy link
Contributor

SanketDG commented Nov 6, 2017

Is this issue done now?

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

@AmirYalamov
Copy link
Contributor

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

@raraizan
Copy link

raraizan commented Jun 18, 2018

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)

@AmirYalamov
Copy link
Contributor

AmirYalamov commented Jun 27, 2018

I am currently taking a look at ascii().

@AbhishekSinghDhadwal
Copy link

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

@lyfofvipin
Copy link

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?

@SubhamPaul21
Copy link

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?

@Jyotikarajpal
Copy link

Is the issue still open?

@brunoccalmeida
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests