summaryrefslogtreecommitdiffstats
path: root/Python/mysnprintf.c
blob: a08e249b42b1ef25a268c6f6c500d7aacc6227ce (plain)
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
#include "Python.h"

/* snprintf() wrappers.  If the platform has vsnprintf, we use it, else we
   emulate it in a half-hearted way.  Even if the platform has it, we wrap
   it because platforms differ in what vsnprintf does in case the buffer
   is too small:  C99 behavior is to return the number of characters that
   would have been written had the buffer not been too small, and to set
   the last byte of the buffer to \0.  At least MS _vsnprintf returns a
   negative value instead, and fills the entire buffer with non-\0 data.

   The wrappers ensure that str[size-1] is always \0 upon return.

   PyOS_snprintf and PyOS_vsnprintf never write more than size bytes
   (including the trailing '\0') into str.

   If the platform doesn't have vsnprintf, and the buffer size needed to
   avoid truncation exceeds size by more than 512, Python aborts with a
   Py_FatalError.

   Return value (rv):

    When 0 <= rv < size, the output conversion was unexceptional, and
    rv characters were written to str (excluding a trailing \0 byte at
    str[rv]).

    When rv >= size, output conversion was truncated, and a buffer of
    size rv+1 would have been needed to avoid truncation.  str[size-1]
    is \0 in this case.

    When rv < 0, "something bad happened".  str[size-1] is \0 in this
    case too, but the rest of str is unreliable.  It could be that
    an error in format codes was detected by libc, or on platforms
    with a non-C99 vsnprintf simply that the buffer wasn't big enough
    to avoid truncation, or on platforms without any vsnprintf that
    PyMem_Malloc couldn't obtain space for a temp buffer.

   CAUTION:  Unlike C99, str != NULL and size > 0 are required.
*/

int
PyOS_snprintf(char *str, size_t size, const  char  *format, ...)
{
    int rc;
    va_list va;

    va_start(va, format);
    rc = PyOS_vsnprintf(str, size, format, va);
    va_end(va);
    return rc;
}

int
PyOS_vsnprintf(char *str, size_t size, const char  *format, va_list va)
{
    int len;  /* # bytes written, excluding \0 */
#ifdef HAVE_SNPRINTF
#define _PyOS_vsnprintf_EXTRA_SPACE 1
#else
#define _PyOS_vsnprintf_EXTRA_SPACE 512
    char *buffer;
#endif
    assert(str != NULL);
    assert(size > 0);
    assert(format != NULL);
    /* We take a size_t as input but return an int.  Sanity check
     * our input so that it won't cause an overflow in the
     * vsnprintf return value or the buffer malloc size.  */
    if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
        len = -666;
        goto Done;
    }

#ifdef HAVE_SNPRINTF
    len = vsnprintf(str, size, format, va);
#else
    /* Emulate it. */
    buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
    if (buffer == NULL) {
        len = -666;
        goto Done;
    }

    len = vsprintf(buffer, format, va);
    if (len < 0)
        /* ignore the error */;

    else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE)
        Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");

    else {
        const size_t to_copy = (size_t)len < size ?
                                (size_t)len : size - 1;
        assert(to_copy < size);
        memcpy(str, buffer, to_copy);
        str[to_copy] = '\0';
    }
    PyMem_FREE(buffer);
#endif
Done:
    if (size > 0)
        str[size-1] = '\0';
    return len;
#undef _PyOS_vsnprintf_EXTRA_SPACE
}
n class="hl str"> The fullname is a str and the optional path is a str or None. Returns a Loader object or None. """ class MetaPathFinder(Finder): """Abstract base class for import finders on sys.meta_path.""" @abc.abstractmethod def find_module(self, fullname, path): """Abstract method which, when implemented, should find a module. The fullname is a str and the path is a list of strings or None. Returns a Loader object or None. """ def invalidate_caches(self): """An optional method for clearing the finder's cache, if any. This method is used by importlib.invalidate_caches(). """ _register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter, machinery.PathFinder, machinery.WindowsRegistryFinder) class PathEntryFinder(Finder): """Abstract base class for path entry finders used by PathFinder.""" @abc.abstractmethod def find_loader(self, fullname): """Abstract method which, when implemented, returns a module loader or a possible part of a namespace. The fullname is a str. Returns a 2-tuple of (Loader, portion) where portion is a sequence of file system locations contributing to part of a namespace package. The sequence may be empty and the loader may be None. """ return None, [] find_module = _bootstrap._find_module_shim def invalidate_caches(self): """An optional method for clearing the finder's cache, if any. This method is used by PathFinder.invalidate_caches(). """ _register(PathEntryFinder, machinery.FileFinder) class Loader(metaclass=abc.ABCMeta): """Abstract base class for import loaders. The optional method module_repr(module) may be defined to provide a repr for a module when appropriate (see PEP 420). The __repr__() method on the module type will use the method as appropriate. """ @abc.abstractmethod def load_module(self, fullname): """Abstract method which when implemented should load a module. The fullname is a str. ImportError is raised on failure. """ raise ImportError def module_repr(self, module): """Return a module's repr. Used by the module type when the method does not raise NotImplementedError. """ raise NotImplementedError def init_module_attrs(self, module): """Set the module's __loader__ attribute.""" module.__loader__ = self class ResourceLoader(Loader): """Abstract base class for loaders which can return data from their back-end storage. This ABC represents one of the optional protocols specified by PEP 302. """ @abc.abstractmethod def get_data(self, path): """Abstract method which when implemented should return the bytes for the specified path. The path must be a str.""" raise IOError class InspectLoader(Loader): """Abstract base class for loaders which support inspection about the modules they can load. This ABC represents one of the optional protocols specified by PEP 302. """ @abc.abstractmethod def is_package(self, fullname): """Abstract method which when implemented should return whether the module is a package. The fullname is a str. Returns a bool. Raises ImportError is the module cannot be found. """ raise ImportError def get_code(self, fullname): """Method which returns the code object for the module. The fullname is a str. Returns a types.CodeType if possible, else returns None if a code object does not make sense (e.g. built-in module). Raises ImportError if the module cannot be found. """ source = self.get_source(fullname) if source is None: return None return self.source_to_code(source) @abc.abstractmethod def get_source(self, fullname): """Abstract method which should return the source code for the module. The fullname is a str. Returns a str. Raises ImportError if the module cannot be found. """ raise ImportError def source_to_code(self, data, path='<string>'): """Compile 'data' into a code object. The 'data' argument can be anything that compile() can handle. The'path' argument should be where the data was retrieved (when applicable).""" return compile(data, path, 'exec', dont_inherit=True) def init_module_attrs(self, module): """Initialize the __loader__ and __package__ attributes of the module. The name of the module is gleaned from module.__name__. The __package__ attribute is set based on self.is_package(). """ super().init_module_attrs(module) _bootstrap._init_package_attrs(self, module) load_module = _bootstrap._LoaderBasics.load_module _register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter, machinery.ExtensionFileLoader) class ExecutionLoader(InspectLoader): """Abstract base class for loaders that wish to support the execution of modules as scripts. This ABC represents one of the optional protocols specified in PEP 302. """ @abc.abstractmethod def get_filename(self, fullname): """Abstract method which should return the value that __file__ is to be set to. Raises ImportError if the module cannot be found. """ raise ImportError def get_code(self, fullname): """Method to return the code object for fullname. Should return None if not applicable (e.g. built-in module). Raise ImportError if the module cannot be found. """ source = self.get_source(fullname) if source is None: return None try: path = self.get_filename(fullname) except ImportError: return self.source_to_code(source) else: return self.source_to_code(source, path) def init_module_attrs(self, module): """Initialize the module's attributes. It is assumed that the module's name has been set on module.__name__. It is also assumed that any path returned by self.get_filename() uses (one of) the operating system's path separator(s) to separate filenames from directories in order to set __path__ intelligently. InspectLoader.init_module_attrs() sets __loader__ and __package__. """ super().init_module_attrs(module) _bootstrap._init_file_attrs(self, module) class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader): """Abstract base class partially implementing the ResourceLoader and ExecutionLoader ABCs.""" _register(FileLoader, machinery.SourceFileLoader, machinery.SourcelessFileLoader) class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader): """Abstract base class for loading source code (and optionally any corresponding bytecode). To support loading from source code, the abstractmethods inherited from ResourceLoader and ExecutionLoader need to be implemented. To also support loading from bytecode, the optional methods specified directly by this ABC is required. Inherited abstractmethods not implemented in this ABC: * ResourceLoader.get_data * ExecutionLoader.get_filename """ def path_mtime(self, path): """Return the (int) modification time for the path (str).""" if self.path_stats.__func__ is SourceLoader.path_stats: raise IOError return int(self.path_stats(path)['mtime']) def path_stats(self, path): """Return a metadata dict for the source pointed to by the path (str). Possible keys: - 'mtime' (mandatory) is the numeric timestamp of last source code modification; - 'size' (optional) is the size in bytes of the source code. """ if self.path_mtime.__func__ is SourceLoader.path_mtime: raise IOError return {'mtime': self.path_mtime(path)} def set_data(self, path, data): """Write the bytes to the path (if possible). Accepts a str path and data as bytes. Any needed intermediary directories are to be created. If for some reason the file cannot be written because of permissions, fail silently. """ _register(SourceLoader, machinery.SourceFileLoader)