Skip to content

Commit

Permalink
Fix Windows extension build
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Nov 3, 2023
1 parent 7c3a93b commit 44f8535
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ jobs:

strategy:
matrix:
# We don't test on Windows currently due to issues
# build libmaxminddb there.
platform: [macos-latest, ubuntu-latest]
platform: [macos-latest, ubuntu-latest, windows-latest]
python-version: [3.8, 3.9, "3.10", 3.11, 3.12]

name: Python ${{ matrix.python-version }} on ${{ matrix.platform }}
Expand Down
28 changes: 22 additions & 6 deletions extension/maxminddb.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#ifdef MS_WINDOWS
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <maxminddb.h>
#include <netinet/in.h>
#include <structmember.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
#include <maxminddb.h>
#include <stdbool.h>
#include <structmember.h>

#define __STDC_FORMAT_MACROS
#include <inttypes.h>
Expand Down Expand Up @@ -53,6 +59,7 @@ typedef struct {
} Metadata_obj;
// clang-format on

static bool can_read(const char *path);
static int get_record(PyObject *self, PyObject *args, PyObject **record);
static bool format_sockaddr(struct sockaddr *addr, char *dst);
static PyObject *from_entry_data_list(MMDB_entry_data_list_s **entry_data_list);
Expand Down Expand Up @@ -97,8 +104,7 @@ static int Reader_init(PyObject *self, PyObject *args, PyObject *kwds) {
return -1;
}

if (0 != access(filename, R_OK)) {

if (!can_read(filename)) {
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath);
Py_XDECREF(filepath);
return -1;
Expand Down Expand Up @@ -737,7 +743,7 @@ static PyObject *from_map(MMDB_entry_data_list_s **entry_data_list) {

const uint32_t map_size = (*entry_data_list)->entry_data.data_size;

uint i;
uint32_t i;
// entry_data_list cannot start out NULL (see from_entry_data_list). We
// check it in the loop because it may become NULL.
// coverity[check_after_deref]
Expand Down Expand Up @@ -778,7 +784,7 @@ static PyObject *from_array(MMDB_entry_data_list_s **entry_data_list) {
return NULL;
}

uint i;
uint32_t i;
// entry_data_list cannot start out NULL (see from_entry_data_list). We
// check it in the loop because it may become NULL.
// coverity[check_after_deref]
Expand Down Expand Up @@ -826,6 +832,16 @@ static PyObject *from_uint128(const MMDB_entry_data_list_s *entry_data_list) {
return py_obj;
}

static bool can_read(const char *path) {
#ifdef MS_WINDOWS
int rv = _access(path, 04);
#else
int rv = access(path, R_OK);
#endif

return rv == 0;
}

static PyMethodDef Reader_methods[] = {
{"get",
Reader_get,
Expand Down

0 comments on commit 44f8535

Please sign in to comment.