Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[osh] Add support for shopt -s nocasematch #1748

Merged
merged 8 commits into from
Oct 28, 2023
Merged

Conversation

ellen364
Copy link
Collaborator

All tests in spec/nocasematch-match/test.sh now pass (3 failures previously allowed).

Still need to do the C++ translation.

Fixes #701

ellen364 and others added 4 commits October 17, 2023 07:16
All tests in spec/nocasematch-match/test.sh now pass (3 failures previously allowed).

Still need to do the C++ translation.
It appears Optional[T] roughly means Union[T, NoneTyp]
Changed the interface, but didn't implement it
Copy link
Contributor

@andychu andychu left a comment

Choose a reason for hiding this comment

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

This is excellent! Thank you

Only thing is that I think we should wrap the constants

BTW I fixed the C++ build, and you can look at the diff to see what happened. It was pretty easy!


Also I forgot to mention that the type checking is done with

devtools/types.sh check-oils

which I documented here!

/~https://github.com/oilshell/oil/wiki/Oil-Dev-Cheat-Sheet

(Melvin and Aidan have been getting by without this, but I welcome updates to the dev docs!)

osh/cmd_eval.py Outdated
@@ -111,6 +111,9 @@
RaiseControlFlow = 1 << 1 # eval/source builtins
Optimize = 1 << 2

# flags for fnmatch
FnmCasefold = 1 << 4
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should wrap this constant value in Python, which will make the C++ translation easier:

Example here:

/~https://github.com/oilshell/oil/blob/master/pyext/posixmodule.c#L2566

and then we can do

from libc import FNM_CASEFOLD

@@ -80,6 +80,10 @@
# (( val = a[key] )) # osh/sh_expr_eval.py:509 (Id.Arith_LBracket)
#

# flags for libc functions
FnmCasefold = 1 << 4
RegIcase = 1 << 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto here, let's create a constant that we can import like this

from libc import REG_ICASE (or whatever its spelling is)

BTW when I fiddle with pyext/, I also use pyext/libc_test.py, in case that's helpful

@@ -1042,14 +1046,17 @@ def EvalB(self, node):
raise AssertionError(op_id) # should never happen

if arg_type == bool_arg_type_e.Str:
eval_flags = 0
if self.exec_opts.nocasematch():
eval_flags |= FnmCasefold
Copy link
Contributor

Choose a reason for hiding this comment

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

how about calling these fnmatch_flags and regex_flags ?

Also in this case we can probably use a ternary expression

fnmatch_flags = FNM_CASEFOLD if self.exec_opts.nocasematch() else 0

@andychu
Copy link
Contributor

andychu commented Oct 26, 2023

Hi Ellen, this looks like it's very close, and could go in the next release? Let me know if you have any questions / issues

Thanks again for the contribution!

@ellen364
Copy link
Collaborator Author

Hi Andy, no questions at this point I don't think. (Though I do need to remind myself of the detail.) Just been one of those weeks.

What's the time frame to get these changes into the release?

Rather than define C constants like FNM_CASEFOLD in the Python modules, make them available from pyext/libc.

Refactor flag variable names and replace if statements with ternary operators where there are two outcomes.
@ellen364
Copy link
Collaborator Author

I've pushed up the refactoring, which has caused more C++ errors. Don't currently understand why the consts aren't in libc, but thinking it's to do with cpp/libc.cc.

Thoughts on the refactoring itself:

  • decided to use ternary expression in more places than you mentioned because each of the conditions for setting flags boiled down to "some flag or 0"
  • kept the const export very simple in pyext/libc.c but looking at the pyext/posixmodule.c example suspect it needs more error handling

We should move away from this style with mycpp/yaks "modules"
@andychu andychu changed the base branch from master to soil-staging October 28, 2023 02:28
@andychu andychu merged commit e0043e0 into soil-staging Oct 28, 2023
16 checks passed
@andychu
Copy link
Contributor

andychu commented Oct 28, 2023

Great thank you!

I added some stuff to cpp/preamble.h to fix the C++ translation -- that's our style now, but we may use a different style later (notes on Zulip)


Yeah Python's AddIntConstant function can apparently fail, probably with memory allocation, but it looks like they just return anyway, without doing anything ...

(In our own C++ code we would use DCHECK(), but meh )

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

Successfully merging this pull request may close these issues.

nocasematch shell option doesn't seem to work
2 participants