Skip to content
This repository was archived by the owner on Mar 18, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions fatoptimizer/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@


def _chr_check_args(args):
code_point = args[0]
return 0 <= code_point <= 0x10ffff
# args[0] => code_point
return 0 <= args[0] <= 0x10ffff
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the purpose of this change. It makes the code less readable. The fact that you added a comment means that the code is unclear. I consider that if the code is clear enough, it doesn't require a comment.

Your commit message says "Elimination of unused variables", but it's wrong: the variable is used.



def _complex_check_args(args):
code_point = args[0]
return 0 <= code_point <= 0x10ffff
# args[0] => code_point
return 0 <= args[0] <= 0x10ffff


def _divmod_check_args(args):
Expand All @@ -23,32 +23,31 @@ def _divmod_check_args(args):


def _ord_check_args(args):
string = args[0]
return (len(string) == 1)
# param args[0] is a string
return (len(args[0]) == 1)


def _bytes_check_args(args):
arg = args[0]
if not isinstance(arg, tuple):
if not isinstance(args[0], tuple):
return True
return all(0 <= item <= 255 for item in arg)
return all(0 <= item <= 255 for item in args[0])


def _pow_check_args(config, args):
num = args[0]
exp = args[1]
# num => args[0]
# exp => args[1]
if len(args) >= 3:
mod = args[2]
# mod => args[2]
return check_pow(config, args[0], args[1], args[2])
else:
mod = None
return check_pow(config, num, exp, mod)
# mod => None
return check_pow(config, args[0], args[1], None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines look duplicated. The purpose of the mod variable is to avoid code duplication which is a bad programming practice. Moreover, the new core looks less readable, it's hard to to guess the purpose of each parameter of the check_pow() call.



def add_pure_builtins(config):
def add(name, *args, **kw):
func = getattr(builtins, name)
pure_func = PureFunction(func, name, *args, **kw)
config._pure_builtins[name] = pure_func
config._pure_builtins[name] = PureFunction(func, name, *args, **kw)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use a temporary variable to make the code more readable.


ANY_TYPE = None

Expand Down Expand Up @@ -76,6 +75,7 @@ def add(name, *args, **kw):
add('int', 1, FLOAT_TYPES + STR_TYPES,
# catch ValueError for int('xyz')
exceptions=ValueError)

add('len', 1, ITERABLE_TYPES)
add('list', 1, ITERABLE_TYPES)
add('oct', 1, int)
Expand Down
3 changes: 2 additions & 1 deletion fatoptimizer/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import builtins

from .tools import get_constant_size, ITERABLE_TYPES

# ISSUE : SystemError: Parent module '' not loaded, cannot perform relative import
# StackOverFlow search : http://stackoverflow.com/questions/16981921/relative-imports-in-python-3
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you added this comment. Your probably misused Python. Maybe you tried to import a module whereas you were on the wrong directory. To be clear: the code works very well. All unit tests pass, so you must not add such comment.


class Config:
# FIXME: use dir()?
Expand Down
3 changes: 2 additions & 1 deletion fatoptimizer/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ def add(obj_type, name, *args, **kw):
check_args=check_encoding,
exceptions=UnicodeEncodeError)
# FIXME: add more str methods

add(str, 'upper', 0)
add(str,'lower', 0)
# FIXME: tuple: count, index
7 changes: 7 additions & 0 deletions test_fatoptimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,13 @@ def test_str_encode(self):
self.check_dont_optimize(r'"ab\xff".encode("big5")')
self.check_dont_optimize(r'"ab\xff".encode("ascii", "backslashreplace")')

def test_str_upper(self):

self.check_optimize(r'"abc".upper()','"ABC"')

def test_str_lower(self):

self.check_optimize(r'"ABC".lower()','"abc"')

if __name__ == "__main__":
unittest.main()