kjnh10 / pcm

Command line tool for programming contests
MIT License
6 stars 0 forks source link

UnboundLocalError: local variable 'problem_title' referenced before assignment #51

Closed vpeopleonatank closed 3 years ago

vpeopleonatank commented 3 years ago

I use pcm ss to listen for Competitive Companion(CC) (example contest https://codeforces.com/contest/1523) , but error show as tittle when pcm parse from CC.

This is full error message from cli ``` stderr: node:child_process:333 ex = new Error('Command failed: ' + cmd + '\n' + stderr); ^ Error: Command failed: pwd && pcm ppp https://codeforces.com/contest/1523/problem/F Traceback (most recent call last): File "/home/vpoat/.local/bin/pcm", line 11, in load_entry_point('pcm', 'console_scripts', 'pcm')() File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 829, in __call__ return self.main(*args, **kwargs) File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 782, in main rv = self.invoke(ctx) File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 1259, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 1066, in invoke return ctx.invoke(self.callback, **ctx.params) File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 610, in invoke return callback(*args, **kwargs) File "/home/vpoat/.local/lib/python3.8/site-packages/click/decorators.py", line 73, in new_func return ctx.invoke(f, obj, *args, **kwargs) File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 610, in invoke return callback(*args, **kwargs) File "/home/vpoat/git/pcm/pcm/__main__.py", line 159, in ppp work_dir = _prepare_problem(config, task_url, prob_name, force) File "/home/vpoat/git/pcm/pcm/__main__.py", line 202, in _prepare_problem if problem_title: UnboundLocalError: local variable 'problem_title' referenced before assignment at ChildProcess.exithandler (node:child_process:333:12) at ChildProcess.emit (node:events:376:20) at maybeClose (node:internal/child_process:1063:16) at Process.ChildProcess._handle.onexit (node:internal/child_process:295:5) { killed: false, code: 1, signal: null, cmd: 'pwd && pcm ppp https://codeforces.com/contest/1523/problem/F', stdout: '/home/vpoat/procon-work/Codeforces\n' + '[INFO] online-judge-tools 11.3.0 (+ online-judge-api-client 10.9.0)\n' + '[INFO] load cookie from: /home/vpoat/.local/share/online-judge-tools/cookie.jar\n' + '[NETWORK] GET: https://codeforces.com/contest/1523/problem/F\n' + '[NETWORK] 200 OK\n' + '[INFO] save cookie to: /home/vpoat/.local/share/online-judge-tools/cookie.jar\n' + '[INFO] append the downloading history: /home/vpoat/.cache/online-judge-tools/download-history.jsonl\n' + '\n' + '[INFO] sample 0\n' + '[INFO] input: sample-1\n' + '3 4\n' + '1 1\n' + '2 3\n' + '5 2\n' + '2 2 12\n' + '5 1 4\n' + '6 2 11\n' + '3 5 10\n' + '\n' + '[SUCCESS] saved to: test/sample-1.in\n' + '[INFO] output: sample-1\n' + '3\n' + '\n' + '[SUCCESS] saved to: test/sample-1.out\n', stderr: 'Traceback (most recent call last):\n' + ' File "/home/vpoat/.local/bin/pcm", line 11, in \n' + " load_entry_point('pcm', 'console_scripts', 'pcm')()\n" + ' File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 829, in __call__\n' + ' return self.main(*args, **kwargs)\n' + ' File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 782, in main\n' + ' rv = self.invoke(ctx)\n' + ' File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 1259, in invoke\n' + ' return _process_result(sub_ctx.command.invoke(sub_ctx))\n' + ' File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 1066, in invoke\n' + ' return ctx.invoke(self.callback, **ctx.params)\n' + ' File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 610, in invoke\n' + ' return callback(*args, **kwargs)\n' + ' File "/home/vpoat/.local/lib/python3.8/site-packages/click/decorators.py", line 73, in new_func\n' + ' return ctx.invoke(f, obj, *args, **kwargs)\n' + ' File "/home/vpoat/.local/lib/python3.8/site-packages/click/core.py", line 610, in invoke\n' + ' return callback(*args, **kwargs)\n' + ' File "/home/vpoat/git/pcm/pcm/__main__.py", line 159, in ppp\n' + ' work_dir = _prepare_problem(config, task_url, prob_name, force)\n' + ' File "/home/vpoat/git/pcm/pcm/__main__.py", line 202, in _prepare_problem\n' + ' if problem_title:\n' + "UnboundLocalError: local variable 'problem_title' referenced before assignment\n" } ```
kjnh10 commented 3 years ago

could you try the command below? pcm ppp https://codeforces.com/contest/1523/problem/F

By reading through the error, it seems that the error is not from 'pcm ss' but from 'pcm ppp'.

kjnh10 commented 3 years ago

At least, in my environment, both pcm ppp and pcm ss works fine.

vpeopleonatank commented 3 years ago

pcm ppp and pcm pp works for me, only pcm ss has problem. What is your nodejs version? Mine is 15.5.0

kjnh10 commented 3 years ago

my node version is v8.10.0

kjnh10 commented 3 years ago

can you reproduce this error? I guess that this was a one-time error.

And also happen for other problems?

kjnh10 commented 3 years ago

I think your error happened because the part below failed at that time due to a network error or something.

        try:
            problem_data = problem.download_data()
            problem_title = problem_data.name

note that this part is in the function below.

def ppp(config: Config, task_url: str, current_dir: bool, prob_name: str, force: bool, execute_hook: bool) -> None:
    if current_dir:
        config.pref['problem_root_dir'] = '.'
    work_dir = _prepare_problem(config, task_url, prob_name, force)

    # execute custom_hook_command
    if not execute_hook: return
    try:
        cstr = config.pref['ppp']['custom_hook_command']['after'].format(dirname=work_dir.resolve())
        print(cstr)
        subprocess.run(cstr, shell=True)
    except Exception as e:
        if config.verbose:
            print(e)

def _prepare_problem(config: Config, task_url: str, prob_name: str = '', force: bool = False, from_pp: bool = False) -> Path:
    problem_dir: Path
    problem_title: str
    if task_url != '':
        problem: Problem = cast(Problem, onlinejudge.dispatch.problem_from_url(task_url))
        assert problem != None
        problem_dir = get_work_directory(config, problem, from_pp=from_pp)
        try:
            problem_data = problem.download_data()
            problem_title = problem_data.name
        except:
            pass
    else:
        problem_dir = Path('./prob').resolve()

    if prob_name:
        problem_dir = problem_dir.parent / prob_name

    if Path(problem_dir).exists():
        if force:
            shutil.rmtree(problem_dir)
        else:
            print(f'{problem_dir} directory already exists')
            return problem_dir

    problem_dir.parent.mkdir(parents=True, exist_ok=True)
    shutil.copytree(config.pref['template_dir'], f'{problem_dir}/')
    # download sample cases
    if task_url:
        _download_sample(task_url, problem_dir)
        if problem_title:
            with open(problem_dir / problem_title, "w"):
                pass
    return problem_dir

anyway, I should modify to initialize 'problem_title' at first.

vpeopleonatank commented 3 years ago

can you reproduce this error? I guess that this was a one-time error. And also happen for other problems?

This error often happens when I use pcm ss. pcm pp and pcm ppp always work without error

kjnh10 commented 3 years ago

pcm ss internally call pcm ppp, and your error message indicates that pcm ppp failed by "UnboundLocalError". So I think this is not from your node version as long as pcm ppp is called. pcm ppp is a pure python code and is not depending on nodejs.

As mentioned, "UnboundLocalError" happened because the try section of the __prepare_problem failed. when the try section fails, 'problem title' is not initialized, and in the last part of __prepare_problem the variable is referenced and "UnboundLocalError" happens. So I can fix the error by initializing the variable at first.

def _prepare_problem(config: Config, task_url: str, prob_name: str = '', force: bool = False, from_pp: bool = False) -> Path:
    problem_dir: Path
    problem_title: str
    if task_url != '':
        problem: Problem = cast(Problem, onlinejudge.dispatch.problem_from_url(task_url))
        assert problem != None
        problem_dir = get_work_directory(config, problem, from_pp=from_pp)
        try:
            problem_data = problem.download_data()
            problem_title = problem_data.name
        except:
            pass
    else:
        problem_dir = Path('./prob').resolve()

But I don't know why the try section fails only when using pcm ss in your environment.

kjnh10 commented 3 years ago

Could you try reinstalling via developer-mode? if you can, we can investigate more.

# uninstalling the current version.
pip uninstall pcm

# install for developing
git clone https://github.com/kjnh10/pcm.git
cd pcm
pip install --editable ./
kjnh10 commented 3 years ago

and check out the branch and test pcm ss and let's see what happens

git checkout investigate-#51
vpeopleonatank commented 3 years ago

and check out the branch and test pcm ss and let's see what happens

git checkout investigate-#51

I get SyntaxError when run pcm ss image

kjnh10 commented 3 years ago

sorry, i inserted tiny bug... could you pull the new commit i have just pushed? git pull origin investigate-\#51

vpeopleonatank commented 3 years ago

sorry, i inserted tiny bug... could you pull the new commit i have just pushed? git pull origin investigate-\#51

When I use pcm ss now, It sometimes shows error like this image

kjnh10 commented 3 years ago

is this because the contest was running and the server was busy? 'sometimes' means pcm ss succeeded sometimes?

vpeopleonatank commented 3 years ago

Contest which I fetch was over. pcm ss sometimes runs without error.

kjnh10 commented 3 years ago

I meant that when you fetch, another contest was running and the codeforce's server was busy.

Anyway, for the 503 Server error, I don't have anything to do. But by the fix I did, even though 503 server error for getting problem title, preparing problem directory became to succeed.

I will include this fix in the next release which will be in a week.

Is this enough for you?

vpeopleonatank commented 3 years ago

That 's enough for me. Thanks for your work.

kjnh10 commented 3 years ago

v0.8.1 has been released, which includes this fix.

kjnh10 commented 3 years ago

if you still have an issue with v0.8.1, please reopen this issue.