FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/Modules/posixmodule.c at master · pylee/python · GitHub
python/Modules/posixmodule.c at master · pylee/python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
pylee
/
python
Public
forked from
glix/python
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
Modules
/
posixmodule.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
9022 lines (8094 loc) · 225 KB
Breadcrumbs
python
/
Modules
/
posixmodule.c
Copy path
File metadata and controls
9022 lines (8094 loc) · 225 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* POSIX module implementation */
/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
module actually calls itself 'nt' or 'os2', not 'posix', and a few
functions are either unimplemented or implemented differently. The source
assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
of the compiler used. Different compilers define their own feature
test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
independent macro PYOS_OS2 should be defined. On OS/2 the default
compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
as the compiler specific macro for the EMX port of gcc to OS/2. */
/* See also ../Dos/dosmodule.c */
#ifdef
__APPLE__
/*
* Step 1 of support for weak-linking a number of symbols existing on
* OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
* at the end of this file for more information.
*/
# pragma
weak lchown
# pragma
weak statvfs
# pragma
weak fstatvfs
#endif
/* __APPLE__ */
#define
PY_SSIZE_T_CLEAN
#include
"Python.h"
#include
"structseq.h"
#if
defined(
__VMS
)
# include
<unixio.h>
#endif
/* defined(__VMS) */
#ifdef
__cplusplus
extern
"C"
{
#endif
PyDoc_STRVAR
(
posix__doc__
,
"This module provides access to operating system functionality that is\n\
standardized by the C Standard and the POSIX standard (a thinly\n\
disguised Unix interface). Refer to the library manual and\n\
corresponding Unix manual entries for more information on calls."
);
#ifndef
Py_USING_UNICODE
/* This is used in signatures of functions. */
#define
Py_UNICODE
void
#endif
#if
defined(
PYOS_OS2
)
#define
INCL_DOS
#define
INCL_DOSERRORS
#define
INCL_DOSPROCESS
#define
INCL_NOPMAPI
#include
<os2.h>
#if
defined(
PYCC_GCC
)
#include
<ctype.h>
#include
<io.h>
#include
<stdio.h>
#include
<process.h>
#endif
#include
"osdefs.h"
#endif
#ifdef
HAVE_SYS_TYPES_H
#include
<sys/types.h>
#endif
/* HAVE_SYS_TYPES_H */
#ifdef
HAVE_SYS_STAT_H
#include
<sys/stat.h>
#endif
/* HAVE_SYS_STAT_H */
#ifdef
HAVE_SYS_WAIT_H
#include
<sys/wait.h>
/* For WNOHANG */
#endif
#ifdef
HAVE_SIGNAL_H
#include
<signal.h>
#endif
#ifdef
HAVE_FCNTL_H
#include
<fcntl.h>
#endif
/* HAVE_FCNTL_H */
#ifdef
HAVE_GRP_H
#include
<grp.h>
#endif
#ifdef
HAVE_SYSEXITS_H
#include
<sysexits.h>
#endif
/* HAVE_SYSEXITS_H */
#ifdef
HAVE_SYS_LOADAVG_H
#include
<sys/loadavg.h>
#endif
/* Various compilers have only certain posix functions */
/* XXX Gosh I wish these were all moved into pyconfig.h */
#if
defined(
PYCC_VACPP
)
&&
defined(
PYOS_OS2
)
#include
<process.h>
#else
#if
defined(
__WATCOMC__
)
&&
!defined(
__QNX__
)
/* Watcom compiler */
#define
HAVE_GETCWD
1
#define
HAVE_OPENDIR
1
#define
HAVE_SYSTEM
1
#if
defined(
__OS2__
)
#define
HAVE_EXECV
1
#define
HAVE_WAIT
1
#endif
#include
<process.h>
#else
#ifdef
__BORLANDC__
/* Borland compiler */
#define
HAVE_EXECV
1
#define
HAVE_GETCWD
1
#define
HAVE_OPENDIR
1
#define
HAVE_PIPE
1
#define
HAVE_POPEN
1
#define
HAVE_SYSTEM
1
#define
HAVE_WAIT
1
#else
#ifdef
_MSC_VER
/* Microsoft compiler */
#define
HAVE_GETCWD
1
#define
HAVE_SPAWNV
1
#define
HAVE_EXECV
1
#define
HAVE_PIPE
1
#define
HAVE_POPEN
1
#define
HAVE_SYSTEM
1
#define
HAVE_CWAIT
1
#define
HAVE_FSYNC
1
#define
fsync
_commit
#else
#if
defined(
PYOS_OS2
)
&&
defined(
PYCC_GCC
)
||
defined(
__VMS
)
/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
#else
/* all other compilers */
/* Unix functions that the configure script doesn't check for */
#define
HAVE_EXECV
1
#define
HAVE_FORK
1
#if
defined(
__USLC__
)
&&
defined(
__SCO_VERSION__
)
/* SCO UDK Compiler */
#define
HAVE_FORK1
1
#endif
#define
HAVE_GETCWD
1
#define
HAVE_GETEGID
1
#define
HAVE_GETEUID
1
#define
HAVE_GETGID
1
#define
HAVE_GETPPID
1
#define
HAVE_GETUID
1
#define
HAVE_KILL
1
#define
HAVE_OPENDIR
1
#define
HAVE_PIPE
1
#ifndef
__rtems__
#define
HAVE_POPEN
1
#endif
#define
HAVE_SYSTEM
1
#define
HAVE_WAIT
1
#define
HAVE_TTYNAME
1
#endif
/* PYOS_OS2 && PYCC_GCC && __VMS */
#endif
/* _MSC_VER */
#endif
/* __BORLANDC__ */
#endif
/* ! __WATCOMC__ || __QNX__ */
#endif
/* ! __IBMC__ */
#ifndef
_MSC_VER
#if
defined(
__sgi
)
&&
_COMPILER_VERSION
>=
700
/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
(default) */
extern
char
*
ctermid_r
(
char
*
);
#endif
#ifndef
HAVE_UNISTD_H
#if
defined(
PYCC_VACPP
)
extern
int
mkdir
(
char
*
);
#else
#if
( defined(
__WATCOMC__
)
||
defined(
_MSC_VER
) )
&&
!defined(
__QNX__
)
extern
int
mkdir
(
const
char
*
);
#else
extern
int
mkdir
(
const
char
*
,
mode_t
);
#endif
#endif
#if
defined(
__IBMC__
)
||
defined(
__IBMCPP__
)
extern
int
chdir
(
char
*
);
extern
int
rmdir
(
char
*
);
#else
extern
int
chdir
(
const
char
*
);
extern
int
rmdir
(
const
char
*
);
#endif
#ifdef
__BORLANDC__
extern
int
chmod
(
const
char
*
,
int
);
#else
extern
int
chmod
(
const
char
*
,
mode_t
);
#endif
/*#ifdef HAVE_FCHMOD
extern int fchmod(int, mode_t);
#endif*/
/*#ifdef HAVE_LCHMOD
extern int lchmod(const char *, mode_t);
#endif*/
extern
int
chown
(
const
char
*
,
uid_t
,
gid_t
);
extern
char
*
getcwd
(
char
*
,
int
);
extern
char
*
strerror
(
int
);
extern
int
link
(
const
char
*
,
const
char
*
);
extern
int
rename
(
const
char
*
,
const
char
*
);
extern
int
stat
(
const
char
*
,
struct
stat
*
);
extern
int
unlink
(
const
char
*
);
extern
int
pclose
(
FILE
*
);
#ifdef
HAVE_SYMLINK
extern
int
symlink
(
const
char
*
,
const
char
*
);
#endif
/* HAVE_SYMLINK */
#ifdef
HAVE_LSTAT
extern
int
lstat
(
const
char
*
,
struct
stat
*
);
#endif
/* HAVE_LSTAT */
#endif
/* !HAVE_UNISTD_H */
#endif
/* !_MSC_VER */
#ifdef
HAVE_UTIME_H
#include
<utime.h>
#endif
/* HAVE_UTIME_H */
#ifdef
HAVE_SYS_UTIME_H
#include
<sys/utime.h>
#define
HAVE_UTIME_H
/* pretend we do for the rest of this file */
#endif
/* HAVE_SYS_UTIME_H */
#ifdef
HAVE_SYS_TIMES_H
#include
<sys/times.h>
#endif
/* HAVE_SYS_TIMES_H */
#ifdef
HAVE_SYS_PARAM_H
#include
<sys/param.h>
#endif
/* HAVE_SYS_PARAM_H */
#ifdef
HAVE_SYS_UTSNAME_H
#include
<sys/utsname.h>
#endif
/* HAVE_SYS_UTSNAME_H */
#ifdef
HAVE_DIRENT_H
#include
<dirent.h>
#define
NAMLEN
(
dirent
) strlen((dirent)->d_name)
#else
#if
defined(
__WATCOMC__
)
&&
!defined(
__QNX__
)
#include
<direct.h>
#define
NAMLEN
(
dirent
) strlen((dirent)->d_name)
#else
#define
dirent
direct
#define
NAMLEN
(
dirent
) (dirent)->d_namlen
#endif
#ifdef
HAVE_SYS_NDIR_H
#include
<sys/ndir.h>
#endif
#ifdef
HAVE_SYS_DIR_H
#include
<sys/dir.h>
#endif
#ifdef
HAVE_NDIR_H
#include
<ndir.h>
#endif
#endif
#ifdef
_MSC_VER
#ifdef
HAVE_DIRECT_H
#include
<direct.h>
#endif
#ifdef
HAVE_IO_H
#include
<io.h>
#endif
#ifdef
HAVE_PROCESS_H
#include
<process.h>
#endif
#include
"osdefs.h"
#include
<windows.h>
#include
<shellapi.h>
/* for ShellExecute() */
#define
popen
_popen
#define
pclose
_pclose
#endif
/* _MSC_VER */
#if
defined(
PYCC_VACPP
)
&&
defined(
PYOS_OS2
)
#include
<io.h>
#endif
/* OS2 */
#ifndef
MAXPATHLEN
#if
defined(
PATH_MAX
)
&&
PATH_MAX
>
1024
#define
MAXPATHLEN
PATH_MAX
#else
#define
MAXPATHLEN
1024
#endif
#endif
/* MAXPATHLEN */
#ifdef
UNION_WAIT
/* Emulate some macros on systems that have a union instead of macros */
#ifndef
WIFEXITED
#define
WIFEXITED
(
u_wait
) (!(u_wait).w_termsig && !(u_wait).w_coredump)
#endif
#ifndef
WEXITSTATUS
#define
WEXITSTATUS
(
u_wait
) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
#endif
#ifndef
WTERMSIG
#define
WTERMSIG
(
u_wait
) ((u_wait).w_termsig)
#endif
#define
WAIT_TYPE
union wait
#define
WAIT_STATUS_INT
(
s
) (s.w_status)
#else
/* !UNION_WAIT */
#define
WAIT_TYPE
int
#define
WAIT_STATUS_INT
(
s
) (s)
#endif
/* UNION_WAIT */
/* Don't use the "_r" form if we don't need it (also, won't have a
prototype for it, at least on Solaris -- maybe others as well?). */
#if
defined(
HAVE_CTERMID_R
)
&&
defined(
WITH_THREAD
)
#define
USE_CTERMID_R
#endif
#if
defined(
HAVE_TMPNAM_R
)
&&
defined(
WITH_THREAD
)
#define
USE_TMPNAM_R
#endif
/* choose the appropriate stat and fstat functions and return structs */
#undef
STAT
#if
defined(
MS_WIN64
)
||
defined(
MS_WINDOWS
)
# define
STAT
win32_stat
# define
FSTAT
win32_fstat
# define
STRUCT_STAT
struct win32_stat
#else
# define
STAT
stat
# define
FSTAT
fstat
# define
STRUCT_STAT
struct stat
#endif
#if
defined(
MAJOR_IN_MKDEV
)
#include
<sys/mkdev.h>
#else
#if
defined(
MAJOR_IN_SYSMACROS
)
#include
<sys/sysmacros.h>
#endif
#if
defined(
HAVE_MKNOD
)
&&
defined(
HAVE_SYS_MKDEV_H
)
#include
<sys/mkdev.h>
#endif
#endif
/* Return a dictionary corresponding to the POSIX environment table */
#ifdef
WITH_NEXT_FRAMEWORK
/* On Darwin/MacOSX a shared library or framework has no access to
** environ directly, we must obtain it with _NSGetEnviron().
*/
#include
<crt_externs.h>
static
char
*
*
environ
;
#elif
!defined(
_MSC_VER
)
&&
( !defined(
__WATCOMC__
)
||
defined(
__QNX__
) )
extern
char
*
*
environ
;
#endif
/* !_MSC_VER */
static
PyObject
*
convertenviron
(
void
)
{
PyObject
*
d
;
char
*
*
e
;
d
=
PyDict_New
();
if
(
d
==
NULL
)
return
NULL
;
#ifdef
WITH_NEXT_FRAMEWORK
if
(
environ
==
NULL
)
environ
=
*
_NSGetEnviron
();
#endif
if
(
environ
==
NULL
)
return
d
;
/* This part ignores errors */
for
(
e
=
environ
;
*
e
!=
NULL
;
e
++
) {
PyObject
*
k
;
PyObject
*
v
;
char
*
p
=
strchr
(
*
e
,
'='
);
if
(
p
==
NULL
)
continue
;
k
=
PyString_FromStringAndSize
(
*
e
, (
int
)(
p
-
*
e
));
if
(
k
==
NULL
) {
PyErr_Clear
();
continue
;
}
v
=
PyString_FromString
(
p
+
1
);
if
(
v
==
NULL
) {
PyErr_Clear
();
Py_DECREF
(
k
);
continue
;
}
if
(
PyDict_GetItem
(
d
,
k
)
==
NULL
) {
if
(
PyDict_SetItem
(
d
,
k
,
v
)
!=
0
)
PyErr_Clear
();
}
Py_DECREF
(
k
);
Py_DECREF
(
v
);
}
#if
defined(
PYOS_OS2
)
{
APIRET
rc
;
char
buffer
[
1024
];
/* OS/2 Provides a Documented Max of 1024 Chars */
rc
=
DosQueryExtLIBPATH
(
buffer
,
BEGIN_LIBPATH
);
if
(
rc
==
NO_ERROR
) {
/* (not a type, envname is NOT 'BEGIN_LIBPATH') */
PyObject
*
v
=
PyString_FromString
(
buffer
);
PyDict_SetItemString
(
d
,
"BEGINLIBPATH"
,
v
);
Py_DECREF
(
v
);
}
rc
=
DosQueryExtLIBPATH
(
buffer
,
END_LIBPATH
);
if
(
rc
==
NO_ERROR
) {
/* (not a typo, envname is NOT 'END_LIBPATH') */
PyObject
*
v
=
PyString_FromString
(
buffer
);
PyDict_SetItemString
(
d
,
"ENDLIBPATH"
,
v
);
Py_DECREF
(
v
);
}
}
#endif
return
d
;
}
/* Set a POSIX-specific error from errno, and return NULL */
static
PyObject
*
posix_error
(
void
)
{
return
PyErr_SetFromErrno
(
PyExc_OSError
);
}
static
PyObject
*
posix_error_with_filename
(
char
*
name
)
{
return
PyErr_SetFromErrnoWithFilename
(
PyExc_OSError
,
name
);
}
#ifdef
Py_WIN_WIDE_FILENAMES
static
PyObject
*
posix_error_with_unicode_filename
(
Py_UNICODE
*
name
)
{
return
PyErr_SetFromErrnoWithUnicodeFilename
(
PyExc_OSError
,
name
);
}
#endif
/* Py_WIN_WIDE_FILENAMES */
static
PyObject
*
posix_error_with_allocated_filename
(
char
*
name
)
{
PyObject
*
rc
=
PyErr_SetFromErrnoWithFilename
(
PyExc_OSError
,
name
);
PyMem_Free
(
name
);
return
rc
;
}
#ifdef
MS_WINDOWS
static
PyObject
*
win32_error
(
char
*
function
,
char
*
filename
)
{
/* XXX We should pass the function name along in the future.
(_winreg.c also wants to pass the function name.)
This would however require an additional param to the
Windows error object, which is non-trivial.
*/
errno
=
GetLastError
();
if
(
filename
)
return
PyErr_SetFromWindowsErrWithFilename
(
errno
,
filename
);
else
return
PyErr_SetFromWindowsErr
(
errno
);
}
#ifdef
Py_WIN_WIDE_FILENAMES
static
PyObject
*
win32_error_unicode
(
char
*
function
,
Py_UNICODE
*
filename
)
{
/* XXX - see win32_error for comments on 'function' */
errno
=
GetLastError
();
if
(
filename
)
return
PyErr_SetFromWindowsErrWithUnicodeFilename
(
errno
,
filename
);
else
return
PyErr_SetFromWindowsErr
(
errno
);
}
static
int
convert_to_unicode
(
PyObject
*
*
param
)
{
if
(
PyUnicode_CheckExact
(
*
param
))
Py_INCREF
(
*
param
);
else
if
(
PyUnicode_Check
(
*
param
))
/* For a Unicode subtype that's not a Unicode object,
return a true Unicode object with the same data. */
*
param
=
PyUnicode_FromUnicode
(
PyUnicode_AS_UNICODE
(
*
param
),
PyUnicode_GET_SIZE
(
*
param
));
else
*
param
=
PyUnicode_FromEncodedObject
(
*
param
,
Py_FileSystemDefaultEncoding
,
"strict"
);
return
(
*
param
)
!=
NULL
;
}
#endif
/* Py_WIN_WIDE_FILENAMES */
#endif
#if
defined(
PYOS_OS2
)
/**********************************************************************
* Helper Function to Trim and Format OS/2 Messages
**********************************************************************/
static
void
os2_formatmsg
(
char
*
msgbuf
,
int
msglen
,
char
*
reason
)
{
msgbuf
[
msglen
]
=
'\0'
;
/* OS/2 Doesn't Guarantee a Terminator */
if
(
strlen
(
msgbuf
)
>
0
) {
/* If Non-Empty Msg, Trim CRLF */
char
*
lastc
=
&
msgbuf
[
strlen
(
msgbuf
)
-
1
];
while
(
lastc
>
msgbuf
&&
isspace
(
Py_CHARMASK
(
*
lastc
)))
*
lastc
--
=
'\0'
;
/* Trim Trailing Whitespace (CRLF) */
}
/* Add Optional Reason Text */
if
(
reason
) {
strcat
(
msgbuf
,
" : "
);
strcat
(
msgbuf
,
reason
);
}
}
/**********************************************************************
* Decode an OS/2 Operating System Error Code
*
* A convenience function to lookup an OS/2 error code and return a
* text message we can use to raise a Python exception.
*
* Notes:
* The messages for errors returned from the OS/2 kernel reside in
* the file OSO001.MSG in the \OS2 directory hierarchy.
*
**********************************************************************/
static
char
*
os2_strerror
(
char
*
msgbuf
,
int
msgbuflen
,
int
errorcode
,
char
*
reason
)
{
APIRET
rc
;
ULONG
msglen
;
/* Retrieve Kernel-Related Error Message from OSO001.MSG File */
Py_BEGIN_ALLOW_THREADS
rc
=
DosGetMessage
(
NULL
,
0
,
msgbuf
,
msgbuflen
,
errorcode
,
"oso001.msg"
,
&
msglen
);
Py_END_ALLOW_THREADS
if
(
rc
==
NO_ERROR
)
os2_formatmsg
(
msgbuf
,
msglen
,
reason
);
else
PyOS_snprintf
(
msgbuf
,
msgbuflen
,
"unknown OS error #%d"
,
errorcode
);
return
msgbuf
;
}
/* Set an OS/2-specific error and return NULL. OS/2 kernel
errors are not in a global variable e.g. 'errno' nor are
they congruent with posix error numbers. */
static
PyObject
*
os2_error
(
int
code
)
{
char
text
[
1024
];
PyObject
*
v
;
os2_strerror
(
text
,
sizeof
(
text
),
code
,
""
);
v
=
Py_BuildValue
(
"(is)"
,
code
,
text
);
if
(
v
!=
NULL
) {
PyErr_SetObject
(
PyExc_OSError
,
v
);
Py_DECREF
(
v
);
}
return
NULL
;
/* Signal to Python that an Exception is Pending */
}
#endif
/* OS2 */
/* POSIX generic methods */
static
PyObject
*
posix_fildes
(
PyObject
*
fdobj
,
int
(
*
func
)(
int
))
{
int
fd
;
int
res
;
fd
=
PyObject_AsFileDescriptor
(
fdobj
);
if
(
fd
<
0
)
return
NULL
;
Py_BEGIN_ALLOW_THREADS
res
=
(
*
func
)(
fd
);
Py_END_ALLOW_THREADS
if
(
res
<
0
)
return
posix_error
();
Py_INCREF
(
Py_None
);
return
Py_None
;
}
#ifdef
Py_WIN_WIDE_FILENAMES
static
int
unicode_file_names
(
void
)
{
static
int
canusewide
=
-1
;
if
(
canusewide
==
-1
) {
/* As per doc for ::GetVersion(), this is the correct test for
the Windows NT family. */
canusewide
=
(
GetVersion
()
<
0x80000000
) ?
1
:
0
;
}
return
canusewide
;
}
#endif
static
PyObject
*
posix_1str
(
PyObject
*
args
,
char
*
format
,
int
(
*
func
)(
const
char
*
))
{
char
*
path1
=
NULL
;
int
res
;
if
(!
PyArg_ParseTuple
(
args
,
format
,
Py_FileSystemDefaultEncoding
,
&
path1
))
return
NULL
;
Py_BEGIN_ALLOW_THREADS
res
=
(
*
func
)(
path1
);
Py_END_ALLOW_THREADS
if
(
res
<
0
)
return
posix_error_with_allocated_filename
(
path1
);
PyMem_Free
(
path1
);
Py_INCREF
(
Py_None
);
return
Py_None
;
}
static
PyObject
*
posix_2str
(
PyObject
*
args
,
char
*
format
,
int
(
*
func
)(
const
char
*
,
const
char
*
))
{
char
*
path1
=
NULL
,
*
path2
=
NULL
;
int
res
;
if
(!
PyArg_ParseTuple
(
args
,
format
,
Py_FileSystemDefaultEncoding
,
&
path1
,
Py_FileSystemDefaultEncoding
,
&
path2
))
return
NULL
;
Py_BEGIN_ALLOW_THREADS
res
=
(
*
func
)(
path1
,
path2
);
Py_END_ALLOW_THREADS
PyMem_Free
(
path1
);
PyMem_Free
(
path2
);
if
(
res
!=
0
)
/* XXX how to report both path1 and path2??? */
return
posix_error
();
Py_INCREF
(
Py_None
);
return
Py_None
;
}
#ifdef
Py_WIN_WIDE_FILENAMES
static
PyObject
*
win32_1str
(
PyObject
*
args
,
char
*
func
,
char
*
format
,
BOOL
(__stdcall
*
funcA
)(
LPCSTR
),
char
*
wformat
,
BOOL
(__stdcall
*
funcW
)(
LPWSTR
))
{
PyObject
*
uni
;
char
*
ansi
;
BOOL
result
;
if
(
unicode_file_names
()) {
if
(!
PyArg_ParseTuple
(
args
,
wformat
,
&
uni
))
PyErr_Clear
();
else
{
Py_BEGIN_ALLOW_THREADS
result
=
funcW
(
PyUnicode_AsUnicode
(
uni
));
Py_END_ALLOW_THREADS
if
(!
result
)
return
win32_error_unicode
(
func
,
PyUnicode_AsUnicode
(
uni
));
Py_INCREF
(
Py_None
);
return
Py_None
;
}
}
if
(!
PyArg_ParseTuple
(
args
,
format
,
&
ansi
))
return
NULL;
Py_BEGIN_ALLOW_THREADS
result
=
funcA
(
ansi
);
Py_END_ALLOW_THREADS
if
(!
result
)
return
win32_error
(
func
,
ansi
);
Py_INCREF
(
Py_None
);
return
Py_None
;
}
/* This is a reimplementation of the C library's chdir function,
but one that produces Win32 errors instead of DOS error codes.
chdir is essentially a wrapper around SetCurrentDirectory; however,
it also needs to set "magic" environment variables indicating
the per-drive current directory, which are of the form =<drive>: */
static
BOOL
__stdcall
win32_chdir
(
LPCSTR
path
)
{
char
new_path
[
MAX_PATH
+
1
];
int
result
;
char
env
[
4
]
=
"=x:"
;
if
(!
SetCurrentDirectoryA
(
path
))
return
FALSE
;
result
=
GetCurrentDirectoryA
(
MAX_PATH
+
1
,
new_path
);
if
(!
result
)
return
FALSE
;
/* In the ANSI API, there should not be any paths longer
than MAX_PATH. */
assert
(
result
<=
MAX_PATH
+1
);
if
(
strncmp
(
new_path
,
"\\\\"
,
2
)
==
0
||
strncmp
(
new_path
,
"//"
,
2
)
==
0
)
/* UNC path, nothing to do. */
return
TRUE;
env
[
1
]
=
new_path
[
0
];
return
SetEnvironmentVariableA
(
env
,
new_path
);
}
/* The Unicode version differs from the ANSI version
since the current directory might exceed MAX_PATH characters */
static
BOOL
__stdcall
win32_wchdir
(
LPCWSTR
path
)
{
wchar_t
_new_path
[
MAX_PATH
+
1
],
*
new_path
=
_new_path
;
int
result
;
wchar_t
env
[
4
]
=
L"=x:"
;
if
(!
SetCurrentDirectoryW
(
path
))
return
FALSE;
result
=
GetCurrentDirectoryW
(
MAX_PATH
+
1
,
new_path
);
if
(!
result
)
return
FALSE;
if
(
result
>
MAX_PATH
+
1
) {
new_path
=
malloc
(
result
*
sizeof
(
wchar_t
));
if
(!
new_path
) {
SetLastError
(
ERROR_OUTOFMEMORY
);
return
FALSE;
}
result
=
GetCurrentDirectoryW
(
result
,
new_path
);
if
(!
result
) {
free
(
new_path
);
return
FALSE;
}
}
if
(
wcsncmp
(
new_path
,
L"\\\\"
,
2
)
==
0
||
wcsncmp
(
new_path
,
L"//"
,
2
)
==
0
)
/* UNC path, nothing to do. */
return
TRUE;
env
[
1
]
=
new_path
[
0
];
result
=
SetEnvironmentVariableW
(
env
,
new_path
);
if
(
new_path
!=
_new_path
)
free
(
new_path
);
return
result
;
}
#endif
#ifdef
MS_WINDOWS
/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
- time stamps are restricted to second resolution
- file modification times suffer from forth-and-back conversions between
UTC and local time
Therefore, we implement our own stat, based on the Win32 API directly.
*/
#define
HAVE_STAT_NSEC
1
struct
win32_stat
{
int
st_dev
;
__int64
st_ino
;
unsigned short
st_mode
;
int
st_nlink
;
int
st_uid
;
int
st_gid
;
int
st_rdev
;
__int64
st_size
;
int
st_atime
;
int
st_atime_nsec
;
int
st_mtime
;
int
st_mtime_nsec
;
int
st_ctime
;
int
st_ctime_nsec
;
};
static
__int64
secs_between_epochs
=
11644473600
;
/* Seconds between 1.1.1601 and 1.1.1970 */
static
void
FILE_TIME_to_time_t_nsec
(
FILETIME
*
in_ptr
,
int
*
time_out
,
int
*
nsec_out
)
{
/* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
/* Cannot simply cast and dereference in_ptr,
since it might not be aligned properly */
__int64
in
;
memcpy
(
&
in
,
in_ptr
,
sizeof
(
in
));
*
nsec_out
=
(
int
)(
in
%
10000000
)
*
100
;
/* FILETIME is in units of 100 nsec. */
/* XXX Win32 supports time stamps past 2038; we currently don't */
*
time_out
=
Py_SAFE_DOWNCAST
((
in
/
10000000
)
-
secs_between_epochs
,
__int64
,
int
);
}
static
void
time_t_to_FILE_TIME
(
int
time_in
,
int
nsec_in
,
FILETIME
*
out_ptr
)
{
/* XXX endianness */
__int64
out
;
out
=
time_in
+
secs_between_epochs
;
out
=
out
*
10000000
+
nsec_in
/
100
;
memcpy
(
out_ptr
,
&
out
,
sizeof
(
out
));
}
/* Below, we *know* that ugo+r is 0444 */
#if
_S_IREAD
!=
0400
#error
Unsupported C library
#endif
static
int
attributes_to_mode
(
DWORD
attr
)
{
int
m
=
0
;
if
(
attr
&
FILE_ATTRIBUTE_DIRECTORY
)
m
|=
_S_IFDIR
|
0111
;
/* IFEXEC for user,group,other */
else
m
|=
_S_IFREG
;
if
(
attr
&
FILE_ATTRIBUTE_READONLY
)
m
|=
0444
;
else
m
|=
0666
;
return
m
;
}
static
int
attribute_data_to_stat
(
WIN32_FILE_ATTRIBUTE_DATA
*
info
,
struct
win32_stat
*
result
)
{
memset
(
result
,
0
,
sizeof
(
*
result
));
result
->
st_mode
=
attributes_to_mode
(
info
->
dwFileAttributes
);
result
->
st_size
=
(((
__int64
)
info
->
nFileSizeHigh
)<<
32
)
+
info
->
nFileSizeLow
;
FILE_TIME_to_time_t_nsec
(
&
info
->
ftCreationTime
,
&
result
->
st_ctime
,
&
result
->
st_ctime_nsec
);
FILE_TIME_to_time_t_nsec
(
&
info
->
ftLastWriteTime
,
&
result
->
st_mtime
,
&
result
->
st_mtime_nsec
);
FILE_TIME_to_time_t_nsec
(
&
info
->
ftLastAccessTime
,
&
result
->
st_atime
,
&
result
->
st_atime_nsec
);
return
0
;
}
/* Emulate GetFileAttributesEx[AW] on Windows 95 */
static
int
checked
=
0
;
static
BOOL
(
CALLBACK
*
gfaxa
)(
LPCSTR
,
GET_FILEEX_INFO_LEVELS
,
LPVOID
);
static
BOOL
(
CALLBACK
*
gfaxw
)(
LPCWSTR
,
GET_FILEEX_INFO_LEVELS
,
LPVOID
);
static
void
check_gfax
()
{
HINSTANCE
hKernel32
;
if
(
checked
)
return
;
checked
=
1
;
hKernel32
=
GetModuleHandle
(
"KERNEL32"
);
*
(
FARPROC
*
)
&
gfaxa
=
GetProcAddress
(
hKernel32
,
"GetFileAttributesExA"
);
*
(
FARPROC
*
)
&
gfaxw
=
GetProcAddress
(
hKernel32
,
"GetFileAttributesExW"
);
}
static
BOOL
attributes_from_dir
(
LPCSTR
pszFile
,
LPWIN32_FILE_ATTRIBUTE_DATA
pfad
)
{
HANDLE
hFindFile
;
WIN32_FIND_DATAA
FileData
;
hFindFile
=
FindFirstFileA
(
pszFile
,
&
FileData
);
if
(
hFindFile
==
INVALID_HANDLE_VALUE
)
return
FALSE;
FindClose
(
hFindFile
);
pfad
->
dwFileAttributes
=
FileData
.
dwFileAttributes
;
pfad
->
ftCreationTime
=
FileData
.
ftCreationTime
;
pfad
->
ftLastAccessTime
=
FileData
.
ftLastAccessTime
;
pfad
->
ftLastWriteTime
=
FileData
.
ftLastWriteTime
;
pfad
->
nFileSizeHigh
=
FileData
.
nFileSizeHigh
;
pfad
->
nFileSizeLow
=
FileData
.
nFileSizeLow
;
return
TRUE;
}
static
BOOL
attributes_from_dir_w
(
LPCWSTR
pszFile
,
LPWIN32_FILE_ATTRIBUTE_DATA
pfad
)
{
HANDLE
hFindFile
;
WIN32_FIND_DATAW
FileData
;
hFindFile
=
FindFirstFileW
(
pszFile
,
&
FileData
);
if
(
hFindFile
==
INVALID_HANDLE_VALUE
)
return
FALSE;
FindClose
(
hFindFile
);
pfad
->
dwFileAttributes
=
FileData
.
dwFileAttributes
;
pfad
->
ftCreationTime
=
FileData
.
ftCreationTime
;
pfad
->
ftLastAccessTime
=
FileData
.
ftLastAccessTime
;
pfad
->
ftLastWriteTime
=
FileData
.
ftLastWriteTime
;
pfad
->
nFileSizeHigh
=
FileData
.
nFileSizeHigh
;
pfad
->
nFileSizeLow
=
FileData
.
nFileSizeLow
;
return
TRUE;
}
static
BOOL
WINAPI
Py_GetFileAttributesExA
(
LPCSTR
pszFile
,
GET_FILEEX_INFO_LEVELS
level
,
LPVOID
pv
)
{
BOOL
result
;
LPWIN32_FILE_ATTRIBUTE_DATA
pfad
=
pv
;
/* First try to use the system's implementation, if that is
available and either succeeds to gives an error other than
that it isn't implemented. */
check_gfax
();
if
(
gfaxa
) {
result
=
gfaxa
(
pszFile
,
level
,
pv
);
if
(
result
||
GetLastError
()
!=
ERROR_CALL_NOT_IMPLEMENTED
)
return
result
;
}
/* It's either not present, or not implemented.
Emulate using FindFirstFile. */
if
(
level
!=
GetFileExInfoStandard
) {
SetLastError
(
ERROR_INVALID_PARAMETER
);
return
FALSE;
}
/* Use GetFileAttributes to validate that the file name
does not contain wildcards (which FindFirstFile would
accept). */
if
(
GetFileAttributesA
(
pszFile
)
==
0xFFFFFFFF
)
return
FALSE;
return
attributes_from_dir
(
pszFile
,
pfad
);
}
static
BOOL
WINAPI
Py_GetFileAttributesExW
(
LPCWSTR
pszFile
,
GET_FILEEX_INFO_LEVELS
level
,
LPVOID
pv
)
{
BOOL
result
;
LPWIN32_FILE_ATTRIBUTE_DATA
pfad
=
pv
;
/* First try to use the system's implementation, if that is
available and either succeeds to gives an error other than
that it isn't implemented. */
check_gfax
();
if
(
gfaxa
) {
result
=
gfaxw
(
pszFile
,
level
,
pv
);
if
(
result
||
GetLastError
()
!=
ERROR_CALL_NOT_IMPLEMENTED
)
return
result
;
}
/* It's either not present, or not implemented.
Emulate using FindFirstFile. */
if
(
level
!=
GetFileExInfoStandard
) {
SetLastError
(
ERROR_INVALID_PARAMETER
);
return
FALSE;
}
/* Use GetFileAttributes to validate that the file name
does not contain wildcards (which FindFirstFile would
accept). */
if
(
GetFileAttributesW
(
pszFile
)
==
0xFFFFFFFF
)
return
FALSE;
return
attributes_from_dir_w
(
pszFile
,
pfad
);
}
static
int
win32_stat
(
const
char
*
path
,
struct
win32_stat
*
result
)
{
WIN32_FILE_ATTRIBUTE_DATA
info
;
int
code
;
char
*
dot
;
/* XXX not supported on Win95 and NT 3.x */
if
(!
Py_GetFileAttributesExA
(
path
,
GetFileExInfoStandard
,
&
info
)) {
if
(
GetLastError
()
!=
ERROR_SHARING_VIOLATION
) {
/* Protocol violation: we explicitly clear errno, instead of
setting it to a POSIX error. Callers should use GetLastError. */
errno
=
0
;
return
-1
;
}
else
{
/* Could not get attributes on open file. Fall back to
reading the directory. */
if
(!
attributes_from_dir
(
path
,
&
info
)) {
/* Very strange. This should not fail now */
errno
=
0
;
return
-1
;
}
}
}
code
=
attribute_data_to_stat
(
&
info
,
result
);
if
(
code
!=
0
)
return
code
;
/* Set S_IFEXEC if it is an .exe, .bat, ... */
dot
=
strrchr
(
path
,
'.'
);
if
(
dot
) {
if
(
stricmp
(
dot
,
".bat"
)
==
0
||
stricmp
(
dot
,
".cmd"
)
==
0
||
stricmp
(
dot
,
".exe"
)
==
0
||
stricmp
(
dot
,
".com"
)
==
0
)
result
->
st_mode
|=
0111
;
}
return
code
;
}
static
int
win32_wstat
(
const
wchar_t
*
path
,
struct
win32_stat
*
result
)
{
int
code
;
const
wchar_t
*
dot
;
WIN32_FILE_ATTRIBUTE_DATA
info
;
/* XXX not supported on Win95 and NT 3.x */
if
(!
Py_GetFileAttributesExW
(
path
,
GetFileExInfoStandard
,
&
info
)) {
if
(
GetLastError
()
!=
ERROR_SHARING_VIOLATION
) {
/* Protocol violation: we explicitly clear errno, instead of
setting it to a POSIX error. Callers should use GetLastError. */
errno
=
0
;
return
-1
;
}
else
{
/* Could not get attributes on open file. Fall back to
reading the directory. */
if
(!
attributes_from_dir_w
(
path
,
&
info
)) {
/* Very strange. This should not fail now */
errno
=
0
;
return
-1
;
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL