C Extension Seg Faults
I'm having trouble tracking down some segmentation faults with a C
extension. I can see where it's failing but don't know how to fix
it. Basically, my python programme builds a pair of large tuples of
tuples. I want to unpack these tuples and process them. The code in
question looks something like:
... snip ...
SizeN = PyTuple_Size(PlacementN);
SizeP = PyTuple_Size(PlacementP);
for(ixN = 0; ixN < SizeN; ixN++) {
LenN = (int) PyTuple_Size(PyList_GetItem(PlacementN, ixN));
for(Count = 0; Count < LenN; Count++)
*(GatesN+Count) = (int)
PyInt_AsLong(PyTuple_GetItem(PyTuple_GetItem(PyList_GetItem(PlacementN,
ixN), Count), GATE));
for(ixP = 0; ixP < SizeP; ixP++) {
printf("I feel lucky (%d)...", ixP); fflush(stdout);
LenP = (int) PyTuple_Size(PyList_GetItem(PlacementP,
ixP));
printf("Ta-Da!\n"); fflush(stdout);
for(Count = 0; Count < LenP; Count++)
*(GatesP+Count) = (int)
PyInt_AsLong(PyTuple_GetItem(PyTuple_GetItem(PyList_GetItem(PlacementP,
ixP), Count), GATE));
[ MUCH PROCESSING ]
... snip ...
The inner loop runs a few hundred times (263) then fails with a
segmentation fault. I've trolled through the documentation online and
in the various .h files but can't seem to find anything.
Anybody have an idea what really obvious foolish thing I'm doing?
Thanks,
-Daryl
--
http://mail.python.org/mailman/listinfo/python-list
Re: C Extension Seg Faults
> It's not worth staring at code to try to figure out things like that. > Run it under gdb, examine the relevant data objects when it crashes > and see what's broken (e.g. some pointer is unexpectedly null), then > set a conditional breakpoint that stops execution when that incorrect > condition happens (e.g. the pointer gets set to null), run the program > til the breakpoint triggers, see what other condition X caused the > condition that caused the crash, set another breakpoint that triggers > when X occurs, etc. Work backwards systematically til you find the bug. Yeah, not 30 seconds after I posted it I found the problem. Under some (funny) conditions the extension would be called with a list instead of a tuple. My debug case (of course) worked fine but the full test would fail. Sorry for the noise, -Daryl -- http://mail.python.org/mailman/listinfo/python-list
Limits on search length
I am trying to locate all lines in a suite of files with quoted strings of
particular lengths. A search pattern like r'".{15}"' finds 15-character
strings very nicely. But I have some very long ones, and a pattern like
r'".{272}"' fails miserably, even though I know I have at least one
272-character string.
In the short term, I can resort to locating the character positions of the
quotes, but this seemed like such an elegant solution I hate to see it not
work. The program is given below (sans imports), in case someone can spot
something I'm overlooking:
# Example usage: search.py *.txt \".{15}\"
filePattern = sys.argv[1]
searchPattern = sys.argv[2]
cpat = re.compile(searchPattern)
for fn in glob.glob(filePattern):
f = open(fn, "r")
if f:
lineNumber = 0
for line in f:
lineNumber += 1
m = cpat.search(line)
if m is not None:
print fn, "(", lineNumber, ")", line
f.close()
--
Daryl Lee
Open the Present -- it's a Gift!
--
http://mail.python.org/mailman/listinfo/python-list
Deep comparison of sets?
The second assertion in the following code fails:
class Value(object):
def __init__(self, value):
super(Value, self).__init__()
self.value = value
def __cmp__(self, other):
if self.value > other.value: return 1
if self.value < other.value: return -1
return 0
if __name__ == "__main__":
v1 = Value('one')
v2 = Value('one')
assert v1 == v2
s1 = set([v1])
s2 = set([v2])
assert s1 == s2
Is there any way to compare the two sets so that __cmp__ is called (I
guess this would be called a deep comparison) rather than just
(shallowly) comparing each object in the set?
--
Daryl Spitzer
--
http://mail.python.org/mailman/listinfo/python-list
