@@ -8133,39 +8133,45 @@ static PyObject *
81338133os_sched_getaffinity_impl (PyObject * module , pid_t pid )
81348134/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
81358135{
8136- int cpu , ncpus , count ;
8136+ int ncpus = NCPUS_START ;
81378137 size_t setsize ;
8138- cpu_set_t * mask = NULL ;
8139- PyObject * res = NULL ;
8138+ cpu_set_t * mask ;
81408139
8141- ncpus = NCPUS_START ;
81428140 while (1 ) {
81438141 setsize = CPU_ALLOC_SIZE (ncpus );
81448142 mask = CPU_ALLOC (ncpus );
8145- if (mask == NULL )
8143+ if (mask == NULL ) {
81468144 return PyErr_NoMemory ();
8147- if (sched_getaffinity (pid , setsize , mask ) == 0 )
8145+ }
8146+ if (sched_getaffinity (pid , setsize , mask ) == 0 ) {
81488147 break ;
8148+ }
81498149 CPU_FREE (mask );
8150- if (errno != EINVAL )
8150+ if (errno != EINVAL ) {
81518151 return posix_error ();
8152+ }
81528153 if (ncpus > INT_MAX / 2 ) {
8153- PyErr_SetString (PyExc_OverflowError , "could not allocate "
8154- "a large enough CPU set" );
8154+ PyErr_SetString (PyExc_OverflowError ,
8155+ "could not allocate a large enough CPU set" );
81558156 return NULL ;
81568157 }
8157- ncpus = ncpus * 2 ;
8158+ ncpus *= 2 ;
81588159 }
81598160
8160- res = PySet_New (NULL );
8161- if (res == NULL )
8161+ PyObject * res = PySet_New (NULL );
8162+ if (res == NULL ) {
81628163 goto error ;
8163- for (cpu = 0 , count = CPU_COUNT_S (setsize , mask ); count ; cpu ++ ) {
8164+ }
8165+
8166+ int cpu = 0 ;
8167+ int count = CPU_COUNT_S (setsize , mask );
8168+ for (; count ; cpu ++ ) {
81648169 if (CPU_ISSET_S (cpu , setsize , mask )) {
81658170 PyObject * cpu_num = PyLong_FromLong (cpu );
81668171 -- count ;
8167- if (cpu_num == NULL )
8172+ if (cpu_num == NULL ) {
81688173 goto error ;
8174+ }
81698175 if (PySet_Add (res , cpu_num )) {
81708176 Py_DECREF (cpu_num );
81718177 goto error ;
@@ -8177,12 +8183,12 @@ os_sched_getaffinity_impl(PyObject *module, pid_t pid)
81778183 return res ;
81788184
81798185error :
8180- if (mask )
8186+ if (mask ) {
81818187 CPU_FREE (mask );
8188+ }
81828189 Py_XDECREF (res );
81838190 return NULL ;
81848191}
8185-
81868192#endif /* HAVE_SCHED_SETAFFINITY */
81878193
81888194#endif /* HAVE_SCHED_H */
@@ -14333,44 +14339,49 @@ os_get_terminal_size_impl(PyObject *module, int fd)
1433314339/*[clinic input]
1433414340os.cpu_count
1433514341
14336- Return the number of CPUs in the system; return None if indeterminable .
14342+ Return the number of logical CPUs in the system.
1433714343
14338- This number is not equivalent to the number of CPUs the current process can
14339- use. The number of usable CPUs can be obtained with
14340- ``len(os.sched_getaffinity(0))``
14344+ Return None if indeterminable.
1434114345[clinic start generated code]*/
1434214346
1434314347static PyObject *
1434414348os_cpu_count_impl (PyObject * module )
14345- /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3 ]*/
14349+ /*[clinic end generated code: output=5fc29463c3936a9c input=ba2f6f8980a0e2eb ]*/
1434614350{
14347- int ncpu = 0 ;
14351+ int ncpu ;
1434814352#ifdef MS_WINDOWS
14349- #ifdef MS_WINDOWS_DESKTOP
14353+ # ifdef MS_WINDOWS_DESKTOP
1435014354 ncpu = GetActiveProcessorCount (ALL_PROCESSOR_GROUPS );
14351- #endif
14355+ # else
14356+ ncpu = 0 ;
14357+ # endif
14358+
1435214359#elif defined(__hpux )
1435314360 ncpu = mpctl (MPC_GETNUMSPUS , NULL , NULL );
14361+
1435414362#elif defined(HAVE_SYSCONF ) && defined(_SC_NPROCESSORS_ONLN )
1435514363 ncpu = sysconf (_SC_NPROCESSORS_ONLN );
14364+
1435614365#elif defined(__VXWORKS__ )
1435714366 ncpu = _Py_popcount32 (vxCpuEnabledGet ());
14367+
1435814368#elif defined(__DragonFly__ ) || \
1435914369 defined(__OpenBSD__ ) || \
1436014370 defined(__FreeBSD__ ) || \
1436114371 defined(__NetBSD__ ) || \
1436214372 defined(__APPLE__ )
14363- int mib [ 2 ] ;
14373+ ncpu = 0 ;
1436414374 size_t len = sizeof (ncpu );
14365- mib [0 ] = CTL_HW ;
14366- mib [1 ] = HW_NCPU ;
14367- if (sysctl (mib , 2 , & ncpu , & len , NULL , 0 ) != 0 )
14375+ int mib [2 ] = {CTL_HW , HW_NCPU };
14376+ if (sysctl (mib , 2 , & ncpu , & len , NULL , 0 ) != 0 ) {
1436814377 ncpu = 0 ;
14378+ }
1436914379#endif
14370- if (ncpu >= 1 )
14371- return PyLong_FromLong (ncpu );
14372- else
14380+
14381+ if (ncpu < 1 ) {
1437314382 Py_RETURN_NONE ;
14383+ }
14384+ return PyLong_FromLong (ncpu );
1437414385}
1437514386
1437614387
0 commit comments