You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1518 lines
46 KiB

Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60901 | eric.smith | 2008-02-19 14:21:56 +0100 (Tue, 19 Feb 2008) | 1 line Added PEP 3101. ........ r60907 | georg.brandl | 2008-02-20 20:12:36 +0100 (Wed, 20 Feb 2008) | 2 lines Fixes contributed by Ori Avtalion. ........ r60909 | eric.smith | 2008-02-21 00:34:22 +0100 (Thu, 21 Feb 2008) | 1 line Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description). ........ r60910 | eric.smith | 2008-02-21 00:39:28 +0100 (Thu, 21 Feb 2008) | 1 line Now that PyOS_ascii_formatd supports the 'n' format, simplify the float formatting code to just call it. ........ r60918 | andrew.kuchling | 2008-02-21 15:23:38 +0100 (Thu, 21 Feb 2008) | 2 lines Close manifest file. This change doesn't make any difference to CPython, but is a necessary fix for Jython. ........ r60921 | guido.van.rossum | 2008-02-21 18:46:16 +0100 (Thu, 21 Feb 2008) | 2 lines Remove news about float repr() -- issue 1580 is still in limbo. ........ r60923 | guido.van.rossum | 2008-02-21 19:18:37 +0100 (Thu, 21 Feb 2008) | 5 lines Removed uses of dict.has_key() from distutils, and uses of callable() from copy_reg.py, so the interpreter now starts up without warnings when '-3' is given. More work like this needs to be done in the rest of the stdlib. ........ r60924 | thomas.heller | 2008-02-21 19:28:48 +0100 (Thu, 21 Feb 2008) | 4 lines configure.ac: Remove the configure check for _Bool, it is already done in the top-level Python configure script. configure, fficonfig.h.in: regenerated. ........ r60925 | thomas.heller | 2008-02-21 19:52:20 +0100 (Thu, 21 Feb 2008) | 3 lines Replace 'has_key()' with 'in'. Replace 'raise Error, stuff' with 'raise Error(stuff)'. ........ r60927 | raymond.hettinger | 2008-02-21 20:24:53 +0100 (Thu, 21 Feb 2008) | 1 line Update more instances of has_key(). ........ r60928 | guido.van.rossum | 2008-02-21 20:46:35 +0100 (Thu, 21 Feb 2008) | 3 lines Fix a few typos and layout glitches (more work is needed). Move 2.5 news to Misc/HISTORY. ........ r60936 | georg.brandl | 2008-02-21 21:33:38 +0100 (Thu, 21 Feb 2008) | 2 lines #2079: typo in userdict docs. ........ r60938 | georg.brandl | 2008-02-21 21:38:13 +0100 (Thu, 21 Feb 2008) | 2 lines Part of #2154: minimal syntax fixes in doc example snippets. ........ r60942 | raymond.hettinger | 2008-02-22 04:16:42 +0100 (Fri, 22 Feb 2008) | 1 line First draft for itertools.product(). Docs and other updates forthcoming. ........ r60955 | nick.coghlan | 2008-02-22 11:54:06 +0100 (Fri, 22 Feb 2008) | 1 line Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery) ........ r60956 | georg.brandl | 2008-02-22 13:31:45 +0100 (Fri, 22 Feb 2008) | 2 lines A lot more typo fixes by Ori Avtalion. ........ r60957 | georg.brandl | 2008-02-22 13:56:34 +0100 (Fri, 22 Feb 2008) | 2 lines Don't reference pyshell. ........ r60958 | georg.brandl | 2008-02-22 13:57:05 +0100 (Fri, 22 Feb 2008) | 2 lines Another fix. ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60901 | eric.smith | 2008-02-19 14:21:56 +0100 (Tue, 19 Feb 2008) | 1 line Added PEP 3101. ........ r60907 | georg.brandl | 2008-02-20 20:12:36 +0100 (Wed, 20 Feb 2008) | 2 lines Fixes contributed by Ori Avtalion. ........ r60909 | eric.smith | 2008-02-21 00:34:22 +0100 (Thu, 21 Feb 2008) | 1 line Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description). ........ r60910 | eric.smith | 2008-02-21 00:39:28 +0100 (Thu, 21 Feb 2008) | 1 line Now that PyOS_ascii_formatd supports the 'n' format, simplify the float formatting code to just call it. ........ r60918 | andrew.kuchling | 2008-02-21 15:23:38 +0100 (Thu, 21 Feb 2008) | 2 lines Close manifest file. This change doesn't make any difference to CPython, but is a necessary fix for Jython. ........ r60921 | guido.van.rossum | 2008-02-21 18:46:16 +0100 (Thu, 21 Feb 2008) | 2 lines Remove news about float repr() -- issue 1580 is still in limbo. ........ r60923 | guido.van.rossum | 2008-02-21 19:18:37 +0100 (Thu, 21 Feb 2008) | 5 lines Removed uses of dict.has_key() from distutils, and uses of callable() from copy_reg.py, so the interpreter now starts up without warnings when '-3' is given. More work like this needs to be done in the rest of the stdlib. ........ r60924 | thomas.heller | 2008-02-21 19:28:48 +0100 (Thu, 21 Feb 2008) | 4 lines configure.ac: Remove the configure check for _Bool, it is already done in the top-level Python configure script. configure, fficonfig.h.in: regenerated. ........ r60925 | thomas.heller | 2008-02-21 19:52:20 +0100 (Thu, 21 Feb 2008) | 3 lines Replace 'has_key()' with 'in'. Replace 'raise Error, stuff' with 'raise Error(stuff)'. ........ r60927 | raymond.hettinger | 2008-02-21 20:24:53 +0100 (Thu, 21 Feb 2008) | 1 line Update more instances of has_key(). ........ r60928 | guido.van.rossum | 2008-02-21 20:46:35 +0100 (Thu, 21 Feb 2008) | 3 lines Fix a few typos and layout glitches (more work is needed). Move 2.5 news to Misc/HISTORY. ........ r60936 | georg.brandl | 2008-02-21 21:33:38 +0100 (Thu, 21 Feb 2008) | 2 lines #2079: typo in userdict docs. ........ r60938 | georg.brandl | 2008-02-21 21:38:13 +0100 (Thu, 21 Feb 2008) | 2 lines Part of #2154: minimal syntax fixes in doc example snippets. ........ r60942 | raymond.hettinger | 2008-02-22 04:16:42 +0100 (Fri, 22 Feb 2008) | 1 line First draft for itertools.product(). Docs and other updates forthcoming. ........ r60955 | nick.coghlan | 2008-02-22 11:54:06 +0100 (Fri, 22 Feb 2008) | 1 line Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery) ........ r60956 | georg.brandl | 2008-02-22 13:31:45 +0100 (Fri, 22 Feb 2008) | 2 lines A lot more typo fixes by Ori Avtalion. ........ r60957 | georg.brandl | 2008-02-22 13:56:34 +0100 (Fri, 22 Feb 2008) | 2 lines Don't reference pyshell. ........ r60958 | georg.brandl | 2008-02-22 13:57:05 +0100 (Fri, 22 Feb 2008) | 2 lines Another fix. ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60901 | eric.smith | 2008-02-19 14:21:56 +0100 (Tue, 19 Feb 2008) | 1 line Added PEP 3101. ........ r60907 | georg.brandl | 2008-02-20 20:12:36 +0100 (Wed, 20 Feb 2008) | 2 lines Fixes contributed by Ori Avtalion. ........ r60909 | eric.smith | 2008-02-21 00:34:22 +0100 (Thu, 21 Feb 2008) | 1 line Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description). ........ r60910 | eric.smith | 2008-02-21 00:39:28 +0100 (Thu, 21 Feb 2008) | 1 line Now that PyOS_ascii_formatd supports the 'n' format, simplify the float formatting code to just call it. ........ r60918 | andrew.kuchling | 2008-02-21 15:23:38 +0100 (Thu, 21 Feb 2008) | 2 lines Close manifest file. This change doesn't make any difference to CPython, but is a necessary fix for Jython. ........ r60921 | guido.van.rossum | 2008-02-21 18:46:16 +0100 (Thu, 21 Feb 2008) | 2 lines Remove news about float repr() -- issue 1580 is still in limbo. ........ r60923 | guido.van.rossum | 2008-02-21 19:18:37 +0100 (Thu, 21 Feb 2008) | 5 lines Removed uses of dict.has_key() from distutils, and uses of callable() from copy_reg.py, so the interpreter now starts up without warnings when '-3' is given. More work like this needs to be done in the rest of the stdlib. ........ r60924 | thomas.heller | 2008-02-21 19:28:48 +0100 (Thu, 21 Feb 2008) | 4 lines configure.ac: Remove the configure check for _Bool, it is already done in the top-level Python configure script. configure, fficonfig.h.in: regenerated. ........ r60925 | thomas.heller | 2008-02-21 19:52:20 +0100 (Thu, 21 Feb 2008) | 3 lines Replace 'has_key()' with 'in'. Replace 'raise Error, stuff' with 'raise Error(stuff)'. ........ r60927 | raymond.hettinger | 2008-02-21 20:24:53 +0100 (Thu, 21 Feb 2008) | 1 line Update more instances of has_key(). ........ r60928 | guido.van.rossum | 2008-02-21 20:46:35 +0100 (Thu, 21 Feb 2008) | 3 lines Fix a few typos and layout glitches (more work is needed). Move 2.5 news to Misc/HISTORY. ........ r60936 | georg.brandl | 2008-02-21 21:33:38 +0100 (Thu, 21 Feb 2008) | 2 lines #2079: typo in userdict docs. ........ r60938 | georg.brandl | 2008-02-21 21:38:13 +0100 (Thu, 21 Feb 2008) | 2 lines Part of #2154: minimal syntax fixes in doc example snippets. ........ r60942 | raymond.hettinger | 2008-02-22 04:16:42 +0100 (Fri, 22 Feb 2008) | 1 line First draft for itertools.product(). Docs and other updates forthcoming. ........ r60955 | nick.coghlan | 2008-02-22 11:54:06 +0100 (Fri, 22 Feb 2008) | 1 line Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery) ........ r60956 | georg.brandl | 2008-02-22 13:31:45 +0100 (Fri, 22 Feb 2008) | 2 lines A lot more typo fixes by Ori Avtalion. ........ r60957 | georg.brandl | 2008-02-22 13:56:34 +0100 (Fri, 22 Feb 2008) | 2 lines Don't reference pyshell. ........ r60958 | georg.brandl | 2008-02-22 13:57:05 +0100 (Fri, 22 Feb 2008) | 2 lines Another fix. ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60901 | eric.smith | 2008-02-19 14:21:56 +0100 (Tue, 19 Feb 2008) | 1 line Added PEP 3101. ........ r60907 | georg.brandl | 2008-02-20 20:12:36 +0100 (Wed, 20 Feb 2008) | 2 lines Fixes contributed by Ori Avtalion. ........ r60909 | eric.smith | 2008-02-21 00:34:22 +0100 (Thu, 21 Feb 2008) | 1 line Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description). ........ r60910 | eric.smith | 2008-02-21 00:39:28 +0100 (Thu, 21 Feb 2008) | 1 line Now that PyOS_ascii_formatd supports the 'n' format, simplify the float formatting code to just call it. ........ r60918 | andrew.kuchling | 2008-02-21 15:23:38 +0100 (Thu, 21 Feb 2008) | 2 lines Close manifest file. This change doesn't make any difference to CPython, but is a necessary fix for Jython. ........ r60921 | guido.van.rossum | 2008-02-21 18:46:16 +0100 (Thu, 21 Feb 2008) | 2 lines Remove news about float repr() -- issue 1580 is still in limbo. ........ r60923 | guido.van.rossum | 2008-02-21 19:18:37 +0100 (Thu, 21 Feb 2008) | 5 lines Removed uses of dict.has_key() from distutils, and uses of callable() from copy_reg.py, so the interpreter now starts up without warnings when '-3' is given. More work like this needs to be done in the rest of the stdlib. ........ r60924 | thomas.heller | 2008-02-21 19:28:48 +0100 (Thu, 21 Feb 2008) | 4 lines configure.ac: Remove the configure check for _Bool, it is already done in the top-level Python configure script. configure, fficonfig.h.in: regenerated. ........ r60925 | thomas.heller | 2008-02-21 19:52:20 +0100 (Thu, 21 Feb 2008) | 3 lines Replace 'has_key()' with 'in'. Replace 'raise Error, stuff' with 'raise Error(stuff)'. ........ r60927 | raymond.hettinger | 2008-02-21 20:24:53 +0100 (Thu, 21 Feb 2008) | 1 line Update more instances of has_key(). ........ r60928 | guido.van.rossum | 2008-02-21 20:46:35 +0100 (Thu, 21 Feb 2008) | 3 lines Fix a few typos and layout glitches (more work is needed). Move 2.5 news to Misc/HISTORY. ........ r60936 | georg.brandl | 2008-02-21 21:33:38 +0100 (Thu, 21 Feb 2008) | 2 lines #2079: typo in userdict docs. ........ r60938 | georg.brandl | 2008-02-21 21:38:13 +0100 (Thu, 21 Feb 2008) | 2 lines Part of #2154: minimal syntax fixes in doc example snippets. ........ r60942 | raymond.hettinger | 2008-02-22 04:16:42 +0100 (Fri, 22 Feb 2008) | 1 line First draft for itertools.product(). Docs and other updates forthcoming. ........ r60955 | nick.coghlan | 2008-02-22 11:54:06 +0100 (Fri, 22 Feb 2008) | 1 line Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery) ........ r60956 | georg.brandl | 2008-02-22 13:31:45 +0100 (Fri, 22 Feb 2008) | 2 lines A lot more typo fixes by Ori Avtalion. ........ r60957 | georg.brandl | 2008-02-22 13:56:34 +0100 (Fri, 22 Feb 2008) | 2 lines Don't reference pyshell. ........ r60958 | georg.brandl | 2008-02-22 13:57:05 +0100 (Fri, 22 Feb 2008) | 2 lines Another fix. ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60901 | eric.smith | 2008-02-19 14:21:56 +0100 (Tue, 19 Feb 2008) | 1 line Added PEP 3101. ........ r60907 | georg.brandl | 2008-02-20 20:12:36 +0100 (Wed, 20 Feb 2008) | 2 lines Fixes contributed by Ori Avtalion. ........ r60909 | eric.smith | 2008-02-21 00:34:22 +0100 (Thu, 21 Feb 2008) | 1 line Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description). ........ r60910 | eric.smith | 2008-02-21 00:39:28 +0100 (Thu, 21 Feb 2008) | 1 line Now that PyOS_ascii_formatd supports the 'n' format, simplify the float formatting code to just call it. ........ r60918 | andrew.kuchling | 2008-02-21 15:23:38 +0100 (Thu, 21 Feb 2008) | 2 lines Close manifest file. This change doesn't make any difference to CPython, but is a necessary fix for Jython. ........ r60921 | guido.van.rossum | 2008-02-21 18:46:16 +0100 (Thu, 21 Feb 2008) | 2 lines Remove news about float repr() -- issue 1580 is still in limbo. ........ r60923 | guido.van.rossum | 2008-02-21 19:18:37 +0100 (Thu, 21 Feb 2008) | 5 lines Removed uses of dict.has_key() from distutils, and uses of callable() from copy_reg.py, so the interpreter now starts up without warnings when '-3' is given. More work like this needs to be done in the rest of the stdlib. ........ r60924 | thomas.heller | 2008-02-21 19:28:48 +0100 (Thu, 21 Feb 2008) | 4 lines configure.ac: Remove the configure check for _Bool, it is already done in the top-level Python configure script. configure, fficonfig.h.in: regenerated. ........ r60925 | thomas.heller | 2008-02-21 19:52:20 +0100 (Thu, 21 Feb 2008) | 3 lines Replace 'has_key()' with 'in'. Replace 'raise Error, stuff' with 'raise Error(stuff)'. ........ r60927 | raymond.hettinger | 2008-02-21 20:24:53 +0100 (Thu, 21 Feb 2008) | 1 line Update more instances of has_key(). ........ r60928 | guido.van.rossum | 2008-02-21 20:46:35 +0100 (Thu, 21 Feb 2008) | 3 lines Fix a few typos and layout glitches (more work is needed). Move 2.5 news to Misc/HISTORY. ........ r60936 | georg.brandl | 2008-02-21 21:33:38 +0100 (Thu, 21 Feb 2008) | 2 lines #2079: typo in userdict docs. ........ r60938 | georg.brandl | 2008-02-21 21:38:13 +0100 (Thu, 21 Feb 2008) | 2 lines Part of #2154: minimal syntax fixes in doc example snippets. ........ r60942 | raymond.hettinger | 2008-02-22 04:16:42 +0100 (Fri, 22 Feb 2008) | 1 line First draft for itertools.product(). Docs and other updates forthcoming. ........ r60955 | nick.coghlan | 2008-02-22 11:54:06 +0100 (Fri, 22 Feb 2008) | 1 line Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery) ........ r60956 | georg.brandl | 2008-02-22 13:31:45 +0100 (Fri, 22 Feb 2008) | 2 lines A lot more typo fixes by Ori Avtalion. ........ r60957 | georg.brandl | 2008-02-22 13:56:34 +0100 (Fri, 22 Feb 2008) | 2 lines Don't reference pyshell. ........ r60958 | georg.brandl | 2008-02-22 13:57:05 +0100 (Fri, 22 Feb 2008) | 2 lines Another fix. ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60901 | eric.smith | 2008-02-19 14:21:56 +0100 (Tue, 19 Feb 2008) | 1 line Added PEP 3101. ........ r60907 | georg.brandl | 2008-02-20 20:12:36 +0100 (Wed, 20 Feb 2008) | 2 lines Fixes contributed by Ori Avtalion. ........ r60909 | eric.smith | 2008-02-21 00:34:22 +0100 (Thu, 21 Feb 2008) | 1 line Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description). ........ r60910 | eric.smith | 2008-02-21 00:39:28 +0100 (Thu, 21 Feb 2008) | 1 line Now that PyOS_ascii_formatd supports the 'n' format, simplify the float formatting code to just call it. ........ r60918 | andrew.kuchling | 2008-02-21 15:23:38 +0100 (Thu, 21 Feb 2008) | 2 lines Close manifest file. This change doesn't make any difference to CPython, but is a necessary fix for Jython. ........ r60921 | guido.van.rossum | 2008-02-21 18:46:16 +0100 (Thu, 21 Feb 2008) | 2 lines Remove news about float repr() -- issue 1580 is still in limbo. ........ r60923 | guido.van.rossum | 2008-02-21 19:18:37 +0100 (Thu, 21 Feb 2008) | 5 lines Removed uses of dict.has_key() from distutils, and uses of callable() from copy_reg.py, so the interpreter now starts up without warnings when '-3' is given. More work like this needs to be done in the rest of the stdlib. ........ r60924 | thomas.heller | 2008-02-21 19:28:48 +0100 (Thu, 21 Feb 2008) | 4 lines configure.ac: Remove the configure check for _Bool, it is already done in the top-level Python configure script. configure, fficonfig.h.in: regenerated. ........ r60925 | thomas.heller | 2008-02-21 19:52:20 +0100 (Thu, 21 Feb 2008) | 3 lines Replace 'has_key()' with 'in'. Replace 'raise Error, stuff' with 'raise Error(stuff)'. ........ r60927 | raymond.hettinger | 2008-02-21 20:24:53 +0100 (Thu, 21 Feb 2008) | 1 line Update more instances of has_key(). ........ r60928 | guido.van.rossum | 2008-02-21 20:46:35 +0100 (Thu, 21 Feb 2008) | 3 lines Fix a few typos and layout glitches (more work is needed). Move 2.5 news to Misc/HISTORY. ........ r60936 | georg.brandl | 2008-02-21 21:33:38 +0100 (Thu, 21 Feb 2008) | 2 lines #2079: typo in userdict docs. ........ r60938 | georg.brandl | 2008-02-21 21:38:13 +0100 (Thu, 21 Feb 2008) | 2 lines Part of #2154: minimal syntax fixes in doc example snippets. ........ r60942 | raymond.hettinger | 2008-02-22 04:16:42 +0100 (Fri, 22 Feb 2008) | 1 line First draft for itertools.product(). Docs and other updates forthcoming. ........ r60955 | nick.coghlan | 2008-02-22 11:54:06 +0100 (Fri, 22 Feb 2008) | 1 line Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery) ........ r60956 | georg.brandl | 2008-02-22 13:31:45 +0100 (Fri, 22 Feb 2008) | 2 lines A lot more typo fixes by Ori Avtalion. ........ r60957 | georg.brandl | 2008-02-22 13:56:34 +0100 (Fri, 22 Feb 2008) | 2 lines Don't reference pyshell. ........ r60958 | georg.brandl | 2008-02-22 13:57:05 +0100 (Fri, 22 Feb 2008) | 2 lines Another fix. ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60901 | eric.smith | 2008-02-19 14:21:56 +0100 (Tue, 19 Feb 2008) | 1 line Added PEP 3101. ........ r60907 | georg.brandl | 2008-02-20 20:12:36 +0100 (Wed, 20 Feb 2008) | 2 lines Fixes contributed by Ori Avtalion. ........ r60909 | eric.smith | 2008-02-21 00:34:22 +0100 (Thu, 21 Feb 2008) | 1 line Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description). ........ r60910 | eric.smith | 2008-02-21 00:39:28 +0100 (Thu, 21 Feb 2008) | 1 line Now that PyOS_ascii_formatd supports the 'n' format, simplify the float formatting code to just call it. ........ r60918 | andrew.kuchling | 2008-02-21 15:23:38 +0100 (Thu, 21 Feb 2008) | 2 lines Close manifest file. This change doesn't make any difference to CPython, but is a necessary fix for Jython. ........ r60921 | guido.van.rossum | 2008-02-21 18:46:16 +0100 (Thu, 21 Feb 2008) | 2 lines Remove news about float repr() -- issue 1580 is still in limbo. ........ r60923 | guido.van.rossum | 2008-02-21 19:18:37 +0100 (Thu, 21 Feb 2008) | 5 lines Removed uses of dict.has_key() from distutils, and uses of callable() from copy_reg.py, so the interpreter now starts up without warnings when '-3' is given. More work like this needs to be done in the rest of the stdlib. ........ r60924 | thomas.heller | 2008-02-21 19:28:48 +0100 (Thu, 21 Feb 2008) | 4 lines configure.ac: Remove the configure check for _Bool, it is already done in the top-level Python configure script. configure, fficonfig.h.in: regenerated. ........ r60925 | thomas.heller | 2008-02-21 19:52:20 +0100 (Thu, 21 Feb 2008) | 3 lines Replace 'has_key()' with 'in'. Replace 'raise Error, stuff' with 'raise Error(stuff)'. ........ r60927 | raymond.hettinger | 2008-02-21 20:24:53 +0100 (Thu, 21 Feb 2008) | 1 line Update more instances of has_key(). ........ r60928 | guido.van.rossum | 2008-02-21 20:46:35 +0100 (Thu, 21 Feb 2008) | 3 lines Fix a few typos and layout glitches (more work is needed). Move 2.5 news to Misc/HISTORY. ........ r60936 | georg.brandl | 2008-02-21 21:33:38 +0100 (Thu, 21 Feb 2008) | 2 lines #2079: typo in userdict docs. ........ r60938 | georg.brandl | 2008-02-21 21:38:13 +0100 (Thu, 21 Feb 2008) | 2 lines Part of #2154: minimal syntax fixes in doc example snippets. ........ r60942 | raymond.hettinger | 2008-02-22 04:16:42 +0100 (Fri, 22 Feb 2008) | 1 line First draft for itertools.product(). Docs and other updates forthcoming. ........ r60955 | nick.coghlan | 2008-02-22 11:54:06 +0100 (Fri, 22 Feb 2008) | 1 line Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery) ........ r60956 | georg.brandl | 2008-02-22 13:31:45 +0100 (Fri, 22 Feb 2008) | 2 lines A lot more typo fixes by Ori Avtalion. ........ r60957 | georg.brandl | 2008-02-22 13:56:34 +0100 (Fri, 22 Feb 2008) | 2 lines Don't reference pyshell. ........ r60958 | georg.brandl | 2008-02-22 13:57:05 +0100 (Fri, 22 Feb 2008) | 2 lines Another fix. ........
18 years ago
  1. /* implements the string, long, and float formatters. that is,
  2. string.__format__, etc. */
  3. #include <locale.h>
  4. /* Before including this, you must include either:
  5. stringlib/unicodedefs.h
  6. stringlib/stringdefs.h
  7. Also, you should define the names:
  8. FORMAT_STRING
  9. FORMAT_LONG
  10. FORMAT_FLOAT
  11. FORMAT_COMPLEX
  12. to be whatever you want the public names of these functions to
  13. be. These are the only non-static functions defined here.
  14. */
  15. /* Raises an exception about an unknown presentation type for this
  16. * type. */
  17. static void
  18. unknown_presentation_type(STRINGLIB_CHAR presentation_type,
  19. const char* type_name)
  20. {
  21. #if STRINGLIB_IS_UNICODE
  22. /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
  23. hence the two cases. If it is char, gcc complains that the
  24. condition below is always true, hence the ifdef. */
  25. if (presentation_type > 32 && presentation_type < 128)
  26. #endif
  27. PyErr_Format(PyExc_ValueError,
  28. "Unknown format code '%c' "
  29. "for object of type '%.200s'",
  30. (char)presentation_type,
  31. type_name);
  32. #if STRINGLIB_IS_UNICODE
  33. else
  34. PyErr_Format(PyExc_ValueError,
  35. "Unknown format code '\\x%x' "
  36. "for object of type '%.200s'",
  37. (unsigned int)presentation_type,
  38. type_name);
  39. #endif
  40. }
  41. static void
  42. invalid_comma_type(STRINGLIB_CHAR presentation_type)
  43. {
  44. #if STRINGLIB_IS_UNICODE
  45. /* See comment in unknown_presentation_type */
  46. if (presentation_type > 32 && presentation_type < 128)
  47. #endif
  48. PyErr_Format(PyExc_ValueError,
  49. "Cannot specify ',' with '%c'.",
  50. (char)presentation_type);
  51. #if STRINGLIB_IS_UNICODE
  52. else
  53. PyErr_Format(PyExc_ValueError,
  54. "Cannot specify ',' with '\\x%x'.",
  55. (unsigned int)presentation_type);
  56. #endif
  57. }
  58. /*
  59. get_integer consumes 0 or more decimal digit characters from an
  60. input string, updates *result with the corresponding positive
  61. integer, and returns the number of digits consumed.
  62. returns -1 on error.
  63. */
  64. static int
  65. get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
  66. Py_ssize_t *result)
  67. {
  68. Py_ssize_t accumulator, digitval, oldaccumulator;
  69. int numdigits;
  70. accumulator = numdigits = 0;
  71. for (;;(*ptr)++, numdigits++) {
  72. if (*ptr >= end)
  73. break;
  74. digitval = STRINGLIB_TODECIMAL(**ptr);
  75. if (digitval < 0)
  76. break;
  77. /*
  78. This trick was copied from old Unicode format code. It's cute,
  79. but would really suck on an old machine with a slow divide
  80. implementation. Fortunately, in the normal case we do not
  81. expect too many digits.
  82. */
  83. oldaccumulator = accumulator;
  84. accumulator *= 10;
  85. if ((accumulator+10)/10 != oldaccumulator+1) {
  86. PyErr_Format(PyExc_ValueError,
  87. "Too many decimal digits in format string");
  88. return -1;
  89. }
  90. accumulator += digitval;
  91. }
  92. *result = accumulator;
  93. return numdigits;
  94. }
  95. /************************************************************************/
  96. /*********** standard format specifier parsing **************************/
  97. /************************************************************************/
  98. /* returns true if this character is a specifier alignment token */
  99. Py_LOCAL_INLINE(int)
  100. is_alignment_token(STRINGLIB_CHAR c)
  101. {
  102. switch (c) {
  103. case '<': case '>': case '=': case '^':
  104. return 1;
  105. default:
  106. return 0;
  107. }
  108. }
  109. /* returns true if this character is a sign element */
  110. Py_LOCAL_INLINE(int)
  111. is_sign_element(STRINGLIB_CHAR c)
  112. {
  113. switch (c) {
  114. case ' ': case '+': case '-':
  115. return 1;
  116. default:
  117. return 0;
  118. }
  119. }
  120. typedef struct {
  121. STRINGLIB_CHAR fill_char;
  122. STRINGLIB_CHAR align;
  123. int alternate;
  124. STRINGLIB_CHAR sign;
  125. Py_ssize_t width;
  126. int thousands_separators;
  127. Py_ssize_t precision;
  128. STRINGLIB_CHAR type;
  129. } InternalFormatSpec;
  130. #if 0
  131. /* Occassionally useful for debugging. Should normally be commented out. */
  132. static void
  133. DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
  134. {
  135. printf("internal format spec: fill_char %d\n", format->fill_char);
  136. printf("internal format spec: align %d\n", format->align);
  137. printf("internal format spec: alternate %d\n", format->alternate);
  138. printf("internal format spec: sign %d\n", format->sign);
  139. printf("internal format spec: width %zd\n", format->width);
  140. printf("internal format spec: thousands_separators %d\n",
  141. format->thousands_separators);
  142. printf("internal format spec: precision %zd\n", format->precision);
  143. printf("internal format spec: type %c\n", format->type);
  144. printf("\n");
  145. }
  146. #endif
  147. /*
  148. ptr points to the start of the format_spec, end points just past its end.
  149. fills in format with the parsed information.
  150. returns 1 on success, 0 on failure.
  151. if failure, sets the exception
  152. */
  153. static int
  154. parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
  155. Py_ssize_t format_spec_len,
  156. InternalFormatSpec *format,
  157. char default_type,
  158. char default_align)
  159. {
  160. STRINGLIB_CHAR *ptr = format_spec;
  161. STRINGLIB_CHAR *end = format_spec + format_spec_len;
  162. /* end-ptr is used throughout this code to specify the length of
  163. the input string */
  164. Py_ssize_t consumed;
  165. int align_specified = 0;
  166. format->fill_char = '\0';
  167. format->align = default_align;
  168. format->alternate = 0;
  169. format->sign = '\0';
  170. format->width = -1;
  171. format->thousands_separators = 0;
  172. format->precision = -1;
  173. format->type = default_type;
  174. /* If the second char is an alignment token,
  175. then parse the fill char */
  176. if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
  177. format->align = ptr[1];
  178. format->fill_char = ptr[0];
  179. align_specified = 1;
  180. ptr += 2;
  181. }
  182. else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
  183. format->align = ptr[0];
  184. align_specified = 1;
  185. ++ptr;
  186. }
  187. /* Parse the various sign options */
  188. if (end-ptr >= 1 && is_sign_element(ptr[0])) {
  189. format->sign = ptr[0];
  190. ++ptr;
  191. }
  192. /* If the next character is #, we're in alternate mode. This only
  193. applies to integers. */
  194. if (end-ptr >= 1 && ptr[0] == '#') {
  195. format->alternate = 1;
  196. ++ptr;
  197. }
  198. /* The special case for 0-padding (backwards compat) */
  199. if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
  200. format->fill_char = '0';
  201. if (!align_specified) {
  202. format->align = '=';
  203. }
  204. ++ptr;
  205. }
  206. consumed = get_integer(&ptr, end, &format->width);
  207. if (consumed == -1)
  208. /* Overflow error. Exception already set. */
  209. return 0;
  210. /* If consumed is 0, we didn't consume any characters for the
  211. width. In that case, reset the width to -1, because
  212. get_integer() will have set it to zero. -1 is how we record
  213. that the width wasn't specified. */
  214. if (consumed == 0)
  215. format->width = -1;
  216. /* Comma signifies add thousands separators */
  217. if (end-ptr && ptr[0] == ',') {
  218. format->thousands_separators = 1;
  219. ++ptr;
  220. }
  221. /* Parse field precision */
  222. if (end-ptr && ptr[0] == '.') {
  223. ++ptr;
  224. consumed = get_integer(&ptr, end, &format->precision);
  225. if (consumed == -1)
  226. /* Overflow error. Exception already set. */
  227. return 0;
  228. /* Not having a precision after a dot is an error. */
  229. if (consumed == 0) {
  230. PyErr_Format(PyExc_ValueError,
  231. "Format specifier missing precision");
  232. return 0;
  233. }
  234. }
  235. /* Finally, parse the type field. */
  236. if (end-ptr > 1) {
  237. /* More than one char remain, invalid conversion spec. */
  238. PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
  239. return 0;
  240. }
  241. if (end-ptr == 1) {
  242. format->type = ptr[0];
  243. ++ptr;
  244. }
  245. /* Do as much validating as we can, just by looking at the format
  246. specifier. Do not take into account what type of formatting
  247. we're doing (int, float, string). */
  248. if (format->thousands_separators) {
  249. switch (format->type) {
  250. case 'd':
  251. case 'e':
  252. case 'f':
  253. case 'g':
  254. case 'E':
  255. case 'G':
  256. case '%':
  257. case 'F':
  258. case '\0':
  259. /* These are allowed. See PEP 378.*/
  260. break;
  261. default:
  262. invalid_comma_type(format->type);
  263. return 0;
  264. }
  265. }
  266. return 1;
  267. }
  268. /* Calculate the padding needed. */
  269. static void
  270. calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,
  271. Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
  272. Py_ssize_t *n_total)
  273. {
  274. if (width >= 0) {
  275. if (nchars > width)
  276. *n_total = nchars;
  277. else
  278. *n_total = width;
  279. }
  280. else {
  281. /* not specified, use all of the chars and no more */
  282. *n_total = nchars;
  283. }
  284. /* Figure out how much leading space we need, based on the
  285. aligning */
  286. if (align == '>')
  287. *n_lpadding = *n_total - nchars;
  288. else if (align == '^')
  289. *n_lpadding = (*n_total - nchars) / 2;
  290. else if (align == '<' || align == '=')
  291. *n_lpadding = 0;
  292. else {
  293. /* We should never have an unspecified alignment. */
  294. *n_lpadding = 0;
  295. assert(0);
  296. }
  297. *n_rpadding = *n_total - nchars - *n_lpadding;
  298. }
  299. /* Do the padding, and return a pointer to where the caller-supplied
  300. content goes. */
  301. static STRINGLIB_CHAR *
  302. fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,
  303. Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)
  304. {
  305. /* Pad on left. */
  306. if (n_lpadding)
  307. STRINGLIB_FILL(p, fill_char, n_lpadding);
  308. /* Pad on right. */
  309. if (n_rpadding)
  310. STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);
  311. /* Pointer to the user content. */
  312. return p + n_lpadding;
  313. }
  314. #if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
  315. /************************************************************************/
  316. /*********** common routines for numeric formatting *********************/
  317. /************************************************************************/
  318. /* Locale type codes. */
  319. #define LT_CURRENT_LOCALE 0
  320. #define LT_DEFAULT_LOCALE 1
  321. #define LT_NO_LOCALE 2
  322. /* Locale info needed for formatting integers and the part of floats
  323. before and including the decimal. Note that locales only support
  324. 8-bit chars, not unicode. */
  325. typedef struct {
  326. char *decimal_point;
  327. char *thousands_sep;
  328. char *grouping;
  329. } LocaleInfo;
  330. /* describes the layout for an integer, see the comment in
  331. calc_number_widths() for details */
  332. typedef struct {
  333. Py_ssize_t n_lpadding;
  334. Py_ssize_t n_prefix;
  335. Py_ssize_t n_spadding;
  336. Py_ssize_t n_rpadding;
  337. char sign;
  338. Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
  339. Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
  340. any grouping chars. */
  341. Py_ssize_t n_decimal; /* 0 if only an integer */
  342. Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
  343. excluding the decimal itself, if
  344. present. */
  345. /* These 2 are not the widths of fields, but are needed by
  346. STRINGLIB_GROUPING. */
  347. Py_ssize_t n_digits; /* The number of digits before a decimal
  348. or exponent. */
  349. Py_ssize_t n_min_width; /* The min_width we used when we computed
  350. the n_grouped_digits width. */
  351. } NumberFieldWidths;
  352. /* Given a number of the form:
  353. digits[remainder]
  354. where ptr points to the start and end points to the end, find where
  355. the integer part ends. This could be a decimal, an exponent, both,
  356. or neither.
  357. If a decimal point is present, set *has_decimal and increment
  358. remainder beyond it.
  359. Results are undefined (but shouldn't crash) for improperly
  360. formatted strings.
  361. */
  362. static void
  363. parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
  364. Py_ssize_t *n_remainder, int *has_decimal)
  365. {
  366. STRINGLIB_CHAR *end = ptr + len;
  367. STRINGLIB_CHAR *remainder;
  368. while (ptr<end && isdigit(*ptr))
  369. ++ptr;
  370. remainder = ptr;
  371. /* Does remainder start with a decimal point? */
  372. *has_decimal = ptr<end && *remainder == '.';
  373. /* Skip the decimal point. */
  374. if (*has_decimal)
  375. remainder++;
  376. *n_remainder = end - remainder;
  377. }
  378. /* not all fields of format are used. for example, precision is
  379. unused. should this take discrete params in order to be more clear
  380. about what it does? or is passing a single format parameter easier
  381. and more efficient enough to justify a little obfuscation? */
  382. static Py_ssize_t
  383. calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
  384. STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
  385. Py_ssize_t n_number, Py_ssize_t n_remainder,
  386. int has_decimal, const LocaleInfo *locale,
  387. const InternalFormatSpec *format)
  388. {
  389. Py_ssize_t n_non_digit_non_padding;
  390. Py_ssize_t n_padding;
  391. spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
  392. spec->n_lpadding = 0;
  393. spec->n_prefix = n_prefix;
  394. spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
  395. spec->n_remainder = n_remainder;
  396. spec->n_spadding = 0;
  397. spec->n_rpadding = 0;
  398. spec->sign = '\0';
  399. spec->n_sign = 0;
  400. /* the output will look like:
  401. | |
  402. | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
  403. | |
  404. sign is computed from format->sign and the actual
  405. sign of the number
  406. prefix is given (it's for the '0x' prefix)
  407. digits is already known
  408. the total width is either given, or computed from the
  409. actual digits
  410. only one of lpadding, spadding, and rpadding can be non-zero,
  411. and it's calculated from the width and other fields
  412. */
  413. /* compute the various parts we're going to write */
  414. switch (format->sign) {
  415. case '+':
  416. /* always put a + or - */
  417. spec->n_sign = 1;
  418. spec->sign = (sign_char == '-' ? '-' : '+');
  419. break;
  420. case ' ':
  421. spec->n_sign = 1;
  422. spec->sign = (sign_char == '-' ? '-' : ' ');
  423. break;
  424. default:
  425. /* Not specified, or the default (-) */
  426. if (sign_char == '-') {
  427. spec->n_sign = 1;
  428. spec->sign = '-';
  429. }
  430. }
  431. /* The number of chars used for non-digits and non-padding. */
  432. n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
  433. spec->n_remainder;
  434. /* min_width can go negative, that's okay. format->width == -1 means
  435. we don't care. */
  436. if (format->fill_char == '0' && format->align == '=')
  437. spec->n_min_width = format->width - n_non_digit_non_padding;
  438. else
  439. spec->n_min_width = 0;
  440. if (spec->n_digits == 0)
  441. /* This case only occurs when using 'c' formatting, we need
  442. to special case it because the grouping code always wants
  443. to have at least one character. */
  444. spec->n_grouped_digits = 0;
  445. else
  446. spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
  447. spec->n_digits,
  448. spec->n_min_width,
  449. locale->grouping,
  450. locale->thousands_sep);
  451. /* Given the desired width and the total of digit and non-digit
  452. space we consume, see if we need any padding. format->width can
  453. be negative (meaning no padding), but this code still works in
  454. that case. */
  455. n_padding = format->width -
  456. (n_non_digit_non_padding + spec->n_grouped_digits);
  457. if (n_padding > 0) {
  458. /* Some padding is needed. Determine if it's left, space, or right. */
  459. switch (format->align) {
  460. case '<':
  461. spec->n_rpadding = n_padding;
  462. break;
  463. case '^':
  464. spec->n_lpadding = n_padding / 2;
  465. spec->n_rpadding = n_padding - spec->n_lpadding;
  466. break;
  467. case '=':
  468. spec->n_spadding = n_padding;
  469. break;
  470. case '>':
  471. spec->n_lpadding = n_padding;
  472. break;
  473. default:
  474. /* Shouldn't get here, but treat it as '>' */
  475. spec->n_lpadding = n_padding;
  476. assert(0);
  477. break;
  478. }
  479. }
  480. return spec->n_lpadding + spec->n_sign + spec->n_prefix +
  481. spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
  482. spec->n_remainder + spec->n_rpadding;
  483. }
  484. /* Fill in the digit parts of a numbers's string representation,
  485. as determined in calc_number_widths().
  486. No error checking, since we know the buffer is the correct size. */
  487. static void
  488. fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
  489. STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
  490. STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
  491. LocaleInfo *locale, int toupper)
  492. {
  493. /* Used to keep track of digits, decimal, and remainder. */
  494. STRINGLIB_CHAR *p = digits;
  495. #ifndef NDEBUG
  496. Py_ssize_t r;
  497. #endif
  498. if (spec->n_lpadding) {
  499. STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
  500. buf += spec->n_lpadding;
  501. }
  502. if (spec->n_sign == 1) {
  503. *buf++ = spec->sign;
  504. }
  505. if (spec->n_prefix) {
  506. memmove(buf,
  507. prefix,
  508. spec->n_prefix * sizeof(STRINGLIB_CHAR));
  509. if (toupper) {
  510. Py_ssize_t t;
  511. for (t = 0; t < spec->n_prefix; ++t)
  512. buf[t] = STRINGLIB_TOUPPER(buf[t]);
  513. }
  514. buf += spec->n_prefix;
  515. }
  516. if (spec->n_spadding) {
  517. STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
  518. buf += spec->n_spadding;
  519. }
  520. /* Only for type 'c' special case, it has no digits. */
  521. if (spec->n_digits != 0) {
  522. /* Fill the digits with InsertThousandsGrouping. */
  523. #ifndef NDEBUG
  524. r =
  525. #endif
  526. STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
  527. spec->n_digits, spec->n_min_width,
  528. locale->grouping, locale->thousands_sep);
  529. #ifndef NDEBUG
  530. assert(r == spec->n_grouped_digits);
  531. #endif
  532. p += spec->n_digits;
  533. }
  534. if (toupper) {
  535. Py_ssize_t t;
  536. for (t = 0; t < spec->n_grouped_digits; ++t)
  537. buf[t] = STRINGLIB_TOUPPER(buf[t]);
  538. }
  539. buf += spec->n_grouped_digits;
  540. if (spec->n_decimal) {
  541. Py_ssize_t t;
  542. for (t = 0; t < spec->n_decimal; ++t)
  543. buf[t] = locale->decimal_point[t];
  544. buf += spec->n_decimal;
  545. p += 1;
  546. }
  547. if (spec->n_remainder) {
  548. memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
  549. buf += spec->n_remainder;
  550. p += spec->n_remainder;
  551. }
  552. if (spec->n_rpadding) {
  553. STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
  554. buf += spec->n_rpadding;
  555. }
  556. }
  557. static char no_grouping[1] = {CHAR_MAX};
  558. /* Find the decimal point character(s?), thousands_separator(s?), and
  559. grouping description, either for the current locale if type is
  560. LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
  561. none if LT_NO_LOCALE. */
  562. static void
  563. get_locale_info(int type, LocaleInfo *locale_info)
  564. {
  565. switch (type) {
  566. case LT_CURRENT_LOCALE: {
  567. struct lconv *locale_data = localeconv();
  568. locale_info->decimal_point = locale_data->decimal_point;
  569. locale_info->thousands_sep = locale_data->thousands_sep;
  570. locale_info->grouping = locale_data->grouping;
  571. break;
  572. }
  573. case LT_DEFAULT_LOCALE:
  574. locale_info->decimal_point = ".";
  575. locale_info->thousands_sep = ",";
  576. locale_info->grouping = "\3"; /* Group every 3 characters. The
  577. (implicit) trailing 0 means repeat
  578. infinitely. */
  579. break;
  580. case LT_NO_LOCALE:
  581. locale_info->decimal_point = ".";
  582. locale_info->thousands_sep = "";
  583. locale_info->grouping = no_grouping;
  584. break;
  585. default:
  586. assert(0);
  587. }
  588. }
  589. #endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
  590. /************************************************************************/
  591. /*********** string formatting ******************************************/
  592. /************************************************************************/
  593. static PyObject *
  594. format_string_internal(PyObject *value, const InternalFormatSpec *format)
  595. {
  596. Py_ssize_t lpad;
  597. Py_ssize_t rpad;
  598. Py_ssize_t total;
  599. STRINGLIB_CHAR *p;
  600. Py_ssize_t len = STRINGLIB_LEN(value);
  601. PyObject *result = NULL;
  602. /* sign is not allowed on strings */
  603. if (format->sign != '\0') {
  604. PyErr_SetString(PyExc_ValueError,
  605. "Sign not allowed in string format specifier");
  606. goto done;
  607. }
  608. /* alternate is not allowed on strings */
  609. if (format->alternate) {
  610. PyErr_SetString(PyExc_ValueError,
  611. "Alternate form (#) not allowed in string format "
  612. "specifier");
  613. goto done;
  614. }
  615. /* '=' alignment not allowed on strings */
  616. if (format->align == '=') {
  617. PyErr_SetString(PyExc_ValueError,
  618. "'=' alignment not allowed "
  619. "in string format specifier");
  620. goto done;
  621. }
  622. /* if precision is specified, output no more that format.precision
  623. characters */
  624. if (format->precision >= 0 && len >= format->precision) {
  625. len = format->precision;
  626. }
  627. calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
  628. /* allocate the resulting string */
  629. result = STRINGLIB_NEW(NULL, total);
  630. if (result == NULL)
  631. goto done;
  632. /* Write into that space. First the padding. */
  633. p = fill_padding(STRINGLIB_STR(result), len,
  634. format->fill_char=='\0'?' ':format->fill_char,
  635. lpad, rpad);
  636. /* Then the source string. */
  637. memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));
  638. done:
  639. return result;
  640. }
  641. /************************************************************************/
  642. /*********** long formatting ********************************************/
  643. /************************************************************************/
  644. #if defined FORMAT_LONG || defined FORMAT_INT
  645. typedef PyObject*
  646. (*IntOrLongToString)(PyObject *value, int base);
  647. static PyObject *
  648. format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
  649. IntOrLongToString tostring)
  650. {
  651. PyObject *result = NULL;
  652. PyObject *tmp = NULL;
  653. STRINGLIB_CHAR *pnumeric_chars;
  654. STRINGLIB_CHAR numeric_char;
  655. STRINGLIB_CHAR sign_char = '\0';
  656. Py_ssize_t n_digits; /* count of digits need from the computed
  657. string */
  658. Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
  659. produces non-digits */
  660. Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
  661. Py_ssize_t n_total;
  662. STRINGLIB_CHAR *prefix = NULL;
  663. NumberFieldWidths spec;
  664. long x;
  665. /* Locale settings, either from the actual locale or
  666. from a hard-code pseudo-locale */
  667. LocaleInfo locale;
  668. /* no precision allowed on integers */
  669. if (format->precision != -1) {
  670. PyErr_SetString(PyExc_ValueError,
  671. "Precision not allowed in integer format specifier");
  672. goto done;
  673. }
  674. /* special case for character formatting */
  675. if (format->type == 'c') {
  676. /* error to specify a sign */
  677. if (format->sign != '\0') {
  678. PyErr_SetString(PyExc_ValueError,
  679. "Sign not allowed with integer"
  680. " format specifier 'c'");
  681. goto done;
  682. }
  683. /* taken from unicodeobject.c formatchar() */
  684. /* Integer input truncated to a character */
  685. /* XXX: won't work for int */
  686. x = PyLong_AsLong(value);
  687. if (x == -1 && PyErr_Occurred())
  688. goto done;
  689. #ifdef Py_UNICODE_WIDE
  690. if (x < 0 || x > 0x10ffff) {
  691. PyErr_SetString(PyExc_OverflowError,
  692. "%c arg not in range(0x110000) "
  693. "(wide Python build)");
  694. goto done;
  695. }
  696. #else
  697. if (x < 0 || x > 0xffff) {
  698. PyErr_SetString(PyExc_OverflowError,
  699. "%c arg not in range(0x10000) "
  700. "(narrow Python build)");
  701. goto done;
  702. }
  703. #endif
  704. numeric_char = (STRINGLIB_CHAR)x;
  705. pnumeric_chars = &numeric_char;
  706. n_digits = 1;
  707. /* As a sort-of hack, we tell calc_number_widths that we only
  708. have "remainder" characters. calc_number_widths thinks
  709. these are characters that don't get formatted, only copied
  710. into the output string. We do this for 'c' formatting,
  711. because the characters are likely to be non-digits. */
  712. n_remainder = 1;
  713. }
  714. else {
  715. int base;
  716. int leading_chars_to_skip = 0; /* Number of characters added by
  717. PyNumber_ToBase that we want to
  718. skip over. */
  719. /* Compute the base and how many characters will be added by
  720. PyNumber_ToBase */
  721. switch (format->type) {
  722. case 'b':
  723. base = 2;
  724. leading_chars_to_skip = 2; /* 0b */
  725. break;
  726. case 'o':
  727. base = 8;
  728. leading_chars_to_skip = 2; /* 0o */
  729. break;
  730. case 'x':
  731. case 'X':
  732. base = 16;
  733. leading_chars_to_skip = 2; /* 0x */
  734. break;
  735. default: /* shouldn't be needed, but stops a compiler warning */
  736. case 'd':
  737. case 'n':
  738. base = 10;
  739. break;
  740. }
  741. /* The number of prefix chars is the same as the leading
  742. chars to skip */
  743. if (format->alternate)
  744. n_prefix = leading_chars_to_skip;
  745. /* Do the hard part, converting to a string in a given base */
  746. tmp = tostring(value, base);
  747. if (tmp == NULL)
  748. goto done;
  749. pnumeric_chars = STRINGLIB_STR(tmp);
  750. n_digits = STRINGLIB_LEN(tmp);
  751. prefix = pnumeric_chars;
  752. /* Remember not to modify what pnumeric_chars points to. it
  753. might be interned. Only modify it after we copy it into a
  754. newly allocated output buffer. */
  755. /* Is a sign character present in the output? If so, remember it
  756. and skip it */
  757. if (pnumeric_chars[0] == '-') {
  758. sign_char = pnumeric_chars[0];
  759. ++prefix;
  760. ++leading_chars_to_skip;
  761. }
  762. /* Skip over the leading chars (0x, 0b, etc.) */
  763. n_digits -= leading_chars_to_skip;
  764. pnumeric_chars += leading_chars_to_skip;
  765. }
  766. /* Determine the grouping, separator, and decimal point, if any. */
  767. get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
  768. (format->thousands_separators ?
  769. LT_DEFAULT_LOCALE :
  770. LT_NO_LOCALE),
  771. &locale);
  772. /* Calculate how much memory we'll need. */
  773. n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
  774. n_digits, n_remainder, 0, &locale, format);
  775. /* Allocate the memory. */
  776. result = STRINGLIB_NEW(NULL, n_total);
  777. if (!result)
  778. goto done;
  779. /* Populate the memory. */
  780. fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
  781. prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
  782. &locale, format->type == 'X');
  783. done:
  784. Py_XDECREF(tmp);
  785. return result;
  786. }
  787. #endif /* defined FORMAT_LONG || defined FORMAT_INT */
  788. /************************************************************************/
  789. /*********** float formatting *******************************************/
  790. /************************************************************************/
  791. #ifdef FORMAT_FLOAT
  792. #if STRINGLIB_IS_UNICODE
  793. static void
  794. strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
  795. {
  796. Py_ssize_t i;
  797. for (i = 0; i < len; ++i)
  798. buffer[i] = (Py_UNICODE)charbuffer[i];
  799. }
  800. #endif
  801. /* much of this is taken from unicodeobject.c */
  802. static PyObject *
  803. format_float_internal(PyObject *value,
  804. const InternalFormatSpec *format)
  805. {
  806. char *buf = NULL; /* buffer returned from PyOS_double_to_string */
  807. Py_ssize_t n_digits;
  808. Py_ssize_t n_remainder;
  809. Py_ssize_t n_total;
  810. int has_decimal;
  811. double val;
  812. Py_ssize_t precision = format->precision;
  813. Py_ssize_t default_precision = 6;
  814. STRINGLIB_CHAR type = format->type;
  815. int add_pct = 0;
  816. STRINGLIB_CHAR *p;
  817. NumberFieldWidths spec;
  818. int flags = 0;
  819. PyObject *result = NULL;
  820. STRINGLIB_CHAR sign_char = '\0';
  821. int float_type; /* Used to see if we have a nan, inf, or regular float. */
  822. #if STRINGLIB_IS_UNICODE
  823. Py_UNICODE *unicode_tmp = NULL;
  824. #endif
  825. /* Locale settings, either from the actual locale or
  826. from a hard-code pseudo-locale */
  827. LocaleInfo locale;
  828. if (format->alternate)
  829. flags |= Py_DTSF_ALT;
  830. if (type == '\0') {
  831. /* Omitted type specifier. Behaves in the same way as repr(x)
  832. and str(x) if no precision is given, else like 'g', but with
  833. at least one digit after the decimal point. */
  834. flags |= Py_DTSF_ADD_DOT_0;
  835. type = 'r';
  836. default_precision = 0;
  837. }
  838. if (type == 'n')
  839. /* 'n' is the same as 'g', except for the locale used to
  840. format the result. We take care of that later. */
  841. type = 'g';
  842. val = PyFloat_AsDouble(value);
  843. if (val == -1.0 && PyErr_Occurred())
  844. goto done;
  845. if (type == '%') {
  846. type = 'f';
  847. val *= 100;
  848. add_pct = 1;
  849. }
  850. if (precision < 0)
  851. precision = default_precision;
  852. else if (type == 'r')
  853. type = 'g';
  854. /* Cast "type", because if we're in unicode we need to pass a
  855. 8-bit char. This is safe, because we've restricted what "type"
  856. can be. */
  857. buf = PyOS_double_to_string(val, (char)type, precision, flags,
  858. &float_type);
  859. if (buf == NULL)
  860. goto done;
  861. n_digits = strlen(buf);
  862. if (add_pct) {
  863. /* We know that buf has a trailing zero (since we just called
  864. strlen() on it), and we don't use that fact any more. So we
  865. can just write over the trailing zero. */
  866. buf[n_digits] = '%';
  867. n_digits += 1;
  868. }
  869. /* Since there is no unicode version of PyOS_double_to_string,
  870. just use the 8 bit version and then convert to unicode. */
  871. #if STRINGLIB_IS_UNICODE
  872. unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
  873. if (unicode_tmp == NULL) {
  874. PyErr_NoMemory();
  875. goto done;
  876. }
  877. strtounicode(unicode_tmp, buf, n_digits);
  878. p = unicode_tmp;
  879. #else
  880. p = buf;
  881. #endif
  882. /* Is a sign character present in the output? If so, remember it
  883. and skip it */
  884. if (*p == '-') {
  885. sign_char = *p;
  886. ++p;
  887. --n_digits;
  888. }
  889. /* Determine if we have any "remainder" (after the digits, might include
  890. decimal or exponent or both (or neither)) */
  891. parse_number(p, n_digits, &n_remainder, &has_decimal);
  892. /* Determine the grouping, separator, and decimal point, if any. */
  893. get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
  894. (format->thousands_separators ?
  895. LT_DEFAULT_LOCALE :
  896. LT_NO_LOCALE),
  897. &locale);
  898. /* Calculate how much memory we'll need. */
  899. n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
  900. n_remainder, has_decimal, &locale, format);
  901. /* Allocate the memory. */
  902. result = STRINGLIB_NEW(NULL, n_total);
  903. if (result == NULL)
  904. goto done;
  905. /* Populate the memory. */
  906. fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
  907. format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
  908. 0);
  909. done:
  910. PyMem_Free(buf);
  911. #if STRINGLIB_IS_UNICODE
  912. PyMem_Free(unicode_tmp);
  913. #endif
  914. return result;
  915. }
  916. #endif /* FORMAT_FLOAT */
  917. /************************************************************************/
  918. /*********** complex formatting *****************************************/
  919. /************************************************************************/
  920. #ifdef FORMAT_COMPLEX
  921. static PyObject *
  922. format_complex_internal(PyObject *value,
  923. const InternalFormatSpec *format)
  924. {
  925. double re;
  926. double im;
  927. char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
  928. char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
  929. InternalFormatSpec tmp_format = *format;
  930. Py_ssize_t n_re_digits;
  931. Py_ssize_t n_im_digits;
  932. Py_ssize_t n_re_remainder;
  933. Py_ssize_t n_im_remainder;
  934. Py_ssize_t n_re_total;
  935. Py_ssize_t n_im_total;
  936. int re_has_decimal;
  937. int im_has_decimal;
  938. Py_ssize_t precision = format->precision;
  939. Py_ssize_t default_precision = 6;
  940. STRINGLIB_CHAR type = format->type;
  941. STRINGLIB_CHAR *p_re;
  942. STRINGLIB_CHAR *p_im;
  943. NumberFieldWidths re_spec;
  944. NumberFieldWidths im_spec;
  945. int flags = 0;
  946. PyObject *result = NULL;
  947. STRINGLIB_CHAR *p;
  948. STRINGLIB_CHAR re_sign_char = '\0';
  949. STRINGLIB_CHAR im_sign_char = '\0';
  950. int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
  951. int im_float_type;
  952. int add_parens = 0;
  953. int skip_re = 0;
  954. Py_ssize_t lpad;
  955. Py_ssize_t rpad;
  956. Py_ssize_t total;
  957. #if STRINGLIB_IS_UNICODE
  958. Py_UNICODE *re_unicode_tmp = NULL;
  959. Py_UNICODE *im_unicode_tmp = NULL;
  960. #endif
  961. /* Locale settings, either from the actual locale or
  962. from a hard-code pseudo-locale */
  963. LocaleInfo locale;
  964. /* Zero padding is not allowed. */
  965. if (format->fill_char == '0') {
  966. PyErr_SetString(PyExc_ValueError,
  967. "Zero padding is not allowed in complex format "
  968. "specifier");
  969. goto done;
  970. }
  971. /* Neither is '=' alignment . */
  972. if (format->align == '=') {
  973. PyErr_SetString(PyExc_ValueError,
  974. "'=' alignment flag is not allowed in complex format "
  975. "specifier");
  976. goto done;
  977. }
  978. re = PyComplex_RealAsDouble(value);
  979. if (re == -1.0 && PyErr_Occurred())
  980. goto done;
  981. im = PyComplex_ImagAsDouble(value);
  982. if (im == -1.0 && PyErr_Occurred())
  983. goto done;
  984. if (format->alternate)
  985. flags |= Py_DTSF_ALT;
  986. if (type == '\0') {
  987. /* Omitted type specifier. Should be like str(self). */
  988. type = 'r';
  989. default_precision = 0;
  990. if (re == 0.0 && copysign(1.0, re) == 1.0)
  991. skip_re = 1;
  992. else
  993. add_parens = 1;
  994. }
  995. if (type == 'n')
  996. /* 'n' is the same as 'g', except for the locale used to
  997. format the result. We take care of that later. */
  998. type = 'g';
  999. if (precision < 0)
  1000. precision = default_precision;
  1001. else if (type == 'r')
  1002. type = 'g';
  1003. /* Cast "type", because if we're in unicode we need to pass a
  1004. 8-bit char. This is safe, because we've restricted what "type"
  1005. can be. */
  1006. re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
  1007. &re_float_type);
  1008. if (re_buf == NULL)
  1009. goto done;
  1010. im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
  1011. &im_float_type);
  1012. if (im_buf == NULL)
  1013. goto done;
  1014. n_re_digits = strlen(re_buf);
  1015. n_im_digits = strlen(im_buf);
  1016. /* Since there is no unicode version of PyOS_double_to_string,
  1017. just use the 8 bit version and then convert to unicode. */
  1018. #if STRINGLIB_IS_UNICODE
  1019. re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
  1020. if (re_unicode_tmp == NULL) {
  1021. PyErr_NoMemory();
  1022. goto done;
  1023. }
  1024. strtounicode(re_unicode_tmp, re_buf, n_re_digits);
  1025. p_re = re_unicode_tmp;
  1026. im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
  1027. if (im_unicode_tmp == NULL) {
  1028. PyErr_NoMemory();
  1029. goto done;
  1030. }
  1031. strtounicode(im_unicode_tmp, im_buf, n_im_digits);
  1032. p_im = im_unicode_tmp;
  1033. #else
  1034. p_re = re_buf;
  1035. p_im = im_buf;
  1036. #endif
  1037. /* Is a sign character present in the output? If so, remember it
  1038. and skip it */
  1039. if (*p_re == '-') {
  1040. re_sign_char = *p_re;
  1041. ++p_re;
  1042. --n_re_digits;
  1043. }
  1044. if (*p_im == '-') {
  1045. im_sign_char = *p_im;
  1046. ++p_im;
  1047. --n_im_digits;
  1048. }
  1049. /* Determine if we have any "remainder" (after the digits, might include
  1050. decimal or exponent or both (or neither)) */
  1051. parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
  1052. parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
  1053. /* Determine the grouping, separator, and decimal point, if any. */
  1054. get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
  1055. (format->thousands_separators ?
  1056. LT_DEFAULT_LOCALE :
  1057. LT_NO_LOCALE),
  1058. &locale);
  1059. /* Turn off any padding. We'll do it later after we've composed
  1060. the numbers without padding. */
  1061. tmp_format.fill_char = '\0';
  1062. tmp_format.align = '<';
  1063. tmp_format.width = -1;
  1064. /* Calculate how much memory we'll need. */
  1065. n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
  1066. n_re_digits, n_re_remainder,
  1067. re_has_decimal, &locale, &tmp_format);
  1068. /* Same formatting, but always include a sign, unless the real part is
  1069. * going to be omitted, in which case we use whatever sign convention was
  1070. * requested by the original format. */
  1071. if (!skip_re)
  1072. tmp_format.sign = '+';
  1073. n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
  1074. n_im_digits, n_im_remainder,
  1075. im_has_decimal, &locale, &tmp_format);
  1076. if (skip_re)
  1077. n_re_total = 0;
  1078. /* Add 1 for the 'j', and optionally 2 for parens. */
  1079. calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
  1080. format->width, format->align, &lpad, &rpad, &total);
  1081. result = STRINGLIB_NEW(NULL, total);
  1082. if (result == NULL)
  1083. goto done;
  1084. /* Populate the memory. First, the padding. */
  1085. p = fill_padding(STRINGLIB_STR(result),
  1086. n_re_total + n_im_total + 1 + add_parens * 2,
  1087. format->fill_char=='\0' ? ' ' : format->fill_char,
  1088. lpad, rpad);
  1089. if (add_parens)
  1090. *p++ = '(';
  1091. if (!skip_re) {
  1092. fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
  1093. p += n_re_total;
  1094. }
  1095. fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
  1096. p += n_im_total;
  1097. *p++ = 'j';
  1098. if (add_parens)
  1099. *p++ = ')';
  1100. done:
  1101. PyMem_Free(re_buf);
  1102. PyMem_Free(im_buf);
  1103. #if STRINGLIB_IS_UNICODE
  1104. PyMem_Free(re_unicode_tmp);
  1105. PyMem_Free(im_unicode_tmp);
  1106. #endif
  1107. return result;
  1108. }
  1109. #endif /* FORMAT_COMPLEX */
  1110. /************************************************************************/
  1111. /*********** built in formatters ****************************************/
  1112. /************************************************************************/
  1113. PyObject *
  1114. FORMAT_STRING(PyObject *obj,
  1115. STRINGLIB_CHAR *format_spec,
  1116. Py_ssize_t format_spec_len)
  1117. {
  1118. InternalFormatSpec format;
  1119. PyObject *result = NULL;
  1120. /* check for the special case of zero length format spec, make
  1121. it equivalent to str(obj) */
  1122. if (format_spec_len == 0) {
  1123. result = STRINGLIB_TOSTR(obj);
  1124. goto done;
  1125. }
  1126. /* parse the format_spec */
  1127. if (!parse_internal_render_format_spec(format_spec, format_spec_len,
  1128. &format, 's', '<'))
  1129. goto done;
  1130. /* type conversion? */
  1131. switch (format.type) {
  1132. case 's':
  1133. /* no type conversion needed, already a string. do the formatting */
  1134. result = format_string_internal(obj, &format);
  1135. break;
  1136. default:
  1137. /* unknown */
  1138. unknown_presentation_type(format.type, obj->ob_type->tp_name);
  1139. goto done;
  1140. }
  1141. done:
  1142. return result;
  1143. }
  1144. #if defined FORMAT_LONG || defined FORMAT_INT
  1145. static PyObject*
  1146. format_int_or_long(PyObject* obj,
  1147. STRINGLIB_CHAR *format_spec,
  1148. Py_ssize_t format_spec_len,
  1149. IntOrLongToString tostring)
  1150. {
  1151. PyObject *result = NULL;
  1152. PyObject *tmp = NULL;
  1153. InternalFormatSpec format;
  1154. /* check for the special case of zero length format spec, make
  1155. it equivalent to str(obj) */
  1156. if (format_spec_len == 0) {
  1157. result = STRINGLIB_TOSTR(obj);
  1158. goto done;
  1159. }
  1160. /* parse the format_spec */
  1161. if (!parse_internal_render_format_spec(format_spec,
  1162. format_spec_len,
  1163. &format, 'd', '>'))
  1164. goto done;
  1165. /* type conversion? */
  1166. switch (format.type) {
  1167. case 'b':
  1168. case 'c':
  1169. case 'd':
  1170. case 'o':
  1171. case 'x':
  1172. case 'X':
  1173. case 'n':
  1174. /* no type conversion needed, already an int (or long). do
  1175. the formatting */
  1176. result = format_int_or_long_internal(obj, &format, tostring);
  1177. break;
  1178. case 'e':
  1179. case 'E':
  1180. case 'f':
  1181. case 'F':
  1182. case 'g':
  1183. case 'G':
  1184. case '%':
  1185. /* convert to float */
  1186. tmp = PyNumber_Float(obj);
  1187. if (tmp == NULL)
  1188. goto done;
  1189. result = format_float_internal(tmp, &format);
  1190. break;
  1191. default:
  1192. /* unknown */
  1193. unknown_presentation_type(format.type, obj->ob_type->tp_name);
  1194. goto done;
  1195. }
  1196. done:
  1197. Py_XDECREF(tmp);
  1198. return result;
  1199. }
  1200. #endif /* FORMAT_LONG || defined FORMAT_INT */
  1201. #ifdef FORMAT_LONG
  1202. /* Need to define long_format as a function that will convert a long
  1203. to a string. In 3.0, _PyLong_Format has the correct signature. In
  1204. 2.x, we need to fudge a few parameters */
  1205. #if PY_VERSION_HEX >= 0x03000000
  1206. #define long_format _PyLong_Format
  1207. #else
  1208. static PyObject*
  1209. long_format(PyObject* value, int base)
  1210. {
  1211. /* Convert to base, don't add trailing 'L', and use the new octal
  1212. format. We already know this is a long object */
  1213. assert(PyLong_Check(value));
  1214. /* convert to base, don't add 'L', and use the new octal format */
  1215. return _PyLong_Format(value, base, 0, 1);
  1216. }
  1217. #endif
  1218. PyObject *
  1219. FORMAT_LONG(PyObject *obj,
  1220. STRINGLIB_CHAR *format_spec,
  1221. Py_ssize_t format_spec_len)
  1222. {
  1223. return format_int_or_long(obj, format_spec, format_spec_len,
  1224. long_format);
  1225. }
  1226. #endif /* FORMAT_LONG */
  1227. #ifdef FORMAT_INT
  1228. /* this is only used for 2.x, not 3.0 */
  1229. static PyObject*
  1230. int_format(PyObject* value, int base)
  1231. {
  1232. /* Convert to base, and use the new octal format. We already
  1233. know this is an int object */
  1234. assert(PyInt_Check(value));
  1235. return _PyInt_Format((PyIntObject*)value, base, 1);
  1236. }
  1237. PyObject *
  1238. FORMAT_INT(PyObject *obj,
  1239. STRINGLIB_CHAR *format_spec,
  1240. Py_ssize_t format_spec_len)
  1241. {
  1242. return format_int_or_long(obj, format_spec, format_spec_len,
  1243. int_format);
  1244. }
  1245. #endif /* FORMAT_INT */
  1246. #ifdef FORMAT_FLOAT
  1247. PyObject *
  1248. FORMAT_FLOAT(PyObject *obj,
  1249. STRINGLIB_CHAR *format_spec,
  1250. Py_ssize_t format_spec_len)
  1251. {
  1252. PyObject *result = NULL;
  1253. InternalFormatSpec format;
  1254. /* check for the special case of zero length format spec, make
  1255. it equivalent to str(obj) */
  1256. if (format_spec_len == 0) {
  1257. result = STRINGLIB_TOSTR(obj);
  1258. goto done;
  1259. }
  1260. /* parse the format_spec */
  1261. if (!parse_internal_render_format_spec(format_spec,
  1262. format_spec_len,
  1263. &format, '\0', '>'))
  1264. goto done;
  1265. /* type conversion? */
  1266. switch (format.type) {
  1267. case '\0': /* No format code: like 'g', but with at least one decimal. */
  1268. case 'e':
  1269. case 'E':
  1270. case 'f':
  1271. case 'F':
  1272. case 'g':
  1273. case 'G':
  1274. case 'n':
  1275. case '%':
  1276. /* no conversion, already a float. do the formatting */
  1277. result = format_float_internal(obj, &format);
  1278. break;
  1279. default:
  1280. /* unknown */
  1281. unknown_presentation_type(format.type, obj->ob_type->tp_name);
  1282. goto done;
  1283. }
  1284. done:
  1285. return result;
  1286. }
  1287. #endif /* FORMAT_FLOAT */
  1288. #ifdef FORMAT_COMPLEX
  1289. PyObject *
  1290. FORMAT_COMPLEX(PyObject *obj,
  1291. STRINGLIB_CHAR *format_spec,
  1292. Py_ssize_t format_spec_len)
  1293. {
  1294. PyObject *result = NULL;
  1295. InternalFormatSpec format;
  1296. /* check for the special case of zero length format spec, make
  1297. it equivalent to str(obj) */
  1298. if (format_spec_len == 0) {
  1299. result = STRINGLIB_TOSTR(obj);
  1300. goto done;
  1301. }
  1302. /* parse the format_spec */
  1303. if (!parse_internal_render_format_spec(format_spec,
  1304. format_spec_len,
  1305. &format, '\0', '>'))
  1306. goto done;
  1307. /* type conversion? */
  1308. switch (format.type) {
  1309. case '\0': /* No format code: like 'g', but with at least one decimal. */
  1310. case 'e':
  1311. case 'E':
  1312. case 'f':
  1313. case 'F':
  1314. case 'g':
  1315. case 'G':
  1316. case 'n':
  1317. /* no conversion, already a complex. do the formatting */
  1318. result = format_complex_internal(obj, &format);
  1319. break;
  1320. default:
  1321. /* unknown */
  1322. unknown_presentation_type(format.type, obj->ob_type->tp_name);
  1323. goto done;
  1324. }
  1325. done:
  1326. return result;
  1327. }
  1328. #endif /* FORMAT_COMPLEX */