blob: ed697acaa4a1c32a68ee807b284c8b13feb96fda [file] [log] [blame]
Guido van Rossum34679b71993-01-26 13:33:44 +00001
Guido van Rossum1d5735e1994-08-30 08:27:36 +00002/* Thread package.
3 This is intended to be usable independently from Python.
4 The implementation for system foobar is in a file thread_foobar.h
5 which is included by this file dependent on config settings.
6 Stuff shared by all thread_*.h files is collected here. */
7
Martin v. Löwiscdc44512002-01-12 11:05:12 +00008#include "Python.h"
Guido van Rossum1d5735e1994-08-30 08:27:36 +00009
Matthias Klosea2542be2004-08-16 11:35:51 +000010#ifndef _POSIX_THREADS
11/* This means pthreads are not implemented in libc headers, hence the macro
12 not present in unistd.h. But they still can be implemented as an external
13 library (e.g. gnu pth in pthread emulation) */
14# ifdef HAVE_PTHREAD_H
15# include <pthread.h> /* _POSIX_THREADS */
16# endif
17#endif
18
Guido van Rossum2571cc81999-04-07 16:07:23 +000019#ifndef DONT_HAVE_STDIO_H
Guido van Rossum1d5735e1994-08-30 08:27:36 +000020#include <stdio.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000021#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000022
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023#include <stdlib.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +000024
Guido van Rossum49b56061998-10-01 20:42:43 +000025#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000026
Guido van Rossum1d5735e1994-08-30 08:27:36 +000027#ifndef _POSIX_THREADS
28
Guido van Rossum539c6622005-09-14 17:49:54 +000029/* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
Ezio Melotti13925002011-03-16 11:05:33 +020030 enough of the Posix threads package is implemented to support python
Guido van Rossum539c6622005-09-14 17:49:54 +000031 threads.
32
33 This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
Martin Panter7462b6492015-11-02 03:37:02 +000034 a check of __ia64 to verify that we're running on an ia64 system instead
Guido van Rossum539c6622005-09-14 17:49:54 +000035 of a pa-risc system.
36*/
37#ifdef __hpux
38#ifdef _SC_THREADS
39#define _POSIX_THREADS
40#endif
41#endif
42
Sjoerd Mullender66bca321993-12-03 16:54:45 +000043#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000044
Guido van Rossum1984f1e1992-08-04 12:41:02 +000045
Guido van Rossum408027e1996-12-30 16:17:54 +000046#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000047static int thread_debug = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define dprintf(args) (void)((thread_debug & 1) && printf args)
49#define d2printf(args) ((thread_debug & 8) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000050#else
51#define dprintf(args)
52#define d2printf(args)
53#endif
54
Guido van Rossum1984f1e1992-08-04 12:41:02 +000055static int initialized;
56
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000057static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000058
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000059void
60PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000061{
Guido van Rossum408027e1996-12-30 16:17:54 +000062#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (p) {
66 if (*p)
67 thread_debug = atoi(p);
68 else
69 thread_debug = 1;
70 }
Guido van Rossum408027e1996-12-30 16:17:54 +000071#endif /* Py_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 if (initialized)
73 return;
74 initialized = 1;
75 dprintf(("PyThread_init_thread called\n"));
76 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000077}
78
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079/* Support for runtime thread stack size tuning.
80 A value of 0 means using the platform's default stack size
81 or the size specified by the THREAD_STACK_SIZE macro. */
82static size_t _pythread_stacksize = 0;
83
Sjoerd Mullender66bca321993-12-03 16:54:45 +000084#ifdef _POSIX_THREADS
Victor Stinner754851f2011-04-19 23:58:51 +020085#define PYTHREAD_NAME "pthread"
Guido van Rossum1d5735e1994-08-30 08:27:36 +000086#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +000087#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +000088
Guido van Rossumc3f82b61995-01-17 16:29:31 +000089#ifdef NT_THREADS
Victor Stinner754851f2011-04-19 23:58:51 +020090#define PYTHREAD_NAME "nt"
Guido van Rossumc3f82b61995-01-17 16:29:31 +000091#include "thread_nt.h"
92#endif
93
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000094
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095/* return the current thread stack size */
96size_t
97PyThread_get_stacksize(void)
98{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 return _pythread_stacksize;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000100}
101
102/* Only platforms defining a THREAD_SET_STACKSIZE() macro
103 in thread_<platform>.h support changing the stack size.
104 Return 0 if stack size is valid,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 -1 if stack size value is invalid,
106 -2 if setting stack size is not supported. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000107int
108PyThread_set_stacksize(size_t size)
109{
110#if defined(THREAD_SET_STACKSIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 return THREAD_SET_STACKSIZE(size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000114#endif
115}
116
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000117#ifndef Py_HAVE_NATIVE_TLS
118/* If the platform has not supplied a platform specific
119 TLS implementation, provide our own.
120
121 This code stolen from "thread_sgi.h", where it was the only
122 implementation of an existing Python TLS API.
123*/
Tim Petersfda787f2004-10-09 22:33:09 +0000124/* ------------------------------------------------------------------------
125Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000126
Tim Petersfda787f2004-10-09 22:33:09 +0000127Use PyThread_create_key() to create a new key. This is typically shared
128across threads.
129
130Use PyThread_set_key_value(thekey, value) to associate void* value with
131thekey in the current thread. Each thread has a distinct mapping of thekey
132to a void* value. Caution: if the current thread already has a mapping
133for thekey, value is ignored.
134
135Use PyThread_get_key_value(thekey) to retrieve the void* value associated
136with thekey in the current thread. This returns NULL if no value is
137associated with thekey in the current thread.
138
139Use PyThread_delete_key_value(thekey) to forget the current thread's associated
140value for thekey. PyThread_delete_key(thekey) forgets the values associated
141with thekey across *all* threads.
142
143While some of these functions have error-return values, none set any
144Python exception.
145
146None of the functions does memory management on behalf of the void* values.
147You need to allocate and deallocate them yourself. If the void* values
148happen to be PyObject*, these functions don't do refcount operations on
149them either.
150
151The GIL does not need to be held when calling these functions; they supply
152their own locking. This isn't true of PyThread_create_key(), though (see
153next paragraph).
154
155There's a hidden assumption that PyThread_create_key() will be called before
156any of the other functions are called. There's also a hidden assumption
157that calls to PyThread_create_key() are serialized externally.
158------------------------------------------------------------------------ */
159
160/* A singly-linked list of struct key objects remembers all the key->value
161 * associations. File static keyhead heads the list. keymutex is used
162 * to enforce exclusion internally.
163 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000164struct key {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 /* Next record in the list, or NULL if this is the last record. */
166 struct key *next;
Tim Petersfda787f2004-10-09 22:33:09 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 /* The thread id, according to PyThread_get_thread_ident(). */
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200169 unsigned long id;
Tim Petersfda787f2004-10-09 22:33:09 +0000170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 /* The key and its associated value. */
172 int key;
173 void *value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000174};
175
176static struct key *keyhead = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000177static PyThread_type_lock keymutex = NULL;
Tim Petersfda787f2004-10-09 22:33:09 +0000178static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000179
Tim Petersfda787f2004-10-09 22:33:09 +0000180/* Internal helper.
181 * If the current thread has a mapping for key, the appropriate struct key*
182 * is returned. NB: value is ignored in this case!
183 * If there is no mapping for key in the current thread, then:
184 * If value is NULL, NULL is returned.
185 * Else a mapping of key to value is created for the current thread,
186 * and a pointer to a new struct key* is returned; except that if
187 * malloc() can't find room for a new struct key*, NULL is returned.
188 * So when value==NULL, this acts like a pure lookup routine, and when
189 * value!=NULL, this acts like dict.setdefault(), returning an existing
190 * mapping if one exists, else creating a new mapping.
Tim Peters263091e2004-10-10 01:58:44 +0000191 *
192 * Caution: this used to be too clever, trying to hold keymutex only
193 * around the "p->next = keyhead; keyhead = p" pair. That allowed
194 * another thread to mutate the list, via key deletion, concurrent with
195 * find_key() crawling over the list. Hilarity ensued. For example, when
196 * the for-loop here does "p = p->next", p could end up pointing at a
197 * record that PyThread_delete_key_value() was concurrently free()'ing.
198 * That could lead to anything, from failing to find a key that exists, to
199 * segfaults. Now we lock the whole routine.
Tim Petersfda787f2004-10-09 22:33:09 +0000200 */
Tim Peters19717fa2004-10-09 17:38:29 +0000201static struct key *
Victor Stinner590cebe2013-12-13 11:08:56 +0100202find_key(int set_value, int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 struct key *p, *prev_p;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200205 unsigned long id = PyThread_get_thread_ident();
Tim Petersfda787f2004-10-09 22:33:09 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 if (!keymutex)
208 return NULL;
209 PyThread_acquire_lock(keymutex, 1);
210 prev_p = NULL;
211 for (p = keyhead; p != NULL; p = p->next) {
Victor Stinner590cebe2013-12-13 11:08:56 +0100212 if (p->id == id && p->key == key) {
213 if (set_value)
214 p->value = value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 goto Done;
Victor Stinner590cebe2013-12-13 11:08:56 +0100216 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 /* Sanity check. These states should never happen but if
Serhiy Storchaka56a6d852014-12-01 18:28:43 +0200218 * they do we must abort. Otherwise we'll end up spinning
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 * in a tight loop with the lock held. A similar check is done
220 * in pystate.c tstate_delete_common(). */
221 if (p == prev_p)
222 Py_FatalError("tls find_key: small circular list(!)");
223 prev_p = p;
224 if (p->next == keyhead)
225 Py_FatalError("tls find_key: circular list(!)");
226 }
Victor Stinner590cebe2013-12-13 11:08:56 +0100227 if (!set_value && value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 assert(p == NULL);
229 goto Done;
230 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200231 p = (struct key *)PyMem_RawMalloc(sizeof(struct key));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (p != NULL) {
233 p->id = id;
234 p->key = key;
235 p->value = value;
236 p->next = keyhead;
237 keyhead = p;
238 }
Tim Peters263091e2004-10-10 01:58:44 +0000239 Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 PyThread_release_lock(keymutex);
241 return p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000242}
243
Tim Petersfda787f2004-10-09 22:33:09 +0000244/* Return a new key. This must be called before any other functions in
245 * this family, and callers must arrange to serialize calls to this
246 * function. No violations are detected.
247 */
Tim Peters19717fa2004-10-09 17:38:29 +0000248int
249PyThread_create_key(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 /* All parts of this function are wrong if it's called by multiple
252 * threads simultaneously.
253 */
254 if (keymutex == NULL)
255 keymutex = PyThread_allocate_lock();
256 return ++nkeys;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000257}
258
Tim Petersfda787f2004-10-09 22:33:09 +0000259/* Forget the associations for key across *all* threads. */
Tim Peters19717fa2004-10-09 17:38:29 +0000260void
261PyThread_delete_key(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyThread_acquire_lock(keymutex, 1);
266 q = &keyhead;
267 while ((p = *q) != NULL) {
268 if (p->key == key) {
269 *q = p->next;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200270 PyMem_RawFree((void *)p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* NB This does *not* free p->value! */
272 }
273 else
274 q = &p->next;
275 }
276 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000277}
278
Tim Peters19717fa2004-10-09 17:38:29 +0000279int
280PyThread_set_key_value(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 struct key *p;
Tim Petersfda787f2004-10-09 22:33:09 +0000283
Victor Stinner590cebe2013-12-13 11:08:56 +0100284 p = find_key(1, key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (p == NULL)
286 return -1;
287 else
288 return 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000289}
290
Tim Petersfda787f2004-10-09 22:33:09 +0000291/* Retrieve the value associated with key in the current thread, or NULL
292 * if the current thread doesn't have an association for key.
293 */
Tim Peters19717fa2004-10-09 17:38:29 +0000294void *
295PyThread_get_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000296{
Victor Stinner590cebe2013-12-13 11:08:56 +0100297 struct key *p = find_key(0, key, NULL);
Tim Petersfda787f2004-10-09 22:33:09 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (p == NULL)
300 return NULL;
301 else
302 return p->value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000303}
304
Tim Petersfda787f2004-10-09 22:33:09 +0000305/* Forget the current thread's association for key, if any. */
Tim Peters19717fa2004-10-09 17:38:29 +0000306void
307PyThread_delete_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000308{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200309 unsigned long id = PyThread_get_thread_ident();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyThread_acquire_lock(keymutex, 1);
313 q = &keyhead;
314 while ((p = *q) != NULL) {
315 if (p->key == key && p->id == id) {
316 *q = p->next;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200317 PyMem_RawFree((void *)p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 /* NB This does *not* free p->value! */
319 break;
320 }
321 else
322 q = &p->next;
323 }
324 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000325}
326
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000327/* Forget everything not associated with the current thread id.
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200328 * This function is called from PyOS_AfterFork_Child(). It is necessary
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000329 * because other thread ids which were in use at the time of the fork
330 * may be reused for new threads created in the forked process.
331 */
332void
333PyThread_ReInitTLS(void)
334{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200335 unsigned long id = PyThread_get_thread_ident();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 struct key *p, **q;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 if (!keymutex)
339 return;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 /* As with interpreter_lock in PyEval_ReInitThreads()
342 we just create a new lock without freeing the old one */
343 keymutex = PyThread_allocate_lock();
344
345 /* Delete all keys which do not match the current thread id */
346 q = &keyhead;
347 while ((p = *q) != NULL) {
348 if (p->id != id) {
349 *q = p->next;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200350 PyMem_RawFree((void *)p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 /* NB This does *not* free p->value! */
352 }
353 else
354 q = &p->next;
355 }
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000356}
357
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000358#endif /* Py_HAVE_NATIVE_TLS */
Victor Stinner754851f2011-04-19 23:58:51 +0200359
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200360PyDoc_STRVAR(threadinfo__doc__,
361"sys.thread_info\n\
362\n\
363A struct sequence holding information about the thread implementation.");
364
365static PyStructSequence_Field threadinfo_fields[] = {
366 {"name", "name of the thread implementation"},
367 {"lock", "name of the lock implementation"},
368 {"version", "name and version of the thread library"},
369 {0}
370};
371
372static PyStructSequence_Desc threadinfo_desc = {
373 "sys.thread_info", /* name */
374 threadinfo__doc__, /* doc */
375 threadinfo_fields, /* fields */
376 3
377};
378
379static PyTypeObject ThreadInfoType;
380
Victor Stinner754851f2011-04-19 23:58:51 +0200381PyObject*
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200382PyThread_GetInfo(void)
Victor Stinner754851f2011-04-19 23:58:51 +0200383{
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200384 PyObject *threadinfo, *value;
385 int pos = 0;
Victor Stinnere07f5222011-04-20 12:23:26 +0200386#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
387 && defined(_CS_GNU_LIBPTHREAD_VERSION))
Victor Stinner754851f2011-04-19 23:58:51 +0200388 char buffer[255];
389 int len;
Victor Stinnere07f5222011-04-20 12:23:26 +0200390#endif
Victor Stinner754851f2011-04-19 23:58:51 +0200391
Victor Stinner1c8f0592013-07-22 22:24:54 +0200392 if (ThreadInfoType.tp_name == 0) {
393 if (PyStructSequence_InitType2(&ThreadInfoType, &threadinfo_desc) < 0)
394 return NULL;
395 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200396
397 threadinfo = PyStructSequence_New(&ThreadInfoType);
398 if (threadinfo == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200399 return NULL;
400
401 value = PyUnicode_FromString(PYTHREAD_NAME);
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200402 if (value == NULL) {
403 Py_DECREF(threadinfo);
404 return NULL;
405 }
406 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200407
408#ifdef _POSIX_THREADS
409#ifdef USE_SEMAPHORES
410 value = PyUnicode_FromString("semaphore");
411#else
412 value = PyUnicode_FromString("mutex+cond");
413#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200414 if (value == NULL) {
415 Py_DECREF(threadinfo);
Victor Stinner754851f2011-04-19 23:58:51 +0200416 return NULL;
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200417 }
418#else
419 Py_INCREF(Py_None);
420 value = Py_None;
421#endif
422 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200423
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200424#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
425 && defined(_CS_GNU_LIBPTHREAD_VERSION))
426 value = NULL;
Victor Stinner754851f2011-04-19 23:58:51 +0200427 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer));
Victor Stinner98ea54c2014-08-15 23:30:40 +0200428 if (1 < len && (size_t)len < sizeof(buffer)) {
Victor Stinner754851f2011-04-19 23:58:51 +0200429 value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
430 if (value == NULL)
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200431 PyErr_Clear();
Victor Stinner754851f2011-04-19 23:58:51 +0200432 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200433 if (value == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200434#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200435 {
436 Py_INCREF(Py_None);
437 value = Py_None;
438 }
439 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
440 return threadinfo;
Victor Stinner754851f2011-04-19 23:58:51 +0200441}