niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.46k stars 531 forks source link

popen uci #690

Closed ChrisWhittington closed 4 years ago

ChrisWhittington commented 4 years ago

Hi, I have a uci compliant (well, hopefully) engine that won't behave with popen_uci at initialisation.

Logger shows connection made << uci

but no return of engine id, author etc.

the popen_uci code works fine with other test engines and my engine works fine with, for example, cutechess ui.

Is there a possibly that Python-Chess doesn't like file handling operations between sending "uci" and getting back id and readyok etc?

Reason I ask, is that when my own 'dump all uci comms' to HD tool was turned on to try and track this down, its file handler failed when trying to open a file for "w".

ChrisWhittington commented 4 years ago

Narrowed it down to this code, popen_uci doesn't seem to like file operations, as below:

//
void save_block(char* filename, void* d_ptr, int count)
{
    FILE *fp;

    fp = fopen(filename, "wb");  // w for write, b for binary
    fwrite(d_ptr, 1, count, fp);
    fclose(fp);
    return;
}

// returns 1=success
int load_block(char* filename, void* d_ptr, int count, int verbose)
{
    FILE *fp;

    fp = fopen(filename, "rb");  // r for read, b for binary

    if (fp == NULL)
    {
        return (2);
    }

    size_t result;
    result = fread(d_ptr, 1, count, fp);
    fclose(fp);

    if (result != count)
    {
        if (verbose > 0) printf("\nCounts: %zd %d\n", result, count);
        return (3);
    }
    return (1);
}
niklasf commented 4 years ago

popen_uci does not interfere with file operations (or anything the engine does). Does your code depend on being run in a particular working directory? If so, you can use keyword arguments from https://docs.python.org/3/library/subprocess.html#subprocess.Popen like cwd.

ChrisWhittington commented 4 years ago

Yes, it's assuming the working directory is unchanged. And tries to read from "chess-data/B-magic.bin". If the working directory was changed, then the "chess-data" folder won't exist for it, then undefined behaviour follows ... I wasn't expecting working directory to change, is that what's happening, or am I misreading?

niklasf commented 4 years ago

The working directory does not change, but (as always) the default is the working directory of the parent process. Here the directory where python is invoked.