internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: optuna/optuna

Issue:
Feature to determine whether `GridSampler` has exhausted the grid
### Motivation

Currently, there is no good way in which we can determine whether the grid of `GridSampler` is exhausted when resuming a study.
While the method `_get_unvisited_grid_ids()` exists, it is a protected method and should not be used outside of the library.

### Description

I suggest adding a method called something like `is_exhausted()` that is public and simply returns `len(self._get_unvisited_grid_ids()) == 0`

### Alternatives (optional)

Alternatively, it may be useful to add keyword arguments to be passed to `GridSampler` that give more control over what happens if the grid is already exhausted. For example, one could add a keyword like `reevaluate_when_exhausted: bool`.

### Additional context (optional)

I encountered this suggestion when receiving the following warning: `GridSampler` is re-evaluating a configuration because the grid has been exhausted. This may happen due to a timing issue during distributed optimization or when re-running optimizations on already finished studies."

This issue further builds upon the discussion of https://github.com/optuna/optuna/issues/3256


Discussion:
Thanks for the PR. Nice idea. Let me add the contribution-welcome issue label.

Resolving patch:
diff --git a/optuna/samplers/_grid.py b/optuna/samplers/_grid.py
--- a/optuna/samplers/_grid.py
+++ b/optuna/samplers/_grid.py
@@ -277,3 +277,9 @@ def _same_search_space(self, search_space: Mapping[str, Sequence[GridValueType]]
                     return False
 
         return True
+
+    def is_exhausted(self, study: Study) -> bool:
+        """
+        Return True if all the possible params are evaluated, otherwise return False.
+        """
+        return len(self._get_unvisited_grid_ids(study)) == 0


Test patch:
diff --git a/tests/samplers_tests/test_grid.py b/tests/samplers_tests/test_grid.py
--- a/tests/samplers_tests/test_grid.py
+++ b/tests/samplers_tests/test_grid.py
@@ -254,3 +254,12 @@ def test_nan() -> None:
         lambda trial: 1 if np.isnan(trial.suggest_categorical("x", [0, float("nan")])) else 0
     )
     assert len(study.get_trials()) == 2
+
+
+def test_is_exhausted() -> None:
+    search_space = {"a": [0, 50]}
+    sampler = samplers.GridSampler(search_space)
+    study = optuna.create_study(sampler=sampler)
+    assert not sampler.is_exhausted(study)
+    study.optimize(lambda trial: trial.suggest_categorical("a", [0, 50]))
+    assert sampler.is_exhausted(study)
Repository: CTFd/CTFd

Issue:
Searching teams from the admin panel



Discussion:

Resolving patch:
diff --git a/CTFd/admin/teams.py b/CTFd/admin/teams.py
--- a/CTFd/admin/teams.py
+++ b/CTFd/admin/teams.py
@@ -13,6 +13,27 @@
 @admin_teams.route('/admin/teams/<int:page>')
 @admins_only
 def admin_teams_view(page):
+    q = request.args.get('q')
+    if q:
+        field = request.args.get('field')
+        teams = []
+        errors = []
+        if field == 'id':
+            if q.isnumeric():
+                teams = Teams.query.filter(Teams.id == q).order_by(Teams.id.asc()).all()
+            else:
+                teams = []
+                errors.append('Your ID search term is not numeric')
+        elif field == 'name':
+            teams = Teams.query.filter(Teams.name.like('%{}%'.format(q))).order_by(Teams.id.asc()).all()
+        elif field == 'email':
+            teams = Teams.query.filter(Teams.email.like('%{}%'.format(q))).order_by(Teams.id.asc()).all()
+        elif field == 'affiliation':
+            teams = Teams.query.filter(Teams.affiliation.like('%{}%'.format(q))).order_by(Teams.id.asc()).all()
+        elif field == 'country':
+            teams = Teams.query.filter(Teams.country.like('%{}%'.format(q))).order_by(Teams.id.asc()).all()
+        return render_template('admin/teams.html', teams=teams, pages=None, curr_page=None, q=q, field=field)
+
     page = abs(int(page))
     results_per_page = 50
     page_start = results_per_page * (page - 1)


Test patch:
Repository: CTFd/CTFd

Issue:
email module error when running under Gunicorn
While sending a mail under a production environment deployed with Gunicorn, utils.py will return a exception like that:
```
  File "/home/ubuntu/CTFd/CTFd/utils.py", line 477, in sendmail
    msg = email.mime.text.MIMEText(text)
AttributeError: 'module' object has no attribute 'text'
```
But no exception would be found while running under the debug mode using `python serve.py`.
And the solution is also easy, just adding 4 lines into utils.py:477:
```python
try:
    msg = email.mime.text.MIMEText(text)
except:
    from email.mime.text import MIMEText
    msg = MIMEText(text)
```


Discussion:
What Python version are you using? 

Resolving patch:
diff --git a/CTFd/utils.py b/CTFd/utils.py
--- a/CTFd/utils.py
+++ b/CTFd/utils.py
@@ -1,5 +1,4 @@
 import datetime
-import email
 import functools
 import hashlib
 import json
@@ -10,6 +9,7 @@
 import re
 import requests
 import shutil
+import six
 import smtplib
 import socket
 import subprocess
@@ -21,11 +21,11 @@
 import zipfile
 import io
 
+from email.mime.text import MIMEText
 from flask import current_app as app, request, redirect, url_for, session, render_template, abort
 from flask_caching import Cache
 from flask_migrate import Migrate, upgrade as migrate_upgrade, stamp as migrate_stamp
 from itsdangerous import Signer
-import six
 from six.moves.urllib.parse import urlparse, urljoin
 from werkzeug.utils import secure_filename
 
@@ -474,7 +474,7 @@ def sendmail(addr, text):
             data['SSL'] = get_config('mail_ssl')
 
         smtp = get_smtp(**data)
-        msg = email.mime.text.MIMEText(text)
+        msg = MIMEText(text)
         msg['Subject'] = "Message from {0}".format(ctf_name)
         msg['From'] = mailfrom_addr
         msg['To'] = addr


Test patch:
Repository: CTFd/CTFd

Issue:
Awards is not accounted in `user.place()`
[`user.place()`][user_place] is not accounting awards while [`get_standings()`][get_standings] does. This causes different ordering in scoreboard and team profile. Is this by design?

[user_place]: https://github.com/CTFd/CTFd/blob/master/CTFd/models.py#L200
[get_standings]: https://github.com/CTFd/CTFd/blob/master/CTFd/scoreboard.py#L11



Discussion:
This isn't intentional and is a known issue for me. I thought I had fixed it in some branch though. Feel free to submit a PR if you write a fix for it. 

Resolving patch:
diff --git a/CTFd/models.py b/CTFd/models.py
--- a/CTFd/models.py
+++ b/CTFd/models.py
@@ -8,6 +8,7 @@
 from flask_sqlalchemy import SQLAlchemy
 from passlib.hash import bcrypt_sha256
 from sqlalchemy.exc import DatabaseError
+from sqlalchemy.sql.expression import union_all
 
 
 def sha512(string):
@@ -198,22 +199,44 @@ def score(self, admin=False):
             return 0
 
     def place(self, admin=False):
-        score = db.func.sum(Challenges.value).label('score')
-        quickest = db.func.max(Solves.date).label('quickest')
-        teams = db.session.query(Solves.teamid).join(Teams).join(Challenges).filter(Teams.banned == False)
+        scores = db.session.query(
+            Solves.teamid.label('teamid'),
+            db.func.sum(Challenges.value).label('score'),
+            db.func.max(Solves.date).label('date')
+        ).join(Challenges).group_by(Solves.teamid)
+
+        awards = db.session.query(
+            Awards.teamid.label('teamid'),
+            db.func.sum(Awards.value).label('score'),
+            db.func.max(Awards.date).label('date')
+        ).group_by(Awards.teamid)
 
         if not admin:
             freeze = Config.query.filter_by(key='freeze').first()
             if freeze and freeze.value:
                 freeze = int(freeze.value)
                 freeze = datetime.datetime.utcfromtimestamp(freeze)
-                teams = teams.filter(Solves.date < freeze)
+                scores = scores.filter(Solves.date < freeze)
+                awards = awards.filter(Awards.date < freeze)
+
+        results = union_all(scores, awards).alias('results')
+
+        sumscore = db.func.sum(results.columns.score).label('sumscore')
+        quickest = db.func.max(results.columns.date).label('quickest')
+
+        standings_query = db.session.query(results.columns.teamid)\
+            .join(Teams)\
+            .group_by(results.columns.teamid)\
+            .order_by(sumscore.desc(), quickest)
+
+        if not admin:
+            standings_query = standings_query.filter(Teams.banned == False)
 
-        teams = teams.group_by(Solves.teamid).order_by(score.desc(), quickest).all()
+        standings = standings_query.all()
 
         # http://codegolf.stackexchange.com/a/4712
         try:
-            i = teams.index((self.id,)) + 1
+            i = standings.index((self.id,)) + 1
             k = i % 10
             return "%d%s" % (i, "tsnrhtdd"[(i / 10 % 10 != 1) * (k < 4) * k::4])
         except ValueError:


Test patch:
Repository: CTFd/CTFd

Issue:
Put documentation around caching
CTFd relies on a cache to make sure that it doesnt overload the DB with unnecessary queries. 

`config.py` should be very clear that this is the case and we should make sure that the user knows that they need to have a cache available. 


Discussion:

Resolving patch:
diff --git a/CTFd/config.py b/CTFd/config.py
--- a/CTFd/config.py
+++ b/CTFd/config.py
@@ -108,14 +108,18 @@ class Config(object):
     CACHE_TYPE specifies how CTFd should cache configuration values. If CACHE_TYPE is set to 'redis', CTFd will make use
     of the REDIS_URL specified in environment variables. You can also choose to hardcode the REDIS_URL here.
 
+    It is important that you specify some sort of cache as CTFd uses it to store values received from the database.
+
     CACHE_REDIS_URL is the URL to connect to Redis server.
-    Example: redis://user:password@localhost:6379/2.
+    Example: redis://user:password@localhost:6379
 
     http://pythonhosted.org/Flask-Caching/#configuring-flask-caching
     '''
-    CACHE_TYPE = "simple"
-    if CACHE_TYPE == 'redis':
-        CACHE_REDIS_URL = os.environ.get('REDIS_URL')
+    CACHE_REDIS_URL = os.environ.get('REDIS_URL')
+    if CACHE_REDIS_URL:
+        CACHE_TYPE = 'redis'
+    else:
+        CACHE_TYPE = 'simple'
 
 
 class TestingConfig(Config):
diff --git a/CTFd/utils.py b/CTFd/utils.py
--- a/CTFd/utils.py
+++ b/CTFd/utils.py
@@ -406,7 +406,7 @@ def delete_file(file_id):
 @cache.memoize()
 def get_config(key):
     with app.app_context():
-        value = app.config.get(key)
+        value = app.config.get(key.upper()) or app.config.get(key.lower())
         if value:
             if value and value.isdigit():
                 return int(value)


Test patch:
Repository: CTFd/CTFd

Issue:
Configuration can be auto-filled potentially causing issues



Discussion:

Resolving patch:
diff --git a/CTFd/admin/__init__.py b/CTFd/admin/__init__.py
--- a/CTFd/admin/__init__.py
+++ b/CTFd/admin/__init__.py
@@ -143,8 +143,15 @@ def admin_config():
         mail_server = utils.set_config("mail_server", request.form.get('mail_server', None))
         mail_port = utils.set_config("mail_port", request.form.get('mail_port', None))
 
-        mail_username = utils.set_config("mail_username", request.form.get('mail_username', None))
-        mail_password = utils.set_config("mail_password", request.form.get('mail_password', None))
+        if request.form.get('mail_useauth', None) and (request.form.get('mail_u', None) or request.form.get('mail_p', None)):
+            if len(request.form.get('mail_u')) > 0:
+                mail_username = utils.set_config("mail_username", request.form.get('mail_u', None))
+            if len(request.form.get('mail_p')) > 0:
+                mail_password = utils.set_config("mail_password", request.form.get('mail_p', None))
+
+        elif request.form.get('mail_useauth', None) is None:
+            utils.set_config("mail_username", None)
+            utils.set_config("mail_password", None)
 
         ctf_name = utils.set_config("ctf_name", request.form.get('ctf_name', None))
         ctf_theme = utils.set_config("ctf_theme", request.form.get('ctf_theme', None))


Test patch:
Repository: CTFd/CTFd

Issue:
Hidden challenges can be answered without being unhidden
If attacker obtains the key of Hidden Challenge using some method, attacker can answer Hidden Challenge.

It is possible by sending Hidden Challenge's key and id to `/chal`.
Hidden Challenge's id is a sequential number, so it can be searched by brute force.

Measures
- If id of Hidden Challeng is specified, do not perform processing.
- Complicate id.



Discussion:
I'll fix this but I would like to know how you expect an attacker to know the key value or to get around the rate limiting implemented on the challenge submission endpoint? 




Resolving patch:
diff --git a/CTFd/challenges.py b/CTFd/challenges.py
--- a/CTFd/challenges.py
+++ b/CTFd/challenges.py
@@ -306,6 +306,8 @@ def chal(chalid):
         print("[{0}] {1} submitted {2} with kpm {3}".format(*data))
 
         chal = Challenges.query.filter_by(id=chalid).first_or_404()
+        if chal.hidden:
+            abort(404)
         chal_class = get_chal_class(chal.type)
 
         # Anti-bruteforce / submitting keys too quickly


Test patch:
diff --git a/tests/helpers.py b/tests/helpers.py
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -84,8 +84,10 @@ def get_scores(user):
     return scores['standings']
 
 
-def gen_challenge(db, name='chal_name', description='chal_description', value=100, category='chal_category', type='standard'):
+def gen_challenge(db, name='chal_name', description='chal_description', value=100, category='chal_category', type='standard', hidden=False):
     chal = Challenges(name, description, value, category)
+    if hidden:
+        chal.hidden = hidden
     db.session.add(chal)
     db.session.commit()
     return chal
diff --git a/tests/user/test_challenges.py b/tests/user/test_challenges.py
--- a/tests/user/test_challenges.py
+++ b/tests/user/test_challenges.py
@@ -349,3 +349,27 @@ def test_that_view_challenges_unregistered_works():
         data = json.loads(data)
         assert data['status'] == -1
     destroy_ctfd(app)
+
+
+def test_hidden_challenge_is_unsolveable():
+    """Test that hidden challenges return 404 and do not insert a solve or wrong key"""
+    app = create_ctfd()
+    with app.app_context():
+        register_user(app)
+        client = login_as_user(app)
+        chal = gen_challenge(app.db, hidden=True)
+        flag = gen_flag(app.db, chal=chal.id, flag='flag')
+        with client.session_transaction() as sess:
+            data = {
+                "key": 'flag',
+                "nonce": sess.get('nonce')
+            }
+        r = client.post('/chal/{}'.format(chal.id), data=data)
+        assert r.status_code == 404
+
+        solves = Solves.query.all()
+        assert len(solves) == 0
+
+        wrong_keys = WrongKeys.query.all()
+        assert len(wrong_keys) == 0
+    destroy_ctfd(app)
Repository: CTFd/CTFd

Issue:
Optimize top10 call
https://github.com/CTFd/CTFd/blob/master/CTFd/scoreboard.py#L125-L127

This code seems pretty wasteful and is likely getting hit fairly often. Optimizing this to be a single database query is likely a good idea. 


Discussion:

Resolving patch:
diff --git a/CTFd/scoreboard.py b/CTFd/scoreboard.py
--- a/CTFd/scoreboard.py
+++ b/CTFd/scoreboard.py
@@ -122,37 +122,42 @@ def topteams(count):
 
     standings = get_standings(count=count)
 
-    for i, team in enumerate(standings):
-        solves = Solves.query.filter_by(teamid=team.teamid)
-        awards = Awards.query.filter_by(teamid=team.teamid)
+    team_ids = [team.teamid for team in standings]
 
-        freeze = utils.get_config('freeze')
+    solves = Solves.query.filter(Solves.teamid.in_(team_ids))
+    awards = Awards.query.filter(Awards.teamid.in_(team_ids))
 
-        if freeze:
-            solves = solves.filter(Solves.date < utils.unix_time_to_utc(freeze))
-            awards = awards.filter(Awards.date < utils.unix_time_to_utc(freeze))
+    freeze = utils.get_config('freeze')
+
+    if freeze:
+        solves = solves.filter(Solves.date < utils.unix_time_to_utc(freeze))
+        awards = awards.filter(Awards.date < utils.unix_time_to_utc(freeze))
 
-        solves = solves.all()
-        awards = awards.all()
+    solves = solves.all()
+    awards = awards.all()
 
+    for i, team in enumerate(team_ids):
         json['places'][i + 1] = {
-            'id': team.teamid,
-            'name': team.name,
+            'id': standings[i].teamid,
+            'name': standings[i].name,
             'solves': []
         }
-        for x in solves:
-            json['places'][i + 1]['solves'].append({
-                'chal': x.chalid,
-                'team': x.teamid,
-                'value': x.chal.value,
-                'time': utils.unix_time(x.date)
-            })
+        for solve in solves:
+            if solve.teamid == team:
+                json['places'][i + 1]['solves'].append({
+                    'chal': solve.chalid,
+                    'team': solve.teamid,
+                    'value': solve.chal.value,
+                    'time': utils.unix_time(solve.date)
+                })
         for award in awards:
-            json['places'][i + 1]['solves'].append({
-                'chal': None,
-                'team': award.teamid,
-                'value': award.value,
-                'time': utils.unix_time(award.date)
-            })
+            if award.teamid == team:
+                json['places'][i + 1]['solves'].append({
+                    'chal': None,
+                    'team': award.teamid,
+                    'value': award.value,
+                    'time': utils.unix_time(award.date)
+                })
         json['places'][i + 1]['solves'] = sorted(json['places'][i + 1]['solves'], key=lambda k: k['time'])
+
     return jsonify(json)


Test patch:
Repository: CTFd/CTFd

Issue:
Upgrade to Font Awesome 5
CTFd 1.1 is meant to be a major front end improvement. Font Awesome 5 is out and I think if there's a time to make this change it would be now. 

https://fontawesome.com/how-to-use/upgrading-from-4


Discussion:

Resolving patch:
diff --git a/CTFd/views.py b/CTFd/views.py
--- a/CTFd/views.py
+++ b/CTFd/views.py
@@ -44,9 +44,9 @@ def setup():
         <h3 class="text-center">
             <p>A cool CTF platform from <a href="https://ctfd.io">ctfd.io</a></p>
             <p>Follow us on social media:</p>
-            <a href="https://twitter.com/ctfdio"><i class="fa fa-twitter fa-2x" aria-hidden="true"></i></a>&nbsp;
-            <a href="https://facebook.com/ctfdio"><i class="fa fa-facebook-official fa-2x" aria-hidden="true"></i></a>&nbsp;
-            <a href="https://github.com/ctfd"><i class="fa fa-github fa-2x" aria-hidden="true"></i></a>
+            <a href="https://twitter.com/ctfdio"><i class="fab fa-twitter fa-2x" aria-hidden="true"></i></a>&nbsp;
+            <a href="https://facebook.com/ctfdio"><i class="fab fa-facebook fa-2x" aria-hidden="true"></i></a>&nbsp;
+            <a href="https://github.com/ctfd"><i class="fab fa-github fa-2x" aria-hidden="true"></i></a>
         </h3>
         <br>
         <h4 class="text-center">


Test patch:
Repository: CTFd/CTFd

Issue:
Certain templates can be force loaded
There are certain templates which are accessible because of the static_html handler. This isn't a vulnerability because it appears like no sensitive information is loaded but it should be fixed. 

It's possible that the template handler itself should just be entirely removed. 


Discussion:

Resolving patch:
diff --git a/CTFd/views.py b/CTFd/views.py
--- a/CTFd/views.py
+++ b/CTFd/views.py
@@ -115,10 +115,7 @@ def custom_css():
 def static_html(template):
     page = utils.get_page(template)
     if page is None:
-        try:
-            return render_template('%s.html' % template)
-        except TemplateNotFound:
-            abort(404)
+        abort(404)
     else:
         if page.auth_required and utils.authed() is False:
             return redirect(url_for('auth.login', next=request.path))


Test patch:
Repository: CTFd/CTFd

Issue:
Docker startup getting stuck on mysqladmin ping
**Environment**:

  - CTFd Version/Commit: ctfd/ctfd:latest from Docker hub (17 days old)
  - Operating System: Amazon Linux AMI 2017.09.j x86_64 ECS HVM GP2
  - Web Browser and Version: N/A

**What happened?**

Trying to setup CTFd with AWS ECS and RDS Aurora.
If I don't set the DATABASE_URL env variable, it works fine and starts.
If I do set the DATABASE_URL to mysql+pymysql://ctfd:<MYPASSWORD>@ctfd.<resource-id>i.eu-west-1.rds.amazonaws.com/ctfd I get stuck on docker-entrypoint.sh:7 `while ! mysqladmin ping -h db --silent; do`

**What did you expect to happen?**

That the ping should succeed and startup continue

**How to reproduce your issue**

Create an ECS task with ctfd/ctfd as image source, set env variable SECRET_KEY and DATABASE_URL. Start container.

I have made sure the container can access the database by running `docker exec container-id mysql -h ctfd.<resource-id>.eu-west-1.rds.amazonaws.com -p<SECRET PASSWORD>` which works.

**Any associated stack traces or error logs**

Just stuck on "Waiting on MySQL"


My question is basically: am I doing something wrong and should somehow make that "db" resolve to the database or is the script incorrect and should take the value of DATABASE_URL into account?


Discussion:
Sorry I missed this issue. Within docker-compose each of the containers (including the db) get a hostname related to the service. 

I think we should be taking into account the DATABASE_URL value. 

If you have the time a PR would be great!

Resolving patch:
diff --git a/wsgi.py b/wsgi.py
deleted file mode 100644
--- a/wsgi.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from CTFd import create_app
-
-app = create_app()


Test patch: