jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_evaluate_dict_key_as_underscore(self):
    ec = self._makeContext()
    self.assertEqual(ec.evaluate('d/_'), 'under')
//  [python]
def render(self, name, value, attrs=None, renderer=None, **kwargs):
    """Render all textareas with correct plural labels."""
    unit = value
    values = unit.get_target_plurals()
    lang = unit.translation.language
    plural = unit.translation.plural
    tabindex = self.attrs["tabindex"]
    placeables = [hl[2] for hl in highlight_string(unit.source_string, unit)]

    # Need to add extra class
    attrs["class"] = "translation-editor form-control highlight-editor"
    attrs["tabindex"] = tabindex
    attrs["lang"] = lang.code
    attrs["dir"] = lang.direction
    attrs["rows"] = 3
    attrs["data-max"] = unit.get_max_length()
    attrs["data-mode"] = unit.edit_mode
    attrs["data-placeables"] = "|".join(re.escape(pl) for pl in placeables if pl)
    if unit.readonly:
        attrs["readonly"] = 1

    # Okay we have more strings
    ret = []
    plurals = unit.get_source_plurals()
    base_id = f"id_{unit.checksum}"
    for idx, val in enumerate(values):
        # Generate ID
        fieldname = f"{name}_{idx}"
        fieldid = f"{base_id}_{idx}"
        attrs["id"] = fieldid
        attrs["tabindex"] = tabindex + idx
        if idx and len(plurals) > 1:
            source = plurals[1]
        else:
            source = plurals[0]

        # Render textare
        textarea = super().render(fieldname, val, attrs, renderer, **kwargs)
        # Label for plural
        label = escape(unit.translation.language)
        if len(values) != 1:
            label = f"{label}, {plural.get_plural_label(idx)}"
        ret.append(
            render_to_string(
                "snippets/editor.html",
                {
                    "toolbar": self.get_toolbar(lang, fieldid, unit, idx, source),
                    "fieldid": fieldid,
                    "label": mark_safe(label),
                    "textarea": textarea,
                    "max_length": attrs["data-max"],
                    "length": len(val),
                    "source_length": len(source),
                    "rtl_toggle": self.get_rtl_toggle(lang, fieldid),
                },
            )
        )

    # Show plural formula for more strings
    if len(values) > 1:
        ret.append(
            render_to_string(
                "snippets/plural-formula.html",
                {"plural": plural, "user": self.profile.user},
            )
        )

    # Join output
    return mark_safe("".join(ret))
//  [python]
def render(self, name, value, attrs=None, renderer=None, **kwargs):
    """Render all textareas with correct plural labels."""
    unit = value
    values = unit.get_target_plurals()
    lang = unit.translation.language
    plural = unit.translation.plural
    tabindex = self.attrs["tabindex"]
    placeables = [hl[2] for hl in highlight_string(unit.source_string, unit)]

    # Need to add extra class
    attrs["class"] = "translation-editor form-control highlight-editor"
    attrs["tabindex"] = tabindex
    attrs["lang"] = lang.code
    attrs["dir"] = lang.direction
    attrs["rows"] = 3
    attrs["data-max"] = unit.get_max_length()
    attrs["data-mode"] = unit.edit_mode
    attrs["data-placeables"] = "|".join(re.escape(pl) for pl in placeables if pl)
    if unit.readonly:
        attrs["readonly"] = 1

    # Okay we have more strings
    ret = []
    plurals = unit.get_source_plurals()
    base_id = f"id_{unit.checksum}"
    for idx, val in enumerate(values):
        # Generate ID
        fieldname = f"{name}_{idx}"
        fieldid = f"{base_id}_{idx}"
        attrs["id"] = fieldid
        attrs["tabindex"] = tabindex + idx
        if idx and len(plurals) > 1:
            source = plurals[1]
        else:
            source = plurals[0]

        # Render textare
        textarea = super().render(fieldname, val, attrs, renderer, **kwargs)
        # Label for plural
        label = str(unit.translation.language)
        if len(values) != 1:
            label = f"{label}, {plural.get_plural_label(idx)}"
        ret.append(
            render_to_string(
                "snippets/editor.html",
                {
                    "toolbar": self.get_toolbar(lang, fieldid, unit, idx, source),
                    "fieldid": fieldid,
                    "label": mark_safe(label),
                    "textarea": textarea,
                    "max_length": attrs["data-max"],
                    "length": len(val),
                    "source_length": len(source),
                    "rtl_toggle": self.get_rtl_toggle(lang, fieldid),
                },
            )
        )

    # Show plural formula for more strings
    if len(values) > 1:
        ret.append(
            render_to_string(
                "snippets/plural-formula.html",
                {"plural": plural, "user": self.profile.user},
            )
        )

    # Join output
    return mark_safe("".join(ret))
//  [python]
def tearDown(self):
    self.file.close()
//  [python]
def tearDown(self):
    if os.path.isfile(self.filename):
        os.unlink(self.filename)
//  [python]
def from_data(*, data: oai.Operation, path: str, method: str, tag: str) -> Union[Endpoint, ParseError]:
    """ Construct an endpoint from the OpenAPI data """

    if data.operationId is None:
        return ParseError(data=data, detail="Path operations with operationId are not yet supported")

    endpoint = Endpoint(
        path=path,
        method=method,
        description=utils.remove_string_escapes(data.description) if data.description else "",
        name=data.operationId,
        requires_security=bool(data.security),
        tag=tag,
    )

    result = Endpoint._add_parameters(endpoint, data)
    if isinstance(result, ParseError):
        return result
    result = Endpoint._add_responses(result, data.responses)
    if isinstance(result, ParseError):
        return result
    result = Endpoint._add_body(result, data)

    return result
//  [python]
def from_data(*, data: oai.Operation, path: str, method: str, tag: str) -> Union[Endpoint, ParseError]:
    """ Construct an endpoint from the OpenAPI data """

    if data.operationId is None:
        return ParseError(data=data, detail="Path operations with operationId are not yet supported")

    endpoint = Endpoint(
        path=path,
        method=method,
        description=data.description,
        name=data.operationId,
        requires_security=bool(data.security),
        tag=tag,
    )

    result = Endpoint._add_parameters(endpoint, data)
    if isinstance(result, ParseError):
        return result
    result = Endpoint._add_responses(result, data.responses)
    if isinstance(result, ParseError):
        return result
    result = Endpoint._add_body(result, data)

    return result
//  [python]
def get_release_file(root, request):
    session = DBSession()

    f = ReleaseFile.by_id(session, int(request.matchdict['file_id']))
    url = f.url
    if url.startswith('http://pypi.python.org'):
        url = 'https' + url[4:]

    rv = {'id': f.id,
          'url': url,
          'filename': f.filename,
          }
    f.downloads += 1
    f.release.downloads += 1
    f.release.package.downloads += 1
    session.add(f.release.package)
    session.add(f.release)
    session.add(f)
    return rv
//  [python]
def get_release_file(root, request):
    session = DBSession()

    f = ReleaseFile.by_id(session, int(request.matchdict['file_id']))
    rv = {'id': f.id,
          'url': f.url,
          'filename': f.filename,
          }
    f.downloads += 1
    f.release.downloads += 1
    f.release.package.downloads += 1
    session.add(f.release.package)
    session.add(f.release)
    session.add(f)
    return rv
//  [python]
def requestStarted(self, job):
    """Handle a request for a qute: scheme.

    This method must be reimplemented by all custom URL scheme handlers.
    The request is asynchronous and does not need to be handled right away.

    Args:
        job: QWebEngineUrlRequestJob
    """
    url = job.requestUrl()

    if url.scheme() in ['chrome-error', 'chrome-extension']:
        # WORKAROUND for https://bugreports.qt.io/browse/QTBUG-63378
        job.fail(QWebEngineUrlRequestJob.UrlInvalid)
        return

    # Only the browser itself or qute:// pages should access any of those
    # URLs.
    # The request interceptor further locks down qute://settings/set.
    try:
        initiator = job.initiator()
    except AttributeError:
        # Added in Qt 5.11
        pass
    else:
        if initiator.isValid() and initiator.scheme() != 'qute':
            log.misc.warning("Blocking malicious request from {} to {}"
                             .format(initiator.toDisplayString(),
                                     url.toDisplayString()))
            job.fail(QWebEngineUrlRequestJob.RequestDenied)
            return

    if job.requestMethod() != b'GET':
        job.fail(QWebEngineUrlRequestJob.RequestDenied)
        return

    assert url.scheme() == 'qute'

    log.misc.debug("Got request for {}".format(url.toDisplayString()))
    try:
        mimetype, data = qutescheme.data_for_url(url)
    except qutescheme.NoHandlerFound:
        log.misc.debug("No handler found for {}".format(
            url.toDisplayString()))
        job.fail(QWebEngineUrlRequestJob.UrlNotFound)
    except qutescheme.QuteSchemeOSError:
        # FIXME:qtwebengine how do we show a better error here?
        log.misc.exception("OSError while handling qute://* URL")
        job.fail(QWebEngineUrlRequestJob.UrlNotFound)
    except qutescheme.QuteSchemeError:
        # FIXME:qtwebengine how do we show a better error here?
        log.misc.exception("Error while handling qute://* URL")
        job.fail(QWebEngineUrlRequestJob.RequestFailed)
    except qutescheme.Redirect as e:
        qtutils.ensure_valid(e.url)
        job.redirect(e.url)
    else:
        log.misc.debug("Returning {} data".format(mimetype))

        # We can't just use the QBuffer constructor taking a QByteArray,
        # because that somehow segfaults...
        # https://www.riverbankcomputing.com/pipermail/pyqt/2016-September/038075.html
        buf = QBuffer(parent=self)
        buf.open(QIODevice.WriteOnly)
        buf.write(data)
        buf.seek(0)
        buf.close()
        job.reply(mimetype.encode('ascii'), buf)
//  [python]
def requestStarted(self, job):
    """Handle a request for a qute: scheme.

    This method must be reimplemented by all custom URL scheme handlers.
    The request is asynchronous and does not need to be handled right away.

    Args:
        job: QWebEngineUrlRequestJob
    """
    url = job.requestUrl()

    if url.scheme() in ['chrome-error', 'chrome-extension']:
        # WORKAROUND for https://bugreports.qt.io/browse/QTBUG-63378
        job.fail(QWebEngineUrlRequestJob.UrlInvalid)
        return

    assert job.requestMethod() == b'GET'
    assert url.scheme() == 'qute'
    log.misc.debug("Got request for {}".format(url.toDisplayString()))
    try:
        mimetype, data = qutescheme.data_for_url(url)
    except qutescheme.NoHandlerFound:
        log.misc.debug("No handler found for {}".format(
            url.toDisplayString()))
        job.fail(QWebEngineUrlRequestJob.UrlNotFound)
    except qutescheme.QuteSchemeOSError:
        # FIXME:qtwebengine how do we show a better error here?
        log.misc.exception("OSError while handling qute://* URL")
        job.fail(QWebEngineUrlRequestJob.UrlNotFound)
    except qutescheme.QuteSchemeError:
        # FIXME:qtwebengine how do we show a better error here?
        log.misc.exception("Error while handling qute://* URL")
        job.fail(QWebEngineUrlRequestJob.RequestFailed)
    except qutescheme.Redirect as e:
        qtutils.ensure_valid(e.url)
        job.redirect(e.url)
    else:
        log.misc.debug("Returning {} data".format(mimetype))

        # We can't just use the QBuffer constructor taking a QByteArray,
        # because that somehow segfaults...
        # https://www.riverbankcomputing.com/pipermail/pyqt/2016-September/038075.html
        buf = QBuffer(parent=self)
        buf.open(QIODevice.WriteOnly)
        buf.write(data)
        buf.seek(0)
        buf.close()
        job.reply(mimetype.encode('ascii'), buf)
//  [python]
def stmt(self, stmt, msg=None):
    mod = ast.Module([stmt], [])
    self.mod(mod, msg)
//  [python]
def stmt(self, stmt, msg=None):
    mod = ast.Module([stmt])
    self.mod(mod, msg)
//  [python]
def expr(self, node, msg=None, *, exc=ValueError):
    mod = ast.Module([ast.Expr(node)], [])
    self.mod(mod, msg, exc=exc)
//  [python]
def expr(self, node, msg=None, *, exc=ValueError):
    mod = ast.Module([ast.Expr(node)])
    self.mod(mod, msg, exc=exc)
//  [python]
def setUp(self):
    shape = (2, 4, 3)
    rand = np.random.random
    self.x = rand(shape) + rand(shape).astype(np.complex)*1j
    self.x[0,:, 1] = [nan, inf, -inf, nan]
    self.dtype = self.x.dtype
    self.file = tempfile.NamedTemporaryFile()
    self.filename = self.file.name
//  [python]
def setUp(self):
    shape = (2, 4, 3)
    rand = np.random.random
    self.x = rand(shape) + rand(shape).astype(np.complex)*1j
    self.x[0,:, 1] = [nan, inf, -inf, nan]
    self.dtype = self.x.dtype
    self.filename = tempfile.mktemp()
//  [python]
def test_dump(self):
    node = ast.parse('spam(eggs, "and cheese")')
    self.assertEqual(ast.dump(node),
        "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
        "args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese')], "
        "keywords=[]))], type_ignores=[])"
    )
    self.assertEqual(ast.dump(node, annotate_fields=False),
        "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
        "Constant('and cheese')], []))], [])"
    )
    self.assertEqual(ast.dump(node, include_attributes=True),
        "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=4), "
        "args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5, "
        "end_lineno=1, end_col_offset=9), Constant(value='and cheese', "
        "lineno=1, col_offset=11, end_lineno=1, end_col_offset=23)], keywords=[], "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24), "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)], type_ignores=[])"
    )
//  [python]
def test_dump(self):
    node = ast.parse('spam(eggs, "and cheese")')
    self.assertEqual(ast.dump(node),
        "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
        "args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese')], "
        "keywords=[]))])"
    )
    self.assertEqual(ast.dump(node, annotate_fields=False),
        "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
        "Constant('and cheese')], []))])"
    )
    self.assertEqual(ast.dump(node, include_attributes=True),
        "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=4), "
        "args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5, "
        "end_lineno=1, end_col_offset=9), Constant(value='and cheese', "
        "lineno=1, col_offset=11, end_lineno=1, end_col_offset=23)], keywords=[], "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24), "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)])"
    )
//  [python]
def test_bad_integer(self):
    # issue13436: Bad error message with invalid numeric values
    body = [ast.ImportFrom(module='time',
                           names=[ast.alias(name='sleep')],
                           level=None,
                           lineno=None, col_offset=None)]
    mod = ast.Module(body, [])
    with self.assertRaises(ValueError) as cm:
        compile(mod, 'test', 'exec')
    self.assertIn("invalid integer value: None", str(cm.exception))
//  [python]
def test_bad_integer(self):
    # issue13436: Bad error message with invalid numeric values
    body = [ast.ImportFrom(module='time',
                           names=[ast.alias(name='sleep')],
                           level=None,
                           lineno=None, col_offset=None)]
    mod = ast.Module(body)
    with self.assertRaises(ValueError) as cm:
        compile(mod, 'test', 'exec')
    self.assertIn("invalid integer value: None", str(cm.exception))
//  [python]
def test_invalid_sum(self):
    pos = dict(lineno=2, col_offset=3)
    m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], [])
    with self.assertRaises(TypeError) as cm:
        compile(m, "<test>", "exec")
    self.assertIn("but got <_ast.expr", str(cm.exception))
//  [python]
def test_invalid_sum(self):
    pos = dict(lineno=2, col_offset=3)
    m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
    with self.assertRaises(TypeError) as cm:
        compile(m, "<test>", "exec")
    self.assertIn("but got <_ast.expr", str(cm.exception))
//  [python]
def test_module(self):
    body = [ast.Num(42)]
    x = ast.Module(body, [])
    self.assertEqual(x.body, body)
//  [python]
def test_module(self):
    body = [ast.Num(42)]
    x = ast.Module(body)
    self.assertEqual(x.body, body)
//  [python]
def test_fix_missing_locations(self):
    src = ast.parse('write("spam")')
    src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
                                      [ast.Str('eggs')], [])))
    self.assertEqual(src, ast.fix_missing_locations(src))
    self.maxDiff = None
    self.assertEqual(ast.dump(src, include_attributes=True),
        "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), "
        "args=[Constant(value='spam', lineno=1, col_offset=6, end_lineno=1, "
        "end_col_offset=12)], keywords=[], lineno=1, col_offset=0, end_lineno=1, "
        "end_col_offset=13), lineno=1, col_offset=0, end_lineno=1, "
        "end_col_offset=13), Expr(value=Call(func=Name(id='spam', ctx=Load(), "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=0), "
        "args=[Constant(value='eggs', lineno=1, col_offset=0, end_lineno=1, "
        "end_col_offset=0)], keywords=[], lineno=1, col_offset=0, end_lineno=1, "
        "end_col_offset=0), lineno=1, col_offset=0, end_lineno=1, end_col_offset=0)], "
        "type_ignores=[])"
    )
//  [python]
def test_fix_missing_locations(self):
    src = ast.parse('write("spam")')
    src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
                                      [ast.Str('eggs')], [])))
    self.assertEqual(src, ast.fix_missing_locations(src))
    self.maxDiff = None
    self.assertEqual(ast.dump(src, include_attributes=True),
        "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), "
        "args=[Constant(value='spam', lineno=1, col_offset=6, end_lineno=1, "
        "end_col_offset=12)], keywords=[], lineno=1, col_offset=0, end_lineno=1, "
        "end_col_offset=13), lineno=1, col_offset=0, end_lineno=1, "
        "end_col_offset=13), Expr(value=Call(func=Name(id='spam', ctx=Load(), "
        "lineno=1, col_offset=0, end_lineno=1, end_col_offset=0), "
        "args=[Constant(value='eggs', lineno=1, col_offset=0, end_lineno=1, "
        "end_col_offset=0)], keywords=[], lineno=1, col_offset=0, end_lineno=1, "
        "end_col_offset=0), lineno=1, col_offset=0, end_lineno=1, end_col_offset=0)])"
    )
//  [python]
def test_level_as_none(self):
    body = [ast.ImportFrom(module='time',
                           names=[ast.alias(name='sleep')],
                           level=None,
                           lineno=0, col_offset=0)]
    mod = ast.Module(body, [])
    code = compile(mod, 'test', 'exec')
    ns = {}
    exec(code, ns)
    self.assertIn('sleep', ns)
//  [python]
def test_level_as_none(self):
    body = [ast.ImportFrom(module='time',
                           names=[ast.alias(name='sleep')],
                           level=None,
                           lineno=0, col_offset=0)]
    mod = ast.Module(body)
    code = compile(mod, 'test', 'exec')
    ns = {}
    exec(code, ns)
    self.assertIn('sleep', ns)
//  [python]
def get_problem_responses(request, course_id):
    """
    Initiate generation of a CSV file containing all student answers
    to a given problem.

    Responds with JSON
        {"status": "... status message ..."}

    if initiation is successful (or generation task is already running).

    Responds with BadRequest if problem location is faulty.
    """
    course_key = CourseKey.from_string(course_id)
    problem_location = request.POST.get('problem_location', '')

    try:
        problem_key = UsageKey.from_string(problem_location)
        # Are we dealing with an "old-style" problem location?
        run = problem_key.run
        if not run:
            problem_key = course_key.make_usage_key_from_deprecated_string(problem_location)
        if problem_key.course_key != course_key:
            raise InvalidKeyError(type(problem_key), problem_key)
    except InvalidKeyError:
        return JsonResponseBadRequest(_("Could not find problem with this location."))

    try:
        instructor_task.api.submit_calculate_problem_responses_csv(request, course_key, problem_location)
        success_status = _(
            "The problem responses report is being created."
            " To view the status of the report, see Pending Tasks below."
        )
        return JsonResponse({"status": success_status})
    except AlreadyRunningError:
        already_running_status = _(
            "A problem responses report generation task is already in progress. "
            "Check the 'Pending Tasks' table for the status of the task. "
            "When completed, the report will be available for download in the table below."
        )
        return JsonResponse({"status": already_running_status})
//  [python]
def get_problem_responses(request, course_id):
    """
    Initiate generation of a CSV file containing all student answers
    to a given problem.

    Responds with JSON
        {"status": "... status message ..."}

    if initiation is successful (or generation task is already running).

    Responds with BadRequest if problem location is faulty.
    """
    course_key = CourseKey.from_string(course_id)
    problem_location = request.GET.get('problem_location', '')

    try:
        problem_key = UsageKey.from_string(problem_location)
        # Are we dealing with an "old-style" problem location?
        run = problem_key.run
        if not run:
            problem_key = course_key.make_usage_key_from_deprecated_string(problem_location)
        if problem_key.course_key != course_key:
            raise InvalidKeyError(type(problem_key), problem_key)
    except InvalidKeyError:
        return JsonResponseBadRequest(_("Could not find problem with this location."))

    try:
        instructor_task.api.submit_calculate_problem_responses_csv(request, course_key, problem_location)
        success_status = _(
            "The problem responses report is being created."
            " To view the status of the report, see Pending Tasks below."
        )
        return JsonResponse({"status": success_status})
    except AlreadyRunningError:
        already_running_status = _(
            "A problem responses report generation task is already in progress. "
            "Check the 'Pending Tasks' table for the status of the task. "
            "When completed, the report will be available for download in the table below."
        )
        return JsonResponse({"status": already_running_status})
//  [python]
def get_student_progress_url(request, course_id):
    """
    Get the progress url of a student.
    Limited to staff access.

    Takes query parameter unique_student_identifier and if the student exists
    returns e.g. {
        'progress_url': '/../...'
    }
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    user = get_student_from_identifier(request.POST.get('unique_student_identifier'))

    progress_url = reverse('student_progress', kwargs={'course_id': course_id.to_deprecated_string(), 'student_id': user.id})

    response_payload = {
        'course_id': course_id.to_deprecated_string(),
        'progress_url': progress_url,
    }
    return JsonResponse(response_payload)
//  [python]
def get_student_progress_url(request, course_id):
    """
    Get the progress url of a student.
    Limited to staff access.

    Takes query paremeter unique_student_identifier and if the student exists
    returns e.g. {
        'progress_url': '/../...'
    }
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    user = get_student_from_identifier(request.GET.get('unique_student_identifier'))

    progress_url = reverse('student_progress', kwargs={'course_id': course_id.to_deprecated_string(), 'student_id': user.id})

    response_payload = {
        'course_id': course_id.to_deprecated_string(),
        'progress_url': progress_url,
    }
    return JsonResponse(response_payload)
//  [python]
def test_invalid_identitifer(self):
    m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))], [])
    ast.fix_missing_locations(m)
    with self.assertRaises(TypeError) as cm:
        compile(m, "<test>", "exec")
    self.assertIn("identifier must be of type str", str(cm.exception))
//  [python]
def test_invalid_identitifer(self):
    m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))])
    ast.fix_missing_locations(m)
    with self.assertRaises(TypeError) as cm:
        compile(m, "<test>", "exec")
    self.assertIn("identifier must be of type str", str(cm.exception))
//  [python]
def list_course_role_members(request, course_id):
    """
    List instructors and staff.
    Requires instructor access.

    rolename is one of ['instructor', 'staff', 'beta', 'ccx_coach']

    Returns JSON of the form {
        "course_id": "some/course/id",
        "staff": [
            {
                "username": "staff1",
                "email": "staff1@example.org",
                "first_name": "Joe",
                "last_name": "Shmoe",
            }
        ]
    }
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_with_access(
        request.user, 'instructor', course_id, depth=None
    )

    rolename = request.POST.get('rolename')

    if rolename not in ROLES:
        return HttpResponseBadRequest()

    def extract_user_info(user):
        """ convert user into dicts for json view """
        return {
            'username': user.username,
            'email': user.email,
            'first_name': user.first_name,
            'last_name': user.last_name,
        }

    response_payload = {
        'course_id': course_id.to_deprecated_string(),
        rolename: map(extract_user_info, list_with_level(
            course, rolename
        )),
    }
    return JsonResponse(response_payload)
//  [python]
def list_course_role_members(request, course_id):
    """
    List instructors and staff.
    Requires instructor access.

    rolename is one of ['instructor', 'staff', 'beta', 'ccx_coach']

    Returns JSON of the form {
        "course_id": "some/course/id",
        "staff": [
            {
                "username": "staff1",
                "email": "staff1@example.org",
                "first_name": "Joe",
                "last_name": "Shmoe",
            }
        ]
    }
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_with_access(
        request.user, 'instructor', course_id, depth=None
    )

    rolename = request.GET.get('rolename')

    if rolename not in ROLES:
        return HttpResponseBadRequest()

    def extract_user_info(user):
        """ convert user into dicts for json view """
        return {
            'username': user.username,
            'email': user.email,
            'first_name': user.first_name,
            'last_name': user.last_name,
        }

    response_payload = {
        'course_id': course_id.to_deprecated_string(),
        rolename: map(extract_user_info, list_with_level(
            course, rolename
        )),
    }
    return JsonResponse(response_payload)
//  [python]
def list_instructor_tasks(request, course_id):
    """
    List instructor tasks.

    Takes optional query paremeters.
        - With no arguments, lists running tasks.
        - `problem_location_str` lists task history for problem
        - `problem_location_str` and `unique_student_identifier` lists task
            history for problem AND student (intersection)
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    problem_location_str = strip_if_string(request.POST.get('problem_location_str', False))
    student = request.POST.get('unique_student_identifier', None)
    if student is not None:
        student = get_student_from_identifier(student)

    if student and not problem_location_str:
        return HttpResponseBadRequest(
            "unique_student_identifier must accompany problem_location_str"
        )

    if problem_location_str:
        try:
            module_state_key = course_id.make_usage_key_from_deprecated_string(problem_location_str)
        except InvalidKeyError:
            return HttpResponseBadRequest()
        if student:
            # Specifying for a single student's history on this problem
            tasks = instructor_task.api.get_instructor_task_history(course_id, module_state_key, student)
        else:
            # Specifying for single problem's history
            tasks = instructor_task.api.get_instructor_task_history(course_id, module_state_key)
    else:
        # If no problem or student, just get currently running tasks
        tasks = instructor_task.api.get_running_instructor_tasks(course_id)

    response_payload = {
        'tasks': map(extract_task_features, tasks),
    }
    return JsonResponse(response_payload)
//  [python]
def list_instructor_tasks(request, course_id):
    """
    List instructor tasks.

    Takes optional query paremeters.
        - With no arguments, lists running tasks.
        - `problem_location_str` lists task history for problem
        - `problem_location_str` and `unique_student_identifier` lists task
            history for problem AND student (intersection)
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    problem_location_str = strip_if_string(request.GET.get('problem_location_str', False))
    student = request.GET.get('unique_student_identifier', None)
    if student is not None:
        student = get_student_from_identifier(student)

    if student and not problem_location_str:
        return HttpResponseBadRequest(
            "unique_student_identifier must accompany problem_location_str"
        )

    if problem_location_str:
        try:
            module_state_key = course_id.make_usage_key_from_deprecated_string(problem_location_str)
        except InvalidKeyError:
            return HttpResponseBadRequest()
        if student:
            # Specifying for a single student's history on this problem
            tasks = instructor_task.api.get_instructor_task_history(course_id, module_state_key, student)
        else:
            # Specifying for single problem's history
            tasks = instructor_task.api.get_instructor_task_history(course_id, module_state_key)
    else:
        # If no problem or student, just get currently running tasks
        tasks = instructor_task.api.get_running_instructor_tasks(course_id)

    response_payload = {
        'tasks': map(extract_task_features, tasks),
    }
    return JsonResponse(response_payload)
//  [python]
def change_due_date(request, course_id):
    """
    Grants a due date extension to a student for a particular unit.
    """
    course = get_course_by_id(SlashSeparatedCourseKey.from_deprecated_string(course_id))
    student = require_student_from_identifier(request.POST.get('student'))
    unit = find_unit(course, request.POST.get('url'))
    due_date = parse_datetime(request.POST.get('due_datetime'))
    set_due_date_extension(course, unit, student, due_date)

    return JsonResponse(_(
        'Successfully changed due date for student {0} for {1} '
        'to {2}').format(student.profile.name, _display_unit(unit),
                         due_date.strftime('%Y-%m-%d %H:%M')))
//  [python]
def change_due_date(request, course_id):
    """
    Grants a due date extension to a student for a particular unit.
    """
    course = get_course_by_id(SlashSeparatedCourseKey.from_deprecated_string(course_id))
    student = require_student_from_identifier(request.GET.get('student'))
    unit = find_unit(course, request.GET.get('url'))
    due_date = parse_datetime(request.GET.get('due_datetime'))
    set_due_date_extension(course, unit, student, due_date)

    return JsonResponse(_(
        'Successfully changed due date for student {0} for {1} '
        'to {2}').format(student.profile.name, _display_unit(unit),
                         due_date.strftime('%Y-%m-%d %H:%M')))
//  [python]
def list_entrance_exam_instructor_tasks(request, course_id):  # pylint: disable=invalid-name
    """
    List entrance exam related instructor tasks.

    Takes either of the following query parameters
        - unique_student_identifier is an email or username
        - all_students is a boolean
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_by_id(course_id)
    student = request.POST.get('unique_student_identifier', None)
    if student is not None:
        student = get_student_from_identifier(student)

    try:
        entrance_exam_key = course_id.make_usage_key_from_deprecated_string(course.entrance_exam_id)
    except InvalidKeyError:
        return HttpResponseBadRequest(_("Course has no valid entrance exam section."))
    if student:
        # Specifying for a single student's entrance exam history
        tasks = instructor_task.api.get_entrance_exam_instructor_task_history(course_id, entrance_exam_key, student)
    else:
        # Specifying for all student's entrance exam history
        tasks = instructor_task.api.get_entrance_exam_instructor_task_history(course_id, entrance_exam_key)

    response_payload = {
        'tasks': map(extract_task_features, tasks),
    }
    return JsonResponse(response_payload)
//  [python]
def list_entrance_exam_instructor_tasks(request, course_id):  # pylint: disable=invalid-name
    """
    List entrance exam related instructor tasks.

    Takes either of the following query parameters
        - unique_student_identifier is an email or username
        - all_students is a boolean
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_by_id(course_id)
    student = request.GET.get('unique_student_identifier', None)
    if student is not None:
        student = get_student_from_identifier(student)

    try:
        entrance_exam_key = course_id.make_usage_key_from_deprecated_string(course.entrance_exam_id)
    except InvalidKeyError:
        return HttpResponseBadRequest(_("Course has no valid entrance exam section."))
    if student:
        # Specifying for a single student's entrance exam history
        tasks = instructor_task.api.get_entrance_exam_instructor_task_history(course_id, entrance_exam_key, student)
    else:
        # Specifying for all student's entrance exam history
        tasks = instructor_task.api.get_entrance_exam_instructor_task_history(course_id, entrance_exam_key)

    response_payload = {
        'tasks': map(extract_task_features, tasks),
    }
    return JsonResponse(response_payload)
//  [python]
def list_forum_members(request, course_id):
    """
    Lists forum members of a certain rolename.
    Limited to staff access.

    The requesting user must be at least staff.
    Staff forum admins can access all roles EXCEPT for FORUM_ROLE_ADMINISTRATOR
        which is limited to instructors.

    Takes query parameter `rolename`.
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_by_id(course_id)
    has_instructor_access = has_access(request.user, 'instructor', course)
    has_forum_admin = has_forum_access(
        request.user, course_id, FORUM_ROLE_ADMINISTRATOR
    )

    rolename = request.POST.get('rolename')

    # default roles require either (staff & forum admin) or (instructor)
    if not (has_forum_admin or has_instructor_access):
        return HttpResponseBadRequest(
            "Operation requires staff & forum admin or instructor access"
        )

    # EXCEPT FORUM_ROLE_ADMINISTRATOR requires (instructor)
    if rolename == FORUM_ROLE_ADMINISTRATOR and not has_instructor_access:
        return HttpResponseBadRequest("Operation requires instructor access.")

    # filter out unsupported for roles
    if rolename not in [FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA]:
        return HttpResponseBadRequest(strip_tags(
            "Unrecognized rolename '{}'.".format(rolename)
        ))

    try:
        role = Role.objects.get(name=rolename, course_id=course_id)
        users = role.users.all().order_by('username')
    except Role.DoesNotExist:
        users = []

    def extract_user_info(user):
        """ Convert user to dict for json rendering. """
        return {
            'username': user.username,
            'email': user.email,
            'first_name': user.first_name,
            'last_name': user.last_name,
        }

    response_payload = {
        'course_id': course_id.to_deprecated_string(),
        rolename: map(extract_user_info, users),
    }
    return JsonResponse(response_payload)
//  [python]
def list_forum_members(request, course_id):
    """
    Lists forum members of a certain rolename.
    Limited to staff access.

    The requesting user must be at least staff.
    Staff forum admins can access all roles EXCEPT for FORUM_ROLE_ADMINISTRATOR
        which is limited to instructors.

    Takes query parameter `rolename`.
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_by_id(course_id)
    has_instructor_access = has_access(request.user, 'instructor', course)
    has_forum_admin = has_forum_access(
        request.user, course_id, FORUM_ROLE_ADMINISTRATOR
    )

    rolename = request.GET.get('rolename')

    # default roles require either (staff & forum admin) or (instructor)
    if not (has_forum_admin or has_instructor_access):
        return HttpResponseBadRequest(
            "Operation requires staff & forum admin or instructor access"
        )

    # EXCEPT FORUM_ROLE_ADMINISTRATOR requires (instructor)
    if rolename == FORUM_ROLE_ADMINISTRATOR and not has_instructor_access:
        return HttpResponseBadRequest("Operation requires instructor access.")

    # filter out unsupported for roles
    if rolename not in [FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA]:
        return HttpResponseBadRequest(strip_tags(
            "Unrecognized rolename '{}'.".format(rolename)
        ))

    try:
        role = Role.objects.get(name=rolename, course_id=course_id)
        users = role.users.all().order_by('username')
    except Role.DoesNotExist:
        users = []

    def extract_user_info(user):
        """ Convert user to dict for json rendering. """
        return {
            'username': user.username,
            'email': user.email,
            'first_name': user.first_name,
            'last_name': user.last_name,
        }

    response_payload = {
        'course_id': course_id.to_deprecated_string(),
        rolename: map(extract_user_info, users),
    }
    return JsonResponse(response_payload)
//  [python]
def require_post_params(*args, **kwargs):
    """
    Checks for required parameters or renders a 400 error.
    (decorator with arguments)

    `args` is a *list of required POST parameter names.
    `kwargs` is a **dict of required POST parameter names
        to string explanations of the parameter
    """
    required_params = []
    required_params += [(arg, None) for arg in args]
    required_params += [(key, kwargs[key]) for key in kwargs]
    # required_params = e.g. [('action', 'enroll or unenroll'), ['emails', None]]

    def decorator(func):  # pylint: disable=missing-docstring
        def wrapped(*args, **kwargs):  # pylint: disable=missing-docstring
            request = args[0]

            error_response_data = {
                'error': 'Missing required query parameter(s)',
                'parameters': [],
                'info': {},
            }

            for (param, extra) in required_params:
                default = object()
                if request.POST.get(param, default) == default:
                    error_response_data['parameters'].append(param)
                    error_response_data['info'][param] = extra

            if len(error_response_data['parameters']) > 0:
                return JsonResponse(error_response_data, status=400)
            else:
                return func(*args, **kwargs)
        return wrapped
    return decorator
//  [python]
def require_post_params(*args, **kwargs):
    """
    Checks for required parameters or renders a 400 error.
    (decorator with arguments)

    Functions like 'require_query_params', but checks for
    POST parameters rather than GET parameters.
    """
    required_params = []
    required_params += [(arg, None) for arg in args]
    required_params += [(key, kwargs[key]) for key in kwargs]
    # required_params = e.g. [('action', 'enroll or unenroll'), ['emails', None]]

    def decorator(func):  # pylint: disable=missing-docstring
        def wrapped(*args, **kwargs):  # pylint: disable=missing-docstring
            request = args[0]

            error_response_data = {
                'error': 'Missing required query parameter(s)',
                'parameters': [],
                'info': {},
            }

            for (param, extra) in required_params:
                default = object()
                if request.POST.get(param, default) == default:
                    error_response_data['parameters'].append(param)
                    error_response_data['info'][param] = extra

            if len(error_response_data['parameters']) > 0:
                return JsonResponse(error_response_data, status=400)
            else:
                return func(*args, **kwargs)
        return wrapped
    return decorator
//  [python]
def modify_access(request, course_id):
    """
    Modify staff/instructor access of other user.
    Requires instructor access.

    NOTE: instructors cannot remove their own instructor access.

    Query parameters:
    unique_student_identifer is the target user's username or email
    rolename is one of ['instructor', 'staff', 'beta', 'ccx_coach']
    action is one of ['allow', 'revoke']
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_with_access(
        request.user, 'instructor', course_id, depth=None
    )
    try:
        user = get_student_from_identifier(request.POST.get('unique_student_identifier'))
    except User.DoesNotExist:
        response_payload = {
            'unique_student_identifier': request.POST.get('unique_student_identifier'),
            'userDoesNotExist': True,
        }
        return JsonResponse(response_payload)

    # Check that user is active, because add_users
    # in common/djangoapps/student/roles.py fails
    # silently when we try to add an inactive user.
    if not user.is_active:
        response_payload = {
            'unique_student_identifier': user.username,
            'inactiveUser': True,
        }
        return JsonResponse(response_payload)

    rolename = request.POST.get('rolename')
    action = request.POST.get('action')

    if rolename not in ROLES:
        error = strip_tags("unknown rolename '{}'".format(rolename))
        log.error(error)
        return HttpResponseBadRequest(error)

    # disallow instructors from removing their own instructor access.
    if rolename == 'instructor' and user == request.user and action != 'allow':
        response_payload = {
            'unique_student_identifier': user.username,
            'rolename': rolename,
            'action': action,
            'removingSelfAsInstructor': True,
        }
        return JsonResponse(response_payload)

    if action == 'allow':
        allow_access(course, user, rolename)
    elif action == 'revoke':
        revoke_access(course, user, rolename)
    else:
        return HttpResponseBadRequest(strip_tags(
            "unrecognized action '{}'".format(action)
        ))

    response_payload = {
        'unique_student_identifier': user.username,
        'rolename': rolename,
        'action': action,
        'success': 'yes',
    }
    return JsonResponse(response_payload)
//  [python]
def modify_access(request, course_id):
    """
    Modify staff/instructor access of other user.
    Requires instructor access.

    NOTE: instructors cannot remove their own instructor access.

    Query parameters:
    unique_student_identifer is the target user's username or email
    rolename is one of ['instructor', 'staff', 'beta', 'ccx_coach']
    action is one of ['allow', 'revoke']
    """
    course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_with_access(
        request.user, 'instructor', course_id, depth=None
    )
    try:
        user = get_student_from_identifier(request.GET.get('unique_student_identifier'))
    except User.DoesNotExist:
        response_payload = {
            'unique_student_identifier': request.GET.get('unique_student_identifier'),
            'userDoesNotExist': True,
        }
        return JsonResponse(response_payload)

    # Check that user is active, because add_users
    # in common/djangoapps/student/roles.py fails
    # silently when we try to add an inactive user.
    if not user.is_active:
        response_payload = {
            'unique_student_identifier': user.username,
            'inactiveUser': True,
        }
        return JsonResponse(response_payload)

    rolename = request.GET.get('rolename')
    action = request.GET.get('action')

    if rolename not in ROLES:
        error = strip_tags("unknown rolename '{}'".format(rolename))
        log.error(error)
        return HttpResponseBadRequest(error)

    # disallow instructors from removing their own instructor access.
    if rolename == 'instructor' and user == request.user and action != 'allow':
        response_payload = {
            'unique_student_identifier': user.username,
            'rolename': rolename,
            'action': action,
            'removingSelfAsInstructor': True,
        }
        return JsonResponse(response_payload)

    if action == 'allow':
        allow_access(course, user, rolename)
    elif action == 'revoke':
        revoke_access(course, user, rolename)
    else:
        return HttpResponseBadRequest(strip_tags(
            "unrecognized action '{}'".format(action)
        ))

    response_payload = {
        'unique_student_identifier': user.username,
        'rolename': rolename,
        'action': action,
        'success': 'yes',
    }
    return JsonResponse(response_payload)