python-c-api

Large POST data is corrupted when using Django/PyISAPIe/IIS

人盡茶涼 提交于 2019-12-06 11:13:57
I'm running into a problem with large POST data (>16384 bytes) when using Django 1.2.3, PyISAPIe v1.1.0-rc4, and IIS 7.5. For example, when submitting approx. 60kB of form data using POST, the following happens: The first 16kB block of POST data are correct The next 16kB block is a repeat of the first block The next 16kB is another repeat of the first block The rest (<16kB) is correct again The interesting part is that when using content-type="multipart/form-data" , it works fine. Using this information I tracked down the likely location of the bug to WSGIRequest._get_raw_post_data in django

Python: Usage of PyDateTime_FromTimestamp

故事扮演 提交于 2019-12-06 09:34:19
I'm working on a python c-extension and want to create an instance of python datetime object with a unix timestamp. On the documentation site ( http://docs.python.org/c-api/datetime.html ) I found the function PyDateTime_FromTimestamp() which returns a new reference based on an input parameter. The description is as follows: Create and return a new datetime.datetime object given an argument tuple suitable for passing to datetime.datetime.fromtimestamp(). I tried out to call the function with a PyFloat_Object but the function always returns NULL (even if I simply put in 0). Does somebody have

How does Python 3 know how to pickle extension types, especially Numpy arrays?

与世无争的帅哥 提交于 2019-12-06 08:49:26
问题 Numpy arrays, being extension types (aka defined using in extensions the C API), declare additional fields outside the scope of the Python interpreter (for example the data attribute, which is a Buffer Structure , as documented in Numpy's array interface. To be able to serialize it, Python 2 used to use the __reduce__ function as part of the pickle protocol, as stated in the doc, and explained here. But, even if __reduce__ still exists in Python 3, the Pickle protocol section (and Pickling

Parsing User Defined Types Using PyArg_ParseTuple

你。 提交于 2019-12-06 07:29:59
问题 How to parse userdefined types (or types from an existing non-standard library) using PyArg_ParseTuple ? 回答1: Instead of using the plain O format, as Martijn suggested, I normally prefer using the O& format. It allows you to pass a function that will be called to convert any PyObject* to an arbitrary C (double) pointer. Here is some example usage, in which I'm converting a passed value to a pointer to my own object type: /** * This method should return 0 if it does not work or 1 in case it

C Python API Extensions is ignoring open(errors=“ignore”) and keeps throwing the encoding exception anyways

久未见 提交于 2019-12-06 06:16:10
Given a file /myfiles/file_with_invalid_encoding.txt with invalid UTF8 as: parse this correctly Føö»BÃ¥r also parse this correctly I am using the builtin Python open function from the C API as follows the minimal example (excluding C Python setup boilerplate): const char* filepath = "/myfiles/file_with_invalid_encoding.txt"; PyObject* iomodule = PyImport_ImportModule( "builtins" ); if( iomodule == NULL ) { PyErr_PrintEx(100); return; } PyObject* openfunction = PyObject_GetAttrString( iomodule, "open" ); if( openfunction == NULL ) { PyErr_PrintEx(100); return; } PyObject* openfile = PyObject

Import and use standard Python module from inside Python C extension

大兔子大兔子 提交于 2019-12-06 02:53:37
问题 I have Python extension module written in C. I want to use in this C code one of the standard Python modules, for example os or shutil . How is best to do this? 回答1: PyObject* os = PyImport_ImportModuleNoBlock("os"); if (os == NULL) return NULL; someattr = PyObject_GetAttrString(os, "someattr"); Py_DECREF(os); If you import the module only once e.g., in init_yourmodule() function then use PyImport_ImportModule("os") . 回答2: Don't. Instead, change your extension module so that it provides a

When is PyEval_InitThreads meant to be called? [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-06 02:43:22
问题 This question already has answers here : PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam) (7 answers) Closed 6 years ago . I'm a bit confused about when I'm supposed to call PyEval_InitThreads . In general, I understand that PyEval_InitThreads must be called whenever a non-Python thread (i.e. a thread that is spawned within an extension module) is used. However, I'm confused if PyEval_InitThreads is for C programs which embed the Python interpreter, or

Why and where python interned strings when executing `a = 'python'` while the source code does not show that?

纵饮孤独 提交于 2019-12-05 21:39:25
I am trying to learn the intern mechanism of python using in the implementation of string object. But in both PyObject *PyString_FromString(const char *str) and PyObject *PyString_FromStringAndSize(const char *str, Py_ssize_t size) python interned strings only when its size is 0 or 1. PyObject * PyString_FromString(const char *str) { fprintf(stdout, "creating %s\n", str);------------[1] //... //creating... /* share short strings */ if (size == 0) { PyObject *t = (PyObject *)op; PyString_InternInPlace(&t); op = (PyStringObject *)t; nullstring = op; Py_INCREF(op); } else if (size == 1) {

How to get a char* from a PyObject which points to a string

可紊 提交于 2019-12-05 19:20:44
How can I get a char* from a PyObject which points to a string. For example, this is the python script, Test.Connect("272.22.20.65", 1234) and this is the C++ code, static PyObject* Connect(PyObject *self, PyObject *args) { PyObject* pIP; PyObject* pPort; if (!PyArg_UnpackTuple(args, "Connect", 2, 2, &pIP, &pPort)) { return NULL; } const char* zIP = GetAsString(pIP); long iPort = PyLong_AsLong(pPort); I want to get that IP address as a char* (GetAsString is a dummy function :D ). Please note that I'm using Python 3.1. P.S. I don't think this question got the correct answer , since there is no

How to get reference count of a PyObject?

让人想犯罪 __ 提交于 2019-12-05 14:32:09
问题 How to get reference count of a PyObject from C++? There are functions Py_INCREF and Py_DECREF which increase/decrease it, but I haven't found any function which return object's reference count. I need it for debugging purposes. 回答1: The reference count of each and every object is stored in the PyObject itself, in a variable called ob_refcnt. You can directly access that. typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt; # Reference count struct _typeobject *ob_type; }