How to parse extended integer type in python C extension module? - python-c-api

I am trying to pass a (large) integer from python to an extension module, but I am unable to parse pythons arbitrary precision integers to 256-bit unsigned integers uint256. Here is the C callee:
#include <Python.h>
typedef unsigned _ExtInt(256) uint256;
static PyObject* test(PyObject* self, PyObject* args)
{
uint256 x;
if(!PyArg_ParseTuple(args, "O", &x)) {
puts("Could not parse the python arg");
return NULL;
}
// simple addition
x += (uint256) 1;
return Py_BuildValue("O", x);
}
// ... initalize extension module here ...
In python I run something like
import extension_module
extension_module.test(1)
And I get the error:
Bus error: 10
Or
Segmentation fault: 11
However, if I remove the simple addition x += (uint256) 1; it will atleast not throw any error and return the argument.
How do I parse extended-integer types in my C extension module?

Related

Is Swift's handling of CVarArg for String buggy?

While writing a Swift wrapper for a C wrapper of a C++ library, I've stumbled on some weird bugs regarding Swift's CVarArg. The C wrapper I already have uses variadic functions which I converted to functions using va_list as an argument so they could be imported (since Swift cannot import C variadic functions). When passing arguments to such a function, once bridged to Swift, it uses the private _cVarArgEncoding property of types conforming to CVarArg to "encode" the values which are then sent as a pointer to the C function. It seems however that this encoding is faulty for Swift Strings.
To demonstrate, I've created the following package:
Package.swift
// swift-tools-version:5.2
import PackageDescription
let package = Package(
name: "CVarArgTest",
products: [
.executable(
name: "CVarArgTest",
targets: ["CVarArgTest"]),
],
targets: [
.target(
name: "CLib"),
.target(
name: "CVarArgTest",
dependencies: ["CLib"])
]
)
CLib
CTest.h
#ifndef CTest_h
#define CTest_h
#include <stdio.h>
/// Prints out the strings provided in args
/// #param num The number of strings in `args`
/// #param args A `va_list` of strings
void test_va_arg_str(int num, va_list args);
/// Prints out the integers provided in args
/// #param num The number of integers in `args`
/// #param args A `va_list` of integers
void test_va_arg_int(int num, va_list args);
/// Just prints the string
/// #param str The string
void test_str_print(const char * str);
#endif /* CTest_h */
CTest.c
#include "CTest.h"
#include <stdarg.h>
void test_va_arg_str(int num, va_list args)
{
printf("Printing %i strings...\n", num);
for (int i = 0; i < num; i++) {
const char * str = va_arg(args, const char *);
puts(str);
}
}
void test_va_arg_int(int num, va_list args)
{
printf("Printing %i integers...\n", num);
for (int i = 0; i < num; i++) {
int foo = va_arg(args, int);
printf("%i\n", foo);
}
}
void test_str_print(const char * str)
{
puts(str);
}
main.swift
import Foundation
import CLib
// The literal String is perfectly bridged to the CChar pointer expected by the function
test_str_print("Hello, World!")
// Prints the integers as expected
let argsInt: [CVarArg] = [123, 456, 789]
withVaList(argsInt) { listPtr in
test_va_arg_int(Int32(argsInt.count), listPtr)
}
// ERROR: Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
let argsStr: [CVarArg] = ["Test", "Testing", "The test"]
withVaList(argsStr) { listPtr in
test_va_arg_str(Int32(argsStr.count), listPtr)
}
The package is available here as well.
As commented in the code above, printing a String via C or a va_list containing Ints works as expected, but when converted to const char *, there's an exception (EXC_BAD_ACCESS (code=EXC_I386_GPFLT)).
So, in short: did I mess up the C side of it or is Swift doing something wrong here? I've tested this in Xcode 11.5 and 12.0b2. If it's a bug, I'll be happy to report it.
This one's a bit tricky: your string is actually being bridged to an Objective-C NSString * rather than a C char *:
(lldb) p str
(const char *) $0 = 0x3cbe9f4c5d32b745 ""
(lldb) p (id)str
(NSTaggedPointerString *) $1 = 0x3cbe9f4c5d32b745 #"Test"
(If you're wondering why it's an NSTaggedPointerString rather than just an NSString, this article is a great read -- in short, the string is short enough to be stored directly in the bytes of the pointer variable rather than in an object on the heap.
Looking at the source code for withVaList, we see that a type's va_list representation is determined by its implementation of the _cVarArgEncoding property of the CVarArg protocol. The standard library has some implementations of this protocol for some basic integer and pointer types, but there's nothing for String here. So who's converting our string to an NSString?
Searching around the Swift repo on GitHub, we find that Foundation is the culprit:
//===----------------------------------------------------------------------===//
// CVarArg for bridged types
//===----------------------------------------------------------------------===//
extension CVarArg where Self: _ObjectiveCBridgeable {
/// Default implementation for bridgeable types.
public var _cVarArgEncoding: [Int] {
let object = self._bridgeToObjectiveC()
_autorelease(object)
return _encodeBitsAsWords(object)
}
}
In plain English: any object which can be bridged to Objective-C is encoded as a vararg by converting to an Objective-C object and encoding a pointer to that object. C varargs are not type-safe, so your test_va_arg_str just assumes it's a char* and passes it to puts, which crashes.
So is this a bug? I don't think so -- I suppose this behavior is probably intentional for compatibility with functions like NSLog that are more commonly used with Objective-C objects than C ones. However, it's certainly a surprising pitfall, and it's probably one of the reasons why Swift doesn't like to let you call C variadic functions.
You'll want to work around this by manually converting your strings to C-strings. This can get a bit ugly if you have an array of strings that you want to convert without making unnecessary copies, but here's a function that should be able to do it.
extension Collection where Element == String {
/// Converts an array of strings to an array of C strings, without copying.
func withCStrings<R>(_ body: ([UnsafePointer<CChar>]) throws -> R) rethrows -> R {
return try withCStrings(head: [], body: body)
}
// Recursively call withCString on each of the strings.
private func withCStrings<R>(head: [UnsafePointer<CChar>],
body: ([UnsafePointer<CChar>]) throws -> R) rethrows -> R {
if let next = self.first {
// Get a C string, add it to the result array, and recurse on the remainder of the collection
return try next.withCString { cString in
var head = head
head.append(cString)
return try dropFirst().withCStrings(head: head, body: body)
}
} else {
// Base case: no more strings; call the body closure with the array we've built
return try body(head)
}
}
}
func withVaListOfCStrings<R>(_ args: [String], body: (CVaListPointer) -> R) -> R {
return args.withCStrings { cStrings in
withVaList(cStrings, body)
}
}
let argsStr: [String] = ["Test", "Testing", "The test"]
withVaListOfCStrings(argsStr) { listPtr in
test_va_arg_str(Int32(argsStr.count), listPtr)
}
// Output:
// Printing 3 strings...
// Test
// Testing
// The test

Issues running mex commands on Octave

I am using Ubuntu and am trying to use Octave to run a Matlab script that runs the mex compiler on some cpp files https://github.com/yuxng/MDP_Tracking/blob/master/compile.m I have the OpenCV requirement already installed but am receiving some errors.
Basically the commands in octave are
compile
MOT_test
The errors I got from the compile command are shown below. Based on my web search plus this thread it seems like this issue of compiling the MDP Tracker remains unsolved. I have minimal experience running mex and octave so there is not much more I can do.
octave:4> compile
In file included from imResampleMex.cpp:7:0:
wrappers.hpp:22:24: error: ‘wrCalloc’ declared as an ‘inline’ variable
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size); }
^~~~~~
wrappers.hpp:22:24: error: ‘size_t’ was not declared in this scope
wrappers.hpp:22:36: error: ‘size_t’ was not declared in this scope
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size); }
^~~~~~
wrappers.hpp:22:48: error: expression list treated as compound expression in initializer [-fpermissive]
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size); }
^
wrappers.hpp:23:24: error: ‘wrMalloc’ declared as an ‘inline’ variable
inline void* wrMalloc( size_t size ) { return malloc(size); }
^~~~~~
wrappers.hpp:23:24: error: ‘size_t’ was not declared in this scope
wrappers.hpp: In function ‘void wrFree(void*)’:
wrappers.hpp:24:44: error: ‘free’ was not declared in this scope
inline void wrFree( void * ptr ) { free(ptr); }
^
wrappers.hpp: At global scope:
wrappers.hpp:29:17: error: ‘size_t’ was not declared in this scope
void* alMalloc( size_t size, int alignment ) {
^~~~~~
wrappers.hpp:29:30: error: expected primary-expression before ‘int’
void* alMalloc( size_t size, int alignment ) {
^~~
wrappers.hpp:29:44: error: expression list treated as compound expression in initializer [-fpermissive]
void* alMalloc( size_t size, int alignment ) {
^
imResampleMex.cpp: In function ‘void resampleCoef(int, int, int&, int*&, int*&, T*&, int*, int)’:
imResampleMex.cpp:22:39: error: ‘alMalloc’ cannot be used as a function
wts = (T*)alMalloc(nMax*sizeof(T),16);
^
imResampleMex.cpp:23:43: error: ‘alMalloc’ cannot be used as a function
yas = (int*)alMalloc(nMax*sizeof(int),16);
^
imResampleMex.cpp:24:43: error: ‘alMalloc’ cannot be used as a function
ybs = (int*)alMalloc(nMax*sizeof(int),16);
^
imResampleMex.cpp: In function ‘void resample(T*, T*, int, int, int, int, int, T)’:
imResampleMex.cpp:49:43: error: ‘alMalloc’ cannot be used as a function
T *C = (T*) alMalloc((ha+4)*sizeof(T),16); for(y=ha; y<ha+4; y++) C[y]=0;
^
warning: mkoctfile exited with failure status
warning: called from
mkoctfile at line 171 column 5
mex at line 29 column 18
compile at line 17 column 2
Compilation finished.
octave:5>

Freeglut doesn't initialize when using it from Swift

I've tried to use the Freeglut library in a Swift 4 Project. When the
void glutInit(int *argcp, char **argv);
function is shifted to Swift, its declaration is
func glutInit(_ pargc: UnsafeMutablePointer<Int32>!, _ argv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!)
Since I don't need the real arguments from the command line I want to make up the two arguments. I tried to define **argv in the Bridging-Header.h file
#include <OpenGL/gl.h>
#include <GL/glut.h>
char ** argv[1] = {"t"};
and use them in main.swift
func main() {
var argcp: Int32 = 1
glutInit(&argcp, argv!) // EXC_BAD_ACCESS
glutInitDisplayMode(UInt32(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH));
glutCreateWindow("my project")
glutDisplayFunc(display)
initOpenGL()
glutMainLoop()
but with that I get Thread 1: EXC_BAD_ACCESS (code=1, address=0x74) at the line with glutInit().
How can I initialize glut properly? How can I get an UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>! so that it works?
The reason the right code in C char * argv[1] = {"t"}; does not work is because Swift imports fixed size C-array as a tuple, not a pointer to the first element.
But your char ** argv[1] = {"t"}; is completely wrong. Each Element of argv needs to be char **, but you assign char * ("t"). Xcode must have shown you a warning at first build:
warning: incompatible pointer types initializing 'char **' with an expression of type 'char [2]'
You should better take incompatible pointer types warning as error, unless you know what you are doing completely.
Generally, you should better not write some codes generating actual code/data like char * argv[1] = {"t"}; in a header file.
You can try it with Swift code.
As you know, when you want to pass a pointer to single element T, you declare a var of type T and pass &varName to the function you call.
As argcp in your code.
As well, when you want to pass a pointer to multiple element T, you declare a var of type [T] (Array<T>) and pass &arrName to the function you call.
(Ignoring immutable case to simplify.)
The parameter argv matches this case, where T == UnsafeMutablePointer<Int8>?.
So declare a var of type [UnsafeMutablePointer<Int8>?].
func main() {
var argc: Int32 = 1
var argv: [UnsafeMutablePointer<Int8>?] = [
strdup("t")
]
defer { argv.forEach{free($0)} }
glutInit(&argc, &argv)
//...
}
But I wonder if you really want to pass something to glutInit().
You can try something like this:
func main() {
var argc: Int32 = 0 //<- 0
glutInit(&argc, nil)
//...
}
I'm not sure if freeglut accept this, but you can find some articles on the web saying that this works in some implementation of Glut.

C How can I write a value to an entire data structure, rather than just an element?

I am attempting to write a little C test program for reading data from a vending machine. I have the circuit hooked up, now the hard part is the code.
The machine uses UART that calls for 9 total data bits. Here is my code. Attempting to write directly to the full 9 bit data type does not work, but writing to an element of it does.
struct nineBit { //To make 9 bit character types for an array.
unsigned int data : 8; //8 data bits.
unsigned int mode : 1; //1 'mode' bit.
} data[35]; //Make an array of 9 bit data to store the incoming data block.
void setup() {
Serial1.begin(9600, SERIAL_9N1); //Start the UART.
}
void loop() {
data[0] = Serial1.read(); //Works if 'data[0].data is entered instead.
//How can I transfer this all in one command?
}
Errors are
rx.cpp: In function 'void loop()':
rx.cpp:11:12: error: no match for 'operator=' (operand types are 'nineBit' and 'int')
void setup() {
^
rx.cpp:11:12: note: candidates are:
rx.cpp:1:8: note: nineBit& nineBit::operator=(const nineBit&)
^
rx.cpp:1:8: note: no known conversion for argument 1 from 'int' to 'const nineBit&'
rx.cpp:1:8: note: nineBit& nineBit::operator=(nineBit&&)
rx.cpp:1:8: note: no known conversion for argument 1 from 'int' to 'nineBit&&'
make[1]: *** [../build/target/user/platform-6rx.o] Error 1
make: *** [user] Error 2
Error: Could not compile. Please review your code.
I assume you are using arduino or something similar. So Serial1.read() returns char. char is a signed 1 byte (8 bits) field. And your struct nineBit has 9 bits. How do you expect of writing 8 bits to 9 bited structure?
A note about your structure: it doesn't have size equal 9 bits. Instance of any variable can have size evaluated in bytes only. So if you want to store 9 bits you have to create a two byted structure or more.
And in fact sizeof(nineBit) equals 4 because your bit field have unsigned int type. If you want to reduce size of your structure you have to change bit field type to either short or char.
Let's assume your serial transports two bytes per every structure. So you have to read two bytes and then assign them:
struct nineBit {
char data : 8; //8 data bits.
char mode : 1; //1 'mode' bit.
} data[35];
void setup() {
Serial1.begin(9600, SERIAL_9N1); //Start the UART.
}
void loop() {
char byte1=Serial1.read();
char byte2=Serial1.read();
data[0].data=byte1;
data[0].mode=byte2;
}
If you want to use only a single line you have to write a C function or overload operator= if you use C++.
C way
struct nineBit {
char data : 8; //8 data bits.
char mode : 1; //1 'mode' bit.
} data[35];
void writeToNineBit(struct nineBit *value){
char byte1=Serial1.read();
char byte2=Serial1.read();
value->data=byte1;
value->mode=byte2;
}
void setup() {
Serial1.begin(9600, SERIAL_9N1); //Start the UART.
}
void loop() {
writeToNineBit(data+0); // or &data[0].. 0 is an index in array..
}
C++ way
struct nineBit {
char data : 8; //8 data bits.
char mode : 1; //1 'mode' bit.
// assume you have to assign data without mode..
nineBit& operator=(char b){
this->data=b;
}
} data[35];
void setup() {
Serial1.begin(9600, SERIAL_9N1); //Start the UART.
}
void loop() {
data[0]=Serial1.read(); // now it works cause you have operator overloading in your structure..
}

What does error conflicting types for '' mean?

i got an error that said "error: conflicting types for '____'. What does that mean?
Quickfix:
Make sure that your functions are declared once and only once before they are called. For example, change:
main(){ myfun(3.4); }
double myfun(double x){ return x; }
To:
double myfun(double x){ return x; }
main(){ myfun(3.4); }
Or add a separate function declaration:
double myfun(double x);
main(){ myfun(3.4); }
double myfun(double x){ return x; }
Possible causes for the error
Function was called before being declared
Function defined overrides a function declared in an included header.
Function was defined twice in the same file
Declaration and definition don't match
Declaration conflict in the included headers
What's really going on
error: conflicting types for ‘foo’ means that a function was defined more than once with different type signatures.
A file that includes two functions with the same name but different return types would throw this error, for example:
int foo(){return 1;}
double foo(){return 1.0;}
Indeed, when compiled with GCC we get the following errors:
foo.c:5:8: error: conflicting types for ‘foo’
double foo(){return 1.0;}
^
foo.c:4:5: note: previous definition of ‘foo’ was here
int foo(){return 1;}
^
Now, if instead we had a file with two function definitions with the same name
double foo(){return 1;}
double foo(){return 1.0;}
We would get a 'redefinition' error instead:
foo.c:5:8: error: redefinition of ‘foo’
double foo(){return 1.0;}
^
foo.c:4:8: note: previous definition of ‘foo’ was here
double foo(){return 1;}
^
Implicit function declaration
So why does the following code throw error: conflicting types for ‘foo’?
main(){ foo(); }
double foo(){ return 1.0; }
The reason is implicit function declaration.
When the compiler first encounters foo() in the main function, it will assume a type signature for the function foo of int foo(). By default, implicit functions are assumed to return integers, and the input argument types are derived from what you're passing into the function (in this case, nothing).
Obviously, the compiler is wrong to make this assumption, but the specs for the C (and thus Objective-C) language are old, cranky, and not very clever. Maybe implicitly declaring functions saved some development time by reducing compiler complexity back in the day, but now we're stuck with a terrible feature that should have never made it into the language. In fact, implicit declarations were made illegal in C99.
That said, once you know what's going on, it should be easy to dig out the root cause of your problem.
it's probably because your function "_" already exists in your library. It happened to me with this function:
I was using stdio.h
int getline (char s[ ] , int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
When I changed "getline" to "getlinexxx" and gcc compiled it:
int getlinexxx (char s[], int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
And the problem was gone
What datatype is '___'?
My guess is that you're trying to initialize a variable of a type that can't accept the initial value. Like saying int i = "hello";
If you're trying to assign it from a call that returns an NSMutableDictionary, that's probably your trouble. Posting the line of code would definitely help diagnose warnings and errors in it.