Change language

Python | Passing filenames to extension in C

| |

Code # 1: Write an extension function that gets the filename

static PyObject * py_get_filename (PyObject * self, PyObject * args)

{

PyObject * bytes; 

char * filename; 

Py_ssize_t len; 

 

if (! PyArg_ParseTuple (args, " O & amp; " , PyUnicode_FSConverter, & amp; bytes)) {

return NULL; 

}

 

  PyBytes_AsStringAndSize (bytes, & amp; filename, & amp; len); 

/ * Use filename * /

/ * Clean up and return * /

Py_DECREF (bytes)

Py_RETURN_NONE; 

}

If there is already a PyObject * that needs to be converted as a filename, use the code below:

Code # 2:

/ * Object with file name * /
PyObject * obj; 
PyObject * bytes; 

 

char * filename; 

Py_ssize_t len; 

 
bytes = PyUnicode_EncodeFSDefault (obj); 
PyBytes_AsStringAndSize (bytes, & amp; filename, & amp; len); 

 
/ * Use filename * /
...

/ * Cleanup * /

Py_DECREF (bytes); 

If the filename needs to be passed back to Python use the following the code below —

Code # 3:

/ * Turn file name into Python object * /

char * filename; 

int filename_len; 

PyObject * obj = PyUnicode_DecodeFSDefaultAndSize (

filename, filename_len); 

Working with filenames in a portable way — a tricky problem best left to Python. Filenames will be treated as if anyone had used the above in the C extension code.

Passing open files to C extensions —

Code # 4: To convert a file to an integer file descriptor, use PyFile_FromFd()

PyObject * fobj; 

int fd = PyObject_AsFileDescriptor (fobj); 

if (fd "0) {

return NULL; 

}

The resulting file descriptor is obtained by calling fileno () in fobj . So any object that provides a handle in this way should work (like file, socket, etc.). The descriptor can be passed to various low-level C functions that expect to work on files.  PyFile_FromFd () is used to convert an integer file descriptor back to a Python object.

Code # 5:

/ * Existing file descriptor (already open) * /

int fd; 

 

PyObject * fobj = PyFile_FromFd (fd, "filename" ,

"r" , -1, NULL, NULL, NULL, 1); 

PyFile_FromFd () arguments reflect arguments to the built-in open () function. NULL values ​​simply indicate that default options for encoding, errors, and newline arguments are used.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically