Skip to content

Commit

Permalink
Fixes gameiter segfault (#8)
Browse files Browse the repository at this point in the history
* wip: investigates segfaults

* fixes seg faults

- fixes seg faults when a NP is the last event of a game
- note there is evidence the code is skipping events
  around subs, which needs further investigation

* updates csv output

* fixes test to download file before accessing
  • Loading branch information
bdilday committed Jun 10, 2020
1 parent 61d4a7b commit a939b1f
Show file tree
Hide file tree
Showing 11 changed files with 603 additions and 320 deletions.
28 changes: 27 additions & 1 deletion pychadwick/chadwick.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ctypes
from ctypes import (
POINTER,
c_char_p,
c_char,
c_int,
pointer,
Expand All @@ -12,6 +13,7 @@

import pandas as pd

from .league import CWLeague
from .game import CWGame
from .gameiter import CWGameIterator
from .roster import CWRoster
Expand Down Expand Up @@ -104,7 +106,6 @@ def cw_gameiter_next(self, gameiter_ptr):
func.argtypes = (POINTER(CWGameIterator),)
return func(gameiter_ptr)


def _download_to_tempfile(self, url):
fh = tempfile.NamedTemporaryFile(delete=False)
fh.write(requests.get(url).content)
Expand All @@ -117,6 +118,7 @@ def games(self, file_path):
cw_game_read = self.libchadwick.cw_game_read
cw_game_read.restype = POINTER(CWGame)
cw_game_read.argtypes = (ctypes.c_void_p,)

cw_file_find_first_game = self.libchadwick.cw_file_find_first_game
cw_file_find_first_game.restype = ctypes.c_int
cw_file_find_first_game.argtypes = (ctypes.c_void_p,)
Expand Down Expand Up @@ -155,6 +157,30 @@ def listicize_event_string(event_bytes):
for event_item in event_bytes.decode().split(",")
]

def cw_league_read(self, file_name):
cw_league_init_read_file = self.libchadwick.cw_league_init_read_file
cw_league_init_read_file.argtypes = (POINTER(CWLeague), c_char_p)
cw_league_init_read_file.restype = None
league = pointer(CWLeague())
cw_league_init_read_file(league, file_name)
return league

def process_game_csv(self, game_ptr, roster_visitor=None, roster_home=None):
cwevent_process_game = self.libchadwick.cwevent_process_game
cwevent_process_game.argtypes = (
POINTER(CWGame),
POINTER(CWRoster),
POINTER(CWRoster),
)
cwevent_process_game.restype = None

if not roster_home:
roster_home = pointer(CWRoster())
if not roster_visitor:
roster_visitor = pointer(CWRoster())

cwevent_process_game(game_ptr, roster_visitor, roster_home)

def process_game(self, game_ptr, roster_visitor=None, roster_home=None):
cwevent_process_game_record = self.libchadwick.cwevent_process_game_record
cwevent_process_game_record.argtypes = (
Expand Down
1 change: 1 addition & 0 deletions pychadwick/libutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .cwlib import _ChadwickLibrary

ChadwickLibrary = _ChadwickLibrary()
6 changes: 4 additions & 2 deletions pychadwick/libutils/cwlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

class _ChadwickLibrary:
library_path = (
pathlib.Path(__file__).absolute().parent.parent / "lib" / "cwevent" / "libcwevent.so"
pathlib.Path(__file__).absolute().parent.parent
/ "lib"
/ "cwevent"
/ "libcwevent.so"
)

def __init__(self):
Expand All @@ -15,4 +18,3 @@ def libchadwick(self):
if self._dll is None:
self._dll = ctypes.cdll.LoadLibrary(self.library_path)
return self._dll

29 changes: 12 additions & 17 deletions pychadwick/pych.py → scripts/pych.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
create_string_buffer,
)

from pychadwick import CWGame
from pychadwick import CWRoster
from pychadwick import CWLeague
from pychadwick.chadwick import Chadwick
from pychadwick.game import CWGame
from pychadwick.roster import CWRoster
from pychadwick.league import CWLeague


def read_rosters():
lib_path = (
"/home/bdilday/.venvs/pychadwick/lib/python3.7/"
"site-packages/pychadwick-0.1.0-py3.7-linux-x86_64.egg/"
"pychadwick/build/cwevent/libcwevent.so"
)
dll = ctypes.cdll.LoadLibrary(lib_path)
chadwick = Chadwick()
dll = chadwick.libchadwick

filename = b"/home/bdilday/github/chadwickbureau/retrosheet/event/regular/TEAM1961"
f = dll.cw_league_read_file
f.argtypes = (POINTER(CWLeague), c_char_p)
Expand All @@ -42,12 +40,9 @@ def read_rosters():


def make_game():
lib_path = (
"/home/bdilday/.venvs/pychadwick/lib/python3.7/"
"site-packages/pychadwick-0.1.0-py3.7-linux-x86_64.egg/"
"pychadwick/build/cwevent/libcwevent.so"
)
dll = ctypes.cdll.LoadLibrary(lib_path)
chadwick = Chadwick()
dll = chadwick.libchadwick


cw_game_create = dll.cw_game_create
cw_game_create.restype = POINTER(CWGame)
Expand All @@ -58,8 +53,8 @@ def make_game():


def main():
# p = read_rosters()
p = make_game()
p = read_rosters()
#p = make_game()
print(p)


Expand Down
21 changes: 21 additions & 0 deletions src/pychadwicklib/cwlib/book.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,27 @@ cw_scorebook_remove_game(CWScorebook *scorebook, char *game_id)
return NULL;
}

static int
cw_scorebook_strip_comments(FILE *file)
{
while (1) {
char buf[256], *tok, *com;
if (fgets(buf, 256, file) == NULL) {
return 0;
}

tok = cw_strtok(buf);
com = cw_strtok(NULL);

if (tok && !strcmp(tok, "com") && com) {
// strip it b moving the FILE pointer past it
}
else {
return 1;
}
}
}

static int
cw_scorebook_read_comments(CWScorebook *scorebook, FILE *file)
{
Expand Down
Loading

0 comments on commit a939b1f

Please sign in to comment.