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

cr doesn't work on Python 3.13? #19

Open
ehmatthes opened this issue Dec 19, 2024 · 2 comments
Open

cr doesn't work on Python 3.13? #19

ehmatthes opened this issue Dec 19, 2024 · 2 comments

Comments

@ehmatthes
Copy link

I was trying out CodeRed for the first time, and getting inconsistent behavior between several projects I was deploying. I finally narrowed it down to cr working in 3.12 environments, and failing in 3.13 environments.

This is on macOS, using zsh, but I think this is an issue relating to the use of urlopen() in cr/api.py. In 3.13, urlopen() no longer accepts a cafile argument. I got it to work by modifying request_json() in api.py, but it's just a demo; I'm not sure the best place to put that change, or if I used the best approach.

Reproduction

Here's a 3.12 session that makes a cr check call against a fake project, returning the expected Error: No Webapp matches the given query. A 3.13 session with the same call generates an API error.

~/test_code/cr_cli_test$ export CR_TOKEN=<redacted>
~/test_code/cr_cli_test$ python -V
Python 3.12.8
~/test_code/cr_cli_test$ uv venv .venv && source .venv/bin/activate      
Using Python 3.12.8 interpreter at: /Users/eric/.pyenv/versions/3.12.8/bin/python3.12
...
(cr_cli_test) ~/test_code/cr_cli_test$ uv pip install cr
...
 + cr==1.7
...
(cr_cli_test) ~/test_code/cr_cli_test$ cr check fake-project
Error: No Webapp matches the given query.
(cr_cli_test) ~/test_code/cr_cli_test$ deactivate
~/test_code/cr_cli_test$ rm -rf .venv
~/test_code/cr_cli_test$ pyenv local 3.13.1
~/test_code/cr_cli_test$ uv venv .venv && source .venv/bin/activate
Using Python 3.13.1 interpreter at: /Users/eric/.pyenv/versions/3.13.1/bin/python3.13
...
(cr_cli_test) ~/test_code/cr_cli_test$ uv pip install cr
...
 + cr==1.7
...
(cr_cli_test) ~/test_code/cr_cli_test$ cr check fake-project
Error: Error contacting CodeRed API. Please try again shortly.
(cr_cli_test) ~/test_code/cr_cli_test$ cr check fake-project --debug
INFO     Read config files: []                                                                                                                                                                config.py:36
DEBUG    Config `token`: `<redacted>`                                                                                                                                   config.py:67
ERROR    Fatal: Error contacting CodeRed API. Please try again shortly.                                                                                                                         cli.py:680
         ╭──────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ─────────────────────────────────────────────────────────────────────────╮           
         │ /Users/eric/test_code/cr_cli_test/.venv/lib/python3.13/site-packages/cr/api.py:457 in coderedapi                                                                                   │           
         │                                                                                                                                                                                    │           
         │   454 │   """
         │   455 │   endpoint = endpoint.lstrip("/")                                                                                                                                          │           
         │   456 │   try:                                                                                                                                                                     │           
         │ ❱ 457 │   │   code, d = request_json(                                                                                                                                              │           
         │   458 │   │   │   f"https://app.codered.cloud/{endpoint}",                                                                                                                         │           
         │   459 │   │   │   method=method,                                                                                                                                                   │           
         │   460 │   │   │   headers={                                                                                                                                                        │           
         │                                                                                                                                                                                    │           
         │ /Users/eric/test_code/cr_cli_test/.venv/lib/python3.13/site-packages/cr/api.py:429 in request_json                                                                                 │           
         │                                                                                                                                                                                    │           
         │   426 │                                                                                                                                                                            │           
         │   427 │   # Open the request and read the response.                                                                                                                                │           
         │   428 │   try:                                                                                                                                                                     │           
         │ ❱ 429 │   │   r = urlopen(req, timeout=timeout, cafile=certifi.where())                                                                                                            │           
         │   430 │   │   d = _response_to_json(r)                                                                                                                                             │           
         │   431 │   │   code = r.code                                                                                                                                                        │           
         │   432 │   │   LOGGER.info("%s %s %d", method, url, code)                                                                                                                           │           
         ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯           
         TypeError: urlopen() got an unexpected keyword argument 'cafile'
@ehmatthes
Copy link
Author

ehmatthes commented Dec 19, 2024

Demo fix

In the 3.13 venv, I made the following change in api.py and the cr check call worked.

def request_json(
    ...
) -> Tuple[int, dict]:
    """
    Makes an HTTP request and parses the JSON response.
    """
    ...
    # Open the request and read the response.


    import ssl
    context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    context.load_verify_locations(cafile=certifi.where())
    try:
        r = urlopen(req, timeout=timeout, context=context)


        ...
    except HTTPError as err:
        ...

    return (code, d)
(cr_cli_test) ~/test_code/cr_cli_test$ python -V
Python 3.13.1
(cr_cli_test) ~/test_code/cr_cli_test$ cr check fake-project
Error: No Webapp matches the given query.

@vsalvino
Copy link
Contributor

vsalvino commented Dec 19, 2024

Thanks Eric! We haven't tested cr in 3.13, which was a bit of a miss because we did just add 3.13 support on CodeRed Cloud a few weeks go. Anecdotally most people seem to be using the binary in their day-to-day (myself included). Thanks for the fix, I'll get this merged in.

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

No branches or pull requests

2 participants