472,325 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 software developers and data experts.

Problems with adding a new built-in data type

Evening all,

I'm trying to add a new built-in number data type to Python with its own
syntax, so I'm working directly with the interpreter rather than
creating my own extension module (side note: I've appended something
extra to the version thing in the Makefile - I doubt this is relevant to
the problem but it's probably best you have all the info). The complex
data type is similar to what I'm trying to do so I've been following
that as an example. I've successfully extended the tokenizer and the
parsenumber() function in compile.c to do what I want and written the
data type in my two new files Objects/mynewobject.c and
Include/mynewobject.h. I've included mynewobject.h in Python.h and added
the two files to the Makefile.

However, when I tried to run the code by typing in the syntax to produce
my data type, Python suffers a segmentation fault. I traced this to an
attempt to get the hash of a null pointer when adding the new instance
of my type after parsenumber() was called. For completeness, here's the
backtrace:

Program received signal SIGSEGV, Segmentation fault.
0x08080e9a in PyObject_Hash (v=0x81486c0) at Objects/object.c:1162
1162 if (tp->tp_hash != NULL)
(gdb) bt
#0 0x08080e9a in PyObject_Hash (v=0x81486c0) at Objects/object.c:1162
#1 0x0808e199 in tuplehash (v=0x40324574) at Objects/tupleobject.c:244
#2 0x08080eae in PyObject_Hash (v=0x40324574) at Objects/object.c:1163
#3 0x0807b46c in PyDict_GetItem (op=0x40319c14, key=0x40324574) at
Objects/dictobject.c:492
#4 0x080c72c7 in com_add (c=0xbfffed60, list=0x40326114,
dict=0x40319c14, v=0x40326290) at Python/compile.c:975
#5 0x080c74ac in com_addconst (c=0xbfffed60, v=0x40326290) at
Python/compile.c:1001
#6 0x080c90e3 in com_atom (c=0xbfffed60, n=0x4028d500) at
Python/compile.c:1689

The crash appears to be caused because while v exists, v's members are
all 0 and so tp becomes 0 when it is made equal to v->ob_type and you
get a NULL pointer exception. As far as I can tell, this v is the second
object in the tuple created in com_add() and is supposed to be the type
of the object being added to the dictionary in a tuple. Not knowing why
it was coming out as zero, I did some more poking around (been doing a
lot of that over the past 5 days...) and found that there is a file
called bltinmodule.c with a bunch of lines, one of which mentions the
complex data type that I am using as a guide:

SETBUILTIN("complex", &PyComplex_Type);

So I made one of these for mynewobject and added it. I figured from this
bit of code that the reason I was getting NULL pointer exceptions was
because my type hadn't been initialised in some way and that this would
do it. But here's the problem: despite trying for longer than I've slept
in the past week, I can't get it to work. With that line in the Python
interpreter segfaults as soon as it starts (while trying to import the
os module) and so it can't even finish compiling the extension modules.
If I comment out the SETBUILTIN line I added it will compile fine but
then I go back to the first problem.

I have structured my object identically to the complex object as far as
I can tell, which works. So the question is, what's broken? What have I
missed when adding a new builtin?

(This mess is all happening on Linux tillinshir 2.6.7-gentoo-r11-gb #1
Wed Aug 4 11:13:14 NZST 2004 i686 mobile AMD Athlon(tm) XP 2000+
AuthenticAMD GNU/Linux, if that matters.)

Thanks in advance,
Geoff Biggs
Jul 18 '05 #1
1 2153
No need to worry anymore, I've fixed the problem. If you're making a
built-in type, remember to define your PyTypeObject with something other
than NULL in the PyObject_HEAD_INIT. Non-built-ins don't need to worry
about this because it gets filled in later for them (as in the example
in the docs), but built-ins appear to need to fill it in themselves.

Geoff Biggs

Geoff Biggs wrote:
Evening all,

I'm trying to add a new built-in number data type to Python with its own
syntax, so I'm working directly with the interpreter rather than
creating my own extension module (side note: I've appended something
extra to the version thing in the Makefile - I doubt this is relevant to
the problem but it's probably best you have all the info). The complex
data type is similar to what I'm trying to do so I've been following
that as an example. I've successfully extended the tokenizer and the
parsenumber() function in compile.c to do what I want and written the
data type in my two new files Objects/mynewobject.c and
Include/mynewobject.h. I've included mynewobject.h in Python.h and added
the two files to the Makefile.

However, when I tried to run the code by typing in the syntax to produce
my data type, Python suffers a segmentation fault. I traced this to an
attempt to get the hash of a null pointer when adding the new instance
of my type after parsenumber() was called. For completeness, here's the
backtrace:

Program received signal SIGSEGV, Segmentation fault.
0x08080e9a in PyObject_Hash (v=0x81486c0) at Objects/object.c:1162
1162 if (tp->tp_hash != NULL)
(gdb) bt
#0 0x08080e9a in PyObject_Hash (v=0x81486c0) at Objects/object.c:1162
#1 0x0808e199 in tuplehash (v=0x40324574) at Objects/tupleobject.c:244
#2 0x08080eae in PyObject_Hash (v=0x40324574) at Objects/object.c:1163
#3 0x0807b46c in PyDict_GetItem (op=0x40319c14, key=0x40324574) at
Objects/dictobject.c:492
#4 0x080c72c7 in com_add (c=0xbfffed60, list=0x40326114,
dict=0x40319c14, v=0x40326290) at Python/compile.c:975
#5 0x080c74ac in com_addconst (c=0xbfffed60, v=0x40326290) at
Python/compile.c:1001
#6 0x080c90e3 in com_atom (c=0xbfffed60, n=0x4028d500) at
Python/compile.c:1689

The crash appears to be caused because while v exists, v's members are
all 0 and so tp becomes 0 when it is made equal to v->ob_type and you
get a NULL pointer exception. As far as I can tell, this v is the second
object in the tuple created in com_add() and is supposed to be the type
of the object being added to the dictionary in a tuple. Not knowing why
it was coming out as zero, I did some more poking around (been doing a
lot of that over the past 5 days...) and found that there is a file
called bltinmodule.c with a bunch of lines, one of which mentions the
complex data type that I am using as a guide:

SETBUILTIN("complex", &PyComplex_Type);

So I made one of these for mynewobject and added it. I figured from this
bit of code that the reason I was getting NULL pointer exceptions was
because my type hadn't been initialised in some way and that this would
do it. But here's the problem: despite trying for longer than I've slept
in the past week, I can't get it to work. With that line in the Python
interpreter segfaults as soon as it starts (while trying to import the
os module) and so it can't even finish compiling the extension modules.
If I comment out the SETBUILTIN line I added it will compile fine but
then I go back to the first problem.

I have structured my object identically to the complex object as far as
I can tell, which works. So the question is, what's broken? What have I
missed when adding a new builtin?

(This mess is all happening on Linux tillinshir 2.6.7-gentoo-r11-gb #1
Wed Aug 4 11:13:14 NZST 2004 i686 mobile AMD Athlon(tm) XP 2000+
AuthenticAMD GNU/Linux, if that matters.)

Thanks in advance,
Geoff Biggs

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PyUser | last post by:
Hello, Just downloaded the 2.3.2 bzip2 package and trying to install. When I run 'make', I get the following error: ..... ..... running build...
2
by: V. Srinivas | last post by:
Hi, I tried to install my strong named assembly to GAC. But I get the following error message - 1. If I drag and drop the assembly to GAC using...
19
by: Bill Davidson | last post by:
My wife's friend had a website built for his company, and it looks great using a PC. When viewed on a Mac using either Safari or Explorer the...
1
by: Randy | last post by:
I built and employment report from the employment table. I then had to add a field to the table. I now need to add that field to the report. I...
7
by: Wysiwyg | last post by:
Is there any way to add an embedded resource to a project without copying it to the project's directory? I have shared resources and don't want each...
0
by: Wysiwyg | last post by:
Is there any way to add an embedded resource to a project without copying it to the project's directory? I have shared resources and don't want each...
33
by: wespvp | last post by:
When I try to run thread_test.c from the CVS tip, it hangs in an infinite CPU loop on both linux (RedHat AS 3.0, gcc 3.2.3) and Mac OS X 10.3.3 (gcc...
1
by: Andy26 | last post by:
Hello: I currently have an Access2000 form that is built on a query that is using a table that is linked via ODBC connection. The table pulls...
5
by: Jonathan Boivin | last post by:
Hi, I've got some problems with loading bills using a bill usercontrol I built. If I load all current bills in my test environment (156)...
5
by: royals | last post by:
Hello Access gurus, I am a self-taught Access “developer” and have a ton to learn so please allow me to sound stupid. I am using 2003 on a XP...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.