Skip to content

Commit

Permalink
Fix py2 __bool__, remove unclear unicode alts
Browse files Browse the repository at this point in the history
Resolves   #699, #700.
  • Loading branch information
evhub committed Dec 8, 2022
1 parent 58ed15d commit dab943a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 32 deletions.
43 changes: 21 additions & 22 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,26 +238,26 @@ While Coconut syntax is based off of the latest Python 3, Coconut code compiled

To make Coconut built-ins universal across Python versions, Coconut makes available on any Python version built-ins that only exist in later versions, including **automatically overwriting Python 2 built-ins with their Python 3 counterparts.** Additionally, Coconut also [overwrites some Python 3 built-ins for optimization and enhancement purposes](#enhanced-built-ins). If access to the original Python versions of any overwritten built-ins is desired, the old built-ins can be retrieved by prefixing them with `py_`. Specifically, the overwritten built-ins are:

- `py_chr`,
- `py_hex`,
- `py_input`,
- `py_int`,
- `py_map`,
- `py_object`,
- `py_oct`,
- `py_open`,
- `py_print`,
- `py_range`,
- `py_str`,
- `py_super`,
- `py_zip`,
- `py_filter`,
- `py_reversed`,
- `py_enumerate`,
- `py_raw_input`,
- `py_xrange`,
- `py_repr`, and
- `py_breakpoint`.
- `py_chr`
- `py_hex`
- `py_input`
- `py_int`
- `py_map`
- `py_object`
- `py_oct`
- `py_open`
- `py_print`
- `py_range`
- `py_str`
- `py_super`
- `py_zip`
- `py_filter`
- `py_reversed`
- `py_enumerate`
- `py_raw_input`
- `py_xrange`
- `py_repr`
- `py_breakpoint`

_Note: Coconut's `repr` can be somewhat tricky, as it will attempt to remove the `u` before reprs of unicode strings on Python 2, but will not always be able to do so if the unicode string is nested._

Expand Down Expand Up @@ -938,11 +938,10 @@ Coconut supports Unicode alternatives to many different operator symbols. The Un
⊋ (\u228b) => ">"
∧ (\u2227) or ∩ (\u2229) => "&"
∨ (\u2228) or ∪ (\u222a) => "|"
⊻ (\u22bb) or ⊕ (\u2295) => "^"
⊻ (\u22bb) => "^"
« (\xab) => "<<"
» (\xbb) => ">>"
… (\u2026) => "..."
⋅ (\u22c5) => "@" (only matrix multiplication)
λ (\u03bb) => "lambda"
```

Expand Down
4 changes: 2 additions & 2 deletions coconut/compiler/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ class Grammar(object):
comp_dubstar_pipe = Literal("..**>") | fixto(Literal("\u2218**>"), "..**>")
comp_back_dubstar_pipe = Literal("<**..") | fixto(Literal("<**\u2218"), "<**..")
amp = Literal("&") | fixto(Literal("\u2227") | Literal("\u2229"), "&")
caret = Literal("^") | fixto(Literal("\u22bb") | Literal("\u2295"), "^")
caret = Literal("^") | fixto(Literal("\u22bb"), "^")
unsafe_bar = ~Literal("|>") + ~Literal("|*") + Literal("|") | fixto(Literal("\u2228") | Literal("\u222a"), "|")
bar = ~rbanana + unsafe_bar | invalid_syntax("\xa6", "invalid broken bar character", greedy=True)
percent = Literal("%")
Expand Down Expand Up @@ -728,7 +728,7 @@ class Grammar(object):
)
div_slash = slash | fixto(Literal("\xf7") + ~slash, "/")
div_dubslash = dubslash | fixto(combine(Literal("\xf7") + slash), "//")
matrix_at = at | fixto(Literal("\u22c5"), "@")
matrix_at = at

test = Forward()
test_no_chain, dubcolon = disable_inside(test, unsafe_dubcolon)
Expand Down
4 changes: 1 addition & 3 deletions coconut/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ def get_bool_env_var(env_var, default=False):
"\\??\\*?\\*?\u21a6", # |>
"\u21a4\\*?\\*?", # <|
"<?\\*?\\*?\u2218\\*?\\*?>?", # ..
"\u22c5", # *
"\xd7", # *
"\u2191", # **
"\xf7", # /
"\u207b", # -
Expand All @@ -688,10 +688,8 @@ def get_bool_env_var(env_var, default=False):
"\u2228", # |
"\u222a", # |
"\u22bb", # ^
"\u2295", # ^
"\xab", # <<
"\xbb", # >>
"\xd7", # @
"\u2026", # ...
"\u2286", # C=
"\u2287", # ^reversed
Expand Down
19 changes: 14 additions & 5 deletions coconut/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
VERSION = "2.1.1"
VERSION_NAME = "The Spanish Inquisition"
# False for release, int >= 1 for develop
DEVELOP = 20
DEVELOP = 21
ALPHA = False # for pre releases rather than post releases

# -----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -108,19 +108,28 @@ class object(object):
def __ne__(self, other):
eq = self == other
return _coconut.NotImplemented if eq is _coconut.NotImplemented else not eq
def __nonzero__(self):
self_bool = _coconut.getattr(self, "__bool__", None)
if self_bool is not None:
try:
result = self_bool()
except _coconut.NotImplementedError:
pass
else:
if result is not _coconut.NotImplemented:
return result
return True
class int(_coconut_py_int):
__slots__ = ()
if hasattr(_coconut_py_int, "__doc__"):
__doc__ = _coconut_py_int.__doc__
__doc__ = getattr(_coconut_py_int, "__doc__", "<see help(py_int)>")
class __metaclass__(type):
def __instancecheck__(cls, inst):
return _coconut.isinstance(inst, (_coconut_py_int, _coconut_py_long))
def __subclasscheck__(cls, subcls):
return _coconut.issubclass(subcls, (_coconut_py_int, _coconut_py_long))
class range(object):
__slots__ = ("_xrange",)
if hasattr(_coconut_py_xrange, "__doc__"):
__doc__ = _coconut_py_xrange.__doc__
__doc__ = getattr(_coconut_py_xrange, "__doc__", "<see help(py_xrange)>")
def __init__(self, *args):
self._xrange = _coconut_py_xrange(*args)
def __iter__(self):
Expand Down
6 changes: 6 additions & 0 deletions coconut/tests/src/cocotest/agnostic/main.coco
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,12 @@ def main_test() -> bool:
assert multiset((1, 2)) == m{1, 2} == multiset(m{1, 2})
assert multiset({1: 2, 2: 1}) == m{1, 1, 2}
assert m{} `isinstance` multiset
assert m{} `isinstance` collections.abc.Set
assert m{} `isinstance` collections.abc.MutableSet
assert True `isinstance` bool
class HasBool:
def __bool__(self) = False
assert not HasBool()
return True

def test_asyncio() -> bool:
Expand Down

0 comments on commit dab943a

Please sign in to comment.