In C extension .pyd, sizeof INT64 = 4?

A

Allen

My C extension works wrong, and debug it, found that sizeof (INT64) =
4, not 8.
I compile on Windows XP platform.
Please tell me how to fix it to support INT64?
Thanks.
 
A

Allen

My C extension works wrong, and debug it, found that sizeof (INT64) =
4, not 8.
I compile on Windows XP platform.
Please tell me how to fix it to support INT64?
Thanks.

I find it is strange. In an exe win32 console project, sizeof(INT64) =
8.

My code is just like this:

/* my.h header file */

#ifdef __cplusplus
extern "C" {
#endif

static PyObject* method(PyObject* self, PyObject *args);

#idef __cplusplus
}
#endif

/* my.cpp source file */
PyObject* method(PyObject* self, PyObject *args)
{
INT64 nValue; /* LINE_HERE */
INT32 nRet;
nRet = DoSomeCOperations(nValue);
return PyBuildValue("i", nRet);
}

If I changed INT64 nValue to be static INT64 nValue at LINE_HERE, it
is ok.
Why?
 
G

Gabriel Genellina

PyObject* method(PyObject* self, PyObject *args)
{
INT64 nValue; /* LINE_HERE */
INT32 nRet;
nRet = DoSomeCOperations(nValue);
return PyBuildValue("i", nRet);
}

If I changed INT64 nValue to be static INT64 nValue at LINE_HERE, it
is ok.
Why?

I don't know, but I don't see either where Python is involved... you don't
use any argument and the returned Python object is not an INT64...
 
A

Allen

I don't know, but I don't see either where Python is involved... you don't
use any argument and the returned Python object is not an INT64...

The problem is obviously compiler involved.
But I don't know why the sizeof INT64 is changed to be 4.
 
G

Gabriel Genellina

The problem is obviously compiler involved.
But I don't know why the sizeof INT64 is changed to be 4.

This prints 8, compiled with Visual C++ 2005 Express, Python 2.5.1, inside
a Python extension with all the normal definitions and #include's

void test(void)
{
INT64 i=1;
printf("%d\n", sizeof(i));
}
 
?

=?GB2312?B?Ik1hcnRpbiB2LiBMbyJ3aXMi?=

Allen said:
My C extension works wrong, and debug it, found that sizeof (INT64) =
4, not 8.
I compile on Windows XP platform.
Please tell me how to fix it to support INT64?

What *is* INT64? It's not a builtin type of standard C, it isn't
defined by Microsoft C, and it isn't predefined by Python.

So it must be something that you have defined, and apparently
incorrectly. How did you define it?

Regards,
Martin
 
A

Allen

What *is* INT64? It's not a builtin type of standard C, it isn't
defined by Microsoft C, and it isn't predefined by Python.

So it must be something that you have defined, and apparently
incorrectly. How did you define it?

Regards,
Martin

Thanks for your reply.

I defined in this way:

#ifndef WIN32
typedef long long INT64;
#else
typedef __int64 INT64;
#endif

I feel it strange that when I use INT64 as local variable, it will run
error.
When I change the local variable to be static or global, it will be
ok.
 
A

Allen

Thanks for your reply.

I defined in this way:

#ifndef WIN32
typedef long long INT64;
#else
typedef __int64 INT64;
#endif

I feel it strange that when I use INT64 as local variable, it will run
error.
When I change the local variable to be static or global, it will be
ok.

I feel very very sorry.
It is my fault.
I used INT64 and initialize its value from PyArg_ParseTuple.
The code is PyArg_ParseTuple(args, "l", &nValue).
It should be PyArg_ParseTuple(args, "L", &nValue).

Thanks all of you.

Regars,
Allen Chen
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

I used INT64 and initialize its value from PyArg_ParseTuple.
The code is PyArg_ParseTuple(args, "l", &nValue).
It should be PyArg_ParseTuple(args, "L", &nValue).

That's still incorrect. For the L format flag, use PY_LONG_LONG,
not your own INT64 type. More generally: always use the type
documented in

http://docs.python.org/api/arg-parsing.html

Regards,
Martin
 
T

Tommy Nordgren

My C extension works wrong, and debug it, found that sizeof (INT64) =
4, not 8.
I compile on Windows XP platform.
Please tell me how to fix it to support INT64?
Thanks.

--
http://mail.python.org/mailman/listinfo/python-list
On compilers that can gnerate both 32 and 64 bit executables, the
size of some
data types vary according to compiler options.
You are probably using a typedef long INT64;
In order for INT64 to always be 64 bits/8bytes you should declare:
typedef long long INT64;
 
G

Gabriel Genellina

What *is*INT64? It's not a builtin type of standard C, it isn't
defined by Microsoft C, and it isn't predefined by Python.

So it must be something that you have defined, and apparently
incorrectly. How did you define it?

It is defined in basetsd.h, included by winnt.h; the OP should not
redefine it, and that also explains why I could compile my test
program without any problem.
 
L

Laurent Pointal

Allen said:
PY_LONG_LONG is decleared as __int64 on windows. There is no
difference.

IMHO. Make your code platform independant and use your compiler capacities.

If for an 'L' argument PyArg_ParseTuple requires a PY_LONG_LONG, then give
it a PY_LONG_LONG and assign the parsed value into an INT64 after parsing.

You dont know if PY_LONG_LONG or INT64 wil not be defined differently in the
future, so use them where they are specified, do the assignment, and leave
the compiler warn you if anything become invalid in the futur.

My 2 cents.

A+

Laurent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top